Class: Wongi::Engine::OptionalNode

Inherits:
JoinNode show all
Defined in:
lib/wongi-engine/beta/optional_node.rb

Instance Attribute Summary

Attributes inherited from JoinNode

#alpha, #assignment_pattern, #filters, #tests

Attributes inherited from BetaNode

#children, #parent, #rete

Instance Method Summary collapse

Methods inherited from JoinNode

compile, #equivalent?

Methods inherited from BetaNode

#assignment_node, #beta_memory, #depth, #filter_node, #join_node, #ncc_node, #neg_node, #network, #optional_node, #refresh

Methods included from CoreExt

included

Constructor Details

#initialize(parent, tests, assignment) ⇒ OptionalNode

Returns a new instance of OptionalNode.



8
9
10
# File 'lib/wongi-engine/beta/optional_node.rb', line 8

def initialize parent, tests, assignment
  super parent, tests, assignment, []
end

Instance Method Details

#alpha_activate(wme) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wongi-engine/beta/optional_node.rb', line 12

def alpha_activate wme
  parent.tokens.each do |token|
    if matches? token, wme
      if token.has_optional?
        token.has_optional = false
        token.delete_children
      end
      propagate_activation(token, wme, collect_assignments(wme))
      jr = OptionalJoinResult.new token, wme
      token.opt_join_results << jr
      wme.opt_join_results << jr
    end
  end
end

#beta_activate(token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wongi-engine/beta/optional_node.rb', line 27

def beta_activate token
  match = false
  alpha.wmes.each do |wme|
    assignments = collect_assignments(wme)
    if matches? token, wme
      match = true
      propagate_activation(token, wme, assignments)
      jr = OptionalJoinResult.new token, wme
      token.opt_join_results << jr
      wme.opt_join_results << jr
    end
  end
  unless match
    token.has_optional = true
    propagate_activation(token, nil, {})
  end
end

#delete_token(token) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/wongi-engine/beta/optional_node.rb', line 55

def delete_token token
  tokens.delete token
  token.opt_join_results.each do |ojr|
    ojr.wme.opt_join_results.delete ojr
  end
  token.opt_join_results.clear
end

#refresh_child(child) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/wongi-engine/beta/optional_node.rb', line 45

def refresh_child child
  tmp = children
  self.children = [ child ]
  refresh # do the beta part
  alpha.wmes.each do |wme|
    alpha_activate wme
  end
  self.children = tmp
end