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, #getVariablesRecurse, #size, #to_sym

Methods inherited from Node

#distribute, #each, #each_line, #each_maxterm, #each_minterm, #eql?, #getVariables, #hash, #inspect, #op, #simplify, #size, #to_std_conjunctive, #to_std_disjunctive, #to_sym

Constructor Details

#initialize(child) ⇒ NodeNot

Creates a NOT node with a child.



750
751
752
# File 'lib/logic_tools/logictree.rb', line 750

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

Instance Method Details

#dupObject

Duplicates the node.



755
756
757
# File 'lib/logic_tools/logictree.rb', line 755

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

#evalObject

Computes the value of the node.



760
761
762
# File 'lib/logic_tools/logictree.rb', line 760

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.


768
769
770
771
772
773
774
775
# File 'lib/logic_tools/logictree.rb', line 768

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.


781
782
783
784
785
786
787
788
# File 'lib/logic_tools/logictree.rb', line 781

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.



798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/logic_tools/logictree.rb', line 798

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


793
794
795
# File 'lib/logic_tools/logictree.rb', line 793

def to_sum_product(flattened = false) # :nodoc:
    return NodeNot.new(@child.to_sum_product(flatten))
end