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

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



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

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|
          ! ec.kind_of?( NegTemplate ) and ec.contains?( member )
        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
70
71
# File 'lib/wongi-engine/beta/join_node.rb', line 59

def alpha_activate wme
  ws = '  ' * depth
  # puts "#{ws}JOIN #{@id} right-activated with #{wme}"
  collected = collect_assignments( wme )
  # puts "PARENT HAS #{parent.tokens.length} TOKENS"
  self.parent.tokens.each do |token|
    # puts "#{ws}matching with token"
    if matches?( token, wme ) && passes_filters?( token, wme, collected )
      # puts "#{ws}JOIN RIGHT-MATCHED, PROPAGATING"
      propagate_activation token, wme, collected
    end
  end
end

#beta_activate(token) ⇒ Object



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

def beta_activate token
  ws = '  ' * depth
  self.alpha.wmes.each do |wme|
    collected = collect_assignments( wme )
    if matches?( token, wme ) && passes_filters?( token, wme, collected )
      propagate_activation token, wme, collected
    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



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

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