Class: LogicTools::NodeNot

Inherits:
NodeUnary show all
Defined in:
lib/logic_tools/logictree.rb

Overview

Represents a NOT node.

Instance Attribute Summary

Attributes inherited from NodeUnary

#child, #op

Instance Method Summary collapse

Methods inherited from NodeUnary

#==, #each, #get_variablesRecurse, #include?, #is_parent?, #size, #to_sym

Methods inherited from Node

#cover?, #distribute, #each, #each_line, #each_maxterm, #each_minterm, #eql?, #eval_input, #get_variables, #hash, #include?, #inspect, #is_parent?, #op, #reduce, #simplify, #size, #to_cover, #to_std_conjunctive, #to_std_disjunctive, #to_sym

Constructor Details

#initialize(child) ⇒ NodeNot

Creates a NOT node with a child.



948
949
950
# File 'lib/logic_tools/logictree.rb', line 948

def initialize(child)
    super(:not,child)
end

Instance Method Details

#cloneObject

Duplicates the node.



953
954
955
# File 'lib/logic_tools/logictree.rb', line 953

def clone # :nodoc:
    return NodeNot.new(@child.clone)
end

#evalObject

Computes the value of the node.



958
959
960
# File 'lib/logic_tools/logictree.rb', line 958

def eval # :nodoc:
    return !child.eval
end

#flattenObject

Creates a new tree where the and, or or not operator of

the current node is flattened.

Default: simply duplicates the node.


966
967
968
969
970
971
972
973
# File 'lib/logic_tools/logictree.rb', line 966

def flatten # :nodoc:
    nchild = @child.flatten
    if nchild.op == :not then
        return nchild.child
    else
        return NodeNot.new(nchild)
    end
end

#flatten_deepObject

Creates a new tree where all the and, or and not operators

from the current node are flattened.

Default: simply duplicate.


979
980
981
982
983
984
985
986
# File 'lib/logic_tools/logictree.rb', line 979

def flatten_deep # :nodoc:
    nchild = @child.flatten_deep
    if nchild.op == :not then
        return nchild.child
    else
        return NodeNot.new(child)
    end
end

#to_sObject

Converts to a string.



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/logic_tools/logictree.rb', line 1019

def to_s # :nodoc:
    return @str if @str
    # Is the child a binary node?
    if child.op == :or || child.op == :and then
        # Yes must put parenthesis
        @str = "~(" + child.to_s + ")"
    else
        # No
        @str = "~" + child.to_s
    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


991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/logic_tools/logictree.rb', line 991

def to_sum_product(flattened = false) # :nodoc:
    # print "NOT to_sum_product with tree=#{self}\n"
    # return NodeNot.new(@child.to_sum_product(flatten))
    # Flatten deeply if required.
    nnode = flattened ? self : self.flatten_deep
    if (nnode.op != :not) then
        # Not a NOT any longer.
        return nnode.to_sum_product
    end
    # Still a NOT, so apply De Morgan's law.
    child = nnode.child
    if child.op == :or then
        # Can apply De Morgan's law for OR.
        return NodeAnd.new( *child.each.map do |n|
            NodeNot.new(n).to_sum_product
        end ).to_sum_product
    elsif child.op == :and then
        # Can apply De Morgan's law for AND.
        return NodeOr.new( *child.each.map do |n|
            NodeNot.new(n).to_sum_product
        end )
    else
        # Nothing to do more.
        return nnode
    end
end