Class: Wongi::Engine::OptionalNode

Inherits:
BetaNode
  • Object
show all
Defined in:
lib/wongi-engine/beta/optional_node.rb

Instance Attribute Summary collapse

Attributes inherited from BetaNode

#children, #parent, #rete

Instance Method Summary collapse

Methods inherited from BetaNode

#assignment_node, #beta_deactivate_children, #depth, #empty?, #overlay, #refresh, #root?, #size, #tokens

Methods included from CoreExt

included

Constructor Details

#initialize(parent, alpha, tests, assignments) ⇒ OptionalNode

Returns a new instance of OptionalNode.



8
9
10
11
12
13
# 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

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



6
7
8
# File 'lib/wongi-engine/beta/optional_node.rb', line 6

def alpha
  @alpha
end

#assignment_patternObject (readonly)

Returns the value of attribute assignment_pattern.



6
7
8
# File 'lib/wongi-engine/beta/optional_node.rb', line 6

def assignment_pattern
  @assignment_pattern
end

#testsObject (readonly)

Returns the value of attribute tests.



6
7
8
# File 'lib/wongi-engine/beta/optional_node.rb', line 6

def tests
  @tests
end

Instance Method Details

#alpha_activate(wme, children: self.children) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



78
79
80
81
82
# 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) ⇒ Object



84
85
86
87
88
89
90
91
# 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