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?, #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.



920
921
922
# File 'lib/logic_tools/logictree.rb', line 920

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

Instance Method Details

#dupObject

Duplicates the node.



925
926
927
# File 'lib/logic_tools/logictree.rb', line 925

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

#evalObject

Computes the value of the node.



930
931
932
# File 'lib/logic_tools/logictree.rb', line 930

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.


938
939
940
941
942
943
944
945
# File 'lib/logic_tools/logictree.rb', line 938

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.


951
952
953
954
955
956
957
958
# File 'lib/logic_tools/logictree.rb', line 951

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.



990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/logic_tools/logictree.rb', line 990

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


963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/logic_tools/logictree.rb', line 963

def to_sum_product(flattened = false) # :nodoc:
    # 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