class Wongi::Engine::OptionalNode

Attributes

alpha[R]
assignment_pattern[R]
tests[R]

Public Class Methods

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

Public Instance Methods

alpha_activate(wme, children: self.children) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 15
def alpha_activate(wme, children: self.children)
  assignments = collect_assignments(wme)
  tokens.each do |token|
    next unless matches? token, wme

    optional = overlay.opt_join_results_for(token: token).empty?

    children.each do |child|
      if optional
        # we're going to change the optional state so the old ones need to be removed
        child.tokens.each do |ct|
          child.beta_deactivate(ct) if ct.child_of?(token)
        end
      end
      child.beta_activate Token.new(child, token, wme, assignments)
    end
    overlay.add_opt_join_result(OptionalJoinResult.new(token, wme))
  end
end
alpha_deactivate(wme) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 35
def alpha_deactivate(wme)
  # p alpha_deactivate: {wme:}
  overlay.opt_join_results_for(wme: wme).each do |ojr|
    tokens.each do |token|
      next unless token == ojr.token

      overlay.remove_opt_join_result(ojr)
      next unless overlay.opt_join_results_for(token: token).empty?

      children.each do |child|
        child.tokens.each do |ct|
          child.beta_deactivate(ct) if ct.child_of?(token)
        end
        child.beta_activate Token.new(child, token, nil, {})
      end
    end
  end
end
beta_activate(token) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 54
def beta_activate(token)
  return if tokens.find { |t| t.duplicate? token }

  overlay.add_token(token)

  match = false
  template = specialize(alpha.template, tests, token)
  select_wmes(template).each do |wme|
    assignments = collect_assignments(wme)
    next unless matches? token, wme

    match = true
    children.each do |child|
      child.beta_activate Token.new(child, token, wme, assignments)
    end
    make_opt_result token, wme
  end
  return if match

  children.each do |child|
    child.beta_activate Token.new(child, token, nil, {})
  end
end
beta_deactivate(token) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 78
def beta_deactivate(token)
  # p beta_deactivate: {class: self.class, object_id:, token:}
  overlay.remove_token(token)
  beta_deactivate_children(token: token)
end
refresh_child(child) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 84
def refresh_child(child)
  tokens.each do |token|
    child.beta_activate(Token.new(child, token, nil, {}))
  end
  select_wmes(alpha.template).each do |wme|
    alpha_activate wme, children: [child]
  end
end

Private Instance Methods

collect_assignments(wme) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 102
def collect_assignments(wme)
  assignments = {}
  return assignments if assignment_pattern.nil?

  assignments[assignment_pattern.subject] = TokenAssignment.new(wme, :subject) if assignment_pattern.subject != :_
  assignments[assignment_pattern.predicate] = TokenAssignment.new(wme, :predicate) if assignment_pattern.predicate != :_
  assignments[assignment_pattern.object] = TokenAssignment.new(wme, :object) if assignment_pattern.object != :_
  assignments
end
matches?(token, wme) click to toggle source
# File lib/wongi-engine/beta/optional_node.rb, line 95
def matches?(token, wme)
  @tests.each do |test|
    return false unless test.matches?(token, wme)
  end
  true
end