wongi-engine 0.1.4 → 0.2.0
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/.hgignore +6 -0
- data/.hgtags +2 -4
- data/.travis.yml +19 -6
- data/README.md +18 -442
- data/examples/ex02.rb +3 -2
- data/examples/graphviz.rb +1 -0
- data/lib/wongi-engine/alpha_memory.rb +11 -6
- data/lib/wongi-engine/beta/assignment_node.rb +0 -15
- data/lib/wongi-engine/beta/beta_memory.rb +7 -31
- data/lib/wongi-engine/beta/beta_node.rb +25 -95
- data/lib/wongi-engine/beta/join_node.rb +14 -46
- data/lib/wongi-engine/beta/ncc_node.rb +8 -34
- data/lib/wongi-engine/beta/ncc_partner.rb +3 -19
- data/lib/wongi-engine/beta/neg_node.rb +7 -16
- data/lib/wongi-engine/beta/optional_node.rb +23 -30
- data/lib/wongi-engine/beta/or_node.rb +0 -51
- data/lib/wongi-engine/beta/production_node.rb +1 -0
- data/lib/wongi-engine/compiler.rb +115 -0
- data/lib/wongi-engine/data_overlay.rb +140 -0
- data/lib/wongi-engine/dsl/action/base.rb +11 -0
- data/lib/wongi-engine/dsl/{actions → action}/error_generator.rb +3 -14
- data/lib/wongi-engine/dsl/action/simple_action.rb +60 -0
- data/lib/wongi-engine/dsl/action/simple_collector.rb +52 -0
- data/lib/wongi-engine/dsl/action/statement_generator.rb +45 -0
- data/lib/wongi-engine/dsl/action/trace_action.rb +49 -0
- data/lib/wongi-engine/dsl/any_rule.rb +4 -21
- data/lib/wongi-engine/dsl/assuming.rb +6 -12
- data/lib/wongi-engine/dsl/builder.rb +44 -0
- data/lib/wongi-engine/dsl/clause/assign.rb +15 -0
- data/lib/wongi-engine/dsl/clause/fact.rb +71 -0
- data/lib/wongi-engine/dsl/clause/gen.rb +17 -0
- data/lib/wongi-engine/dsl/clause/generic.rb +38 -0
- data/lib/wongi-engine/dsl/{dsl_extensions.rb → generated.rb} +5 -5
- data/lib/wongi-engine/dsl/ncc_subrule.rb +15 -0
- data/lib/wongi-engine/dsl/query.rb +10 -11
- data/lib/wongi-engine/dsl/rule.rb +84 -0
- data/lib/wongi-engine/dsl.rb +102 -97
- data/lib/wongi-engine/enumerators.rb +21 -0
- data/lib/wongi-engine/error.rb +13 -2
- data/lib/wongi-engine/filter/filter_test.rb +1 -13
- data/lib/wongi-engine/graph.rb +7 -7
- data/lib/wongi-engine/network.rb +108 -181
- data/lib/wongi-engine/ruleset.rb +6 -6
- data/lib/wongi-engine/template.rb +30 -84
- data/lib/wongi-engine/token.rb +3 -34
- data/lib/wongi-engine/version.rb +1 -1
- data/lib/wongi-engine/wme.rb +9 -60
- data/lib/wongi-engine.rb +3 -0
- data/spec/beta_node_spec.rb +2 -0
- data/spec/generation_spec.rb +1 -1
- data/spec/high_level_spec.rb +29 -11
- data/spec/overlay_spec.rb +22 -0
- data/spec/simple_action_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/wme_spec.rb +22 -22
- data/wongi-engine.gemspec +1 -1
- metadata +37 -17
- data/lib/wongi-engine/dsl/action.rb +0 -12
- data/lib/wongi-engine/dsl/actions/simple_action.rb +0 -62
- data/lib/wongi-engine/dsl/actions/simple_collector.rb +0 -51
- data/lib/wongi-engine/dsl/actions/statement_generator.rb +0 -67
- data/lib/wongi-engine/dsl/actions/trace_action.rb +0 -52
- data/lib/wongi-engine/dsl/dsl_builder.rb +0 -44
- data/lib/wongi-engine/dsl/extension_clause.rb +0 -36
- data/lib/wongi-engine/dsl/generation_clause.rb +0 -15
- data/lib/wongi-engine/dsl/generic_production_rule.rb +0 -82
- data/lib/wongi-engine/dsl/ncc_production_rule.rb +0 -21
- data/lib/wongi-engine/dsl/production_rule.rb +0 -4
- data/lib/wongi-engine/model_context.rb +0 -13
|
@@ -1,39 +1,25 @@
|
|
|
1
1
|
module Wongi::Engine
|
|
2
2
|
|
|
3
3
|
class BetaMemory < BetaNode
|
|
4
|
-
|
|
5
|
-
def initialize parent
|
|
6
|
-
super
|
|
7
|
-
@tokens = []
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
# @override
|
|
11
|
-
def beta_memory
|
|
12
|
-
self
|
|
13
|
-
end
|
|
4
|
+
include TokenContainer
|
|
14
5
|
|
|
15
6
|
def seed assignments = {}
|
|
16
7
|
@seed = assignments
|
|
17
8
|
t = Token.new( self, nil, nil, assignments )
|
|
18
|
-
|
|
9
|
+
rete.default_overlay.add_token(t, self)
|
|
19
10
|
end
|
|
20
11
|
|
|
21
12
|
def subst valuations
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
beta_deactivate(tokens.first)
|
|
24
14
|
token = Token.new( self, nil, nil, @seed )
|
|
25
|
-
@tokens << token
|
|
26
|
-
|
|
27
15
|
valuations.each { |variable, value| token.subst variable, value }
|
|
28
|
-
|
|
29
|
-
child.beta_activate token
|
|
30
|
-
end
|
|
16
|
+
beta_activate(token)
|
|
31
17
|
end
|
|
32
18
|
|
|
33
19
|
def beta_activate token
|
|
34
|
-
existing =
|
|
20
|
+
existing = tokens.find { |et| et.duplicate? token }
|
|
35
21
|
return if existing # TODO really?
|
|
36
|
-
|
|
22
|
+
token.overlay.add_token(token, self)
|
|
37
23
|
children.each do |child|
|
|
38
24
|
child.beta_activate token
|
|
39
25
|
end
|
|
@@ -42,7 +28,7 @@ module Wongi::Engine
|
|
|
42
28
|
|
|
43
29
|
def beta_deactivate token
|
|
44
30
|
return nil unless tokens.find token
|
|
45
|
-
|
|
31
|
+
token.overlay.remove_token(token, self)
|
|
46
32
|
token.deleted!
|
|
47
33
|
if token.parent
|
|
48
34
|
token.parent.children.delete token # should this go into Token#destroy?
|
|
@@ -59,15 +45,5 @@ module Wongi::Engine
|
|
|
59
45
|
end
|
|
60
46
|
end
|
|
61
47
|
|
|
62
|
-
# def delete_token token
|
|
63
|
-
# @tokens.delete token
|
|
64
|
-
# end
|
|
65
|
-
|
|
66
|
-
# => TODO: investigate if we really need this
|
|
67
|
-
#def beta_memory
|
|
68
|
-
# self
|
|
69
|
-
#end
|
|
70
|
-
|
|
71
48
|
end
|
|
72
|
-
|
|
73
49
|
end
|
|
@@ -2,12 +2,32 @@ module Wongi::Engine
|
|
|
2
2
|
|
|
3
3
|
class BetaNode
|
|
4
4
|
|
|
5
|
+
module TokenContainer
|
|
6
|
+
def tokens
|
|
7
|
+
Enumerator.new do |y|
|
|
8
|
+
rete.overlays.each do |overlay|
|
|
9
|
+
overlay.raw_tokens(self).dup.each do |token|
|
|
10
|
+
y << token unless token.deleted?
|
|
11
|
+
end
|
|
12
|
+
overlay.raw_tokens(self).reject! &:deleted?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def empty?
|
|
18
|
+
tokens.first.nil?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def size
|
|
22
|
+
tokens.count
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
5
26
|
include CoreExt
|
|
6
27
|
|
|
7
28
|
attr_writer :rete
|
|
8
29
|
attr_reader :parent
|
|
9
30
|
attr_accessor :children
|
|
10
|
-
attr_accessor :context
|
|
11
31
|
attr_predicate :debug
|
|
12
32
|
|
|
13
33
|
def initialize parent = nil
|
|
@@ -18,6 +38,10 @@ module Wongi::Engine
|
|
|
18
38
|
end
|
|
19
39
|
end
|
|
20
40
|
|
|
41
|
+
def root?
|
|
42
|
+
parent.nil?
|
|
43
|
+
end
|
|
44
|
+
|
|
21
45
|
def depth
|
|
22
46
|
@depth ||= if parent.nil?
|
|
23
47
|
0
|
|
@@ -29,8 +53,6 @@ module Wongi::Engine
|
|
|
29
53
|
def rete
|
|
30
54
|
@rete ||= if parent
|
|
31
55
|
parent.rete
|
|
32
|
-
else
|
|
33
|
-
@rete
|
|
34
56
|
end
|
|
35
57
|
end
|
|
36
58
|
|
|
@@ -41,86 +63,12 @@ module Wongi::Engine
|
|
|
41
63
|
abstract :beta_deactivate
|
|
42
64
|
abstract :beta_reactivate
|
|
43
65
|
|
|
44
|
-
def beta_memory
|
|
45
|
-
beta = children.find { |node| BetaMemory === node }
|
|
46
|
-
if beta.nil?
|
|
47
|
-
beta = BetaMemory.new self
|
|
48
|
-
beta.debug = debug?
|
|
49
|
-
beta.refresh
|
|
50
|
-
end
|
|
51
|
-
beta
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def join_node alpha, tests, assignment, alpha_deaf
|
|
55
|
-
existing = children.find{ |node| JoinNode === node && node.equivalent?( alpha, tests, assignment ) }
|
|
56
|
-
return existing if existing
|
|
57
|
-
|
|
58
|
-
node = JoinNode.new self, tests, assignment
|
|
59
|
-
node.alpha = alpha
|
|
60
|
-
alpha.betas << node unless alpha_deaf
|
|
61
|
-
|
|
62
|
-
node
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def optional_node alpha, tests, assignment, alpha_deaf
|
|
66
|
-
node = OptionalNode.new self, alpha, tests, assignment
|
|
67
|
-
alpha.betas << node unless alpha_deaf
|
|
68
|
-
node
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def filter_node test
|
|
72
|
-
existing = children.find{ |node| FilterNode === node && node.equivalent?( test ) }
|
|
73
|
-
return existing if existing
|
|
74
|
-
|
|
75
|
-
node = FilterNode.new self, test
|
|
76
|
-
node.refresh
|
|
77
|
-
node
|
|
78
|
-
end
|
|
79
|
-
|
|
80
66
|
def assignment_node variable, body
|
|
81
67
|
node = AssignmentNode.new self, variable, body
|
|
82
68
|
node.refresh
|
|
83
69
|
node
|
|
84
70
|
end
|
|
85
71
|
|
|
86
|
-
def neg_node alpha, tests, alpha_deaf, unsafe
|
|
87
|
-
node = NegNode.new self, tests, alpha, unsafe
|
|
88
|
-
alpha.betas << node unless alpha_deaf
|
|
89
|
-
node.refresh
|
|
90
|
-
node
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def ncc_node condition, earlier, parameters, alpha_deaf
|
|
94
|
-
bottom = network condition.children, earlier, parameters, alpha_deaf
|
|
95
|
-
self.children.each do |node|
|
|
96
|
-
if node.kind_of?( NccNode ) and node.partner.parent == bottom
|
|
97
|
-
return node
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
ncc = NccNode.new self
|
|
101
|
-
partner = NccPartner.new bottom.beta_memory
|
|
102
|
-
ncc.partner = partner
|
|
103
|
-
partner.ncc = ncc
|
|
104
|
-
partner.divergent = self
|
|
105
|
-
# partner.conjuncts = condition.children.size
|
|
106
|
-
ncc.refresh
|
|
107
|
-
partner.refresh
|
|
108
|
-
ncc
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
CompilationContext = Struct.new :node, :rete, :earlier, :parameters, :alpha_deaf do
|
|
112
|
-
def dup
|
|
113
|
-
self.class.new( node, rete, earlier.dup, parameters.dup, alpha_deaf )
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def network conditions, earlier, parameters, alpha_deaf
|
|
118
|
-
# puts "Getting beta subnetwork"
|
|
119
|
-
conditions.inject(CompilationContext.new self, self.rete, earlier, parameters, alpha_deaf) do |context, condition|
|
|
120
|
-
condition.compile context
|
|
121
|
-
end.node
|
|
122
|
-
end
|
|
123
|
-
|
|
124
72
|
def refresh
|
|
125
73
|
parent.refresh_child self
|
|
126
74
|
end
|
|
@@ -133,24 +81,6 @@ module Wongi::Engine
|
|
|
133
81
|
# => noop
|
|
134
82
|
end
|
|
135
83
|
|
|
136
|
-
def tokens
|
|
137
|
-
Enumerator.new do |y|
|
|
138
|
-
@tokens.dup.each do |token|
|
|
139
|
-
y << token unless token.deleted?
|
|
140
|
-
end
|
|
141
|
-
@tokens.reject! &:deleted?
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
def empty?
|
|
146
|
-
@tokens.empty?
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def size
|
|
150
|
-
@tokens.size
|
|
151
|
-
end
|
|
152
|
-
alias_method :length, :size
|
|
153
|
-
|
|
154
84
|
private
|
|
155
85
|
|
|
156
86
|
def dp message
|
|
@@ -6,7 +6,10 @@ module Wongi
|
|
|
6
6
|
wme.send field
|
|
7
7
|
end
|
|
8
8
|
def inspect
|
|
9
|
-
"
|
|
9
|
+
"#{field} of #{wme}"
|
|
10
|
+
end
|
|
11
|
+
def to_s
|
|
12
|
+
inspect
|
|
10
13
|
end
|
|
11
14
|
end
|
|
12
15
|
|
|
@@ -71,24 +74,18 @@ module Wongi
|
|
|
71
74
|
child.tokens.each do |token|
|
|
72
75
|
if token.wme == wme
|
|
73
76
|
child.beta_deactivate token
|
|
74
|
-
#token.destroy
|
|
75
77
|
end
|
|
76
78
|
end
|
|
77
79
|
end
|
|
78
80
|
end
|
|
79
81
|
|
|
80
82
|
def beta_activate token
|
|
81
|
-
dp "JOIN beta-activated"
|
|
82
83
|
self.alpha.wmes.each do |wme|
|
|
83
|
-
dp "-TESTING WME #{wme}"
|
|
84
|
-
assignments = collect_assignments( wme )
|
|
85
84
|
if matches?( token, wme )
|
|
86
|
-
|
|
85
|
+
assignments = collect_assignments( wme )
|
|
87
86
|
children.each do |beta|
|
|
88
87
|
beta.beta_activate Token.new( beta, token, wme, assignments )
|
|
89
88
|
end
|
|
90
|
-
else
|
|
91
|
-
dp "-NO MATCH"
|
|
92
89
|
end
|
|
93
90
|
end
|
|
94
91
|
end
|
|
@@ -98,7 +95,6 @@ module Wongi
|
|
|
98
95
|
child.tokens.each do |t|
|
|
99
96
|
if t.parent == token
|
|
100
97
|
child.beta_deactivate t
|
|
101
|
-
#token.destroy
|
|
102
98
|
end
|
|
103
99
|
end
|
|
104
100
|
end
|
|
@@ -115,36 +111,6 @@ module Wongi
|
|
|
115
111
|
end
|
|
116
112
|
end
|
|
117
113
|
|
|
118
|
-
def self.compile condition, earlier, parameters
|
|
119
|
-
tests = []
|
|
120
|
-
assignment = Template.new :_, :_, :_
|
|
121
|
-
[:subject, :predicate, :object].each do |field|
|
|
122
|
-
member = condition.send field
|
|
123
|
-
if Template.variable?( member )
|
|
124
|
-
|
|
125
|
-
contains = parameters.include? member
|
|
126
|
-
if earlier.any? do |ec|
|
|
127
|
-
if ec.kind_of?( VariantSet )
|
|
128
|
-
ec.introduces_variable?( member )
|
|
129
|
-
else
|
|
130
|
-
ec.respond_to?( :contains? ) and ec.contains?( member )
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
contains = true
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
if contains
|
|
137
|
-
tests << BetaTest.new( field, member )
|
|
138
|
-
else
|
|
139
|
-
method = (field.to_s + "=").to_sym
|
|
140
|
-
assignment.send method, member
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
return tests, assignment
|
|
146
|
-
end
|
|
147
|
-
|
|
148
114
|
protected
|
|
149
115
|
|
|
150
116
|
def matches? token, wme
|
|
@@ -156,13 +122,15 @@ module Wongi
|
|
|
156
122
|
|
|
157
123
|
def collect_assignments wme
|
|
158
124
|
assignments = {}
|
|
159
|
-
return assignments if
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
125
|
+
return assignments if assignment_pattern.nil?
|
|
126
|
+
if assignment_pattern.subject != :_
|
|
127
|
+
assignments[ assignment_pattern.subject ] = TokenAssignment.new(wme, :subject)
|
|
128
|
+
end
|
|
129
|
+
if assignment_pattern.predicate != :_
|
|
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)
|
|
166
134
|
end
|
|
167
135
|
assignments
|
|
168
136
|
end
|
|
@@ -1,35 +1,18 @@
|
|
|
1
1
|
module Wongi
|
|
2
2
|
module Engine
|
|
3
|
-
class NccSet
|
|
4
|
-
|
|
5
|
-
attr_reader :children
|
|
6
|
-
def initialize conditions
|
|
7
|
-
@children = conditions
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def compile context
|
|
11
|
-
context.node = context.node.beta_memory.ncc_node( self, context.earlier, context.parameters, false )
|
|
12
|
-
context.node.context = context
|
|
13
|
-
context.earlier << self
|
|
14
|
-
context
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
|
|
19
3
|
class NccNode < BetaNode
|
|
4
|
+
include TokenContainer
|
|
20
5
|
|
|
21
|
-
attr_reader :tokens
|
|
22
6
|
attr_accessor :partner
|
|
23
7
|
|
|
24
8
|
def initialize parent
|
|
25
9
|
super
|
|
26
|
-
@tokens = []
|
|
27
10
|
end
|
|
28
11
|
|
|
29
12
|
def beta_activate token
|
|
30
|
-
return if
|
|
13
|
+
return if tokens.find { |t| t.parent == token }
|
|
31
14
|
t = Token.new self, token, nil, {}
|
|
32
|
-
|
|
15
|
+
t.overlay.add_token(t, self)
|
|
33
16
|
partner.tokens.each do |ncc_token|
|
|
34
17
|
next unless ncc_token.ancestors.find { |a| a.equal? token }
|
|
35
18
|
t.ncc_results << ncc_token
|
|
@@ -43,8 +26,9 @@ module Wongi
|
|
|
43
26
|
end
|
|
44
27
|
|
|
45
28
|
def beta_deactivate token
|
|
46
|
-
t =
|
|
47
|
-
return unless
|
|
29
|
+
t = tokens.find { |tok| tok.parent == token }
|
|
30
|
+
return unless t
|
|
31
|
+
t.overlay.remove_token(t, self)
|
|
48
32
|
t.deleted!
|
|
49
33
|
partner.tokens.select { |ncc| ncc.owner == t }.each do |ncc_token|
|
|
50
34
|
ncc_token.owner = nil
|
|
@@ -59,14 +43,14 @@ module Wongi
|
|
|
59
43
|
|
|
60
44
|
def ncc_activate token
|
|
61
45
|
token.deleted = false
|
|
62
|
-
|
|
46
|
+
token.overlay.add_token(token, self)
|
|
63
47
|
children.each do |child|
|
|
64
48
|
child.beta_activate Token.new( child, token, nil, { } )
|
|
65
49
|
end
|
|
66
50
|
end
|
|
67
51
|
|
|
68
52
|
def ncc_deactivate token
|
|
69
|
-
|
|
53
|
+
token.overlay.remove_token(token, self)
|
|
70
54
|
token.deleted!
|
|
71
55
|
children.each do |beta|
|
|
72
56
|
beta.tokens.select { |t| t.parent == token }.each do |t|
|
|
@@ -82,16 +66,6 @@ module Wongi
|
|
|
82
66
|
end
|
|
83
67
|
end
|
|
84
68
|
end
|
|
85
|
-
|
|
86
|
-
# def delete_token token
|
|
87
|
-
# tokens.delete token
|
|
88
|
-
# token.ncc_results.each do |nccr|
|
|
89
|
-
# nccr.wme.tokens.delete( nccr ) if nccr.wme
|
|
90
|
-
# nccr.parent.children.delete nccr
|
|
91
|
-
# end
|
|
92
|
-
# token.ncc_results.clear
|
|
93
|
-
# end
|
|
94
|
-
|
|
95
69
|
end
|
|
96
70
|
end
|
|
97
71
|
end
|
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
module Wongi
|
|
2
2
|
module Engine
|
|
3
3
|
class NccPartner < BetaNode
|
|
4
|
+
include TokenContainer
|
|
4
5
|
|
|
5
|
-
attr_reader :tokens
|
|
6
|
-
# attr_accessor :conjuncts
|
|
7
6
|
attr_accessor :ncc
|
|
8
7
|
attr_accessor :divergent
|
|
9
8
|
|
|
10
|
-
def initialize parent
|
|
11
|
-
super
|
|
12
|
-
# @conjuncts = 0
|
|
13
|
-
@tokens = []
|
|
14
|
-
end
|
|
15
|
-
|
|
16
9
|
def beta_activate token
|
|
17
10
|
t = Token.new self, token, nil, {}
|
|
18
11
|
owner = owner_for( t )
|
|
19
|
-
|
|
12
|
+
t.overlay.add_token(t, self)
|
|
20
13
|
if owner
|
|
21
14
|
owner.ncc_results << t
|
|
22
15
|
t.owner = owner
|
|
@@ -27,6 +20,7 @@ module Wongi
|
|
|
27
20
|
def beta_deactivate t
|
|
28
21
|
token = tokens.find { |tok| tok.parent == t }
|
|
29
22
|
return unless token
|
|
23
|
+
token.overlay.remove_token(token, self)
|
|
30
24
|
token.owner.ncc_results.delete token
|
|
31
25
|
if token.owner.ncc_results.empty?
|
|
32
26
|
ncc.ncc_activate token.owner
|
|
@@ -39,16 +33,6 @@ module Wongi
|
|
|
39
33
|
divergent_token = token.ancestors.find { |t| t.node == divergent }
|
|
40
34
|
ncc.tokens.find { |t| t.ancestors.include? divergent_token }
|
|
41
35
|
end
|
|
42
|
-
|
|
43
|
-
# def delete_token token
|
|
44
|
-
# token.owner.ncc_results.delete token
|
|
45
|
-
# if token.owner.ncc_results.empty?
|
|
46
|
-
# ncc.children.each do |node|
|
|
47
|
-
# node.beta_activate token.owner, nil, {}
|
|
48
|
-
# end
|
|
49
|
-
# end
|
|
50
|
-
|
|
51
|
-
# end
|
|
52
36
|
end
|
|
53
37
|
end
|
|
54
38
|
end
|
|
@@ -9,17 +9,17 @@ module Wongi
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
class NegNode < BetaNode
|
|
12
|
+
include TokenContainer
|
|
12
13
|
|
|
13
14
|
attr_reader :alpha, :tests
|
|
14
15
|
|
|
15
16
|
def initialize parent, tests, alpha, unsafe
|
|
16
17
|
super(parent)
|
|
17
18
|
@tests, @alpha, @unsafe = tests, alpha, unsafe
|
|
18
|
-
@tokens = []
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def alpha_activate wme
|
|
22
|
-
|
|
22
|
+
tokens.each do |token|
|
|
23
23
|
if matches?( token, wme ) && ( @unsafe || ! token.generated?( wme ) )# feedback loop protection
|
|
24
24
|
# order matters for proper invalidation
|
|
25
25
|
make_join_result(token, wme)
|
|
@@ -38,7 +38,7 @@ module Wongi
|
|
|
38
38
|
|
|
39
39
|
def alpha_deactivate wme
|
|
40
40
|
wme.neg_join_results.dup.each do |njr|
|
|
41
|
-
|
|
41
|
+
tokens.each do |token|
|
|
42
42
|
next unless token == njr.token
|
|
43
43
|
njr.unlink
|
|
44
44
|
if token.neg_join_results.empty?
|
|
@@ -51,8 +51,8 @@ module Wongi
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
def beta_activate token
|
|
54
|
-
return if
|
|
55
|
-
|
|
54
|
+
return if tokens.find { |et| et.duplicate? token }
|
|
55
|
+
token.overlay.add_token(token, self)
|
|
56
56
|
alpha.wmes.each do |wme|
|
|
57
57
|
if matches?( token, wme )
|
|
58
58
|
make_join_result(token, wme)
|
|
@@ -67,7 +67,7 @@ module Wongi
|
|
|
67
67
|
|
|
68
68
|
def beta_deactivate token
|
|
69
69
|
return nil unless tokens.find token
|
|
70
|
-
|
|
70
|
+
token.overlay.remove_token(token, self)
|
|
71
71
|
token.deleted!
|
|
72
72
|
if token.parent
|
|
73
73
|
token.parent.children.delete token # should this go into Token#destroy?
|
|
@@ -85,7 +85,7 @@ module Wongi
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
def refresh_child child
|
|
88
|
-
|
|
88
|
+
tokens.each do |token|
|
|
89
89
|
if token.neg_join_results.empty?
|
|
90
90
|
child.beta_activate Token.new( child, token, nil, {} )
|
|
91
91
|
end
|
|
@@ -118,15 +118,6 @@ module Wongi
|
|
|
118
118
|
token.neg_join_results << njr
|
|
119
119
|
wme.neg_join_results << njr
|
|
120
120
|
end
|
|
121
|
-
|
|
122
|
-
def safe_tokens
|
|
123
|
-
Enumerator.new do |y|
|
|
124
|
-
@tokens.dup.each do |token|
|
|
125
|
-
y << token unless token.deleted?
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
121
|
end
|
|
131
122
|
end
|
|
132
123
|
end
|
|
@@ -9,15 +9,15 @@ module Wongi
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
class OptionalNode < BetaNode
|
|
12
|
+
include TokenContainer
|
|
12
13
|
|
|
13
|
-
attr_reader :alpha, :tests, :
|
|
14
|
+
attr_reader :alpha, :tests, :assignment_pattern
|
|
14
15
|
|
|
15
16
|
def initialize parent, alpha, tests, assignments
|
|
16
17
|
super( parent )
|
|
17
18
|
@alpha = alpha
|
|
18
19
|
@tests = tests
|
|
19
|
-
@
|
|
20
|
-
@tokens = []
|
|
20
|
+
@assignment_pattern = assignments
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def make_opt_result token, wme
|
|
@@ -28,13 +28,13 @@ module Wongi
|
|
|
28
28
|
|
|
29
29
|
def alpha_activate wme
|
|
30
30
|
assignments = collect_assignments( wme )
|
|
31
|
-
|
|
31
|
+
tokens.each do |token|
|
|
32
32
|
if matches? token, wme
|
|
33
33
|
children.each do |child|
|
|
34
34
|
if token.optional?
|
|
35
35
|
token.no_optional!
|
|
36
|
-
child.tokens.
|
|
37
|
-
child.beta_deactivate ct
|
|
36
|
+
child.tokens.each do |ct|
|
|
37
|
+
child.beta_deactivate(ct) if ct.parent == token
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
child.beta_activate Token.new( child, token, wme, assignments )
|
|
@@ -46,12 +46,13 @@ module Wongi
|
|
|
46
46
|
|
|
47
47
|
def alpha_deactivate wme
|
|
48
48
|
wme.opt_join_results.dup.each do |ojr|
|
|
49
|
-
|
|
49
|
+
tokens.each do |token|
|
|
50
|
+
next unless token == ojr.token
|
|
50
51
|
ojr.unlink
|
|
51
52
|
if token.opt_join_results.empty?
|
|
52
53
|
children.each do |child|
|
|
53
|
-
child.tokens.
|
|
54
|
-
child.beta_deactivate ct
|
|
54
|
+
child.tokens.each do |ct|
|
|
55
|
+
child.beta_deactivate(ct) if ct.parent == token
|
|
55
56
|
end
|
|
56
57
|
token.optional!
|
|
57
58
|
child.beta_activate Token.new( child, token, nil, { } )
|
|
@@ -62,9 +63,9 @@ module Wongi
|
|
|
62
63
|
end
|
|
63
64
|
|
|
64
65
|
def beta_activate t
|
|
65
|
-
return if
|
|
66
|
+
return if tokens.find { |token| token.parent == t }
|
|
66
67
|
token = Token.new( self, t, nil, { } )
|
|
67
|
-
|
|
68
|
+
token.overlay.add_token(token, self)
|
|
68
69
|
match = false
|
|
69
70
|
alpha.wmes.each do |wme|
|
|
70
71
|
assignments = collect_assignments(wme)
|
|
@@ -85,9 +86,9 @@ module Wongi
|
|
|
85
86
|
end
|
|
86
87
|
|
|
87
88
|
def beta_deactivate t
|
|
88
|
-
token =
|
|
89
|
+
token = tokens.find { |token| token.parent == t }
|
|
89
90
|
return unless token
|
|
90
|
-
|
|
91
|
+
token.overlay.remove_token(token, self)
|
|
91
92
|
token.deleted!
|
|
92
93
|
if token.parent
|
|
93
94
|
token.parent.children.delete token
|
|
@@ -97,7 +98,6 @@ module Wongi
|
|
|
97
98
|
child.tokens.each do |t|
|
|
98
99
|
if t.parent == token
|
|
99
100
|
child.beta_deactivate t
|
|
100
|
-
#token.destroy
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
end
|
|
@@ -133,25 +133,18 @@ module Wongi
|
|
|
133
133
|
|
|
134
134
|
def collect_assignments wme
|
|
135
135
|
assignments = {}
|
|
136
|
-
return assignments if
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if self.assignments.send(field) != :_
|
|
140
|
-
#puts "#{self.assignments.send(field)} = #{wme.send(field)}"
|
|
141
|
-
assignments[ self.assignments.send(field) ] = TokenAssignment.new( wme, field )
|
|
142
|
-
end
|
|
136
|
+
return assignments if assignment_pattern.nil?
|
|
137
|
+
if assignment_pattern.subject != :_
|
|
138
|
+
assignments[ assignment_pattern.subject ] = TokenAssignment.new(wme, :subject)
|
|
143
139
|
end
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
@tokens.dup.each do |token|
|
|
150
|
-
y << token unless token.deleted?
|
|
151
|
-
end
|
|
140
|
+
if assignment_pattern.predicate != :_
|
|
141
|
+
assignments[ assignment_pattern.predicate ] = TokenAssignment.new(wme, :predicate)
|
|
142
|
+
end
|
|
143
|
+
if assignment_pattern.object != :_
|
|
144
|
+
assignments[ assignment_pattern.object ] = TokenAssignment.new(wme, :object)
|
|
152
145
|
end
|
|
146
|
+
assignments
|
|
153
147
|
end
|
|
154
|
-
|
|
155
148
|
end
|
|
156
149
|
end
|
|
157
150
|
end
|