Class: Ruleby::Core::JoinNode

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

Overview

This class is a two-input node that is used to create a cross-product of the two network branches above it. It keeps a memory of the left and right inputs and compares new facts to each. These nodes make up what is called the Beta network.

Direct Known Subclasses

NotNode

Instance Attribute Summary collapse

Attributes inherited from ParentNode

#child_nodes

Attributes inherited from Printable

#parent_nodes

Instance Method Summary collapse

Methods inherited from ParentNode

#add_out_node, #assert, #modify, #propagate, #propagate_assert, #propagate_modify, #propagate_retract, #retract

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

Constructor Details

#initialize(bucket) ⇒ JoinNode

Returns a new instance of JoinNode.



714
715
716
717
718
719
# File 'lib/core/nodes.rb', line 714

def initialize(bucket)
  super
  @left_memory = {}
  @right_memory = {} 
  @ref_nodes = []
end

Instance Attribute Details

#ref_nodesObject

Returns the value of attribute ref_nodes.



712
713
714
# File 'lib/core/nodes.rb', line 712

def ref_nodes
  @ref_nodes
end

Instance Method Details

#assert_left(context) ⇒ Object



731
732
733
734
735
736
737
738
739
740
# File 'lib/core/nodes.rb', line 731

def assert_left(context)
  add_to_left_memory(context)
  @right_memory.values.each do |right_context|
    mr = match_ref_nodes(context,right_context)      
    if (mr.is_match)
      new_context = MatchContext.new context.fact, mr  
      propagate_assert(new_context)
    end
  end
end

#assert_right(context) ⇒ Object



742
743
744
745
746
747
748
749
750
751
# File 'lib/core/nodes.rb', line 742

def assert_right(context)
  @right_memory[context.fact.id] = context
  @left_memory.values.flatten.each do |left_context|
    mr = match_ref_nodes(left_context,context)      
    if (mr.is_match)
      new_context = MatchContext.new context.fact, mr                
      propagate_assert(new_context)
    end
  end
end

#modify_left(context) ⇒ Object



753
754
755
756
757
758
759
760
761
762
# File 'lib/core/nodes.rb', line 753

def modify_left(context)
  @left_memory[context.fact.id] = [context]
  @right_memory.values.each do |right_context|
    mr = match_ref_nodes(context,right_context)
    if (mr.is_match)
      new_context = MatchContext.new context.fact, mr
      propagate_modify(new_context)
    end
  end
end

#modify_right(context) ⇒ Object



764
765
766
767
768
769
770
# File 'lib/core/nodes.rb', line 764

def modify_right(context)
  @right_memory[context.fact.id] = context
  # you can't ref :collect patterns, so there should be anything to do on the left-memory
  #propagate_modify(context)

  # TODO something needs to happen here - but i'm not sure what!
end

#retract_left(fact) ⇒ Object



721
722
723
724
# File 'lib/core/nodes.rb', line 721

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

#retract_resolve(match) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/core/nodes.rb', line 776

def retract_resolve(match)
  # in this method we retract an existing match from memory if it resolves
  # with the match given.  It would probably be better to check if it 
  # resolves with a list of facts.  But the system is not set up for
  # that yet.
  @left_memory.each do |fact_id,contexts|
    contexts.delete_if do |left_context|          
      resolve(left_context.match, match)
    end        
  end
  propagate_retract_resolve(match)
end

#retract_right(fact) ⇒ Object



726
727
728
729
# File 'lib/core/nodes.rb', line 726

def retract_right(fact)
  @right_memory.delete(fact.id)
  propagate_retract(fact)
end

#to_sObject



772
773
774
# File 'lib/core/nodes.rb', line 772

def to_s
  return "#{self.class}:#{object_id} | #{@left_memory.values} | #{@right_memory}"
end