Class: KBS::JoinNode

Inherits:
Object
  • Object
show all
Defined in:
lib/kbs/join_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alpha_memory, beta_memory, tests = []) ⇒ JoinNode



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kbs/join_node.rb', line 8

def initialize(alpha_memory, beta_memory, tests = [])
  @alpha_memory = alpha_memory
  @beta_memory = beta_memory
  @successors = []
  @tests = tests
  @left_linked = true
  @right_linked = true

  alpha_memory.successors << self if alpha_memory
  beta_memory.successors << self if beta_memory
end

Instance Attribute Details

#alpha_memoryObject

Returns the value of attribute alpha_memory.



5
6
7
# File 'lib/kbs/join_node.rb', line 5

def alpha_memory
  @alpha_memory
end

#beta_memoryObject

Returns the value of attribute beta_memory.



5
6
7
# File 'lib/kbs/join_node.rb', line 5

def beta_memory
  @beta_memory
end

#left_linkedObject (readonly)

Returns the value of attribute left_linked.



6
7
8
# File 'lib/kbs/join_node.rb', line 6

def left_linked
  @left_linked
end

#right_linkedObject (readonly)

Returns the value of attribute right_linked.



6
7
8
# File 'lib/kbs/join_node.rb', line 6

def right_linked
  @right_linked
end

#successorsObject

Returns the value of attribute successors.



5
6
7
# File 'lib/kbs/join_node.rb', line 5

def successors
  @successors
end

#testsObject

Returns the value of attribute tests.



5
6
7
# File 'lib/kbs/join_node.rb', line 5

def tests
  @tests
end

Instance Method Details

#left_activate(token) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kbs/join_node.rb', line 38

def left_activate(token)
  # Left activation: a new token from beta memory needs to be joined with facts from alpha memory
  return unless @left_linked && @right_linked

  @alpha_memory.items.each do |fact|
    if perform_join_tests(token, fact)
      new_token = Token.new(token, fact, self)
      token.children << new_token if token
      @successors.each { |s| s.activate(new_token) }
    end
  end
end

#left_deactivate(token) ⇒ Object



65
66
67
68
69
70
# File 'lib/kbs/join_node.rb', line 65

def left_deactivate(token)
  token.children.each do |child|
    @successors.each { |s| s.deactivate(child) if s.respond_to?(:deactivate) }
  end
  token.children.clear
end

#left_relink!Object



24
25
26
27
# File 'lib/kbs/join_node.rb', line 24

def left_relink!
  @left_linked = true
  @beta_memory.tokens.each { |token| left_activate(token) } if @beta_memory
end

#left_unlink!Object



20
21
22
# File 'lib/kbs/join_node.rb', line 20

def left_unlink!
  @left_linked = false
end

#right_activate(fact) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kbs/join_node.rb', line 51

def right_activate(fact)
  return unless @left_linked && @right_linked

  parent_tokens = @beta_memory ? @beta_memory.tokens : [Token.new(nil, nil, nil)]

  parent_tokens.each do |token|
    if perform_join_tests(token, fact)
      new_token = Token.new(token, fact, self)
      token.children << new_token if token
      @successors.each { |s| s.activate(new_token) }
    end
  end
end

#right_deactivate(fact) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kbs/join_node.rb', line 72

def right_deactivate(fact)
  tokens_to_remove = []

  if @beta_memory
    @beta_memory.tokens.each do |token|
      token.children.select { |child| child.fact == fact }.each do |child|
        tokens_to_remove << child
        @successors.each { |s| s.deactivate(child) if s.respond_to?(:deactivate) }
      end
    end
  end

  tokens_to_remove.each { |token| token.parent.children.delete(token) if token.parent }
end

#right_relink!Object



33
34
35
36
# File 'lib/kbs/join_node.rb', line 33

def right_relink!
  @right_linked = true
  @alpha_memory.items.each { |fact| right_activate(fact) } if @alpha_memory
end

#right_unlink!Object



29
30
31
# File 'lib/kbs/join_node.rb', line 29

def right_unlink!
  @right_linked = false
end