Class: Wongi::Engine::JoinNode

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

Direct Known Subclasses

OptionalNode

Instance Attribute Summary collapse

Attributes inherited from BetaNode

#children, #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, filters) ⇒ JoinNode

Returns a new instance of JoinNode.



33
34
35
36
37
38
# File 'lib/wongi-engine/beta/join_node.rb', line 33

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

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



28
29
30
# File 'lib/wongi-engine/beta/join_node.rb', line 28

def alpha
  @alpha
end

#assignment_patternObject (readonly)

Returns the value of attribute assignment_pattern.



30
31
32
# File 'lib/wongi-engine/beta/join_node.rb', line 30

def assignment_pattern
  @assignment_pattern
end

#filtersObject (readonly)

Returns the value of attribute filters.



31
32
33
# File 'lib/wongi-engine/beta/join_node.rb', line 31

def filters
  @filters
end

#testsObject (readonly)

Returns the value of attribute tests.



29
30
31
# File 'lib/wongi-engine/beta/join_node.rb', line 29

def tests
  @tests
end

Class Method Details

.compile(condition, earlier, parameters) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wongi-engine/beta/join_node.rb', line 94

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.kind_of?( NegTemplate ) 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



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

def alpha_activate wme
  dp "JOIN alpha-activated with #{wme}"
  collected = collect_assignments( wme )
  self.parent.tokens.each do |token|
    dp "-MATCHING #{token}"
    if matches?( token, wme ) && passes_filters?( token, wme, collected )
      dp "-JOIN MATCHED, PROPAGATING"
      propagate_activation token, wme, collected
    end
  end
end

#beta_activate(token) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wongi-engine/beta/join_node.rb', line 71

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

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

Returns:

  • (Boolean)


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

def equivalent? alpha, tests, assignment_pattern, filters
  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
    }
  }
  return false unless (self.filters.empty? && filters.empty?) || self.filters.length == filters.length && self.filters.all? { |my_filter|
    filters.include? my_filter
  }
  true
end

#refresh_child(child) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/wongi-engine/beta/join_node.rb', line 85

def refresh_child child
  tmp = children
  self.children = [ child ]
  alpha.wmes.each do |wme|
    alpha_activate wme
  end
  self.children = tmp
end