Class: Ruleby::Core::NotNode

Inherits:
JoinNode show all
Defined in:
lib/core/nodes.rb

Overview

This node class is used when a rule is looking for a fact that does not exist. It is a two-input node, and thus has some of the properties of the JoinNode. NOTE it has not clear how this will work if the NotPattern is declared as the first pattern in a rule.

Instance Attribute Summary

Attributes inherited from JoinNode

#ref_nodes

Attributes inherited from ParentNode

#child_nodes

Attributes inherited from Printable

#parent_nodes

Instance Method Summary collapse

Methods inherited from JoinNode

#to_s

Methods inherited from ParentNode

#add_out_node, #assert, #forks?, #propagate_assert, #propagate_retract, #retract

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

Constructor Details

#initializeNotNode

Returns a new instance of NotNode.



637
638
639
# File 'lib/core/nodes.rb', line 637

def initialize
  super
end

Instance Method Details

#assert_left(context) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/core/nodes.rb', line 665

def assert_left(context)    
  add_to_left_memory(context)      
  if @ref_nodes.empty? && @right_memory.empty?
    propagate_assert(context)
  else
    propagate = true
    @right_memory.values.each do |right_context|
      if match_ref_nodes(context,right_context)     
        propagate = false
        break
      end
    end
    propagate_assert(context) if propagate
  end
end

#assert_right(context) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/core/nodes.rb', line 681

def assert_right(context)                    
  @right_memory[context.fact.id] = context
  if @ref_nodes.empty?
    @left_memory.values.flatten.each do |left_context|
      propagate_retract_resolve(left_context.match)
    end
  else
    @left_memory.values.flatten.each do |left_context|
      if match_ref_nodes(left_context,context)
        # QUESTION is there a more efficient way to retract here?
        propagate_retract_resolve(left_context.match)
      end
    end
  end
end

#retract_left(fact) ⇒ Object



641
642
643
644
# File 'lib/core/nodes.rb', line 641

def retract_left(fact)
  @left_memory.delete(fact.id)
  propagate_retract(fact)
end

#retract_right(fact) ⇒ Object



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/core/nodes.rb', line 646

def retract_right(fact)
  right_context = @right_memory.delete(fact.id)  
  unless right_context == @right_memory.default
    unless @ref_nodes.empty? && !@right_memory.empty?
      @left_memory.values.each do |lm|
        lm.each do |left_context|
          # TODO we should cache the matches on the left that were unmatched 
          # by a result from a NotPattern.  We could hash them by the right 
          # match that caused this.  That we way we would not have to 
          # re-compare the the left and right matches.
          if match_ref_nodes(left_context,right_context)      
            propagate_assert(left_context)
          end
        end
      end   
    end
  end
end