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.



922
923
924
# File 'lib/logic_tools/logictree.rb', line 922

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

Instance Method Details

#cloneObject

Duplicates the node.



927
928
929
# File 'lib/logic_tools/logictree.rb', line 927

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

#evalObject

Computes the value of the node.



932
933
934
# File 'lib/logic_tools/logictree.rb', line 932

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.


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

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.


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

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.



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

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


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

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