wongi-engine 0.2.9 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fdcab85b1479a151cff39d50b422743ba3555ec
4
- data.tar.gz: 5d7cbe8c7a6280b45a16945d38ab99d192821a76
3
+ metadata.gz: b61e762202c7ce3d0abda3c806736d35ff540fdc
4
+ data.tar.gz: 1661ab89b84bff2380fd7514be8380066fc92358
5
5
  SHA512:
6
- metadata.gz: 37d9260e2109b969b58770c15931e9cfb72a4a0c52c8051a16000f6fb5ce2b3b98f3d0c8c969d08ca6d1ad3172343f554e74dd001230c904e668548e6585f3a6
7
- data.tar.gz: d6ec3764500423d2b36997f08f6db4aa2adb55be4cd4c28283e1c1a73bb13dad1194a457081a51a58cedba71742bd09fbd4734d109bf2affac4b2d31dab01473
6
+ metadata.gz: aaf20be03f915512942d39b2b12863d581a9875c481c7321e2e515504b9c20d93a5be66abbeec84be600b6c8c6766c2615c2fc43eed8764d7ca8464796ffa434
7
+ data.tar.gz: eda9d3711b0b62331cb195989159352b96d0a5eaaf0d6366292971e81264a990949fb076109bb0d6fc402d9e2fd15714f83c51387763613453defe663825e0a7
data/.hgtags CHANGED
@@ -11,3 +11,4 @@ b8b5d5ac7b39ce11d9bb86e61fcb04b227aca02d v0.2.7
11
11
  a2f9d52603db027baced51f673daf46b8fe8745a v0.2.9
12
12
  a2f9d52603db027baced51f673daf46b8fe8745a v0.2.9
13
13
  da4529420721bdd153839261e624b3738e7355e9 v0.2.9
14
+ 6db5e2b3978269c475ec03f45bdfb477ca70b427 v0.3.0
data/.travis.yml CHANGED
@@ -1,12 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0
5
- - 2.1
6
- - 2.2
7
- - 2.3.0
8
- - rbx-2.5.8
9
- - rbx-3.14
3
+ - 2.1.10
4
+ - 2.2.6
5
+ - 2.3.3
6
+ - 2.4.0
10
7
  - jruby-9.0
11
8
  sudo: false
12
9
  notifications:
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`
@@ -0,0 +1,15 @@
1
+ module Wongi::Engine
2
+ module DSL::Action
3
+ class AssignAction < SimpleAction
4
+ def initialize(var, &action)
5
+ @var = var
6
+ @action = action
7
+ end
8
+
9
+ def execute(token)
10
+ value = super
11
+ token.set(@var, value)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -13,7 +13,7 @@ module Wongi::Engine
13
13
 
14
14
  def option &block
15
15
  var = VariantRule.new
16
- var.instance_eval &block
16
+ var.forall &block
17
17
  variants << var
18
18
  end
19
19
 
@@ -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
- Generated.create_dsl_method c
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
- def self.create_dsl_method extension
4
+ module ClassMethods
5
+ def create_dsl_method extension
5
6
 
6
- section = extension[:section]
7
- clause = extension[:clause]
8
- action = extension[:action]
9
- body = extension[:body]
10
- acceptor = extension[:accept]
7
+ clause = extension[:clause]
8
+ action = extension[:action]
9
+ body = extension[:body]
10
+ acceptor = extension[:accept]
11
11
 
12
- define_method clause.first do |*args, &block|
12
+ define_method clause.first do |*args, &block|
13
13
 
14
- raise "#{clause.first} can only be invoke in section #{section}, currently in #{@current_section}" if section != @current_section
14
+ if body
15
15
 
16
- if body
16
+ instance_exec *args, &body
17
17
 
18
- instance_exec *args, &body
18
+ elsif acceptor
19
19
 
20
- elsif acceptor
20
+ rule.accept acceptor.new( *args, &block )
21
21
 
22
- accept acceptor.new( *args, &block )
22
+ elsif action
23
23
 
24
- elsif action
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
- c = Clause::Generic.new *args, &block
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
- end
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
- instance_eval &d
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
@@ -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
@@ -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
- @cached_assignments ||= all_assignments
50
+ all_assignments
48
51
  end
49
52
 
50
53
  def [] var
@@ -1,5 +1,5 @@
1
1
  module Wongi
2
2
  module Engine
3
- VERSION = "0.2.9"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -2,74 +2,76 @@ require 'spec_helper'
2
2
 
3
3
  describe "ANY rule" do
4
4
 
5
- before :each do
6
- @engine = Wongi::Engine.create
7
- end
5
+ include Wongi::Engine::DSL
8
6
 
9
- def engine
10
- @engine
11
- end
12
-
13
- context "with just one option" do
14
-
15
- it "should act like a positive matcher" do
16
-
17
- engine << rule('one-option') {
18
- forall {
19
- any {
20
- option {
21
- has 1, 2, :X
22
- has :X, 4, 5
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
- production = engine.productions['one-option']
29
-
30
- engine << [1, 2, 3]
31
- engine << [3, 4, 5]
30
+ production = engine.productions['one-option']
32
31
 
33
- expect(production.size).to eq(1)
32
+ engine << [1, 2, 3]
33
+ engine << [3, 4, 5]
34
34
 
35
- end
35
+ expect(production.size).to eq(1)
36
36
 
37
37
  end
38
38
 
39
- context "with several options" do
39
+ end
40
40
 
41
- specify "all matching branches must pass" do
41
+ context "with several options" do
42
42
 
43
- engine << rule('two-options') {
44
- forall {
45
- has 1, 2, :X
46
- any {
47
- option {
48
- has :X, 4, 5
49
- }
50
- option {
51
- has :X, "four", "five"
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
- production = engine.productions['two-options']
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
- expect(production.size).to eq(2)
68
- expect( engine.collection(:threes) ).to include(3)
69
- expect( engine.collection(:threes) ).to include("three")
64
+ engine << [1, 2, 3]
65
+ engine << [3, 4, 5]
66
+ engine << [1, 2, "three"]
67
+ engine << ["three", "four", "five"]
70
68
 
71
- end
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 {
@@ -101,8 +101,8 @@ describe Wongi::Engine::NccNode do
101
101
  }
102
102
  make {
103
103
  #trace values: true, generation: true
104
- gen self.name, :light_bathroom, :on
105
- gen self.name, :want_action_for, :light_bathroom
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 self.name, :light_bathroom, :on
163
- gen self.name, :want_action_for, :light_bathroom
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.2.9
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: 2016-06-08 00:00:00.000000000 Z
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.5.1
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.