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
data/lib/wongi-engine/token.rb
CHANGED
@@ -1,24 +1,23 @@
|
|
1
1
|
module Wongi::Engine
|
2
|
-
|
3
2
|
class Token
|
4
|
-
|
5
3
|
include CoreExt
|
6
4
|
|
7
|
-
attr_reader :children
|
8
|
-
attr_reader :wme
|
9
|
-
attr_reader :node
|
10
|
-
attr_reader :overlay
|
5
|
+
attr_reader :children, :wme, :node, :overlay, :neg_join_results, :opt_join_results, :ncc_results, :generated_wmes
|
11
6
|
attr_accessor :owner, :parent
|
12
|
-
|
13
|
-
attr_reader :opt_join_results
|
14
|
-
attr_reader :ncc_results
|
15
|
-
attr_reader :generated_wmes
|
7
|
+
|
16
8
|
attr_predicate :optional
|
17
9
|
attr_predicate :deleted
|
18
10
|
|
19
|
-
def initialize
|
20
|
-
@node
|
21
|
-
@
|
11
|
+
def initialize(node, token, wme, assignments)
|
12
|
+
@node = node
|
13
|
+
@parent = token
|
14
|
+
@wme = wme
|
15
|
+
@assignments = assignments
|
16
|
+
@overlay = if wme
|
17
|
+
wme.overlay.highest(token.overlay)
|
18
|
+
else
|
19
|
+
token ? token.overlay : node.rete.default_overlay
|
20
|
+
end
|
22
21
|
@children = []
|
23
22
|
@deleted = false
|
24
23
|
@neg_join_results = []
|
@@ -36,10 +35,8 @@ module Wongi::Engine
|
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
39
|
-
def subst
|
40
|
-
if @assignments.
|
41
|
-
@assignments[ variable ] = value
|
42
|
-
end
|
38
|
+
def subst(variable, value)
|
39
|
+
@assignments[variable] = value if @assignments.key? variable
|
43
40
|
end
|
44
41
|
|
45
42
|
def set(variable, value)
|
@@ -50,19 +47,20 @@ module Wongi::Engine
|
|
50
47
|
all_assignments
|
51
48
|
end
|
52
49
|
|
53
|
-
def []
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
def [](var)
|
51
|
+
a = assignments[var]
|
52
|
+
return unless a
|
53
|
+
|
54
|
+
a.respond_to?(:call) ? a.call(self) : a
|
57
55
|
end
|
58
56
|
|
59
|
-
def has_var?
|
60
|
-
assignments.
|
57
|
+
def has_var?(x)
|
58
|
+
assignments.key? x
|
61
59
|
end
|
62
60
|
|
63
|
-
# TODO ignore assignments?
|
64
|
-
def duplicate?
|
65
|
-
|
61
|
+
# TODO: ignore assignments?
|
62
|
+
def duplicate?(other)
|
63
|
+
parent.equal?(other.parent) && @wme.equal?(other.wme) && assignments == other.assignments
|
66
64
|
end
|
67
65
|
|
68
66
|
def to_s
|
@@ -89,27 +87,30 @@ module Wongi::Engine
|
|
89
87
|
end
|
90
88
|
|
91
89
|
# for neg feedback loop protection
|
92
|
-
def generated?
|
90
|
+
def generated?(wme)
|
93
91
|
return true if generated_wmes.any? { |w| w == wme }
|
94
|
-
|
92
|
+
|
93
|
+
children.any? { |t| t.generated? wme }
|
95
94
|
end
|
96
95
|
|
97
96
|
protected
|
98
97
|
|
99
98
|
def all_assignments
|
100
|
-
raise "Assignments is not a hash" unless @assignments.
|
99
|
+
raise "Assignments is not a hash" unless @assignments.is_a?(Hash)
|
100
|
+
|
101
101
|
if @parent
|
102
102
|
@parent.assignments.merge @assignments
|
103
103
|
else
|
104
104
|
@assignments
|
105
105
|
end
|
106
106
|
end
|
107
|
-
|
108
107
|
end
|
109
108
|
|
110
109
|
class FakeToken < Token
|
111
|
-
def initialize
|
112
|
-
@parent
|
110
|
+
def initialize(token, wme, assignments) # rubocop:disable Lint/MissingSuper
|
111
|
+
@parent = token
|
112
|
+
@wme = wme
|
113
|
+
@assignments = assignments
|
113
114
|
@children = []
|
114
115
|
@neg_join_results = []
|
115
116
|
@opt_join_results = []
|
@@ -117,5 +118,4 @@ module Wongi::Engine
|
|
117
118
|
@generated_wmes = []
|
118
119
|
end
|
119
120
|
end
|
120
|
-
|
121
121
|
end
|
data/lib/wongi-engine/version.rb
CHANGED
data/lib/wongi-engine/wme.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module Wongi::Engine
|
2
|
-
|
3
|
-
WME = Struct.new( :subject, :predicate, :object ) do
|
4
|
-
|
2
|
+
WME = Struct.new(:subject, :predicate, :object) do
|
5
3
|
include CoreExt
|
6
4
|
|
7
5
|
attr_reader :rete
|
@@ -9,11 +7,11 @@ module Wongi::Engine
|
|
9
7
|
attr_reader :generating_tokens
|
10
8
|
attr_reader :neg_join_results, :opt_join_results
|
11
9
|
attr_accessor :overlay
|
10
|
+
|
12
11
|
attr_predicate :deleted
|
13
12
|
attr_predicate :manual
|
14
13
|
|
15
|
-
def initialize
|
16
|
-
|
14
|
+
def initialize(s, p, o, r = nil)
|
17
15
|
manual!
|
18
16
|
|
19
17
|
@deleted = false
|
@@ -24,35 +22,33 @@ module Wongi::Engine
|
|
24
22
|
|
25
23
|
@rete = r
|
26
24
|
|
27
|
-
|
28
|
-
super(
|
29
|
-
|
25
|
+
# TODO: reintroduce Network#import when bringing back RDF support
|
26
|
+
super(s, p, o)
|
30
27
|
end
|
31
28
|
|
32
|
-
def import_into
|
33
|
-
self.class.new(
|
29
|
+
def import_into(r)
|
30
|
+
self.class.new(subject, predicate, object, r).tap do |wme|
|
34
31
|
wme.overlay = overlay
|
35
|
-
wme.manual =
|
32
|
+
wme.manual = manual?
|
36
33
|
end
|
37
34
|
end
|
38
35
|
|
39
36
|
def dup
|
40
|
-
self.class.new(
|
37
|
+
self.class.new(subject, predicate, object, rete).tap do |wme|
|
41
38
|
wme.overlay = overlay
|
42
|
-
wme.manual =
|
39
|
+
wme.manual = manual?
|
43
40
|
end
|
44
41
|
end
|
45
42
|
|
46
|
-
def ==
|
43
|
+
def ==(other)
|
47
44
|
subject == other.subject && predicate == other.predicate && object == other.object
|
48
45
|
end
|
49
46
|
|
50
|
-
def =~
|
51
|
-
raise Wongi::Engine::Error, "Cannot match a WME against a #{template.class}" unless Template
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
47
|
+
def =~(template)
|
48
|
+
raise Wongi::Engine::Error, "Cannot match a WME against a #{template.class}" unless template.is_a?(Template)
|
49
|
+
|
50
|
+
result = match_member(subject, template.subject) & match_member(predicate, template.predicate) & match_member(object, template.object)
|
51
|
+
result if result.match?
|
56
52
|
end
|
57
53
|
|
58
54
|
def generated?
|
@@ -73,7 +69,7 @@ module Wongi::Engine
|
|
73
69
|
|
74
70
|
protected
|
75
71
|
|
76
|
-
def match_member
|
72
|
+
def match_member(mine, theirs)
|
77
73
|
result = WMEMatchData.new
|
78
74
|
if theirs == :_ || mine == theirs
|
79
75
|
result.match!
|
@@ -83,7 +79,5 @@ module Wongi::Engine
|
|
83
79
|
end
|
84
80
|
result
|
85
81
|
end
|
86
|
-
|
87
82
|
end
|
88
|
-
|
89
83
|
end
|
@@ -1,19 +1,17 @@
|
|
1
1
|
module Wongi::Engine
|
2
|
-
|
3
2
|
class WMEMatchData
|
4
|
-
|
5
3
|
attr_reader :assignments
|
6
4
|
|
7
|
-
def initialize
|
5
|
+
def initialize(assignments = {}, match = false)
|
8
6
|
@assignments = assignments
|
9
7
|
@match = match
|
10
8
|
end
|
11
9
|
|
12
|
-
def []
|
10
|
+
def [](key)
|
13
11
|
assignments[key]
|
14
12
|
end
|
15
13
|
|
16
|
-
def []=
|
14
|
+
def []=(key, value)
|
17
15
|
assignments[key] = value
|
18
16
|
end
|
19
17
|
|
@@ -25,10 +23,8 @@ module Wongi::Engine
|
|
25
23
|
@match = true
|
26
24
|
end
|
27
25
|
|
28
|
-
def &
|
29
|
-
WMEMatchData.new(
|
26
|
+
def &(other)
|
27
|
+
WMEMatchData.new(assignments.merge(other.assignments), match? && other.match?)
|
30
28
|
end
|
31
|
-
|
32
29
|
end
|
33
|
-
|
34
30
|
end
|
data/lib/wongi-engine.rb
CHANGED
data/spec/action_class_spec.rb
CHANGED
@@ -1,62 +1,56 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
3
|
describe 'action classes' do
|
5
|
-
|
6
|
-
include Wongi::Engine::DSL
|
4
|
+
include Wongi::Engine::DSL
|
7
5
|
|
8
6
|
let :engine do
|
9
|
-
|
7
|
+
Wongi::Engine.create
|
10
8
|
end
|
11
9
|
|
12
10
|
let :action_class do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
11
|
+
Class.new do
|
12
|
+
class << self
|
13
|
+
attr_accessor :execute_body, :deexecute_body
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute(token)
|
17
|
+
self.class.execute_body.call(token)
|
18
|
+
end
|
19
|
+
|
20
|
+
def deexecute(token)
|
21
|
+
self.class.deexecute_body.call(token)
|
22
|
+
end
|
23
|
+
end
|
27
24
|
end
|
28
25
|
|
29
26
|
it 'should have appropriate callbacks executed' do
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
expect(deexecuted).to be == 1
|
59
|
-
|
27
|
+
executed = 0
|
28
|
+
deexecuted = 0
|
29
|
+
|
30
|
+
klass = action_class
|
31
|
+
|
32
|
+
klass.execute_body = lambda do |_token|
|
33
|
+
executed += 1
|
34
|
+
end
|
35
|
+
klass.deexecute_body = lambda do |_token|
|
36
|
+
deexecuted += 1
|
37
|
+
end
|
38
|
+
|
39
|
+
engine << rule {
|
40
|
+
forall {
|
41
|
+
has :A, :x, :B
|
42
|
+
}
|
43
|
+
make {
|
44
|
+
action klass
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
engine << [1, :x, 2]
|
49
|
+
expect(executed).to be == 1
|
50
|
+
expect(deexecuted).to be == 0
|
51
|
+
|
52
|
+
engine.retract [1, :x, 2]
|
53
|
+
expect(executed).to be == 1
|
54
|
+
expect(deexecuted).to be == 1
|
60
55
|
end
|
61
|
-
|
62
56
|
end
|
data/spec/beta_node_spec.rb
CHANGED
@@ -1,15 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Wongi::Engine::BetaNode do
|
4
|
-
|
5
4
|
include Wongi::Engine::DSL
|
6
5
|
|
7
|
-
let(
|
6
|
+
let(:engine) { Wongi::Engine.create }
|
8
7
|
|
9
8
|
describe '#tokens' do
|
10
|
-
|
11
9
|
it 'should be enumerable' do
|
12
|
-
|
13
10
|
production = engine << rule {
|
14
11
|
forall {
|
15
12
|
has :x, :y, :Z
|
@@ -20,10 +17,7 @@ describe Wongi::Engine::BetaNode do
|
|
20
17
|
engine << [:x, :y, 2]
|
21
18
|
engine << [:x, :y, 3]
|
22
19
|
zs = production.tokens.map { |token| token[:Z] }
|
23
|
-
expect(
|
24
|
-
|
20
|
+
expect(zs).to be == [1, 2, 3]
|
25
21
|
end
|
26
|
-
|
27
22
|
end
|
28
|
-
|
29
23
|
end
|
@@ -1,12 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "issue 4" do
|
4
|
-
|
5
4
|
it "should correctly retract pre-added items from within a rule" do
|
6
|
-
|
7
5
|
engine = Wongi::Engine.create
|
8
6
|
|
9
|
-
10.times{ |i| engine << [i, :is_number, true] }
|
7
|
+
10.times { |i| engine << [i, :is_number, true] }
|
10
8
|
|
11
9
|
engine.rule 'segregate' do
|
12
10
|
forall {
|
@@ -26,11 +24,9 @@ describe "issue 4" do
|
|
26
24
|
|
27
25
|
expect(numbers).to be_empty
|
28
26
|
expect(evens.length).to eq(10)
|
29
|
-
|
30
27
|
end
|
31
28
|
|
32
29
|
it "should correctly retract post-added items from within a rule" do
|
33
|
-
|
34
30
|
engine = Wongi::Engine.create
|
35
31
|
|
36
32
|
engine.rule 'segregate' do
|
@@ -46,22 +42,20 @@ describe "issue 4" do
|
|
46
42
|
}
|
47
43
|
end
|
48
44
|
|
49
|
-
10.times{ |i| engine << [i, :is_number, true] }
|
45
|
+
10.times { |i| engine << [i, :is_number, true] }
|
50
46
|
|
51
47
|
numbers = engine.select :_, :is_number, true
|
52
48
|
evens = engine.select :_, :is_even, true
|
53
49
|
|
54
50
|
expect(numbers).to be_empty
|
55
51
|
expect(evens.length).to eq(10)
|
56
|
-
|
57
52
|
end
|
58
53
|
|
59
54
|
# cascaded processing affects this
|
60
55
|
it "should not retract later items from within a rule" do
|
61
|
-
|
62
56
|
engine = Wongi::Engine.create
|
63
57
|
|
64
|
-
10.times{ |i| engine << [i, :is_number, true] }
|
58
|
+
10.times { |i| engine << [i, :is_number, true] }
|
65
59
|
|
66
60
|
engine.rule 'segregate' do
|
67
61
|
forall {
|
@@ -70,7 +64,7 @@ describe "issue 4" do
|
|
70
64
|
make {
|
71
65
|
action { |token|
|
72
66
|
number = token[:Number]
|
73
|
-
if number
|
67
|
+
if number.even?
|
74
68
|
engine << [number, :is_even, true]
|
75
69
|
engine.retract [number, :is_number, true]
|
76
70
|
engine.retract [number + 1, :is_number, true]
|
@@ -89,14 +83,12 @@ describe "issue 4" do
|
|
89
83
|
expect(numbers).to be_empty
|
90
84
|
expect(evens).to have(5).items
|
91
85
|
expect(odds).to have(5).items
|
92
|
-
|
93
86
|
end
|
94
87
|
|
95
|
-
|
96
88
|
it "should not lose track when another rule affects a set" do
|
97
89
|
engine = Wongi::Engine.create
|
98
90
|
|
99
|
-
10.times{ |i| engine << [i, :is_number, true] }
|
91
|
+
10.times { |i| engine << [i, :is_number, true] }
|
100
92
|
|
101
93
|
engine.rule 'find odds' do
|
102
94
|
forall {
|
@@ -106,7 +98,7 @@ describe "issue 4" do
|
|
106
98
|
make {
|
107
99
|
action { |token|
|
108
100
|
number = token[:Number]
|
109
|
-
if number
|
101
|
+
if number.odd?
|
110
102
|
engine << [number, :is_odd, true]
|
111
103
|
engine.retract [number, :is_number, true]
|
112
104
|
end
|
@@ -121,11 +113,11 @@ describe "issue 4" do
|
|
121
113
|
make {
|
122
114
|
action { |token|
|
123
115
|
number = token[:Number]
|
124
|
-
if number
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
116
|
+
engine << if number.even?
|
117
|
+
[number, :is_even, true]
|
118
|
+
else
|
119
|
+
[number, :probably_odd, true]
|
120
|
+
end
|
129
121
|
}
|
130
122
|
}
|
131
123
|
end
|
@@ -134,7 +126,7 @@ describe "issue 4" do
|
|
134
126
|
evens = engine.select :_, :is_even, true
|
135
127
|
odds = engine.select :_, :is_odd, true
|
136
128
|
|
137
|
-
|
129
|
+
expect(numbers.length).to eq(5)
|
138
130
|
expect(evens.length).to eq(5)
|
139
131
|
expect(odds.length).to eq(5)
|
140
132
|
end
|
data/spec/dataset_spec.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Wongi::Engine::Network do
|
4
|
-
|
3
|
+
describe Wongi::Engine::Network do
|
4
|
+
include Wongi::Engine::DSL
|
5
5
|
it 'should expose compiled productions' do
|
6
|
-
|
7
6
|
ds = Wongi::Engine::Network.new
|
8
7
|
|
9
8
|
ds << rule('test-rule') {
|
@@ -20,8 +19,6 @@ describe Wongi::Engine::Network do
|
|
20
19
|
ds << [1, 2, 3]
|
21
20
|
|
22
21
|
expect(production.size).to eq(1)
|
23
|
-
expect(
|
24
|
-
|
22
|
+
expect(production.size).to be == 1
|
25
23
|
end
|
26
|
-
|
27
24
|
end
|
@@ -1,53 +1,40 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "ASSERT test" do
|
4
|
+
include Wongi::Engine::DSL
|
5
|
+
let(:engine) { Wongi::Engine.create }
|
4
6
|
|
5
|
-
|
6
|
-
@engine = Wongi::Engine.create
|
7
|
-
end
|
8
|
-
|
9
|
-
def engine
|
10
|
-
@engine
|
11
|
-
end
|
12
|
-
|
13
|
-
def production
|
14
|
-
@production
|
15
|
-
end
|
7
|
+
attr_reader :production
|
16
8
|
|
17
|
-
def test_rule
|
18
|
-
@production = (
|
9
|
+
def test_rule(&block)
|
10
|
+
@production = (engine << rule('test-rule', &block))
|
19
11
|
end
|
20
12
|
|
21
13
|
it "should pass with a constant 'true'" do
|
22
|
-
|
23
14
|
test_rule {
|
24
15
|
forall {
|
25
|
-
assert { |
|
16
|
+
assert { |_token|
|
26
17
|
true
|
27
18
|
}
|
28
19
|
}
|
29
20
|
}
|
30
21
|
|
31
22
|
expect(production).to have(1).token
|
32
|
-
|
33
23
|
end
|
34
24
|
|
35
25
|
it "should fail with a constant 'false'" do
|
36
|
-
|
37
26
|
test_rule {
|
38
27
|
forall {
|
39
|
-
assert { |
|
28
|
+
assert { |_token|
|
40
29
|
false
|
41
30
|
}
|
42
31
|
}
|
43
32
|
}
|
44
33
|
|
45
34
|
expect(production).to have(0).tokens
|
46
|
-
|
47
35
|
end
|
48
36
|
|
49
37
|
it "should use the token with no arguments" do
|
50
|
-
|
51
38
|
test_rule {
|
52
39
|
forall {
|
53
40
|
has :X, "is", :Y
|
@@ -57,15 +44,13 @@ describe "ASSERT test" do
|
|
57
44
|
}
|
58
45
|
}
|
59
46
|
|
60
|
-
engine << [
|
47
|
+
engine << %w[resistance is futile]
|
61
48
|
|
62
49
|
expect(production).to have(1).token
|
63
50
|
expect(production.tokens.first[:X]).to eq("resistance")
|
64
|
-
|
65
51
|
end
|
66
52
|
|
67
53
|
it "should be retractable" do
|
68
|
-
|
69
54
|
test_rule {
|
70
55
|
forall {
|
71
56
|
has :X, "is", :Y
|
@@ -75,28 +60,24 @@ describe "ASSERT test" do
|
|
75
60
|
}
|
76
61
|
}
|
77
62
|
|
78
|
-
engine << [
|
79
|
-
engine.retract [
|
63
|
+
engine << %w[resistance is futile]
|
64
|
+
engine.retract %w[resistance is futile]
|
80
65
|
expect(production).to have(0).tokens
|
81
|
-
|
82
66
|
end
|
83
67
|
|
84
68
|
it "should use individual variables with arguments" do
|
85
|
-
|
86
69
|
test_rule {
|
87
70
|
forall {
|
88
71
|
has :X, "is", :Y
|
89
|
-
assert :X, :Y do |
|
72
|
+
assert :X, :Y do |_x, y|
|
90
73
|
y == "futile"
|
91
74
|
end
|
92
75
|
}
|
93
76
|
}
|
94
77
|
|
95
|
-
engine << [
|
78
|
+
engine << %w[resistance is futile]
|
96
79
|
|
97
80
|
expect(production).to have(1).token
|
98
81
|
expect(production.tokens.first[:X]).to eq("resistance")
|
99
|
-
|
100
82
|
end
|
101
|
-
|
102
83
|
end
|