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

#beta_memory, #depth, #filter_node, #join_node, #ncc_node, #neg_node, #network, #optional_node, #update_above

Methods included from CoreExt

included

Constructor Details

#initialize(parent, tests, assignment) ⇒ JoinNode

Returns a new instance of JoinNode.



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

def initialize parent, tests, assignment
  super(parent)
  @tests = tests
  @assignment_pattern = assignment
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

#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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wongi-engine/beta/join_node.rb', line 77

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

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

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/wongi-engine/beta/join_node.rb', line 38

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.all? { |my_test|
    tests.any? { |new_test|
      my_test.equivalent? new_test
    }
  }
  true
end

#left_activate(token) ⇒ Object



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

def left_activate token
  ws = '  ' * depth
  self.alpha.wmes.uniq.each do |wme|
    if matches?( token, wme )
      propagate_activation token, wme, collect_assignments( wme )
    end
  end
end

#right_activate(wme) ⇒ Object



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

def right_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 )
      # puts "#{ws}JOIN RIGHT-MATCHED, PROPAGATING"
      propagate_activation token, wme, collected
    end
  end
end