class Wongi::Engine::NegNode

Attributes

alpha[R]
tests[R]

Public Class Methods

new(parent, tests, alpha, unsafe) click to toggle source
Calls superclass method Wongi::Engine::BetaNode::new
# File lib/wongi-engine/beta/neg_node.rb, line 16
def initialize parent, tests, alpha, unsafe
  super(parent)
  @tests, @alpha, @unsafe = tests, alpha, unsafe
end

Public Instance Methods

alpha_activate(wme) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 21
def alpha_activate wme
  tokens.each do |token|
    if matches?( token, wme ) && ( @unsafe || ! token.generated?( wme ) )# feedback loop protection
      # order matters for proper invalidation
      make_join_result(token, wme)
      #token.delete_children #if token.neg_join_results.empty? # TODO why was this check here? it seems to break things
      children.each do |child|
        child.tokens.each do |t|
          if t.parent == token
            child.beta_deactivate t
            #token.destroy
          end
        end
      end
    end
  end
end
alpha_deactivate(wme) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 39
def alpha_deactivate wme
  wme.neg_join_results.dup.each do |njr|
    tokens.each do |token|
      next unless token == njr.token
      njr.unlink
      if token.neg_join_results.empty?
        children.each do |child|
          child.beta_activate Token.new( child, token, nil, {} )
        end
      end
    end
  end
end
beta_activate(token) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 53
def beta_activate token
  return if tokens.find { |et| et.duplicate? token }
  token.overlay.add_token(token, self)
  alpha.wmes.each do |wme|
    if matches?( token, wme )
      make_join_result(token, wme)
    end
  end
  if token.neg_join_results.empty?
    children.each do |child|
      child.beta_activate Token.new( child, token, nil, {} )
    end
  end
end
beta_deactivate(token) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 68
def beta_deactivate token
  return nil unless tokens.find token
  token.overlay.remove_token(token, self)
  token.deleted!
  if token.parent
    token.parent.children.delete token # should this go into Token#destroy?
  end
  token.neg_join_results.each &:unlink
  children.each do |child|
    child.tokens.each do |t|
      if t.parent == token
        child.beta_deactivate t
        #token.destroy
      end
    end
  end
  token
end
refresh_child(child) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 87
def refresh_child child
  tokens.each do |token|
    if token.neg_join_results.empty?
      child.beta_activate Token.new( child, token, nil, {} )
    end
  end
  alpha.wmes.each do |wme|
    alpha_activate wme
  end
end

Protected Instance Methods

make_join_result(token, wme) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 108
def make_join_result token, wme
  njr = NegJoinResult.new token, wme
  token.neg_join_results << njr
  wme.neg_join_results << njr
end
matches?(token, wme) click to toggle source
# File lib/wongi-engine/beta/neg_node.rb, line 100
def matches? token, wme
  puts "matching #{wme} against #{token}" if debug?
  @tests.each do |test|
    return false unless test.matches?( token, wme )
  end
  true
end