Class: Ruleby::Core::HashedNode
- Defined in:
- lib/core/nodes.rb
Overview
This is a base class for any node that hashes out_nodes by value. A node that inherits this class does not evaluate each condition, instead it looks up the expected value in the hash, and gets a list of out_nodes.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from AtomNode
Attributes inherited from ParentNode
Attributes inherited from Printable
Instance Method Summary collapse
- #add_out_node(node, atom) ⇒ Object
- #assert(fact) ⇒ Object
-
#forks?(atom) ⇒ Boolean
returns true if this node is already being used for the same atom.
-
#initialize(atom) ⇒ HashedNode
constructor
A new instance of HashedNode.
- #retract(fact) ⇒ Object
Methods inherited from AtomNode
Methods inherited from ParentNode
#propagate_assert, #propagate_retract
Methods inherited from Node
Methods inherited from Printable
Constructor Details
#initialize(atom) ⇒ HashedNode
Returns a new instance of HashedNode.
352 353 354 355 356 |
# File 'lib/core/nodes.rb', line 352 def initialize(atom) super @values = {} @values.default = [] end |
Instance Method Details
#add_out_node(node, atom) ⇒ Object
366 367 368 369 370 371 372 373 374 |
# File 'lib/core/nodes.rb', line 366 def add_out_node(node,atom) k = hash_by(atom) v = @values[k] if v.empty? @values[k] = [node] elsif !v.index node @values[k] = v << node end end |
#assert(fact) ⇒ Object
380 381 382 383 384 385 386 |
# File 'lib/core/nodes.rb', line 380 def assert(fact) k = fact.object.send(@atom.method) propagate_assert fact, @values[k] rescue NoMethodError => e # If the method does not exist, it is the same as if it evaluted to # false, and the network traverse stops end |
#forks?(atom) ⇒ Boolean
returns true if this node is already being used for the same atom. That is, if it is used again it will fork the network (or the network may already be forked).
361 362 363 364 |
# File 'lib/core/nodes.rb', line 361 def forks?(atom) k = hash_by(atom) return !@values[k].empty? end |
#retract(fact) ⇒ Object
376 377 378 |
# File 'lib/core/nodes.rb', line 376 def retract(fact) propagate_retract fact, @values.values.flatten end |