logic_analyzer 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93f454e3f8b567810914170b9850e7023993048d27fafa9a83c21be7e7718846
4
- data.tar.gz: ebf24d0492a3e4b6c4884ad679e3cffe6a69c93cb5d8c742bbad13fbc4f36f5d
3
+ metadata.gz: 4bd3488943cd9c9c969fff9828a1ec5a0bfa54a680760ea5ee19af5d65bb22de
4
+ data.tar.gz: 9a8dd7e28b1531aff083c8214d1ba1b6747d522f4d1b0c08b97a6b2b4ce8d54d
5
5
  SHA512:
6
- metadata.gz: e0744be2c9a9558fe72a455381e2bca3a0875a23c9741e5eda2cbb4a09137c24e168bca04e5dbec51f2780ebd0d1142f8b3087f36c8e829ec2eb8e727bc782eb
7
- data.tar.gz: 9107fb46e8b05a0a3cc8bd84a761a4e805a4bdccbe53f63463046e2674b55844392a3624a088fa46d78ddf411bb6263f4fccf3e7f154946d2323ff24cdd1a6af
6
+ metadata.gz: b9ed5d6228ebf17f1cbe4ff572c6e660150e1d60bc7bd02876c6bf9b9b1ba1c59dc770ecebc853cf48b2d653547b28f784ded4ab521abd00e337569be24ad201
7
+ data.tar.gz: def22fbd13228668e7f395a76cd860aeb6498407be58801b746a15e5651b736090bdf1780ff41fa49b9a97148d9f0c86f12982c7b7e2e21bbf37ca30fe8d8e23
@@ -1,6 +1,50 @@
1
1
  require_relative 'logic_analyzer/boolean'
2
2
  require_relative 'logic_analyzer/exception'
3
- require_relative 'logic_analyzer/logic_evaluation'
4
3
  require_relative 'logic_analyzer/logical_context'
5
4
  require_relative 'logic_analyzer/proposition'
6
- require_relative 'logic_analyzer/truth_table'
5
+ require_relative 'logic_analyzer/truth_table'
6
+
7
+ class LogicAnalyzer
8
+ attr_accessor :sentences, :conclusion, :proposition
9
+
10
+ def initialize(*args)
11
+ self.sentences = []
12
+
13
+ unless args.empty?
14
+ args.each do |sentence|
15
+ sentences.append("(#{sentence})")
16
+ end
17
+ end
18
+ end
19
+
20
+ def add_sentence(sentence)
21
+ sentences.append("(#{sentence})")
22
+ end
23
+
24
+ def add_conclusion(conclusion)
25
+ self.conclusion = "(#{conclusion})"
26
+ end
27
+
28
+ def evaluate
29
+ parse
30
+
31
+ prop = Proposition.new(proposition)
32
+ prop.parse
33
+
34
+ prop.evaluate
35
+ end
36
+
37
+ def parse
38
+ if sentences.empty?
39
+ raise ArgumentError 'Cannot evaluate with no propositions'
40
+ elsif sentences.size == 1
41
+ self.proposition = sentences[0]
42
+ else
43
+ self.proposition = if conclusion.nil?
44
+ sentences.join(' and ')
45
+ else
46
+ "(#{sentences.join(' and ')}) then #{conclusion}"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -9,25 +9,23 @@ class Proposition
9
9
  @sentence = syntax_sugars(sentence)
10
10
 
11
11
  @sentence.define_singleton_method(:words) do
12
- split(/\W+/)
13
- end
14
-
15
- @sentence.define_singleton_method(:operands) do
16
- split(/\s|\w|\s/).reject(&:empty?)
12
+ split(/\W+/).reject(&:empty?)
17
13
  end
18
14
  end
19
15
 
20
16
  def syntax_sugars(sentence)
21
17
  sugared = sentence.gsub(/and|&&/, '.and')
22
- .gsub(/or|\|\|/, '.or')
23
- .gsub(/then|>/, '.then')
24
- .gsub(/if_and_only_if|<=>/, '.if_and_only_if')
25
- .gsub(/xor|!=/, '.xor')
26
- .gsub(/not/, '!')
18
+ .gsub(/or|\|\|/, '.or')
19
+ .gsub(/then|>/, '.then')
20
+ .gsub(/if_and_only_if|<=>/, '.if_and_only_if')
21
+ .gsub(/xor|!=/, '.xor')
22
+ .gsub(/not/, '!')
27
23
  end
28
24
 
29
25
  def parse
30
- @variables = sentence.words
26
+ @variables = sentence.words.reject do |var|
27
+ %w[and or then if_and_only_if xor not].include?(var)
28
+ end
31
29
  end
32
30
 
33
31
  def evaluate
@@ -52,6 +50,6 @@ class Proposition
52
50
  end
53
51
 
54
52
  def amount_of_variables
55
- 2**variables.length
53
+ 2 ** variables.length
56
54
  end
57
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logic_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Peralta Bazas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2019-01-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby implementation of a propositional logic parser
14
14
  email:
@@ -19,7 +19,6 @@ files:
19
19
  - lib/logic_analyzer.rb
20
20
  - lib/logic_analyzer/boolean.rb
21
21
  - lib/logic_analyzer/exception.rb
22
- - lib/logic_analyzer/logic_evaluation.rb
23
22
  - lib/logic_analyzer/logical_context.rb
24
23
  - lib/logic_analyzer/proposition.rb
25
24
  - lib/logic_analyzer/truth_table.rb
@@ -1,14 +0,0 @@
1
- require_relative 'proposition'
2
-
3
- module LogicEvaluation
4
- def logic_value
5
- prop = Proposition.new(self)
6
- prop.parse
7
-
8
- prop.evaluate
9
- end
10
- end
11
-
12
- class String
13
- include LogicEvaluation
14
- end