Class: Wongi::Engine::OptionalNode

Inherits:
BetaNode
  • Object
show all
Includes:
TokenContainer
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, #depth, #refresh, #root?

Methods included from CoreExt

included

Constructor Details

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

Returns a new instance of OptionalNode.



16
17
18
19
20
21
# File 'lib/wongi-engine/beta/optional_node.rb', line 16

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.



14
15
16
# File 'lib/wongi-engine/beta/optional_node.rb', line 14

def alpha
  @alpha
end

#assignment_patternObject (readonly)

Returns the value of attribute assignment_pattern.



14
15
16
# File 'lib/wongi-engine/beta/optional_node.rb', line 14

def assignment_pattern
  @assignment_pattern
end

#testsObject (readonly)

Returns the value of attribute tests.



14
15
16
# File 'lib/wongi-engine/beta/optional_node.rb', line 14

def tests
  @tests
end

Instance Method Details

#alpha_activate(wme) ⇒ Object



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

def alpha_activate wme
  assignments = collect_assignments( wme )
  tokens.each do |token|
    if matches? token, wme
      children.each do |child|
        if token.optional?
          token.no_optional!
          child.tokens.each do |ct|
            child.beta_deactivate(ct) if ct.parent == token
          end
        end
        child.beta_activate Token.new( child, token, wme, assignments )
      end
      make_opt_result token, wme
    end
  end
end

#alpha_deactivate(wme) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wongi-engine/beta/optional_node.rb', line 47

def alpha_deactivate wme
  wme.opt_join_results.dup.each do |ojr|
    tokens.each do |token|
      next unless token == ojr.token
      ojr.unlink
      if token.opt_join_results.empty?
        children.each do |child|
          child.tokens.each do |ct|
            child.beta_deactivate(ct) if ct.parent == token
          end
          token.optional!
          child.beta_activate Token.new( child, token, nil, { } )
        end
      end
    end
  end
end

#beta_activate(t) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wongi-engine/beta/optional_node.rb', line 65

def beta_activate t
  return if tokens.find { |token| token.parent == t }
  token = Token.new( self, t, nil, { } )
  token.overlay.add_token(token, self)
  match = false
  alpha.wmes.each do |wme|
    assignments = collect_assignments(wme)
    if 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
  end
  unless match
    token.optional!
    children.each do |child|
      child.beta_activate Token.new( child, token, nil, { } )
    end
  end
end

#beta_deactivate(t) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wongi-engine/beta/optional_node.rb', line 88

def beta_deactivate t
  token = tokens.find { |token| token.parent == t }
  return unless token
  token.overlay.remove_token(token, self)
  token.deleted!
  if token.parent
    token.parent.children.delete token
  end
  token.opt_join_results.each &:unlink
  children.each do |child|
    child.tokens.each do |t|
      if t.parent == token
        child.beta_deactivate t
      end
    end
  end
  token
end

#delete_token(token) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/wongi-engine/beta/optional_node.rb', line 117

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

#make_opt_result(token, wme) ⇒ Object



23
24
25
26
27
# File 'lib/wongi-engine/beta/optional_node.rb', line 23

def make_opt_result token, wme
  jr = OptionalJoinResult.new token, wme
  token.opt_join_results << jr
  wme.opt_join_results << jr
end

#refresh_child(child) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/wongi-engine/beta/optional_node.rb', line 107

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