Class: Ruleby::Core::HashedNode

Inherits:
AtomNode show all
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

EqualsNode, TypeNode

Instance Attribute Summary

Attributes inherited from AtomNode

#atom

Attributes inherited from ParentNode

#child_nodes

Attributes inherited from Printable

#parent_nodes

Instance Method Summary collapse

Methods inherited from AtomNode

#==, #shareable?, #to_s

Methods inherited from ParentNode

#propagate_assert, #propagate_retract

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

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).

Returns:

  • (Boolean)


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