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

#modify, #propagate, #propagate_assert, #propagate_modify, #propagate_retract

Methods inherited from Node

#resolve

Methods inherited from Printable

#print

Constructor Details

#initialize(bucket, atom) ⇒ HashedNode

Returns a new instance of HashedNode.



385
386
387
388
# File 'lib/core/nodes.rb', line 385

def initialize(bucket, atom)
  super
  @values = {}
end

Instance Method Details

#add_out_node(node, path, atom) ⇒ Object



390
391
392
393
394
395
396
397
398
# File 'lib/core/nodes.rb', line 390

def add_out_node(node, path, atom)
  k = hash_by(atom)
  @values[k] = {} if @values[k].nil?
  if @values[k][node].nil?
    @values[k][node] = [path]
  else
    @values[k][node] << path
  end
end

#assert(assertable) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/core/nodes.rb', line 404

def assert(assertable)
  k = assertable.fact.object.send(@atom.slot)

  # TODOwe need to do this for ALL tags if this node is shared
  assertable.add_tag(@atom.tag, k)
  propagate_assert assertable, (@values[k] ? @values[k] : {})
rescue NoMethodError => e
  @bucket.add_error Error.new(:no_method, :warn, {
      :object => assertable.fact.object.to_s,
      :method => e.name,
      :message => e.message
  })
end

#retract(assertable) ⇒ Object



400
401
402
# File 'lib/core/nodes.rb', line 400

def retract(assertable)
  propagate_retract assertable, @values.values.inject({}){|p,c| p.merge(c)} #(@values[k] ? @values[k] : {})
end