class Wongi::Engine::Token

Attributes

children[R]
generated_wmes[R]
ncc_results[R]
neg_join_results[R]
node[R]
opt_join_results[R]
overlay[R]
owner[RW]
parent[RW]
wme[R]

Public Class Methods

new(node, token, wme, assignments) click to toggle source
# File lib/wongi-engine/token.rb, line 19
def initialize node, token, wme, assignments
  @node, @parent, @wme, @assignments = node, token, wme, assignments
  @overlay = wme ? wme.overlay.highest(token.overlay) : (token ? token.overlay : node.rete.default_overlay)
  @children = []
  @deleted = false
  @neg_join_results = []
  @opt_join_results = []
  @ncc_results = []
  @generated_wmes = []
  token.children << self if token
end

Public Instance Methods

[](var) click to toggle source
# File lib/wongi-engine/token.rb, line 50
def [] var
  if a = assignments[ var ]
    a.respond_to?(:call) ? a.call( self ) : a
  end
end
ancestors() click to toggle source
# File lib/wongi-engine/token.rb, line 31
def ancestors
  if parent
    parent.ancestors.unshift parent
  else
    []
  end
end
assignments() click to toggle source
# File lib/wongi-engine/token.rb, line 46
def assignments
  @cached_assignments ||= all_assignments
end
destroy() click to toggle source
# File lib/wongi-engine/token.rb, line 72
def destroy
  deleted!
end
dispose!() click to toggle source
# File lib/wongi-engine/token.rb, line 76
def dispose!
  parent.children.delete(self) if parent
  neg_join_results.dup.each(&:unlink)
  opt_join_results.dup.each(&:unlink)
  @parent = nil
  @wme = nil
end
duplicate?(other) click to toggle source

TODO ignore assignments?

# File lib/wongi-engine/token.rb, line 61
def duplicate? other
  self.parent.equal?(other.parent) && @wme.equal?(other.wme) && self.assignments == other.assignments
end
generated?(wme) click to toggle source

for neg feedback loop protection

# File lib/wongi-engine/token.rb, line 85
def generated? wme
  return true if generated_wmes.any? { |w| w == wme }
  return children.any? { |t| t.generated? wme }
end
has_var?(x) click to toggle source
# File lib/wongi-engine/token.rb, line 56
def has_var? x
  assignments.has_key? x
end
subst(variable, value) click to toggle source
# File lib/wongi-engine/token.rb, line 39
def subst variable, value
  @cached_assignments = nil
  if @assignments.has_key? variable
    @assignments[ variable ] = value
  end
end
to_s() click to toggle source
# File lib/wongi-engine/token.rb, line 65
def to_s
  str = "TOKEN [ #{object_id} parent=#{parent ? parent.object_id : 'nil'} "
  all_assignments.each_pair { |key, value| str << "#{key} => #{value} " }
  str << "]"
  str
end

Protected Instance Methods

all_assignments() click to toggle source
# File lib/wongi-engine/token.rb, line 92
def all_assignments
  raise "Assignments is not a hash" unless @assignments.kind_of?( Hash )
  if @parent
    @parent.assignments.merge @assignments
  else
    @assignments
  end
end