Class: Wongi::Engine::NegNode

Inherits:
BetaNode
  • Object
show all
Defined in:
lib/wongi-engine/beta/neg_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, tests, alpha) ⇒ NegNode

Returns a new instance of NegNode.



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

def initialize(parent, tests, alpha)
  super(parent)
  @tests = tests
  @alpha = alpha
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



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

def alpha
  @alpha
end

#testsObject (readonly)

Returns the value of attribute tests.



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

def tests
  @tests
end

Instance Method Details

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



14
15
16
17
18
19
20
21
22
23
# File 'lib/wongi-engine/beta/neg_node.rb', line 14

def alpha_activate(wme, children: self.children)
  # p alpha_activate: {class: self.class, object_id:, wme:}
  tokens.each do |token|
    next unless matches?(token, wme)

    # order matters for proper invalidation
    overlay.add_neg_join_result(NegJoinResult.new(token, wme))
    beta_deactivate_children(token: token, children: children)
  end
end

#alpha_deactivate(wme) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wongi-engine/beta/neg_node.rb', line 25

def alpha_deactivate(wme)
  # p alpha_deactivate: {class: self.class, object_id:, wme:}
  overlay.neg_join_results_for(wme: wme).each do |njr|
    tokens.each do |token|
      next unless token == njr.token

      overlay.remove_neg_join_result(njr)
      next unless overlay.neg_join_results_for(token: token).empty?

      children.each do |child|
        child.beta_activate(Token.new(child, token, nil))
      end
    end
  end
end

#beta_activate(token) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wongi-engine/beta/neg_node.rb', line 41

def beta_activate(token)
  # p beta_activate: {class: self.class, object_id:, token:}
  return if tokens.find { |t| t.duplicate? token }

  overlay.add_token(token)

  template = specialize(alpha.template, tests, token)
  select_wmes(template).each do |wme|
    overlay.add_neg_join_result(NegJoinResult.new(token, wme)) if matches?(token, wme)
  end
  return if overlay.neg_join_results_for(token: token).any?

  children.each do |child|
    child.beta_activate(Token.new(child, token, nil, {}))
  end
end

#beta_deactivate(token) ⇒ Object



58
59
60
61
62
# File 'lib/wongi-engine/beta/neg_node.rb', line 58

def beta_deactivate(token)
  # p beta_deactivate: {class: self.class, object_id:, token:}
  overlay.remove_token(token)
  beta_deactivate_children(token: token)
end

#matches?(token, wme) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/wongi-engine/beta/neg_node.rb', line 73

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

#refresh_child(child) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/wongi-engine/beta/neg_node.rb', line 64

def refresh_child(child)
  tokens.each do |token|
    child.beta_activate(Token.new(child, token, nil, {})) if overlay.neg_join_results_for(token: token).empty?
  end
  select_wmes(alpha.template).each do |wme|
    alpha_activate wme, children: [child]
  end
end