Class: Wongi::Engine::JoinNode

Inherits:
BetaNode
  • Object
show all
Defined in:
lib/wongi-engine/beta/join_node.rb

Instance Attribute Summary collapse

Attributes inherited from BetaNode

#children, #context, #parent, #rete

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BetaNode

#assignment_node, #beta_memory, #delete_token, #depth, #empty?, #filter_node, #join_node, #ncc_node, #neg_node, #network, #optional_node, #refresh, #size, #tokens

Methods included from CoreExt

included

Constructor Details

#initialize(parent, tests, assignment) ⇒ JoinNode

Returns a new instance of JoinNode.



41
42
43
44
45
# File 'lib/wongi-engine/beta/join_node.rb', line 41

def initialize parent, tests, assignment
  super(parent)
  @tests = tests
  @assignment_pattern = assignment
end

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



37
38
39
# File 'lib/wongi-engine/beta/join_node.rb', line 37

def alpha
  @alpha
end

#assignment_patternObject (readonly)

Returns the value of attribute assignment_pattern.



39
40
41
# File 'lib/wongi-engine/beta/join_node.rb', line 39

def assignment_pattern
  @assignment_pattern
end

#testsObject (readonly)

Returns the value of attribute tests.



38
39
40
# File 'lib/wongi-engine/beta/join_node.rb', line 38

def tests
  @tests
end

Class Method Details

.compile(condition, earlier, parameters) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wongi-engine/beta/join_node.rb', line 118

def self.compile condition, earlier, parameters
  tests = []
  assignment = Template.new :_, :_, :_
  [:subject, :predicate, :object].each do |field|
    member = condition.send field
    if Template.variable?( member )

      contains = parameters.include? member
      if earlier.any? do |ec|
          if ec.kind_of?( VariantSet )
            ec.introduces_variable?( member )
          else
            ec.respond_to?( :contains? ) and ec.contains?( member )
          end
        end
        contains = true
      end

      if contains
        tests << BetaTest.new( field, member )
      else
        method = (field.to_s + "=").to_sym
        assignment.send method, member
      end

    end
  end
  return tests, assignment
end

Instance Method Details

#alpha_activate(wme) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/wongi-engine/beta/join_node.rb', line 58

def alpha_activate wme
  assignments = collect_assignments( wme )
  parent.tokens.each do |token|
    if matches?( token, wme )
      children.each do |beta|
        beta.beta_activate Token.new( beta, token, wme, assignments )
      end
    end
  end
end

#alpha_deactivate(wme) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/wongi-engine/beta/join_node.rb', line 69

def alpha_deactivate wme
  children.each do |child|
    child.tokens.each do |token|
      if token.wme == wme
        child.beta_deactivate token
        #token.destroy
      end
    end
  end
end

#beta_activate(token) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wongi-engine/beta/join_node.rb', line 80

def beta_activate token
  dp "JOIN beta-activated"
  self.alpha.wmes.each do |wme|
    dp "-TESTING WME #{wme}"
    assignments = collect_assignments( wme )
    if matches?( token, wme )
      dp "-WME MATCHED, PROPAGATING"
      children.each do |beta|
        beta.beta_activate Token.new( beta, token, wme, assignments )
      end
    else
      dp "-NO MATCH"
    end
  end
end

#beta_deactivate(token) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/wongi-engine/beta/join_node.rb', line 96

def beta_deactivate token
  children.each do |child|
    child.tokens.each do |t|
      if t.parent == token
        child.beta_deactivate t
        #token.destroy
      end
    end
  end
end

#equivalent?(alpha, tests, assignment_pattern) ⇒ Boolean

Returns:



47
48
49
50
51
52
53
54
55
56
# File 'lib/wongi-engine/beta/join_node.rb', line 47

def equivalent? alpha, tests, assignment_pattern
  return false unless self.alpha == alpha
  return false unless self.assignment_pattern == assignment_pattern
  return false unless (self.tests.empty? && tests.empty?) || self.tests.length == tests.length && self.tests.all? { |my_test|
    tests.any? { |new_test|
      my_test.equivalent? new_test
    }
  }
  true
end

#refresh_child(child) ⇒ Object



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

def refresh_child child
  alpha.wmes.each do |wme|
    assignments = collect_assignments( wme )
    parent.tokens.each do |token|
      if matches?( token, wme )
        child.beta_activate Token.new( child, token, wme, assignments )
      end
    end
  end
end