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?, #eval_input, #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.
756 757 758 |
# File 'lib/logic_tools/logictree.rb', line 756 def initialize(*children) super(:and,*children) end |
Instance Method Details
#clone ⇒ Object
Duplicates the node.
761 762 763 |
# File 'lib/logic_tools/logictree.rb', line 761 def clone # :nodoc: return NodeAnd.new(*@children.map(&:clone)) end |
#eval ⇒ Object
Computes the value of the node.
766 767 768 |
# File 'lib/logic_tools/logictree.rb', line 766 def eval() return !@children.any? {|child| child.eval() == false } end |
#to_s ⇒ Object
Converts to a string.
812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
# File 'lib/logic_tools/logictree.rb', line 812 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
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/logic_tools/logictree.rb', line 773 def to_sum_product(flattened = false) # :nodoc: # print "AND to_sum_product with tree=#{self}\n" # 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 do |child| # print "recurse to_sum_product for child=#{child}\n" # res = child.to_sum_product(true) # print "child=#{child} -> res=#{res}\n" # res child.to_sum_product(true) end # 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 # print "dist=#{dist}\n" nchildren = dist end # print "result=#{nchildren}\n" # # Generate the or # if (nchildren.size > 1) # return NodeOr.new(*nchildren) # else # return nchildren[0] # end return nchildren[0].flatten end |