class Wongi::Engine::Token

Attributes

node[R]
parents[R]
wme[R]

Public Class Methods

new(node, parents, wme, assignments = {}) click to toggle source
# File lib/wongi-engine/token.rb, line 7
def initialize(node, parents, wme, assignments = {})
  @node = node
  @parents = Set.new(Array(parents))
  @wme = wme
  @assignments = assignments
end

Public Instance Methods

[](var) click to toggle source
# File lib/wongi-engine/token.rb, line 40
def [](var)
  a = assignment(var)
  a.respond_to?(:call) ? a.call(self) : a
end
ancestors() click to toggle source
# File lib/wongi-engine/token.rb, line 14
def ancestors
  parents.flat_map(&:ancestors).uniq + parents.to_a
end
assignment(x) click to toggle source
# File lib/wongi-engine/token.rb, line 49
def assignment(x)
  return @assignments[x] if has_own_var?(x)

  parents.each do |parent|
    a = parent.assignment(x)
    return a if a
  end

  nil
end
assignments() click to toggle source
# File lib/wongi-engine/token.rb, line 34
def assignments
  parents.each_with_object({}) do |parent, acc|
    acc.merge!(parent.assignments)
  end.merge(own_assignments)
end
child_of?(token) click to toggle source
# File lib/wongi-engine/token.rb, line 18
def child_of?(token)
  parents.include?(token)
end
duplicate?(other) click to toggle source

TODO: ignore assignments?

# File lib/wongi-engine/token.rb, line 69
def duplicate?(other)
  instance_of?(other.class) &&
    parents == other.parents &&
    wme == other.wme &&
    own_assignments == other.own_assignments
end
has_own_var?(x) click to toggle source
# File lib/wongi-engine/token.rb, line 64
def has_own_var?(x)
  @assignments.key?(x)
end
has_var?(x) click to toggle source
# File lib/wongi-engine/token.rb, line 60
def has_var?(x)
  has_own_var?(x) || parents.any? { _1.has_var?(x) }
end
inspect() click to toggle source
# File lib/wongi-engine/token.rb, line 83
def inspect
  to_s
end
own_assignments() click to toggle source
# File lib/wongi-engine/token.rb, line 30
def own_assignments
  @assignments
end
set(variable, value) click to toggle source
# File lib/wongi-engine/token.rb, line 26
def set(variable, value)
  @assignments[variable] = value
end
subst(variable, value) click to toggle source
# File lib/wongi-engine/token.rb, line 22
def subst(variable, value)
  @assignments[variable] = value if @assignments.key? variable
end
to_s() click to toggle source
# File lib/wongi-engine/token.rb, line 76
def to_s
  str = "TOKEN [ #{object_id} ancestors=#{ancestors.map(&:object_id).map(&:to_s).join('.')} "
  assignments.each_pair { |key, value| str << "#{key}=#{value.is_a?(TokenAssignment) ? "#{value.call} (#{value})" : value} " }
  str << "]"
  str
end
values_at(*vars) click to toggle source
# File lib/wongi-engine/token.rb, line 45
def values_at(*vars)
  vars.map { self[_1] }
end