wongi-engine 0.4.0.pre.alpha10 → 0.4.0.pre.alpha11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dc2418ab6ffabbb410e83db899bed0479fc5912ec24bc6bf5c01f40ca306aa1
4
- data.tar.gz: 4f22e6752c8dfe568eaae071cc7d10b062dee6ddaf8c8ddb2dc201773b7378a1
3
+ metadata.gz: c6618cdaae22d90fec75bb11bbfeb27477d9aca0661d0de91ab04ce3b434ce6e
4
+ data.tar.gz: c9973d3a202faa0882653e99a3db546af9b02f6d1e09af075e4749681903743b
5
5
  SHA512:
6
- metadata.gz: 6f0d335b747ae2495a2cb259d4d538cb4b66f7f97fabb41ffd8c7dc78750df54c04990e60eea1913155388488d867e7090c71aee1ae647d0fdcf20447c455e00
7
- data.tar.gz: d1db63d17198447da1f6761897cda0e5db7af1dc880203146e0deae4c30de89fe0486049ece689e1394d773a9bac79c67cc85ecf45a2655eee84ec25eab7855a
6
+ metadata.gz: 2ec734eba8278f9ed3b8571b67a5e7449c4d172143673953d568bc4299383dc60579c295457010a3e9160de5698ecd529713dad80940751fce6c8b599d05dc91
7
+ data.tar.gz: ead1a3405786c7be13882c24ce8457ff31efa003bd9a84722073ff543ddff028b201c5d769756a2fadadfa84bbf0d5289db810f4e06d574c6b5960cb3a55302e
@@ -61,12 +61,13 @@ module Wongi::Engine
61
61
  aggregated = aggregate.call(tokens.map(&map))
62
62
  assignment = { var => aggregated }
63
63
  children = child ? [child] : self.children
64
- tokens.each do |token|
65
- # TODO: optimize this to work with a diff of actual changes
66
- beta_deactivate_children(token: token, children: children)
67
- end
68
64
  children.each do |beta|
69
- beta.beta_activate(Token.new(beta, tokens, nil, assignment))
65
+ new_token = Token.new(beta, tokens, nil, assignment)
66
+ # nothing changed, skip useless traversal
67
+ next if beta.tokens.find { _1.duplicate?(new_token) }
68
+
69
+ beta.tokens.select { |child| tokens.any? { child.child_of?(_1) } }.each { beta.beta_deactivate(_1) }
70
+ beta.beta_activate(new_token)
70
71
  end
71
72
  end
72
73
  end
@@ -58,7 +58,8 @@ module Wongi::Engine
58
58
  end
59
59
 
60
60
  private def select_wmes(template)
61
- rete.current_overlay.select(template)
61
+ # capture the enumerator
62
+ rete.current_overlay.select(template).to_a
62
63
  end
63
64
 
64
65
  private def specialize(template, tests, token)
@@ -26,6 +26,8 @@ module Wongi::Engine
26
26
  end
27
27
 
28
28
  private def should_assert?(wme, token)
29
+ return true if rete.bypass_consistency_checks?
30
+
29
31
  considered_tokens = Set.new
30
32
  tokens_to_consider = [token]
31
33
  until tokens_to_consider.empty?
@@ -33,11 +35,17 @@ module Wongi::Engine
33
35
  considered_tokens.add(token)
34
36
 
35
37
  # self-affirming reasoning
36
- return false if token.wme == wme
38
+ if token.wme == wme
39
+ # puts "detected self-affirming"
40
+ return false
41
+ end
37
42
 
38
43
  # asserting this WME would invalidate the match
39
44
  # TODO: clean up
40
- return false if token.node.is_a?(NegNode) && wme =~ token.node.alpha.template && token.node.matches?(token, wme) # how much is actually necessary?
45
+ if token.node.is_a?(NegNode) && wme =~ token.node.alpha.template && token.node.matches?(token, wme) # how much is actually necessary?
46
+ # puts "detected self-negating"
47
+ return false
48
+ end
41
49
 
42
50
  token.parents.each { |parent| tokens_to_consider.push(parent) unless considered_tokens.include?(parent) }
43
51
 
@@ -3,9 +3,12 @@ require 'wongi-engine/network/debug'
3
3
 
4
4
  module Wongi::Engine
5
5
  class Network
6
+ include CoreExt
6
7
  attr_accessor :alpha_top, :beta_top, :queries, :results, :alpha_hash
7
8
  attr_reader :productions, :overlays
8
9
 
10
+ attr_predicate :bypass_consistency_checks
11
+
9
12
  include NetworkParts::Collectable
10
13
  private :overlays
11
14
  private :alpha_hash, :alpha_hash=
@@ -202,9 +205,8 @@ module Wongi::Engine
202
205
  compile_alpha Template.new(s, p, o)
203
206
  end
204
207
 
205
- # TODO: pick an alpha with fewer candidates to go through
206
208
  def initial_fill(alpha)
207
- default_overlay.select(:_, :_, :_).each do |wme|
209
+ default_overlay.select(:_, :_, :_).to_a.each do |wme|
208
210
  alpha.activate wme if wme =~ alpha.template
209
211
  end
210
212
  end
@@ -253,16 +255,11 @@ module Wongi::Engine
253
255
  end
254
256
 
255
257
  def select(s, p, o, &block)
256
- matching = current_overlay.select(s, p, o)
257
- if block_given?
258
- matching.each(&block)
259
- else
260
- matching.each
261
- end
258
+ current_overlay.select(s, p, o, &block)
262
259
  end
263
260
 
264
261
  def find(s, p, o)
265
- current_overlay.select(s, p, o).first
262
+ select(s, p, o).first
266
263
  end
267
264
 
268
265
  protected
@@ -142,12 +142,12 @@ module Wongi::Engine
142
142
  when 1
143
143
  case args.first
144
144
  when Template
145
- select_by_template(args.first)
145
+ each_by_template(args.first)
146
146
  else
147
147
  raise ArgumentError
148
148
  end
149
149
  when 3
150
- select_by_template(Template.new(*args))
150
+ each_by_template(Template.new(*args))
151
151
  else
152
152
  raise ArgumentError
153
153
  end
@@ -165,25 +165,21 @@ module Wongi::Engine
165
165
  end
166
166
 
167
167
  def generators(wme)
168
- own_generators = generator_tracker.for_wme(wme)
169
- parent_generators =
168
+ Enumerator.new do |y|
169
+ generator_tracker.for_wme(wme).each { y << _1 }
170
170
  if parent
171
- parent.generators(wme).reject { |t| hidden_token?(t) }.to_set
172
- else
173
- Set.new
171
+ parent.generators(wme).reject { hidden_token?(_1) }.each { y << _1 }
174
172
  end
175
- own_generators.union(parent_generators)
173
+ end
176
174
  end
177
175
 
178
176
  def generated_wmes(token)
179
- own_wmes = generator_tracker.for_token(token)
180
- parent_wmes =
177
+ Enumerator.new do |y|
178
+ generator_tracker.for_token(token).each { y << _1 }
181
179
  if parent && !hidden_token?(token)
182
- parent.generated_wmes(token).reject { hidden_wme?(_1) }.to_set
183
- else
184
- Set.new
180
+ parent.generated_wmes(token).reject { hidden_wme?(_1) }.each { y << _1 }
185
181
  end
186
- own_wmes.union(parent_wmes)
182
+ end
187
183
  end
188
184
 
189
185
  private def own_manual?(wme)
@@ -202,7 +198,9 @@ module Wongi::Engine
202
198
  has_own_wme?(wme) ? wme : nil
203
199
  end
204
200
 
205
- private def has_own_wme?(wme) = wmes.include?(wme)
201
+ private def has_own_wme?(wme)
202
+ wmes.include?(wme)
203
+ end
206
204
 
207
205
  private def find_parents_wme(wme)
208
206
  return unless parent
@@ -218,28 +216,29 @@ module Wongi::Engine
218
216
  end
219
217
  end
220
218
 
221
- private def select_by_template(template)
222
- select_parents_template(template) + select_own_template(template)
219
+ def each_by_template(template)
220
+ Enumerator.new do |y|
221
+ each_own_wme_by_template(template, y)
222
+ each_parent_wme_by_template(template, y)
223
+ end
223
224
  end
224
225
 
225
- private def select_own_template(template)
226
+ private def each_own_wme_by_template(template, y)
226
227
  if template.concrete?
227
228
  wme = find_own_wme(WME.from_concrete_template(template))
228
- wme ? [wme] : []
229
+ y << wme if wme
229
230
  elsif template.root?
230
- wmes
231
+ wmes.each { y << _1 }
231
232
  else
232
233
  indexes.map { |index|
233
234
  index.collections_for_template(template)
234
- }.compact.first
235
+ }.compact.first.each { y << _1 }
235
236
  end
236
237
  end
237
238
 
238
- private def select_parents_template(template)
239
+ private def each_parent_wme_by_template(template, y)
239
240
  if parent
240
- parent.select(template).reject { hidden_wme?(_1) }.to_set
241
- else
242
- Set.new
241
+ parent.each_by_template(template).reject { hidden_wme?(_1) }.each { y << _1 }
243
242
  end
244
243
  end
245
244
 
@@ -309,7 +308,8 @@ module Wongi::Engine
309
308
  def remove_token(token)
310
309
  # p remove_token: {token:}
311
310
 
312
- wmes = generated_wmes(token)
311
+ # capture the entire enumerated state
312
+ wmes = generated_wmes(token).to_a
313
313
 
314
314
  if own_node_tokens(token.node).find { _1.equal?(token) }.nil?
315
315
  if parents_node_tokens(token.node).find { _1.equal?(token) }
@@ -27,12 +27,18 @@ module Wongi::Engine
27
27
  @assignments[variable] = value
28
28
  end
29
29
 
30
+ def own_assignments
31
+ @assignments
32
+ end
33
+
30
34
  def assignments
31
- all_assignments
35
+ parents.each_with_object({}) do |parent, acc|
36
+ acc.merge!(parent.assignments)
37
+ end.merge(own_assignments)
32
38
  end
33
39
 
34
40
  def [](var)
35
- a = assignments[var]
41
+ a = assignment(var)
36
42
  a.respond_to?(:call) ? a.call(self) : a
37
43
  end
38
44
 
@@ -40,8 +46,23 @@ module Wongi::Engine
40
46
  vars.map { self[_1] }
41
47
  end
42
48
 
49
+ def assignment(x)
50
+ return @assignments[x] if has_own_var?(x)
51
+
52
+ parents.each do |parent|
53
+ a = parent.assignment(x)
54
+ return a if a
55
+ end
56
+
57
+ nil
58
+ end
59
+
43
60
  def has_var?(x)
44
- assignments.key? x
61
+ has_own_var?(x) || parents.any? { _1.has_var?(x) }
62
+ end
63
+
64
+ def has_own_var?(x)
65
+ @assignments.key?(x)
45
66
  end
46
67
 
47
68
  # TODO: ignore assignments?
@@ -49,12 +70,12 @@ module Wongi::Engine
49
70
  instance_of?(other.class) &&
50
71
  parents == other.parents &&
51
72
  wme == other.wme &&
52
- assignments == other.assignments
73
+ own_assignments == other.own_assignments
53
74
  end
54
75
 
55
76
  def to_s
56
77
  str = "TOKEN [ #{object_id} ancestors=#{ancestors.map(&:object_id).map(&:to_s).join('.')} "
57
- all_assignments.each_pair { |key, value| str << "#{key}=#{value.is_a?(TokenAssignment) ? "#{value.call} (#{value})" : value} " }
78
+ assignments.each_pair { |key, value| str << "#{key}=#{value.is_a?(TokenAssignment) ? "#{value.call} (#{value})" : value} " }
58
79
  str << "]"
59
80
  str
60
81
  end
@@ -62,13 +83,5 @@ module Wongi::Engine
62
83
  def inspect
63
84
  to_s
64
85
  end
65
-
66
- protected
67
-
68
- def all_assignments
69
- parents.each_with_object({}) do |parent, acc|
70
- acc.merge!(parent.assignments)
71
- end.merge(@assignments)
72
- end
73
86
  end
74
87
  end
@@ -1,5 +1,5 @@
1
1
  module Wongi
2
2
  module Engine
3
- VERSION = "0.4.0-alpha10".freeze
3
+ VERSION = "0.4.0-alpha11".freeze
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "issue 4" do
4
- it "correctlies retract pre-added items from within a rule" do
4
+ it "correctly retracts pre-added items from within a rule" do
5
5
  engine = Wongi::Engine.create
6
6
 
7
7
  10.times { |i| engine << [i, :is_number, true] }
@@ -22,11 +22,11 @@ describe "issue 4" do
22
22
  numbers = engine.select :_, :is_number, true
23
23
  evens = engine.select :_, :is_even, true
24
24
 
25
- expect(numbers.size).to eq(0)
26
- expect(evens.size).to eq(10)
25
+ expect(numbers.count).to eq(0)
26
+ expect(evens.count).to eq(10)
27
27
  end
28
28
 
29
- it "correctlies retract post-added items from within a rule" do
29
+ it "correctly retracts post-added items from within a rule" do
30
30
  engine = Wongi::Engine.create
31
31
 
32
32
  engine.rule 'segregate' do
@@ -47,8 +47,8 @@ describe "issue 4" do
47
47
  numbers = engine.select :_, :is_number, true
48
48
  evens = engine.select :_, :is_even, true
49
49
 
50
- expect(numbers.size).to eq(0)
51
- expect(evens.size).to eq(5)
50
+ expect(numbers.count).to eq(0)
51
+ expect(evens.count).to eq(5)
52
52
  end
53
53
 
54
54
  # cascaded processing affects this
@@ -80,9 +80,9 @@ describe "issue 4" do
80
80
  evens = engine.select :_, :is_even, true
81
81
  odds = engine.select :_, :is_odd, true
82
82
 
83
- expect(numbers.size).to eq(0)
84
- expect(evens.size).to eq(5)
85
- expect(odds.size).to eq(5)
83
+ expect(numbers.count).to eq(0)
84
+ expect(evens.count).to eq(5)
85
+ expect(odds.count).to eq(5)
86
86
  end
87
87
 
88
88
  it "does not lose track when another rule affects a set" do
@@ -126,8 +126,8 @@ describe "issue 4" do
126
126
  evens = engine.select :_, :is_even, true
127
127
  odds = engine.select :_, :is_odd, true
128
128
 
129
- expect(numbers.size).to eq(5)
130
- expect(evens.size).to eq(5)
131
- expect(odds.size).to eq(5)
129
+ expect(numbers.count).to eq(5)
130
+ expect(evens.count).to eq(5)
131
+ expect(odds.count).to eq(5)
132
132
  end
133
133
  end
@@ -115,10 +115,10 @@ describe Wongi::Engine::Network do
115
115
  engine << ["Bob", :age, 43]
116
116
 
117
117
  items = engine.select "Alice", :younger, "Bob"
118
- expect(items.size).to eq(1)
118
+ expect(items.count).to eq(1)
119
119
 
120
120
  items = engine.select "Bob", :older, "Alice"
121
- expect(items.size).to eq(1)
121
+ expect(items.count).to eq(1)
122
122
  end
123
123
 
124
124
  it 'uses collectors' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wongi-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.pre.alpha10
4
+ version: 0.4.0.pre.alpha11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valeri Sokolov