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
-
#clone ⇒ 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
Converts 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.
741 742 743 |
# File 'lib/logic_tools/logictree.rb', line 741 def initialize(*children) super(:and,*children) end |
Instance Method Details
#clone ⇒ Object
Duplicates the node.
746 747 748 |
# File 'lib/logic_tools/logictree.rb', line 746 def clone # :nodoc: return NodeAnd.new(*@children.map(&:clone)) end |
#eval ⇒ Object
Computes the value of the node.
751 752 753 |
# File 'lib/logic_tools/logictree.rb', line 751 def eval() return !@children.any? {|child| child.eval() == false } end |
#to_s ⇒ Object
Converts to a string.
787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
# File 'lib/logic_tools/logictree.rb', line 787 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
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 783 784 |
# File 'lib/logic_tools/logictree.rb', line 758 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 |