class LogicTools::Function
Represents a logic function.
A logic function is described as a set of logic trees (LogicTools::Node
) associated to variables (LogicTools::Variable
) repreenting each one one-bit output. The inputs are represented by the variables contained by the variable nodes (LogicTools::NodeVar
) of the trees.
Functions can be composed together through their variables.
Public Class Methods
new()
click to toggle source
Creates a new empty function.
# File lib/logic_tools/logicfunction.rb, line 24 def initialize @assigns = {} end
Public Instance Methods
add(output,tree)
click to toggle source
Adds an output
variable associated with a tree
for computing it.
# File lib/logic_tools/logicfunction.rb, line 37 def add(output,tree) unless tree.is_a?(Node) raise "Invalid class for a logic tree: #{tree.class}" end @assigns[Variable.get(output)] = tree end
Also aliased as: []=
each(&blk)
click to toggle source
Iterates over the assignments of the functions.
# File lib/logic_tools/logicfunction.rb, line 52 def each(&blk) # No block given? Return an enumerator. return to_enum(:each) unless block_given? # Block given? Apply it. @assigns.each(&blk) end
each_output(&blk)
click to toggle source
Iterates over the output variables of the function.
# File lib/logic_tools/logicfunction.rb, line 61 def each_output(&blk) # No block given? Return an enumerator. return to_enum(:each_output) unless block_given? # Block given? Apply it. @assigns.each_key(&blk) end
each_tree(&blk)
click to toggle source
Iterates over the trees of the function.
# File lib/logic_tools/logicfunction.rb, line 79 def each_tree(&blk) # No block given? Return an enumerator. return to_enum(:each_tree) unless block_given? # Block given? Apply it. @assigns.each_value(&blk) end
get(output)
click to toggle source
Gets the tree corresponding to an output
variable.
# File lib/logic_tools/logicfunction.rb, line 46 def get(output) return @assigns[Variable.get(variable)] end
Also aliased as: []
get_variables()
click to toggle source
Gets a array containing the variables of the function sorted by name.
# File lib/logic_tools/logicfunction.rb, line 70 def get_variables result = @assigns.each_value.reduce([]) do |ar,tree| ar.concat(tree.get_variables) end result.uniq!.sort! return result end