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_memory, #depth, #filter_node, #join_node, #ncc_node, #neg_node, #network, #optional_node, #refresh

Methods included from CoreExt

included

Constructor Details

#initialize(parent, tests, alpha, unsafe) ⇒ NegNode

Returns a new instance of NegNode.



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

def initialize parent, tests, alpha, unsafe
  super(parent)
  @tests, @alpha, @unsafe = tests, alpha, unsafe
  @tokens = []
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



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

def alpha
  @alpha
end

#testsObject (readonly)

Returns the value of attribute tests.



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

def tests
  @tests
end

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Instance Method Details

#alpha_activate(wme) ⇒ Object



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

def alpha_activate wme
  self.tokens.each do |token|
    if matches?( token, wme ) && ( @unsafe || ! token.generated?( wme ) )# feedback loop protection
      # order matters for proper invalidation
      make_join_result(token, wme)
      token.delete_children #if token.neg_join_results.empty? # TODO why was this check here? it seems to break things
    end
  end
end

#beta_activate(token, newwme, assignments) ⇒ Object



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

def beta_activate token, newwme, assignments
  t = Token.new token, newwme, assignments
  t.node = self
  self.tokens << t
  @alpha.wmes.each do |wme|
    if matches?( t, wme )
      make_join_result(t, wme)
    end
  end
  if t.neg_join_results.empty?
    self.children.each do |child|
      child.beta_activate t, nil, {}
    end
  end
end

#delete_token(token) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/wongi-engine/beta/neg_node.rb', line 53

def delete_token token
  tokens.delete token
  token.neg_join_results.each do |njr|
    njr.wme.neg_join_results.delete njr if njr.wme
  end
  token.neg_join_results.clear
end

#refresh_child(child) ⇒ Object



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

def refresh_child child
  safe_tokens.each do |token|
    if token.neg_join_results.empty?
      child.beta_activate token, nil, {}
    end
  end
  alpha.wmes.each do |wme|
    alpha_activate wme
  end
end