Class: Ruleby::Core::TerminalNode

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

Overview

This class represents the bottom node in the network. There is a one to one relation between TerminalNodes and Rules. A terminal node acts as a wrapper for a rule. The class is responsible for keeping a memory of the activations that have been generated by the network.

Constant Summary collapse

@@counter =
0

Instance Attribute Summary collapse

Attributes inherited from Printable

#parent_nodes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

Constructor Details

#initialize(rule) ⇒ TerminalNode

Returns a new instance of TerminalNode.



717
718
719
720
721
# File 'lib/core/nodes.rb', line 717

def initialize(rule)
  super()
  @rule = rule
  @activations = MultiHash.new  
end

Instance Attribute Details

#activationsObject (readonly)

Returns the value of attribute activations.



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

def activations
  @activations
end

Class Method Details

.increment_counterObject



752
753
754
# File 'lib/core/nodes.rb', line 752

def self.increment_counter
  @@counter = @@counter + 1
end

.reset_counterObject



756
757
758
# File 'lib/core/nodes.rb', line 756

def self.reset_counter
  @@counter = 0
end

Instance Method Details

#assert(context) ⇒ Object



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

def assert(context)
  match = context.match
  a = Activation.new(@rule.action, match, @@counter)
  @activations.add match.fact_ids, a
end

#retract(fact) ⇒ Object



730
731
732
# File 'lib/core/nodes.rb', line 730

def retract(fact)
  @activations.remove fact.id
end

#retract_resolve(match) ⇒ Object



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

def retract_resolve(match)
  # in this method we retract an existing activation from memory if its 
  # match 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.
  @activations.delete_if do |activation|
    resolve(activation.match, match)
  end
end

#satisfied?Boolean

Returns:

  • (Boolean)


734
735
736
# File 'lib/core/nodes.rb', line 734

def satisfied?
  return !@activations.values.empty? 
end

#to_sObject



738
739
740
# File 'lib/core/nodes.rb', line 738

def to_s
  return "TerminalNode:#{object_id} | #{@activations.values}"
end