Module: LogicTools
- Defined in:
- lib/logic_tools.rb,
lib/logic_tools/version.rb,
lib/logic_tools/logictree.rb,
lib/logic_tools/logicinput.rb,
lib/logic_tools/logicparse.rb,
lib/logic_tools/logicsimplify.rb
Overview
LogicTools
This module includes all the classes and methods used by the logic_tools set of tools.
Defined Under Namespace
Classes: Implicant, Node, NodeAnd, NodeFalse, NodeNary, NodeNot, NodeOr, NodeTrue, NodeUnary, NodeValue, NodeVar, Parser, SameXImplicants, Transform, VarImp, Variable
Constant Summary collapse
- VERSION =
"0.2.3"
Instance Method Summary collapse
-
#each_input ⇒ Object
Gets an iterator over the input expression (obtained either through options or a through file).
-
#help_short ⇒ Object
Displays a short help message.
-
#string2logic(str) ⇒ Object
The parser/gerator main fuction: converts the text in
strto a logic tree. -
#vars2int(vars) ⇒ Object
Converts the array of variables
varto a bit vector according to their values.
Instance Method Details
#each_input ⇒ Object
Gets an iterator over the input expression
(obtained either through options or a through file).
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/logic_tools/logicinput.rb', line 19 def each_input # No block? Return an enumerator return enum_for(:each_input) unless block_given? # A block? Interrate with it # Process the arguments if ($*.empty?) then # No arguments, shows the help and end. help_short exit(1) end if $*[0] == "-f" or $*[0] == "--file" then # Work from a file, iterate on each line exprs = File.read($*[1]) exprs.gsub!(/\r\n?/, "\n") exprs.each_line do |line| yield(line) end elsif $*[0] == "-h" or $*[0] == "--help" then help_short else # Work directly on the arguments as an expression yield($*.join) end end |
#help_short ⇒ Object
Displays a short help message.
11 12 13 14 15 |
# File 'lib/logic_tools/logicinput.rb', line 11 def help_short name = File.basename($0) puts "Usage: #{name} <\"logic expression\">" puts " or: #{name} -f <file name>" end |
#string2logic(str) ⇒ Object
The parser/gerator main fuction: converts the text in str to a
logic tree.
83 84 85 86 87 88 |
# File 'lib/logic_tools/logicparse.rb', line 83 def string2logic(str) # Remove the spaces str = str.gsub(/\s+/, "") # Parse the string return Transform.new.apply(Parser.new.parse(str)) end |
#vars2int(vars) ⇒ Object
Converts the array of variables var to a bit vector according to
their values
15 16 17 18 19 20 21 |
# File 'lib/logic_tools/logicsimplify.rb', line 15 def vars2int(vars) res = "" vars.each_with_index do |var,i| res[i] = var.value ? "1" : "0" end res end |