logic_tools 0.3.5 → 0.3.6

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
  SHA1:
3
- metadata.gz: 70d95a4f55c418f319f6757dcd33238a96e29942
4
- data.tar.gz: 41b8969baf1766988bfbd87d49ac42bda2c70b95
3
+ metadata.gz: 3fac599575ddf5395c3a3e1517361807b16f0de4
4
+ data.tar.gz: 29be7313218a9c3207222d23c99e7496e4c03a1c
5
5
  SHA512:
6
- metadata.gz: 26fe17b9471050c15cc5df53bc49cc87a4e280954853871845706d84317e2c3b76c79d83efe94257a3536048b6cfb09c1e68414d3cd0e1e1f3f160a26866ca00
7
- data.tar.gz: 07e758d9512566bd492b8644fd074769be0e2db6ee7b18d8df1f713c21aadf8dbdec2beeeb842e8a6512a5b1adc513efcb9d7d319f72f16581081160d256d8a4
6
+ metadata.gz: 7c36a7c2a6af3d8634dc7d9bbc51ac9dc65370b059888833df4950b1eee5104f0c7e2891a8bbe4eb117d9f2dc563c4a2d2687f4ba60d0feeff5d2171b53551c0
7
+ data.tar.gz: 5d7d3e6d2a5fb7c7b5ac1d1162906fdb7ca09f4c067b563ac1d11646b8df9928e86d7cbf416a491378e406e35646b0e66c2d34cbd8143d025bf3cb8ba2fff91c
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ #########################################################################
3
+ # Computes (quickly) the complement of a logic expression #
4
+ #########################################################################
5
+
6
+
7
+ # For building logic trees
8
+ require "logic_tools/logictree.rb"
9
+
10
+ # For parsing the inputs
11
+ require "logic_tools/logicparse.rb"
12
+
13
+ # For converting the inputs to covers.
14
+ require "logic_tools/logicconvert.rb"
15
+
16
+ # For processing the cover (and therfore check tautology)
17
+ require "logic_tools/logiccover.rb"
18
+
19
+ # For the command line interface
20
+ require "logic_tools/logicinput.rb"
21
+
22
+ include LogicTools
23
+
24
+
25
+
26
+
27
+
28
+ ############################
29
+ # The main program
30
+
31
+ # Iterrate on each expression
32
+ each_input do |expr|
33
+ # Parse the expression.
34
+ parsed = string2logic(expr)
35
+ # print "parsed=#{parsed}\n"
36
+
37
+ # Convert it to a cover.
38
+ cover = parsed.to_cover
39
+ # print "cover=#{cover}\n"
40
+
41
+ # Computes the complement
42
+ complement = cover.complement
43
+ # print "complement=#{complement}\n"
44
+
45
+ # Display the result
46
+ print complement.to_tree, "\n"
47
+ end
@@ -13,7 +13,7 @@ module LogicTools
13
13
  # NOTE: cubes are crutial for the performance of the implementation
14
14
  # of the simplifying algorithm.
15
15
  # Hence they are internally represented as strings, since they are
16
- # much more energy efficient and usually faster than arrays.
16
+ # much more memory efficient and usually faster than arrays.
17
17
  #
18
18
  # Then there are two interfaces:
19
19
  # * The standard interface is based on strings and substrings only and
@@ -0,0 +1,101 @@
1
+ ##############################################################
2
+ # Logic function class: used for describing a logic function #
3
+ ##############################################################
4
+
5
+
6
+ require "logic_tools/logictree.rb"
7
+
8
+
9
+ module LogicTools
10
+
11
+
12
+ ##
13
+ # Represents a logic function.
14
+ #
15
+ # A logic function is described as a set of logic trees (LogicTools::Node)
16
+ # associated to variables (LogicTools::Variable) repreenting each one
17
+ # one-bit output. The inputs are represented by the variables contained
18
+ # by the variable nodes (LogicTools::NodeVar) of the trees.
19
+ #
20
+ # Functions can be composed together through their variables.
21
+ class Function
22
+
23
+ ## Creates a new empty function.
24
+ def initialize
25
+ @assigns = {}
26
+ end
27
+
28
+ ## duplicates the function.
29
+ def clone # :nodoc:
30
+ result = Function.new
31
+ @assigns.each { |output,tree| result.add(output,tree.clone) }
32
+ return result
33
+ end
34
+ alias dup clone
35
+
36
+ ## Adds an +output+ variable associated with a +tree+ for computing it.
37
+ def add(output,tree)
38
+ unless tree.is_a?(Node)
39
+ raise "Invalid class for a logic tree: #{tree.class}"
40
+ end
41
+ @assigns[Variable.get(output)] = tree
42
+ end
43
+ alias []= add
44
+
45
+ ## Gets the tree corresponding to an +output+ variable.
46
+ def get(output)
47
+ return @assigns[Variable.get(variable)]
48
+ end
49
+ alias [] get
50
+
51
+ ## Iterates over the assignments of the functions.
52
+ def each(&blk)
53
+ # No block given? Return an enumerator.
54
+ return to_enum(:each) unless block_given?
55
+
56
+ # Block given? Apply it.
57
+ @assigns.each(&blk)
58
+ end
59
+
60
+ ## Iterates over the output variables of the function.
61
+ def each_output(&blk)
62
+ # No block given? Return an enumerator.
63
+ return to_enum(:each_output) unless block_given?
64
+
65
+ # Block given? Apply it.
66
+ @assigns.each_key(&blk)
67
+ end
68
+
69
+ ## Gets a array containing the variables of the function sorted by name.
70
+ def get_variables
71
+ result = @assigns.each_value.reduce([]) do |ar,tree|
72
+ ar.concat(tree.get_variables)
73
+ end
74
+ result.uniq!.sort!
75
+ return result
76
+ end
77
+
78
+ ## Iterates over the trees of the function.
79
+ def each_tree(&blk)
80
+ # No block given? Return an enumerator.
81
+ return to_enum(:each_tree) unless block_given?
82
+
83
+ # Block given? Apply it.
84
+ @assigns.each_value(&blk)
85
+ end
86
+
87
+ ## Convert to a string.
88
+ def to_s # :nodoc:
89
+ result = @assigns.each.reduce("[") do |str,assign|
90
+ str << assign[0].to_s
91
+ str << "="
92
+ str << assign[1].to_s
93
+ str << ","
94
+ end
95
+ result[-1] = "]"
96
+ return result
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -76,6 +76,8 @@ module LogicTools
76
76
 
77
77
  include Enumerable
78
78
 
79
+ alias dup clone
80
+
79
81
  ## Gets a array containing the variables of the tree sorted by name.
80
82
  def get_variables()
81
83
  result = self.get_variablesRecurse
@@ -741,8 +743,8 @@ module LogicTools
741
743
  end
742
744
 
743
745
  ## Duplicates the node.
744
- def dup # :nodoc:
745
- return NodeAnd.new(*@children.map(&:dup))
746
+ def clone # :nodoc:
747
+ return NodeAnd.new(*@children.map(&:clone))
746
748
  end
747
749
 
748
750
  ## Computes the value of the node.
@@ -781,7 +783,7 @@ module LogicTools
781
783
  end
782
784
  end
783
785
 
784
- ## Convert to a string.
786
+ ## Converts to a string.
785
787
  def to_s # :nodoc:
786
788
  return @str if @str
787
789
  @str = ""
@@ -809,8 +811,8 @@ module LogicTools
809
811
  end
810
812
 
811
813
  ## Duplicates the node.
812
- def dup # :nodoc:
813
- return NodeOr.new(*@children.map(&:dup))
814
+ def clone # :nodoc:
815
+ return NodeOr.new(*@children.map(&:clone))
814
816
  end
815
817
 
816
818
  ## Computes the value of the node.
@@ -922,8 +924,8 @@ module LogicTools
922
924
  end
923
925
 
924
926
  ## Duplicates the node.
925
- def dup # :nodoc:
926
- return NodeNot.new(@child.dup)
927
+ def clone # :nodoc:
928
+ return NodeNot.new(@child.clone)
927
929
  end
928
930
 
929
931
  ## Computes the value of the node.
@@ -1,3 +1,3 @@
1
1
  module LogicTools
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logic_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lovic Gauthier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-18 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,9 +102,11 @@ files:
102
102
  - exe/std_dij
103
103
  - exe/truth_tbl
104
104
  - lib/logic_tools.rb
105
+ - lib/logic_tools/complement.rb
105
106
  - lib/logic_tools/is_tautology.rb
106
107
  - lib/logic_tools/logicconvert.rb
107
108
  - lib/logic_tools/logiccover.rb
109
+ - lib/logic_tools/logicfunction.rb
108
110
  - lib/logic_tools/logicgenerator.rb
109
111
  - lib/logic_tools/logicinput.rb
110
112
  - lib/logic_tools/logicparse.rb