Class: Ruleby::Core::TerminalNode
- 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.
Instance Attribute Summary
Attributes inherited from Printable
Instance Method Summary collapse
-
#activations ⇒ Object
attr_reader:activations.
- #assert(context) ⇒ Object
-
#initialize(bucket, rule) ⇒ TerminalNode
constructor
A new instance of TerminalNode.
- #modify(context) ⇒ Object
- #retract(fact) ⇒ Object
- #retract_resolve(match) ⇒ Object
- #satisfied? ⇒ Boolean
- #to_s ⇒ Object
Methods inherited from Node
Methods inherited from Printable
Constructor Details
#initialize(bucket, rule) ⇒ TerminalNode
Returns a new instance of TerminalNode.
912 913 914 915 916 |
# File 'lib/core/nodes.rb', line 912 def initialize(bucket, rule) super(bucket) @rule = rule @activations = MultiHash.new end |
Instance Method Details
#activations ⇒ Object
attr_reader:activations
919 920 921 |
# File 'lib/core/nodes.rb', line 919 def activations @activations end |
#assert(context) ⇒ Object
923 924 925 926 927 |
# File 'lib/core/nodes.rb', line 923 def assert(context) match = context.match a = Activation.new(@rule.action, match, @bucket.counter) @activations.add match.fact_ids, a end |
#modify(context) ⇒ Object
929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/core/nodes.rb', line 929 def modify(context) found = false @activations.each do |id, v| if context.match.fact_ids.sort == id.sort v.modify(context.match) found = true end end if !found and context.match.is_match assert(context) end end |
#retract(fact) ⇒ Object
942 943 944 |
# File 'lib/core/nodes.rb', line 942 def retract(fact) @activations.remove fact.id end |
#retract_resolve(match) ⇒ Object
954 955 956 957 958 959 960 961 962 |
# File 'lib/core/nodes.rb', line 954 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
946 947 948 |
# File 'lib/core/nodes.rb', line 946 def satisfied? return !@activations.values.empty? end |
#to_s ⇒ Object
950 951 952 |
# File 'lib/core/nodes.rb', line 950 def to_s return "TerminalNode:#{object_id} | #{@activations.values}" end |