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.



731
732
733
734
735
# File 'lib/core/nodes.rb', line 731

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

Instance Attribute Details

#activationsObject (readonly)

Returns the value of attribute activations.



736
737
738
# File 'lib/core/nodes.rb', line 736

def activations
  @activations
end

Class Method Details

.increment_counterObject



766
767
768
# File 'lib/core/nodes.rb', line 766

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

.reset_counterObject



770
771
772
# File 'lib/core/nodes.rb', line 770

def self.reset_counter
  @@counter = 0
end

Instance Method Details

#assert(context) ⇒ Object



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

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

#retract(fact) ⇒ Object



744
745
746
# File 'lib/core/nodes.rb', line 744

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

#retract_resolve(match) ⇒ Object



756
757
758
759
760
761
762
763
764
# File 'lib/core/nodes.rb', line 756

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)


748
749
750
# File 'lib/core/nodes.rb', line 748

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

#to_sObject



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

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