wongi-engine 0.2.9 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hgtags +1 -0
- data/.travis.yml +4 -7
- data/CHANGELOG.md +5 -0
- data/lib/wongi-engine/dsl/action/assign_action.rb +15 -0
- data/lib/wongi-engine/dsl/any_rule.rb +1 -1
- data/lib/wongi-engine/dsl/builder.rb +4 -1
- data/lib/wongi-engine/dsl/generated.rb +26 -21
- data/lib/wongi-engine/dsl/rule.rb +3 -5
- data/lib/wongi-engine/dsl.rb +8 -0
- data/lib/wongi-engine/token.rb +5 -2
- data/lib/wongi-engine/version.rb +1 -1
- data/spec/rule_specs/any_rule_spec.rb +51 -49
- data/spec/rule_specs/assign_spec.rb +16 -0
- data/spec/rule_specs/ncc_spec.rb +4 -4
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b61e762202c7ce3d0abda3c806736d35ff540fdc
|
4
|
+
data.tar.gz: 1661ab89b84bff2380fd7514be8380066fc92358
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aaf20be03f915512942d39b2b12863d581a9875c481c7321e2e515504b9c20d93a5be66abbeec84be600b6c8c6766c2615c2fc43eed8764d7ca8464796ffa434
|
7
|
+
data.tar.gz: eda9d3711b0b62331cb195989159352b96d0a5eaaf0d6366292971e81264a990949fb076109bb0d6fc402d9e2fd15714f83c51387763613453defe663825e0a7
|
data/.hgtags
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.3.0
|
4
|
+
|
5
|
+
* Breaking change: the content of `forall` and `make` is no longer evaluated in the rule context. Instead, the rule is accessible as `rule`.
|
6
|
+
* Added `assign` to the `make` section
|
7
|
+
|
3
8
|
## 0.2.9
|
4
9
|
|
5
10
|
* fixed a bug preventing variables being bound to `false` and `nil`
|
@@ -10,7 +10,10 @@ module Wongi::Engine::DSL
|
|
10
10
|
def build &definition
|
11
11
|
instance_eval &definition
|
12
12
|
@clauses.each do |c|
|
13
|
-
|
13
|
+
Wongi::Engine::DSL.sections[c[:section]] ||= Class.new do
|
14
|
+
include Generated
|
15
|
+
end
|
16
|
+
Wongi::Engine::DSL.sections[c[:section]].create_dsl_method(c)
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
@@ -1,42 +1,47 @@
|
|
1
1
|
module Wongi::Engine::DSL
|
2
2
|
module Generated
|
3
3
|
|
4
|
-
|
4
|
+
module ClassMethods
|
5
|
+
def create_dsl_method extension
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
acceptor = extension[:accept]
|
7
|
+
clause = extension[:clause]
|
8
|
+
action = extension[:action]
|
9
|
+
body = extension[:body]
|
10
|
+
acceptor = extension[:accept]
|
11
11
|
|
12
|
-
|
12
|
+
define_method clause.first do |*args, &block|
|
13
13
|
|
14
|
-
|
14
|
+
if body
|
15
15
|
|
16
|
-
|
16
|
+
instance_exec *args, &body
|
17
17
|
|
18
|
-
|
18
|
+
elsif acceptor
|
19
19
|
|
20
|
-
|
20
|
+
rule.accept acceptor.new( *args, &block )
|
21
21
|
|
22
|
-
|
22
|
+
elsif action
|
23
23
|
|
24
|
-
|
24
|
+
c = Clause::Generic.new *args, &block
|
25
|
+
c.name = clause.first
|
26
|
+
c.action = action
|
27
|
+
c.rule = self.rule
|
28
|
+
rule.accept c
|
25
29
|
|
26
|
-
|
27
|
-
c.name = clause.first
|
28
|
-
c.action = action
|
29
|
-
c.rule = self
|
30
|
-
accept c
|
30
|
+
end
|
31
31
|
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
clause[1..-1].each do |al|
|
35
|
+
alias_method al, clause.first
|
36
|
+
end
|
35
37
|
|
36
|
-
clause[1..-1].each do |al|
|
37
|
-
alias_method al, clause.first
|
38
38
|
end
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_accessor :rule
|
39
42
|
|
43
|
+
def self.included(base)
|
44
|
+
base.extend ClassMethods
|
40
45
|
end
|
41
46
|
|
42
47
|
end
|
@@ -4,8 +4,6 @@ module Wongi::Engine
|
|
4
4
|
|
5
5
|
attr_reader :name
|
6
6
|
|
7
|
-
include Generated
|
8
|
-
|
9
7
|
class << self
|
10
8
|
|
11
9
|
def section s, *aliases
|
@@ -13,7 +11,9 @@ module Wongi::Engine
|
|
13
11
|
sections << s
|
14
12
|
define_method s do |&d|
|
15
13
|
@current_section = s
|
16
|
-
|
14
|
+
section = DSL.sections[s].new
|
15
|
+
section.rule = self
|
16
|
+
section.instance_eval &d
|
17
17
|
end
|
18
18
|
aliases.each { |a| alias_method a, s }
|
19
19
|
end
|
@@ -72,8 +72,6 @@ module Wongi::Engine
|
|
72
72
|
rete.install_rule( self )
|
73
73
|
end
|
74
74
|
|
75
|
-
protected
|
76
|
-
|
77
75
|
def accept stuff
|
78
76
|
acceptors[@current_section] << stuff
|
79
77
|
end
|
data/lib/wongi-engine/dsl.rb
CHANGED
@@ -2,6 +2,10 @@ module Wongi::Engine
|
|
2
2
|
module DSL
|
3
3
|
extend self
|
4
4
|
|
5
|
+
def sections
|
6
|
+
@sections ||= {}
|
7
|
+
end
|
8
|
+
|
5
9
|
def ruleset name = nil, &definition
|
6
10
|
rs = Ruleset.new
|
7
11
|
if ! name.nil?
|
@@ -47,6 +51,7 @@ require 'wongi-engine/dsl/action/statement_generator'
|
|
47
51
|
require 'wongi-engine/dsl/action/simple_collector'
|
48
52
|
require 'wongi-engine/dsl/action/trace_action'
|
49
53
|
require 'wongi-engine/dsl/action/error_generator'
|
54
|
+
require 'wongi-engine/dsl/action/assign_action'
|
50
55
|
|
51
56
|
module Wongi::Engine::DSL
|
52
57
|
dsl {
|
@@ -128,5 +133,8 @@ module Wongi::Engine::DSL
|
|
128
133
|
|
129
134
|
clause :action
|
130
135
|
action Action::SimpleAction
|
136
|
+
|
137
|
+
clause :assign
|
138
|
+
action Action::AssignAction
|
131
139
|
}
|
132
140
|
end
|
data/lib/wongi-engine/token.rb
CHANGED
@@ -37,14 +37,17 @@ module Wongi::Engine
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def subst variable, value
|
40
|
-
@cached_assignments = nil
|
41
40
|
if @assignments.has_key? variable
|
42
41
|
@assignments[ variable ] = value
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
45
|
+
def set(variable, value)
|
46
|
+
@assignments[variable] = value
|
47
|
+
end
|
48
|
+
|
46
49
|
def assignments
|
47
|
-
|
50
|
+
all_assignments
|
48
51
|
end
|
49
52
|
|
50
53
|
def [] var
|
data/lib/wongi-engine/version.rb
CHANGED
@@ -2,74 +2,76 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "ANY rule" do
|
4
4
|
|
5
|
-
|
6
|
-
@engine = Wongi::Engine.create
|
7
|
-
end
|
5
|
+
include Wongi::Engine::DSL
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
before :each do
|
8
|
+
@engine = Wongi::Engine.create
|
9
|
+
end
|
10
|
+
|
11
|
+
def engine
|
12
|
+
@engine
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with just one option" do
|
16
|
+
|
17
|
+
it "should act like a positive matcher" do
|
18
|
+
|
19
|
+
engine << rule('one-option') {
|
20
|
+
forall {
|
21
|
+
any {
|
22
|
+
option {
|
23
|
+
has 1, 2, :X
|
24
|
+
has :X, 4, 5
|
24
25
|
}
|
25
26
|
}
|
26
27
|
}
|
28
|
+
}
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
engine << [1, 2, 3]
|
31
|
-
engine << [3, 4, 5]
|
30
|
+
production = engine.productions['one-option']
|
32
31
|
|
33
|
-
|
32
|
+
engine << [1, 2, 3]
|
33
|
+
engine << [3, 4, 5]
|
34
34
|
|
35
|
-
|
35
|
+
expect(production.size).to eq(1)
|
36
36
|
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
end
|
40
40
|
|
41
|
-
|
41
|
+
context "with several options" do
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
43
|
+
specify "all matching branches must pass" do
|
44
|
+
|
45
|
+
engine << rule('two-options') {
|
46
|
+
forall {
|
47
|
+
has 1, 2, :X
|
48
|
+
any {
|
49
|
+
option {
|
50
|
+
has :X, 4, 5
|
51
|
+
}
|
52
|
+
option {
|
53
|
+
has :X, "four", "five"
|
53
54
|
}
|
54
|
-
}
|
55
|
-
make {
|
56
|
-
collect :X, :threes
|
57
55
|
}
|
58
56
|
}
|
57
|
+
make {
|
58
|
+
collect :X, :threes
|
59
|
+
}
|
60
|
+
}
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
-
engine << [1, 2, 3]
|
63
|
-
engine << [3, 4, 5]
|
64
|
-
engine << [1, 2, "three"]
|
65
|
-
engine << ["three", "four", "five"]
|
62
|
+
production = engine.productions['two-options']
|
66
63
|
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
engine << [1, 2, 3]
|
65
|
+
engine << [3, 4, 5]
|
66
|
+
engine << [1, 2, "three"]
|
67
|
+
engine << ["three", "four", "five"]
|
70
68
|
|
71
|
-
|
69
|
+
expect(production.size).to eq(2)
|
70
|
+
expect( engine.collection(:threes) ).to include(3)
|
71
|
+
expect( engine.collection(:threes) ).to include("three")
|
72
72
|
|
73
73
|
end
|
74
74
|
|
75
|
+
end
|
76
|
+
|
75
77
|
end
|
@@ -18,6 +18,22 @@ describe "ASSIGN rule" do
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
+
it 'should be available in the make section' do
|
22
|
+
production = engine << rule {
|
23
|
+
forall {
|
24
|
+
has 1, 2, :X
|
25
|
+
}
|
26
|
+
make {
|
27
|
+
assign(:Y) { |token| token[:X] * 2 }
|
28
|
+
gen :Y, 5, 6
|
29
|
+
}
|
30
|
+
}
|
31
|
+
engine << [1, 2, 21]
|
32
|
+
expect(production.tokens.first[:X]).to be == 21
|
33
|
+
expect(engine.find(42, 5, 6)).not_to be_nil
|
34
|
+
expect(production.tokens.first[:Y]).to be == 42
|
35
|
+
end
|
36
|
+
|
21
37
|
it "should be able to access previous assignments" do
|
22
38
|
|
23
39
|
production = engine << rule {
|
data/spec/rule_specs/ncc_spec.rb
CHANGED
@@ -101,8 +101,8 @@ describe Wongi::Engine::NccNode do
|
|
101
101
|
}
|
102
102
|
make {
|
103
103
|
#trace values: true, generation: true
|
104
|
-
gen
|
105
|
-
gen
|
104
|
+
gen rule.name, :light_bathroom, :on
|
105
|
+
gen rule.name, :want_action_for, :light_bathroom
|
106
106
|
}
|
107
107
|
end
|
108
108
|
|
@@ -159,8 +159,8 @@ describe Wongi::Engine::NccNode do
|
|
159
159
|
}
|
160
160
|
make {
|
161
161
|
#trace values: true, generation: true
|
162
|
-
gen
|
163
|
-
gen
|
162
|
+
gen rule.name, :light_bathroom, :on
|
163
|
+
gen rule.name, :want_action_for, :light_bathroom
|
164
164
|
}
|
165
165
|
end
|
166
166
|
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valeri Sokolov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/wongi-engine/core_ext.rb
|
108
108
|
- lib/wongi-engine/data_overlay.rb
|
109
109
|
- lib/wongi-engine/dsl.rb
|
110
|
+
- lib/wongi-engine/dsl/action/assign_action.rb
|
110
111
|
- lib/wongi-engine/dsl/action/base.rb
|
111
112
|
- lib/wongi-engine/dsl/action/error_generator.rb
|
112
113
|
- lib/wongi-engine/dsl/action/simple_action.rb
|
@@ -184,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
185
|
version: '0'
|
185
186
|
requirements: []
|
186
187
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.
|
188
|
+
rubygems_version: 2.6.10
|
188
189
|
signing_key:
|
189
190
|
specification_version: 4
|
190
191
|
summary: A forward-chaining rule engine in pure Ruby.
|