wongi-engine 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +23 -0
- data/lib/wongi-engine/alpha_memory.rb +1 -4
- data/lib/wongi-engine/beta/assignment_node.rb +2 -1
- data/lib/wongi-engine/beta/beta_memory.rb +7 -2
- data/lib/wongi-engine/beta/beta_node.rb +7 -2
- data/lib/wongi-engine/beta/join_node.rb +8 -2
- data/lib/wongi-engine/beta/ncc_node.rb +1 -0
- data/lib/wongi-engine/beta/neg_node.rb +92 -92
- data/lib/wongi-engine/beta/or_node.rb +3 -1
- data/lib/wongi-engine/dsl.rb +4 -1
- data/lib/wongi-engine/dsl/actions/statement_generator.rb +55 -55
- data/lib/wongi-engine/dsl/assuming.rb +37 -0
- data/lib/wongi-engine/dsl/generic_production_rule.rb +4 -4
- data/lib/wongi-engine/filter/filter_test.rb +1 -0
- data/lib/wongi-engine/network.rb +443 -442
- data/lib/wongi-engine/template.rb +3 -0
- data/lib/wongi-engine/token.rb +132 -138
- data/lib/wongi-engine/version.rb +1 -1
- data/lib/wongi-engine/wme.rb +138 -138
- data/spec/beta_node_spec.rb +27 -0
- data/spec/dataset_spec.rb +1 -0
- data/spec/rule_specs/assuming_spec.rb +66 -0
- data/spec/rule_specs/ncc_spec.rb +33 -0
- data/spec/rule_specs/negative_rule_spec.rb +105 -70
- data/spec/wme_spec.rb +0 -0
- data/wongi-engine.gemspec +0 -0
- metadata +7 -2
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wongi::Engine::BetaNode do
|
4
|
+
|
5
|
+
let( :engine ) { Wongi::Engine.create }
|
6
|
+
|
7
|
+
describe '#tokens' do
|
8
|
+
|
9
|
+
it 'should be enumerable' do
|
10
|
+
|
11
|
+
production = engine << rule {
|
12
|
+
forall {
|
13
|
+
has :x, :y, :Z
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
engine << [:x, :y, 1]
|
18
|
+
engine << [:x, :y, 2]
|
19
|
+
engine << [:x, :y, 3]
|
20
|
+
zs = production.tokens.map { |token| token[:Z] }
|
21
|
+
expect( zs ).to be == [1, 2, 3]
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/dataset_spec.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wongi::Engine::AssumingClause do
|
4
|
+
|
5
|
+
let( :engine ) { Wongi::Engine.create }
|
6
|
+
|
7
|
+
it 'should include base rules' do
|
8
|
+
|
9
|
+
engine << rule( :base ) {
|
10
|
+
forall {
|
11
|
+
has :x, :y, :Z
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
extended = engine << rule {
|
16
|
+
forall {
|
17
|
+
assuming :base
|
18
|
+
has :Z, :u, :W
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
engine << [:x, :y, 1]
|
23
|
+
engine << [:x, :y, 2]
|
24
|
+
engine << [1, :u, :a]
|
25
|
+
engine << [2, :u, :b]
|
26
|
+
result = Hash[ extended.tokens.map { |token| [ token[:Z], token[:W] ] } ]
|
27
|
+
expect( result ).to eq(1 => :a, 2 => :b)
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should check for base rule\'s existence' do
|
32
|
+
|
33
|
+
f = -> {
|
34
|
+
engine << rule {
|
35
|
+
forall {
|
36
|
+
assuming :base
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
expect( &f ).to raise_error Wongi::Engine::UndefinedBaseRule
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should come first in a rule' do
|
46
|
+
|
47
|
+
f = -> {
|
48
|
+
engine << rule( :base ) {
|
49
|
+
forall {
|
50
|
+
has :x, :y, :Z
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
engine << rule {
|
55
|
+
forall {
|
56
|
+
has :Z, :u, :W
|
57
|
+
assuming :base
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
expect( &f ).to raise_error Wongi::Engine::DefinitionError
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/spec/rule_specs/ncc_spec.rb
CHANGED
@@ -22,6 +22,19 @@ describe "NCC rule" do
|
|
22
22
|
}
|
23
23
|
end
|
24
24
|
|
25
|
+
def ncc_rule_post_has
|
26
|
+
rule('ncc post has') {
|
27
|
+
forall {
|
28
|
+
has "base", "is", :Base
|
29
|
+
none {
|
30
|
+
has :Base, 2, :X
|
31
|
+
has :X, 4, 5
|
32
|
+
}
|
33
|
+
has "base", "is", :Base2
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
25
38
|
it 'should pass with a mismatching subchain' do
|
26
39
|
|
27
40
|
engine << ncc_rule
|
@@ -60,6 +73,26 @@ describe "NCC rule" do
|
|
60
73
|
|
61
74
|
end
|
62
75
|
|
76
|
+
it 'can handle an alpha node template introduced after the negative-conjunctive-condition' do
|
77
|
+
|
78
|
+
engine << ncc_rule_post_has
|
79
|
+
|
80
|
+
production = engine.productions['ncc post has']
|
81
|
+
|
82
|
+
engine << ["base", "is", 1]
|
83
|
+
engine << [1, 2, 3]
|
84
|
+
engine << [3, 4, 5]
|
85
|
+
|
86
|
+
expect( production ).to have(0).tokens
|
87
|
+
|
88
|
+
engine.retract [3, 4, 5]
|
89
|
+
expect( production ).to have(1).tokens
|
90
|
+
|
91
|
+
engine.retract ["base", "is", 1]
|
92
|
+
expect( production ).to have(0).tokens
|
93
|
+
|
94
|
+
end
|
95
|
+
|
63
96
|
it 'should clean up correctly' do
|
64
97
|
|
65
98
|
engine.rule :rule1 do
|
@@ -1,70 +1,105 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "negative rule" do
|
4
|
-
|
5
|
-
before :each do
|
6
|
-
@engine = Wongi::Engine.create
|
7
|
-
end
|
8
|
-
|
9
|
-
def engine
|
10
|
-
@engine
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should not introduce variables" do
|
14
|
-
|
15
|
-
proc = lambda {
|
16
|
-
|
17
|
-
engine << rule('one-option') {
|
18
|
-
forall {
|
19
|
-
neg :Foo, :bar, :_
|
20
|
-
}
|
21
|
-
make {
|
22
|
-
action { |tokens|
|
23
|
-
raise "This should never get executed #{tokens}"
|
24
|
-
}
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
}
|
29
|
-
|
30
|
-
expect( &proc ).to raise_error( Wongi::Engine::DefinitionError )
|
31
|
-
|
32
|
-
end
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "negative rule" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@engine = Wongi::Engine.create
|
7
|
+
end
|
8
|
+
|
9
|
+
def engine
|
10
|
+
@engine
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not introduce variables" do
|
14
|
+
|
15
|
+
proc = lambda {
|
16
|
+
|
17
|
+
engine << rule('one-option') {
|
18
|
+
forall {
|
19
|
+
neg :Foo, :bar, :_
|
20
|
+
}
|
21
|
+
make {
|
22
|
+
action { |tokens|
|
23
|
+
raise "This should never get executed #{tokens}"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
}
|
29
|
+
|
30
|
+
expect( &proc ).to raise_error( Wongi::Engine::DefinitionError )
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
specify "variable example 1" do
|
35
|
+
|
36
|
+
prod = engine << rule {
|
37
|
+
forall {
|
38
|
+
has :x, :y, :Z
|
39
|
+
neg :a, :b, :Z
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
engine << [:x, :y, 1]
|
44
|
+
expect( prod ).to have(1).tokens
|
45
|
+
|
46
|
+
engine << [:a, :b, 1]
|
47
|
+
expect( prod ).to have(0).tokens
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
specify "variable example 1" do
|
52
|
+
|
53
|
+
prod = engine << rule {
|
54
|
+
forall {
|
55
|
+
has :x, :y, :Z
|
56
|
+
neg :a, :b, :Z
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
engine << [:a, :b, 1]
|
61
|
+
engine << [:x, :y, 1]
|
62
|
+
expect( prod ).to have(0).tokens
|
63
|
+
|
64
|
+
engine.retract [:a, :b, 1]
|
65
|
+
expect( prod ).to have(1).tokens
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# it "should not create infinite feedback loops by default" do
|
70
|
+
|
71
|
+
# engine << rule('feedback') {
|
72
|
+
# forall {
|
73
|
+
# neg :a, :b, :_
|
74
|
+
# }
|
75
|
+
# make {
|
76
|
+
# gen :a, :b, :c
|
77
|
+
# }
|
78
|
+
# }
|
79
|
+
|
80
|
+
# engine.should have(1).facts
|
81
|
+
|
82
|
+
# end
|
83
|
+
|
84
|
+
it "should create infinite feedback loops with unsafe option" do
|
85
|
+
|
86
|
+
counter = 0
|
87
|
+
exception = Class.new( StandardError )
|
88
|
+
|
89
|
+
proc = lambda {
|
90
|
+
engine << rule('feedback') {
|
91
|
+
forall {
|
92
|
+
neg :a, :b, :_, unsafe: true
|
93
|
+
}
|
94
|
+
make {
|
95
|
+
action { counter += 1 ; if counter > 5 then raise exception.new end }
|
96
|
+
gen :a, :b, :c
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
expect( &proc ).to raise_error( exception )
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/spec/wme_spec.rb
CHANGED
File without changes
|
data/wongi-engine.gemspec
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wongi-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valeri Sokolov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/wongi-engine/dsl/actions/statement_generator.rb
|
116
116
|
- lib/wongi-engine/dsl/actions/trace_action.rb
|
117
117
|
- lib/wongi-engine/dsl/any_rule.rb
|
118
|
+
- lib/wongi-engine/dsl/assuming.rb
|
118
119
|
- lib/wongi-engine/dsl/dsl_builder.rb
|
119
120
|
- lib/wongi-engine/dsl/dsl_extensions.rb
|
120
121
|
- lib/wongi-engine/dsl/extension_clause.rb
|
@@ -142,6 +143,7 @@ files:
|
|
142
143
|
- lib/wongi-engine/version.rb
|
143
144
|
- lib/wongi-engine/wme.rb
|
144
145
|
- lib/wongi-engine/wme_match_data.rb
|
146
|
+
- spec/beta_node_spec.rb
|
145
147
|
- spec/bug_specs/issue_4_spec.rb
|
146
148
|
- spec/dataset_spec.rb
|
147
149
|
- spec/dsl_spec.rb
|
@@ -150,6 +152,7 @@ files:
|
|
150
152
|
- spec/high_level_spec.rb
|
151
153
|
- spec/rule_specs/any_rule_spec.rb
|
152
154
|
- spec/rule_specs/assign_spec.rb
|
155
|
+
- spec/rule_specs/assuming_spec.rb
|
153
156
|
- spec/rule_specs/maybe_rule_spec.rb
|
154
157
|
- spec/rule_specs/ncc_spec.rb
|
155
158
|
- spec/rule_specs/negative_rule_spec.rb
|
@@ -183,6 +186,7 @@ signing_key:
|
|
183
186
|
specification_version: 4
|
184
187
|
summary: A forward-chaining rule engine in pure Ruby.
|
185
188
|
test_files:
|
189
|
+
- spec/beta_node_spec.rb
|
186
190
|
- spec/bug_specs/issue_4_spec.rb
|
187
191
|
- spec/dataset_spec.rb
|
188
192
|
- spec/dsl_spec.rb
|
@@ -191,6 +195,7 @@ test_files:
|
|
191
195
|
- spec/high_level_spec.rb
|
192
196
|
- spec/rule_specs/any_rule_spec.rb
|
193
197
|
- spec/rule_specs/assign_spec.rb
|
198
|
+
- spec/rule_specs/assuming_spec.rb
|
194
199
|
- spec/rule_specs/maybe_rule_spec.rb
|
195
200
|
- spec/rule_specs/ncc_spec.rb
|
196
201
|
- spec/rule_specs/negative_rule_spec.rb
|