class Wongi::Engine::DataOverlay

Attributes

parent[R]
rete[R]

Public Class Methods

new(rete, parent = nil) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 7
def initialize(rete, parent = nil)
  @rete = rete
  @parent = parent
  @raw_wmes = Hash.new { |h, k| h[k] = [] }
  @raw_tokens = Hash.new { |h, k| h[k] = [] }
  rete.add_overlay(self)
end

Public Instance Methods

<<(thing) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 46
def <<(thing)
  case thing
  when Array
    assert WME.new(*thing).tap { |wme| wme.overlay = self }
  when WME
    assert(thing)
  else
    raise Error, "overlays can only accept data"
  end
end
add_token(token, beta) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 127
def add_token(token, beta)
  tokens = raw_tokens(beta)
  tokens << token unless tokens.include?(token)
end
add_wme(wme, alpha) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 118
def add_wme(wme, alpha)
  wmes = raw_wmes(alpha)
  wmes << wme unless wmes.include?(wme)
end
ancestor?(other) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 31
def ancestor?(other)
  return false if parent.nil?
  return true if parent == other
  parent.ancestor?(other)
end
assert(wme) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 57
def assert wme
  @next_cascade ||= []
  @next_cascade << [:assert, wme]
  if @current_cascade.nil?
    @current_cascade = @next_cascade
    @next_cascade = nil
    process_cascade
  end
end
dispose() click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 37
def dispose
  return if self == rete.default_overlay
  rete.remove_overlay(self)
  @raw_tokens.values.each do |tokens|
    tokens.each(&:dispose!)
  end
  @raw_tokens.clear
end
highest(other) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 97
def highest(other)
  return self if self == other
  return self if other.nil?
  return self if ancestor?(other)
  return other if other.ancestor?(self)
  nil # unrelated lineages
end
new_child() click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 15
def new_child
  DataOverlay.new(rete, self)
end
process_cascade() click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 80
def process_cascade
  while @current_cascade
    @current_cascade.each do |(operation, wme, options)|
      case operation
      when :assert
        wme.overlay = self
        rete.real_assert(wme)
      when :retract
        rete.real_retract(wme, options)
        wme.overlay = nil
      end
    end
    @current_cascade = @next_cascade
    @next_cascade = nil
  end
end
raw_tokens(beta) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 140
def raw_tokens(beta)
  @raw_tokens[beta.object_id]
end
raw_wmes(alpha) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 136
def raw_wmes(alpha)
  @raw_wmes[alpha.object_id]
end
remove_token(token, beta) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 132
def remove_token(token, beta)
  raw_tokens(beta).delete(token)
end
remove_wme(wme, alpha) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 123
def remove_wme(wme, alpha)
  raw_wmes(alpha).delete(wme)
end
retract(wme, options = { }) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 67
def retract wme, options = { }
  if wme.is_a? Array
    wme = WME.new(*wme)
  end
  @next_cascade ||= []
  @next_cascade << [:retract, wme, options]
  if @current_cascade.nil?
    @current_cascade = @next_cascade
    @next_cascade = nil
    process_cascade
  end
end
tokens(beta) click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 114
def tokens(beta)
  DeleteSafeEnumerator.new(raw_tokens(beta))
end
with_child() { |overlay| ... } click to toggle source
# File lib/wongi-engine/data_overlay.rb, line 19
def with_child
  return unless block_given?
  new_child.tap do |overlay|
    begin
      result = yield overlay
    ensure
      overlay.dispose
    end
    result
  end
end
wmes(alpha) click to toggle source

TODO: this is inconsistent. A WME retracted in-flight will be visible in active enumerators but a token will not. But this is how it works.

# File lib/wongi-engine/data_overlay.rb, line 110
def wmes(alpha)
  DuplicatingEnumerator.new(raw_wmes(alpha))
end