wongi-engine 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +80 -0
- data/.travis.yml +6 -16
- data/Gemfile +4 -0
- data/README.md +3 -3
- data/Rakefile +1 -2
- data/examples/ex01.rb +1 -1
- data/examples/ex02.rb +3 -7
- data/examples/graphviz.rb +1 -1
- data/examples/rdf.rb +1 -1
- data/examples/timeline.rb +6 -6
- data/lib/wongi-engine/alpha_memory.rb +5 -9
- data/lib/wongi-engine/beta/aggregate_node.rb +93 -0
- data/lib/wongi-engine/beta/assignment_node.rb +10 -18
- data/lib/wongi-engine/beta/beta_memory.rb +20 -9
- data/lib/wongi-engine/beta/beta_node.rb +14 -22
- data/lib/wongi-engine/beta/filter_node.rb +12 -15
- data/lib/wongi-engine/beta/join_node.rb +47 -61
- data/lib/wongi-engine/beta/ncc_node.rb +17 -20
- data/lib/wongi-engine/beta/ncc_partner.rb +17 -17
- data/lib/wongi-engine/beta/neg_node.rb +37 -37
- data/lib/wongi-engine/beta/optional_node.rb +53 -60
- data/lib/wongi-engine/beta/or_node.rb +6 -9
- data/lib/wongi-engine/beta/production_node.rb +6 -9
- data/lib/wongi-engine/beta.rb +1 -0
- data/lib/wongi-engine/compiler.rb +38 -22
- data/lib/wongi-engine/core_ext.rb +11 -20
- data/lib/wongi-engine/data_overlay.rb +23 -26
- data/lib/wongi-engine/dsl/action/assign_action.rb +1 -1
- data/lib/wongi-engine/dsl/action/base.rb +1 -4
- data/lib/wongi-engine/dsl/action/error_generator.rb +9 -9
- data/lib/wongi-engine/dsl/action/simple_action.rb +14 -10
- data/lib/wongi-engine/dsl/action/simple_collector.rb +10 -25
- data/lib/wongi-engine/dsl/action/statement_generator.rb +15 -14
- data/lib/wongi-engine/dsl/action/trace_action.rb +9 -9
- data/lib/wongi-engine/dsl/any_rule.rb +16 -20
- data/lib/wongi-engine/dsl/assuming.rb +8 -15
- data/lib/wongi-engine/dsl/builder.rb +13 -12
- data/lib/wongi-engine/dsl/clause/aggregate.rb +21 -0
- data/lib/wongi-engine/dsl/clause/assign.rb +4 -4
- data/lib/wongi-engine/dsl/clause/fact.rb +10 -6
- data/lib/wongi-engine/dsl/clause/gen.rb +3 -5
- data/lib/wongi-engine/dsl/clause/generic.rb +7 -9
- data/lib/wongi-engine/dsl/generated.rb +6 -12
- data/lib/wongi-engine/dsl/ncc_subrule.rb +6 -9
- data/lib/wongi-engine/dsl/query.rb +12 -15
- data/lib/wongi-engine/dsl/rule.rb +45 -50
- data/lib/wongi-engine/dsl.rb +30 -16
- data/lib/wongi-engine/enumerators.rb +2 -1
- data/lib/wongi-engine/error.rb +6 -7
- data/lib/wongi-engine/filter/asserting_test.rb +4 -7
- data/lib/wongi-engine/filter/equality_test.rb +15 -18
- data/lib/wongi-engine/filter/filter_test.rb +3 -6
- data/lib/wongi-engine/filter/greater_than_or_equal_test.rb +5 -2
- data/lib/wongi-engine/filter/greater_than_test.rb +15 -18
- data/lib/wongi-engine/filter/inequality_test.rb +15 -18
- data/lib/wongi-engine/filter/less_than_or_equal_test.rb +5 -2
- data/lib/wongi-engine/filter/less_than_test.rb +15 -18
- data/lib/wongi-engine/filter.rb +1 -1
- data/lib/wongi-engine/graph.rb +13 -20
- data/lib/wongi-engine/network/collectable.rb +10 -14
- data/lib/wongi-engine/network/debug.rb +16 -24
- data/lib/wongi-engine/network.rb +110 -129
- data/lib/wongi-engine/ruleset.rb +18 -20
- data/lib/wongi-engine/template.rb +31 -30
- data/lib/wongi-engine/token.rb +33 -33
- data/lib/wongi-engine/version.rb +1 -1
- data/lib/wongi-engine/wme.rb +17 -23
- data/lib/wongi-engine/wme_match_data.rb +5 -9
- data/lib/wongi-engine.rb +0 -4
- data/spec/action_class_spec.rb +43 -49
- data/spec/beta_node_spec.rb +2 -8
- data/spec/bug_specs/issue_4_spec.rb +12 -20
- data/spec/dataset_spec.rb +3 -6
- data/spec/filter_specs/assert_test_spec.rb +12 -31
- data/spec/filter_specs/greater_than_equality_test_spec.rb +2 -5
- data/spec/filter_specs/less_test_spec.rb +7 -17
- data/spec/filter_specs/less_than_equality_test_spec.rb +3 -6
- data/spec/generation_spec.rb +45 -54
- data/spec/high_level_spec.rb +95 -141
- data/spec/network_spec.rb +77 -68
- data/spec/overlay_spec.rb +4 -5
- data/spec/rule_specs/aggregate_spec.rb +197 -0
- data/spec/rule_specs/any_rule_spec.rb +4 -19
- data/spec/rule_specs/assign_spec.rb +10 -16
- data/spec/rule_specs/assuming_spec.rb +10 -17
- data/spec/rule_specs/maybe_rule_spec.rb +4 -15
- data/spec/rule_specs/ncc_spec.rb +48 -65
- data/spec/rule_specs/negative_rule_spec.rb +13 -27
- data/spec/rule_specs/or_rule_spec.rb +3 -13
- data/spec/ruleset_spec.rb +11 -17
- data/spec/simple_action_spec.rb +4 -14
- data/spec/wme_spec.rb +14 -21
- data/wongi-engine.gemspec +22 -22
- metadata +16 -37
- data/.hgignore +0 -7
- data/spec/dsl_spec.rb +0 -9
@@ -1,140 +1,126 @@
|
|
1
1
|
module Wongi
|
2
2
|
module Engine
|
3
|
-
|
4
3
|
TokenAssignment = Struct.new(:wme, :field) do
|
5
|
-
def call
|
4
|
+
def call(_token = nil)
|
6
5
|
wme.send field
|
7
6
|
end
|
7
|
+
|
8
8
|
def inspect
|
9
9
|
"#{field} of #{wme}"
|
10
10
|
end
|
11
|
+
|
11
12
|
def to_s
|
12
13
|
inspect
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
17
|
class BetaTest
|
18
|
+
attr_reader :field, :variable
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def initialize field, variable
|
22
|
-
@field, @variable = field, variable
|
20
|
+
def initialize(field, variable)
|
21
|
+
@field = field
|
22
|
+
@variable = variable
|
23
23
|
end
|
24
24
|
|
25
|
-
def matches?
|
26
|
-
assignment = token[
|
27
|
-
field = wme.send(
|
28
|
-
#field.nil? ||
|
29
|
-
!token.has_var?(
|
25
|
+
def matches?(token, wme)
|
26
|
+
assignment = token[variable]
|
27
|
+
field = wme.send(self.field)
|
28
|
+
# field.nil? ||
|
29
|
+
!token.has_var?(variable) || field == assignment
|
30
30
|
end
|
31
31
|
|
32
|
-
def equivalent?
|
33
|
-
other.field ==
|
32
|
+
def equivalent?(other)
|
33
|
+
other.field == field && other.variable == variable
|
34
34
|
end
|
35
|
-
|
36
35
|
end
|
37
36
|
|
38
37
|
class JoinNode < BetaNode
|
39
|
-
|
40
38
|
attr_accessor :alpha
|
41
|
-
attr_reader :tests
|
42
|
-
attr_reader :assignment_pattern
|
39
|
+
attr_reader :tests, :assignment_pattern
|
43
40
|
|
44
|
-
def initialize
|
41
|
+
def initialize(parent, tests, assignment)
|
45
42
|
super(parent)
|
46
43
|
@tests = tests
|
47
44
|
@assignment_pattern = assignment
|
48
45
|
end
|
49
46
|
|
50
|
-
def equivalent?
|
47
|
+
def equivalent?(alpha, tests, assignment_pattern)
|
51
48
|
return false unless self.alpha == alpha
|
52
49
|
return false unless self.assignment_pattern == assignment_pattern
|
53
|
-
return false unless (self.tests.empty? && tests.empty?) || self.tests.length == tests.length && self.tests.all? { |my_test|
|
50
|
+
return false unless (self.tests.empty? && tests.empty?) || (self.tests.length == tests.length && self.tests.all? { |my_test|
|
54
51
|
tests.any? { |new_test|
|
55
52
|
my_test.equivalent? new_test
|
56
53
|
}
|
57
|
-
}
|
54
|
+
})
|
55
|
+
|
58
56
|
true
|
59
57
|
end
|
60
58
|
|
61
|
-
def alpha_activate
|
62
|
-
assignments = collect_assignments(
|
59
|
+
def alpha_activate(wme)
|
60
|
+
assignments = collect_assignments(wme)
|
63
61
|
parent.tokens.each do |token|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
next unless matches?(token, wme)
|
63
|
+
|
64
|
+
children.each do |beta|
|
65
|
+
beta.beta_activate Token.new(beta, token, wme, assignments)
|
68
66
|
end
|
69
67
|
end
|
70
68
|
end
|
71
69
|
|
72
|
-
def alpha_deactivate
|
70
|
+
def alpha_deactivate(wme)
|
73
71
|
children.each do |child|
|
74
72
|
child.tokens.each do |token|
|
75
|
-
if token.wme == wme
|
76
|
-
child.beta_deactivate token
|
77
|
-
end
|
73
|
+
child.beta_deactivate token if token.wme == wme
|
78
74
|
end
|
79
75
|
end
|
80
76
|
end
|
81
77
|
|
82
|
-
def beta_activate
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
78
|
+
def beta_activate(token)
|
79
|
+
alpha.wmes.each do |wme|
|
80
|
+
next unless matches?(token, wme)
|
81
|
+
|
82
|
+
assignments = collect_assignments(wme)
|
83
|
+
children.each do |beta|
|
84
|
+
beta.beta_activate Token.new(beta, token, wme, assignments)
|
89
85
|
end
|
90
86
|
end
|
91
87
|
end
|
92
88
|
|
93
|
-
def beta_deactivate
|
89
|
+
def beta_deactivate(token)
|
94
90
|
children.each do |child|
|
95
91
|
child.tokens.each do |t|
|
96
|
-
if t.parent == token
|
97
|
-
child.beta_deactivate t
|
98
|
-
end
|
92
|
+
child.beta_deactivate t if t.parent == token
|
99
93
|
end
|
100
94
|
end
|
101
95
|
end
|
102
96
|
|
103
|
-
def refresh_child
|
97
|
+
def refresh_child(child)
|
104
98
|
alpha.wmes.each do |wme|
|
105
|
-
assignments = collect_assignments(
|
99
|
+
assignments = collect_assignments(wme)
|
106
100
|
parent.tokens.each do |token|
|
107
|
-
if matches?(
|
108
|
-
child.beta_activate Token.new( child, token, wme, assignments )
|
109
|
-
end
|
101
|
+
child.beta_activate Token.new(child, token, wme, assignments) if matches?(token, wme)
|
110
102
|
end
|
111
103
|
end
|
112
104
|
end
|
113
105
|
|
114
106
|
protected
|
115
107
|
|
116
|
-
def matches?
|
108
|
+
def matches?(token, wme)
|
117
109
|
@tests.each do |test|
|
118
|
-
return false unless test.matches?(
|
110
|
+
return false unless test.matches?(token, wme)
|
119
111
|
end
|
120
112
|
true
|
121
113
|
end
|
122
114
|
|
123
|
-
def collect_assignments
|
115
|
+
def collect_assignments(wme)
|
124
116
|
assignments = {}
|
125
117
|
return assignments if assignment_pattern.nil?
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
if assignment_pattern.
|
130
|
-
assignments[ assignment_pattern.predicate ] = TokenAssignment.new(wme, :predicate)
|
131
|
-
end
|
132
|
-
if assignment_pattern.object != :_
|
133
|
-
assignments[ assignment_pattern.object ] = TokenAssignment.new(wme, :object)
|
134
|
-
end
|
118
|
+
|
119
|
+
assignments[assignment_pattern.subject] = TokenAssignment.new(wme, :subject) if assignment_pattern.subject != :_
|
120
|
+
assignments[assignment_pattern.predicate] = TokenAssignment.new(wme, :predicate) if assignment_pattern.predicate != :_
|
121
|
+
assignments[assignment_pattern.object] = TokenAssignment.new(wme, :object) if assignment_pattern.object != :_
|
135
122
|
assignments
|
136
123
|
end
|
137
|
-
|
138
124
|
end
|
139
125
|
end
|
140
126
|
end
|
@@ -5,49 +5,48 @@ module Wongi
|
|
5
5
|
|
6
6
|
attr_accessor :partner
|
7
7
|
|
8
|
-
def
|
9
|
-
super
|
10
|
-
end
|
11
|
-
|
12
|
-
def beta_activate token
|
8
|
+
def beta_activate(token)
|
13
9
|
return if tokens.find { |t| t.parent == token }
|
10
|
+
|
14
11
|
t = Token.new self, token, nil, {}
|
15
12
|
t.overlay.add_token(t, self)
|
16
13
|
partner.tokens.each do |ncc_token|
|
17
14
|
next unless ncc_token.ancestors.find { |a| a.equal? token }
|
15
|
+
|
18
16
|
t.ncc_results << ncc_token
|
19
17
|
ncc_token.owner = t
|
20
18
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
return unless t.ncc_results.empty?
|
20
|
+
|
21
|
+
children.each do |child|
|
22
|
+
child.beta_activate Token.new(child, t, nil, {})
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
28
|
-
def beta_deactivate
|
26
|
+
def beta_deactivate(token)
|
29
27
|
t = tokens.find { |tok| tok.parent == token }
|
30
28
|
return unless t
|
29
|
+
|
31
30
|
t.overlay.remove_token(t, self)
|
32
31
|
t.deleted!
|
33
32
|
partner.tokens.select { |ncc| ncc.owner == t }.each do |ncc_token|
|
34
33
|
ncc_token.owner = nil
|
35
|
-
t.ncc_results.delete
|
34
|
+
t.ncc_results.delete(ncc_token)
|
36
35
|
end
|
37
36
|
children.each do |beta|
|
38
|
-
beta.tokens.select { |
|
39
|
-
beta.beta_deactivate
|
37
|
+
beta.tokens.select { |child_token| child_token.parent == t }.each do |child_token|
|
38
|
+
beta.beta_deactivate(child_token)
|
40
39
|
end
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
44
|
-
def ncc_activate
|
43
|
+
def ncc_activate(token)
|
45
44
|
children.each do |child|
|
46
|
-
child.beta_activate Token.new(
|
45
|
+
child.beta_activate Token.new(child, token, nil, {})
|
47
46
|
end
|
48
47
|
end
|
49
48
|
|
50
|
-
def ncc_deactivate
|
49
|
+
def ncc_deactivate(token)
|
51
50
|
children.each do |beta|
|
52
51
|
beta.tokens.select { |t| t.parent == token }.each do |t|
|
53
52
|
beta.beta_deactivate t
|
@@ -55,11 +54,9 @@ module Wongi
|
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
def refresh_child
|
57
|
+
def refresh_child(child)
|
59
58
|
tokens.each do |token|
|
60
|
-
if token.ncc_results.empty?
|
61
|
-
child.beta_activate Token.new( child, token, nil, { } )
|
62
|
-
end
|
59
|
+
child.beta_activate Token.new(child, token, nil, {}) if token.ncc_results.empty?
|
63
60
|
end
|
64
61
|
end
|
65
62
|
end
|
@@ -3,35 +3,35 @@ module Wongi
|
|
3
3
|
class NccPartner < BetaNode
|
4
4
|
include TokenContainer
|
5
5
|
|
6
|
-
attr_accessor :ncc
|
7
|
-
attr_accessor :divergent
|
6
|
+
attr_accessor :ncc, :divergent
|
8
7
|
|
9
|
-
def beta_activate
|
8
|
+
def beta_activate(token)
|
10
9
|
t = Token.new self, token, nil, {}
|
11
|
-
owner = owner_for(
|
10
|
+
owner = owner_for(t)
|
12
11
|
t.overlay.add_token(t, self)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
return unless owner
|
13
|
+
|
14
|
+
owner.ncc_results << t
|
15
|
+
t.owner = owner
|
16
|
+
owner.node.ncc_deactivate owner
|
18
17
|
end
|
19
18
|
|
20
|
-
def beta_deactivate
|
19
|
+
def beta_deactivate(t)
|
21
20
|
token = tokens.find { |tok| tok.parent == t }
|
22
21
|
return unless token
|
22
|
+
|
23
23
|
token.overlay.remove_token(token, self)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
|
25
|
+
owner = token.owner
|
26
|
+
return unless owner
|
27
|
+
|
28
|
+
owner.ncc_results.delete token
|
29
|
+
ncc.ncc_activate owner if owner.ncc_results.empty?
|
30
30
|
end
|
31
31
|
|
32
32
|
private
|
33
33
|
|
34
|
-
def owner_for
|
34
|
+
def owner_for(token)
|
35
35
|
divergent_token = token.ancestors.find { |t| t.node == divergent }
|
36
36
|
ncc.tokens.find { |t| t.ancestors.include? divergent_token }
|
37
37
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module Wongi
|
2
2
|
module Engine
|
3
|
-
|
4
3
|
NegJoinResult = Struct.new :token, :wme do
|
5
4
|
def unlink
|
6
5
|
wme.neg_join_results.delete self
|
@@ -13,82 +12,83 @@ module Wongi
|
|
13
12
|
|
14
13
|
attr_reader :alpha, :tests
|
15
14
|
|
16
|
-
def initialize
|
15
|
+
def initialize(parent, tests, alpha, unsafe)
|
17
16
|
super(parent)
|
18
|
-
@tests
|
17
|
+
@tests = tests
|
18
|
+
@alpha = alpha
|
19
|
+
@unsafe = unsafe
|
19
20
|
end
|
20
21
|
|
21
|
-
def alpha_activate
|
22
|
+
def alpha_activate(wme)
|
22
23
|
tokens.each do |token|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
next unless matches?(token, wme) && (@unsafe || !token.generated?(wme)) # feedback loop protection
|
25
|
+
|
26
|
+
# order matters for proper invalidation
|
27
|
+
make_join_result(token, wme)
|
28
|
+
# token.delete_children #if token.neg_join_results.empty? # TODO why was this check here? it seems to break things
|
29
|
+
children.each do |child|
|
30
|
+
child.tokens.each do |t|
|
31
|
+
if t.parent == token
|
32
|
+
child.beta_deactivate t
|
33
|
+
# token.destroy
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
39
|
-
def alpha_deactivate
|
40
|
+
def alpha_deactivate(wme)
|
40
41
|
wme.neg_join_results.dup.each do |njr|
|
41
42
|
tokens.each do |token|
|
42
43
|
next unless token == njr.token
|
44
|
+
|
43
45
|
njr.unlink
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
next unless token.neg_join_results.empty?
|
47
|
+
|
48
|
+
children.each do |child|
|
49
|
+
child.beta_activate Token.new(child, token, nil, {})
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
53
|
-
def beta_activate
|
55
|
+
def beta_activate(token)
|
54
56
|
return if tokens.find { |et| et.duplicate? token }
|
57
|
+
|
55
58
|
token.overlay.add_token(token, self)
|
56
59
|
alpha.wmes.each do |wme|
|
57
|
-
if matches?(
|
58
|
-
make_join_result(token, wme)
|
59
|
-
end
|
60
|
+
make_join_result(token, wme) if matches?(token, wme)
|
60
61
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
return unless token.neg_join_results.empty?
|
63
|
+
|
64
|
+
children.each do |child|
|
65
|
+
child.beta_activate Token.new(child, token, nil, {})
|
65
66
|
end
|
66
67
|
end
|
67
68
|
|
68
|
-
def beta_deactivate
|
69
|
+
def beta_deactivate(token)
|
69
70
|
return nil unless tokens.find token
|
71
|
+
|
70
72
|
token.overlay.remove_token(token, self)
|
71
73
|
token.deleted!
|
72
74
|
if token.parent
|
73
75
|
token.parent.children.delete token # should this go into Token#destroy?
|
74
76
|
end
|
75
|
-
token.neg_join_results.each
|
77
|
+
token.neg_join_results.each(&:unlink)
|
76
78
|
children.each do |child|
|
77
79
|
child.tokens.each do |t|
|
78
80
|
if t.parent == token
|
79
81
|
child.beta_deactivate t
|
80
|
-
#token.destroy
|
82
|
+
# token.destroy
|
81
83
|
end
|
82
84
|
end
|
83
85
|
end
|
84
86
|
token
|
85
87
|
end
|
86
88
|
|
87
|
-
def refresh_child
|
89
|
+
def refresh_child(child)
|
88
90
|
tokens.each do |token|
|
89
|
-
if token.neg_join_results.empty?
|
90
|
-
child.beta_activate Token.new( child, token, nil, {} )
|
91
|
-
end
|
91
|
+
child.beta_activate Token.new(child, token, nil, {}) if token.neg_join_results.empty?
|
92
92
|
end
|
93
93
|
alpha.wmes.each do |wme|
|
94
94
|
alpha_activate wme
|
@@ -97,15 +97,15 @@ module Wongi
|
|
97
97
|
|
98
98
|
protected
|
99
99
|
|
100
|
-
def matches?
|
100
|
+
def matches?(token, wme)
|
101
101
|
puts "matching #{wme} against #{token}" if debug?
|
102
102
|
@tests.each do |test|
|
103
|
-
return false unless test.matches?(
|
103
|
+
return false unless test.matches?(token, wme)
|
104
104
|
end
|
105
105
|
true
|
106
106
|
end
|
107
107
|
|
108
|
-
def make_join_result
|
108
|
+
def make_join_result(token, wme)
|
109
109
|
njr = NegJoinResult.new token, wme
|
110
110
|
token.neg_join_results << njr
|
111
111
|
wme.neg_join_results << njr
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module Wongi
|
2
2
|
module Engine
|
3
|
-
|
4
3
|
OptionalJoinResult = Struct.new :token, :wme do
|
5
4
|
def unlink
|
6
5
|
wme.opt_join_results.delete self
|
@@ -13,100 +12,99 @@ module Wongi
|
|
13
12
|
|
14
13
|
attr_reader :alpha, :tests, :assignment_pattern
|
15
14
|
|
16
|
-
def initialize
|
17
|
-
super(
|
15
|
+
def initialize(parent, alpha, tests, assignments)
|
16
|
+
super(parent)
|
18
17
|
@alpha = alpha
|
19
18
|
@tests = tests
|
20
19
|
@assignment_pattern = assignments
|
21
20
|
end
|
22
21
|
|
23
|
-
def make_opt_result
|
22
|
+
def make_opt_result(token, wme)
|
24
23
|
jr = OptionalJoinResult.new token, wme
|
25
24
|
token.opt_join_results << jr
|
26
25
|
wme.opt_join_results << jr
|
27
26
|
end
|
28
27
|
|
29
|
-
def alpha_activate
|
30
|
-
assignments = collect_assignments(
|
28
|
+
def alpha_activate(wme)
|
29
|
+
assignments = collect_assignments(wme)
|
31
30
|
tokens.each do |token|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
next unless matches? token, wme
|
32
|
+
|
33
|
+
children.each do |child|
|
34
|
+
if token.optional?
|
35
|
+
token.no_optional!
|
36
|
+
child.tokens.each do |ct|
|
37
|
+
child.beta_deactivate(ct) if ct.parent == token
|
39
38
|
end
|
40
|
-
child.beta_activate Token.new( child, token, wme, assignments )
|
41
39
|
end
|
42
|
-
|
40
|
+
child.beta_activate Token.new(child, token, wme, assignments)
|
43
41
|
end
|
42
|
+
make_opt_result token, wme
|
44
43
|
end
|
45
44
|
end
|
46
45
|
|
47
|
-
def alpha_deactivate
|
46
|
+
def alpha_deactivate(wme)
|
48
47
|
wme.opt_join_results.dup.each do |ojr|
|
49
48
|
tokens.each do |token|
|
50
49
|
next unless token == ojr.token
|
50
|
+
|
51
51
|
ojr.unlink
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
token.optional!
|
58
|
-
child.beta_activate Token.new( child, token, nil, { } )
|
52
|
+
next unless token.opt_join_results.empty?
|
53
|
+
|
54
|
+
children.each do |child|
|
55
|
+
child.tokens.each do |ct|
|
56
|
+
child.beta_deactivate(ct) if ct.parent == token
|
59
57
|
end
|
58
|
+
token.optional!
|
59
|
+
child.beta_activate Token.new(child, token, nil, {})
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
def beta_activate
|
65
|
+
def beta_activate(t)
|
66
66
|
return if tokens.find { |token| token.parent == t }
|
67
|
-
|
67
|
+
|
68
|
+
token = Token.new(self, t, nil, {})
|
68
69
|
token.overlay.add_token(token, self)
|
69
70
|
match = false
|
70
71
|
alpha.wmes.each do |wme|
|
71
72
|
assignments = collect_assignments(wme)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
child.beta_activate Token.new( child, token, wme, assignments )
|
76
|
-
end
|
77
|
-
make_opt_result token, wme
|
78
|
-
end
|
79
|
-
end
|
80
|
-
unless match
|
81
|
-
token.optional!
|
73
|
+
next unless matches? token, wme
|
74
|
+
|
75
|
+
match = true
|
82
76
|
children.each do |child|
|
83
|
-
child.beta_activate Token.new(
|
77
|
+
child.beta_activate Token.new(child, token, wme, assignments)
|
84
78
|
end
|
79
|
+
make_opt_result token, wme
|
80
|
+
end
|
81
|
+
return if match
|
82
|
+
|
83
|
+
token.optional!
|
84
|
+
children.each do |child|
|
85
|
+
child.beta_activate Token.new(child, token, nil, {})
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
88
|
-
def beta_deactivate
|
89
|
-
token = tokens.find { |
|
89
|
+
def beta_deactivate(t)
|
90
|
+
token = tokens.find { |own_token| own_token.parent == t }
|
90
91
|
return unless token
|
92
|
+
|
91
93
|
token.overlay.remove_token(token, self)
|
92
94
|
token.deleted!
|
93
|
-
if token.parent
|
94
|
-
|
95
|
-
end
|
96
|
-
token.opt_join_results.each &:unlink
|
95
|
+
token.parent.children.delete token if token.parent
|
96
|
+
token.opt_join_results.each(&:unlink)
|
97
97
|
children.each do |child|
|
98
|
-
child.tokens.each do |
|
99
|
-
if
|
100
|
-
child.beta_deactivate t
|
101
|
-
end
|
98
|
+
child.tokens.each do |child_token|
|
99
|
+
child.beta_deactivate(child_token) if child_token.parent == token
|
102
100
|
end
|
103
101
|
end
|
104
102
|
token
|
105
103
|
end
|
106
104
|
|
107
|
-
def refresh_child
|
105
|
+
def refresh_child(child)
|
108
106
|
tmp = children
|
109
|
-
self.children = [
|
107
|
+
self.children = [child]
|
110
108
|
refresh # do the beta part
|
111
109
|
alpha.wmes.each do |wme|
|
112
110
|
alpha_activate wme
|
@@ -116,25 +114,20 @@ module Wongi
|
|
116
114
|
|
117
115
|
private
|
118
116
|
|
119
|
-
def matches?
|
117
|
+
def matches?(token, wme)
|
120
118
|
@tests.each do |test|
|
121
|
-
return false unless test.matches?(
|
119
|
+
return false unless test.matches?(token, wme)
|
122
120
|
end
|
123
121
|
true
|
124
122
|
end
|
125
123
|
|
126
|
-
def collect_assignments
|
124
|
+
def collect_assignments(wme)
|
127
125
|
assignments = {}
|
128
126
|
return assignments if assignment_pattern.nil?
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
if assignment_pattern.
|
133
|
-
assignments[ assignment_pattern.predicate ] = TokenAssignment.new(wme, :predicate)
|
134
|
-
end
|
135
|
-
if assignment_pattern.object != :_
|
136
|
-
assignments[ assignment_pattern.object ] = TokenAssignment.new(wme, :object)
|
137
|
-
end
|
127
|
+
|
128
|
+
assignments[assignment_pattern.subject] = TokenAssignment.new(wme, :subject) if assignment_pattern.subject != :_
|
129
|
+
assignments[assignment_pattern.predicate] = TokenAssignment.new(wme, :predicate) if assignment_pattern.predicate != :_
|
130
|
+
assignments[assignment_pattern.object] = TokenAssignment.new(wme, :object) if assignment_pattern.object != :_
|
138
131
|
assignments
|
139
132
|
end
|
140
133
|
end
|