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, #forks?, #propagate_assert, #propagate_retract, #retract

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

Constructor Details

#initializeJoinNode

Returns a new instance of JoinNode.



561
562
563
564
565
566
# File 'lib/core/nodes.rb', line 561

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

Instance Attribute Details

#ref_nodesObject

Returns the value of attribute ref_nodes.



559
560
561
# File 'lib/core/nodes.rb', line 559

def ref_nodes
  @ref_nodes
end

Instance Method Details

#assert_left(context) ⇒ Object



578
579
580
581
582
583
584
585
586
587
# File 'lib/core/nodes.rb', line 578

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



589
590
591
592
593
594
595
596
597
598
# File 'lib/core/nodes.rb', line 589

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

#retract_left(fact) ⇒ Object



568
569
570
571
# File 'lib/core/nodes.rb', line 568

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

#retract_resolve(match) ⇒ Object



604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/core/nodes.rb', line 604

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



573
574
575
576
# File 'lib/core/nodes.rb', line 573

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

#to_sObject



600
601
602
# File 'lib/core/nodes.rb', line 600

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