Class: LogicTools::NodeAnd
- Defined in:
- lib/logic_tools/logictree.rb
Overview
Represents an AND node
Instance Attribute Summary
Attributes inherited from NodeNary
Instance Method Summary collapse
-
#dup ⇒ Object
Duplicates the node.
-
#eval ⇒ Object
Computes the value of the node.
-
#initialize(*children) ⇒ NodeAnd
constructor
Creates a new AND node with
children. -
#to_s ⇒ Object
Convert to a string.
-
#to_sum_product(flattened = false) ⇒ Object
Creates a sum fo product from the tree rooted by current node.
Methods inherited from NodeNary
#==, #add, #cover?, #distribute, #each, #flatten, #flatten_deep, #get_variablesRecurse, #include?, #is_parent?, make, #reduce, #sort, #to_sym, #uniq
Methods inherited from Node
#cover?, #distribute, #each, #each_line, #each_maxterm, #each_minterm, #eql?, #flatten, #flatten_deep, #get_variables, #hash, #include?, #inspect, #is_parent?, #op, #reduce, #simplify, #size, #to_cover, #to_std_conjunctive, #to_std_disjunctive, #to_sym
Constructor Details
#initialize(*children) ⇒ NodeAnd
Creates a new AND node with children.
739 740 741 |
# File 'lib/logic_tools/logictree.rb', line 739 def initialize(*children) super(:and,*children) end |
Instance Method Details
#dup ⇒ Object
Duplicates the node.
744 745 746 |
# File 'lib/logic_tools/logictree.rb', line 744 def dup # :nodoc: return NodeAnd.new(*@children.map(&:dup)) end |
#eval ⇒ Object
Computes the value of the node.
749 750 751 |
# File 'lib/logic_tools/logictree.rb', line 749 def eval() return !@children.any? {|child| child.eval() == false } end |
#to_s ⇒ Object
Convert to a string.
785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/logic_tools/logictree.rb', line 785 def to_s # :nodoc: return @str if @str @str = "" # Convert the children to a string @children.each do |child| if (child.op == :or) then # Yes, need parenthesis @str << ( "(" + child.to_s + ")" ) else @str << child.to_s end end return @str end |
#to_sum_product(flattened = false) ⇒ Object
Creates a sum fo product from the tree rooted by current node.
Argument +flattened+ tells if the tree is already flattend
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
# File 'lib/logic_tools/logictree.rb', line 756 def to_sum_product(flattened = false) # :nodoc: # Flatten if required node = flattened ? self : self.flatten_deep return node unless node.is_parent? # Convert each child to sum of product nchildren = node.map {|child| child.to_sum_product(true) } # Distribute while(nchildren.size>1) # print "nchildren=#{nchildren}\n" dist = [] nchildren.each_slice(2) do |left,right| if right then dist << (left.op == :or ? left.distribute(:and,right) : right.distribute(:and,left)) else dist << left end end nchildren = dist end # Generate the or if (nchildren.size > 1) return NodeOr.new(*nchildren) else return nchildren[0] end end |