ruleby 0.1 → 0.2

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.
@@ -0,0 +1,78 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
2
+ require 'ruleby'
3
+ require 'rulebook'
4
+
5
+ class Account
6
+ def initialize(status, title, account_id)
7
+ @status = status
8
+ @title = title
9
+ @account_id = account_id
10
+ end
11
+
12
+ attr :status, true
13
+ attr :title, true
14
+ attr :account_id, true
15
+ end
16
+
17
+ class Address
18
+ def initialize(addr_id, city, state, zip)
19
+ @addr_id = addr_id
20
+ @city = city
21
+ @state = state
22
+ @zip = zip
23
+ end
24
+
25
+ attr :addr_id, true
26
+ attr :city, true
27
+ attr :state, true
28
+ attr :zip, true
29
+ end
30
+
31
+ class TestRulebook < Rulebook
32
+ def rules
33
+ (0..50).each do |index|
34
+ rule "Rule-#{index}" do |r|
35
+ r.when do |has|
36
+ has.a Account
37
+ has.a.status = 'standard'
38
+
39
+ has.addr Address
40
+ has.addr.addr_id = "acc#{index}"
41
+ has.addr.city = 'Foobar'
42
+ has.addr.state = 'FB'
43
+ has.addr.zip = '12345'
44
+ end
45
+
46
+ r.then do |e,vars|
47
+ puts "rule #{index} fired"
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ include Ruleby
55
+
56
+ t1 = Time.new
57
+ engine :engine do |e|
58
+ TestRulebook.new(e).rules
59
+
60
+ t2 = Time.new
61
+ diff = t2.to_f - t1.to_f
62
+ puts 'time to create rule set: ' + diff.to_s
63
+
64
+ e.assert Account.new('standard', nil, nil)
65
+ for k in (0..500)
66
+ e.assert Address.new(('acc'+k.to_s),'Foobar', 'FB', '12345')
67
+ end
68
+
69
+ t3 = Time.new
70
+ diff = t3.to_f - t2.to_f
71
+ puts 'time to assert facts: ' + diff.to_s
72
+
73
+ e.match
74
+
75
+ t4 = Time.new
76
+ diff = t4.to_f - t3.to_f
77
+ puts 'time to match rules: ' + diff.to_s
78
+ end
@@ -0,0 +1,57 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
2
+ require 'ruleby'
3
+ require 'rulebook'
4
+
5
+ class Account
6
+ def initialize(status, title, account_id)
7
+ @status = status
8
+ @title = title
9
+ @account_id = account_id
10
+ end
11
+
12
+ attr :status, true
13
+ attr :title, true
14
+ attr :account_id, true
15
+ end
16
+
17
+ class TestRulebook < Rulebook
18
+ def rules
19
+ (0..50).each do |index|
20
+ rule "Rule-#{index}" do |r|
21
+ r.when do |has|
22
+ has.a Account
23
+ has.a.status = 'standard'
24
+ has.a.title = 'mr'
25
+ has.a.account_id = "acc#{index}"
26
+ end
27
+
28
+ r.then do |e,vars|
29
+ puts "rule #{index} fired"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ include Ruleby
37
+
38
+ t1 = Time.new
39
+ engine :engine do |e|
40
+ TestRulebook.new(e).rules
41
+
42
+ t2 = Time.new
43
+ diff = t2.to_f - t1.to_f
44
+ puts 'time to create rule set: ' + diff.to_s
45
+ for k in (0..500)
46
+ e.assert Account.new('standard', 'mr', ('acc'+k.to_s))
47
+ end
48
+
49
+ t3 = Time.new
50
+ diff = t3.to_f - t2.to_f
51
+ puts 'time to assert facts: ' + diff.to_s
52
+ e.match
53
+
54
+ t4 = Time.new
55
+ diff = t4.to_f - t3.to_f
56
+ puts 'time to match rules: ' + diff.to_s
57
+ end
@@ -0,0 +1,78 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
2
+ require 'ruleby'
3
+ require 'rulebook'
4
+
5
+ class Account
6
+ def initialize(status, title, account_id)
7
+ @status = status
8
+ @title = title
9
+ @account_id = account_id
10
+ end
11
+
12
+ attr :status, true
13
+ attr :title, true
14
+ attr :account_id, true
15
+ end
16
+
17
+ class Address
18
+ def initialize(addr_id, city, state, zip)
19
+ @addr_id = addr_id
20
+ @city = city
21
+ @state = state
22
+ @zip = zip
23
+ end
24
+
25
+ attr :addr_id, true
26
+ attr :city, true
27
+ attr :state, true
28
+ attr :zip, true
29
+ end
30
+
31
+ class TestRulebook < Rulebook
32
+ def rules
33
+ (0..5).each do |index|
34
+ rule "Rule-#{index}" do |r|
35
+ r.when do |has|
36
+ has.a Account
37
+ has.a.status = 'standard'
38
+
39
+ has.addr Address
40
+ has.addr.addr_id = "acc#{index}"
41
+ has.addr.city = 'Foobar'
42
+ has.addr.state = 'FB'
43
+ has.addr.zip = '12345'
44
+ end
45
+
46
+ r.then do |e,vars|
47
+ puts "rule #{index} fired"
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ include Ruleby
55
+
56
+ t1 = Time.new
57
+ engine :engine do |e|
58
+ TestRulebook.new(e).rules
59
+
60
+ t2 = Time.new
61
+ diff = t2.to_f - t1.to_f
62
+ puts 'time to create rule set: ' + diff.to_s
63
+
64
+ e.assert Account.new('standard', nil, nil)
65
+ for k in (0..500)
66
+ e.assert Address.new(('acc'+k.to_s),'Foobar', 'FB', '12345')
67
+ end
68
+
69
+ t3 = Time.new
70
+ diff = t3.to_f - t2.to_f
71
+ puts 'time to assert facts: ' + diff.to_s
72
+
73
+ e.match
74
+
75
+ t4 = Time.new
76
+ diff = t4.to_f - t3.to_f
77
+ puts 'time to match rules: ' + diff.to_s
78
+ end
@@ -0,0 +1,57 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
2
+ require 'ruleby'
3
+ require 'rulebook'
4
+
5
+ class Account
6
+ def initialize(status, title, account_id)
7
+ @status = status
8
+ @title = title
9
+ @account_id = account_id
10
+ end
11
+
12
+ attr :status, true
13
+ attr :title, true
14
+ attr :account_id, true
15
+ end
16
+
17
+ class TestRulebook < Rulebook
18
+ def rules
19
+ (0..5).each do |index|
20
+ rule "Rule-#{index}" do |r|
21
+ r.when do |has|
22
+ has.a Account
23
+ has.a.status = 'standard'
24
+ has.a.title = 'mr'
25
+ has.a.account_id = "acc#{index}"
26
+ end
27
+
28
+ r.then do |e,vars|
29
+ puts "rule #{index} fired"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ include Ruleby
37
+
38
+ t1 = Time.new
39
+ engine :engine do |e|
40
+ TestRulebook.new(e).rules
41
+
42
+ t2 = Time.new
43
+ diff = t2.to_f - t1.to_f
44
+ puts 'time to create rule set: ' + diff.to_s
45
+ for k in (0..500)
46
+ e.assert Account.new('standard', 'mr', ('acc'+k.to_s))
47
+ end
48
+
49
+ t3 = Time.new
50
+ diff = t3.to_f - t2.to_f
51
+ puts 'time to assert facts: ' + diff.to_s
52
+ e.match
53
+
54
+ t4 = Time.new
55
+ diff = t4.to_f - t3.to_f
56
+ puts 'time to match rules: ' + diff.to_s
57
+ end
@@ -0,0 +1,135 @@
1
+ module MissManners
2
+ class MannersData
3
+ def initialize
4
+ @guests16 = [
5
+ Guest.new(:n1, :f, :h3),
6
+ Guest.new(:n1, :f, :h1),
7
+ Guest.new(:n1, :f, :h2),
8
+ Guest.new(:n2, :f, :h3),
9
+ Guest.new(:n2, :f, :h2),
10
+ Guest.new(:n3, :m, :h1),
11
+ Guest.new(:n3, :m, :h3),
12
+ Guest.new(:n4, :m, :h2),
13
+ Guest.new(:n4, :m, :h1),
14
+ Guest.new(:n5, :m, :h2),
15
+ Guest.new(:n5, :m, :h3),
16
+ Guest.new(:n6, :m, :h2),
17
+ Guest.new(:n6, :m, :h1),
18
+ Guest.new(:n7, :f, :h2),
19
+ Guest.new(:n7, :f, :h1),
20
+ Guest.new(:n7, :f, :h3),
21
+ Guest.new(:n8, :f, :h3),
22
+ Guest.new(:n8, :f, :h2),
23
+ Guest.new(:n9, :f, :h1),
24
+ Guest.new(:n9, :f, :h3),
25
+ Guest.new(:n9, :f, :h2),
26
+ Guest.new(:n10, :m, :h2),
27
+ Guest.new(:n10, :m, :h3),
28
+ Guest.new(:n11, :m, :h3),
29
+ Guest.new(:n11, :m, :h2),
30
+ Guest.new(:n11, :m, :h1),
31
+ Guest.new(:n12, :m, :h3),
32
+ Guest.new(:n12, :m, :h1),
33
+ Guest.new(:n13, :m, :h2),
34
+ Guest.new(:n13, :m, :h3),
35
+ Guest.new(:n13, :m, :h1),
36
+ Guest.new(:n14, :f, :h3),
37
+ Guest.new(:n14, :f, :h1),
38
+ Guest.new(:n15, :f, :h3),
39
+ Guest.new(:n15, :f, :h2),
40
+ Guest.new(:n15, :f, :h1),
41
+ Guest.new(:n16, :f, :h3),
42
+ Guest.new(:n16, :f, :h2),
43
+ Guest.new(:n16, :f, :h1),
44
+ LastSeat.new(16)]
45
+
46
+ @guests32 = [
47
+ Guest.new(:n1, :m, :h1),
48
+ Guest.new(:n1, :m, :h3),
49
+ Guest.new(:n2, :f, :h3),
50
+ Guest.new(:n2, :f, :h2),
51
+ Guest.new(:n2, :f, :h1),
52
+ Guest.new(:n3, :f, :h1),
53
+ Guest.new(:n3, :f, :h2),
54
+ Guest.new(:n4, :f, :h3),
55
+ Guest.new(:n4, :f, :h1),
56
+ Guest.new(:n5, :f, :h1),
57
+ Guest.new(:n5, :f, :h2),
58
+ Guest.new(:n6, :m, :h1),
59
+ Guest.new(:n6, :m, :h2),
60
+ Guest.new(:n6, :m, :h3),
61
+ Guest.new(:n7, :f, :h2),
62
+ Guest.new(:n7, :f, :h1),
63
+ Guest.new(:n7, :f, :h3),
64
+ Guest.new(:n8, :f, :h1),
65
+ Guest.new(:n8, :f, :h3),
66
+ Guest.new(:n8, :f, :h2),
67
+ Guest.new(:n9, :f, :h1),
68
+ Guest.new(:n9, :f, :h3),
69
+ Guest.new(:n9, :f, :h2),
70
+ Guest.new(:n10, :m, :h2),
71
+ Guest.new(:n10, :m, :h1),
72
+ Guest.new(:n11, :m, :h2),
73
+ Guest.new(:n11, :m, :h1),
74
+ Guest.new(:n12, :m, :h3),
75
+ Guest.new(:n12, :m, :h2),
76
+ Guest.new(:n13, :m, :h1),
77
+ Guest.new(:n13, :m, :h3),
78
+ Guest.new(:n14, :m, :h3),
79
+ Guest.new(:n14, :m, :h2),
80
+ Guest.new(:n15, :f, :h2),
81
+ Guest.new(:n15, :f, :h1),
82
+ Guest.new(:n15, :f, :h3),
83
+ Guest.new(:n16, :f, :h3),
84
+ Guest.new(:n16, :f, :h2),
85
+ Guest.new(:n16, :f, :h1),
86
+ Guest.new(:n17, :m, :h3),
87
+ Guest.new(:n17, :m, :h2),
88
+ Guest.new(:n18, :f, :h2),
89
+ Guest.new(:n18, :f, :h1),
90
+ Guest.new(:n19, :f, :h1),
91
+ Guest.new(:n19, :f, :h2),
92
+ Guest.new(:n19, :f, :h3),
93
+ Guest.new(:n20, :f, :h1),
94
+ Guest.new(:n20, :f, :h2),
95
+ Guest.new(:n20, :f, :h3),
96
+ Guest.new(:n21, :m, :h2),
97
+ Guest.new(:n21, :m, :h3),
98
+ Guest.new(:n21, :m, :h1),
99
+ Guest.new(:n22, :f, :h1),
100
+ Guest.new(:n22, :f, :h2),
101
+ Guest.new(:n22, :f, :h3),
102
+ Guest.new(:n23, :f, :h3),
103
+ Guest.new(:n23, :f, :h1),
104
+ Guest.new(:n23, :f, :h2),
105
+ Guest.new(:n24, :m, :h1),
106
+ Guest.new(:n24, :m, :h3),
107
+ Guest.new(:n25, :f, :h3),
108
+ Guest.new(:n25, :f, :h2),
109
+ Guest.new(:n25, :f, :h1),
110
+ Guest.new(:n26, :f, :h3),
111
+ Guest.new(:n26, :f, :h2),
112
+ Guest.new(:n26, :f, :h1),
113
+ Guest.new(:n27, :m, :h3),
114
+ Guest.new(:n27, :m, :h1),
115
+ Guest.new(:n27, :m, :h2),
116
+ Guest.new(:n28, :m, :h3),
117
+ Guest.new(:n28, :m, :h1),
118
+ Guest.new(:n29, :m, :h3),
119
+ Guest.new(:n29, :m, :h2),
120
+ Guest.new(:n29, :m, :h1),
121
+ Guest.new(:n30, :m, :h2),
122
+ Guest.new(:n30, :m, :h1),
123
+ Guest.new(:n30, :m, :h3),
124
+ Guest.new(:n31, :m, :h2),
125
+ Guest.new(:n31, :m, :h1),
126
+ Guest.new(:n32, :m, :h1),
127
+ Guest.new(:n32, :m, :h3),
128
+ Guest.new(:n32, :m, :h2),
129
+ LastSeat.new(32)]
130
+ end
131
+
132
+ attr_reader :guests16
133
+ attr_reader :guests32
134
+ end
135
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../../lib/')
2
+ require 'ruleby'
3
+ require 'rulebook'
4
+ require 'model'
5
+ require 'data'
6
+ require 'rules'
7
+
8
+ include Ruleby
9
+ include MissManners
10
+
11
+ t1 = Time.new
12
+ engine :e do |e|
13
+ MannersRulebook.new(e).rules
14
+ MannersData.new.guests16.each do |g|
15
+ e.assert g
16
+ end
17
+ e.assert Context.new(:start)
18
+ e.assert Count.new(1)
19
+ e.match
20
+ end
21
+ t2 = Time.new
22
+ diff = t2.to_f - t1.to_f
23
+ puts diff.to_s
@@ -0,0 +1,182 @@
1
+ module MissManners
2
+
3
+ class Chosen
4
+ def initialize(id,guestName,hobby)
5
+ @id = id
6
+ @guestName = guestName
7
+ @hobby = hobby
8
+ end
9
+ attr_reader :id, :guestName, :hobby
10
+ def to_s
11
+ "{Chosen id=#{@id}, name=#{@guestName}, hobbies=#{@hobby}}"
12
+ end
13
+ end
14
+
15
+ class Context
16
+ START_UP = 0
17
+ ASSIGN_SEATS = 1
18
+ MAKE_PATH = 2
19
+ CHECK_DONE = 3
20
+ PRINT_RESULTS = 4
21
+ STATE_STRINGS = ["START_UP","ASSIGN_SEATS","MAKE_PATH","CHECK_DONE","PRINT_RESULTS"]
22
+ def initialize(state)
23
+ if state == :start
24
+ @state = START_UP
25
+ else
26
+ raise "Context #{state.to_s} does not exist for Context Enum"
27
+ end
28
+ end
29
+ attr :state, true
30
+ def string_value
31
+ return STATE_STRINGS[@state]
32
+ end
33
+ def is_state(state)
34
+ return @state == state
35
+ end
36
+ def to_s
37
+ return "[Context state=" + string_value.to_s + "]";
38
+ end
39
+ end
40
+
41
+ class Count
42
+ def initialize(value)
43
+ @value = value
44
+ end
45
+ attr :value, true
46
+ def ==(object)
47
+ if object.object_id == self.object_id
48
+ return true
49
+ elsif object == nil || !(object.kind_of?(Count))
50
+ return false
51
+ end
52
+ return @value == object.value;
53
+ end
54
+ def to_s
55
+ return "[Count value=#{@value.to_s}]"
56
+ end
57
+ def to_hash
58
+ return value.to_hash
59
+ end
60
+ end
61
+
62
+ class Guest
63
+ def initialize(name,sex,hobby)
64
+ @name = name
65
+ @sex = sex
66
+ @hobby = hobby
67
+ end
68
+ attr_reader :name,:sex,:hobby
69
+ def to_s
70
+ return "[Guest name=" + @name.to_s + ", sex=" + @sex.to_s + ", hobbies=" + @hobby.to_s + "]";
71
+ end
72
+ end
73
+
74
+ class Hobby
75
+ @stringH1 = "h1"
76
+ @stringH2 = "h2"
77
+ @stringH3 = "h3"
78
+ @stringH4 = "h4"
79
+ @stringH5 = "h5"
80
+ HOBBY_STRINGS = [@stringH1,@stringH2,@stringH3,@stringH4,@stringH5]
81
+ def initialize(hobby)
82
+ @hobbyIndex = hobby-1
83
+ @hobby = HOBBY_STRINGS[@hobbyIndex]
84
+ end
85
+ H1 = Hobby.new(1)
86
+ H2 = Hobby.new(2)
87
+ H3 = Hobby.new(3)
88
+ H4 = Hobby.new(4)
89
+ H5 = Hobby.new(5)
90
+ attr_reader :hobby
91
+ def resolve(hobby)
92
+ if @stringH1 == hobby
93
+ return H1
94
+ elsif @stringH2 == hobby
95
+ return H2
96
+ elsif @stringH3 == hobby
97
+ return H3
98
+ elsif @stringH4 == hobby
99
+ return H4
100
+ elsif @stringH5 == hobby
101
+ return H5
102
+ else
103
+ raise "Hobby '" + @hobby.to_s + "' does not exist for Hobby Enum"
104
+ end
105
+ end
106
+ def to_hash
107
+ return @hobbyIndex.to_hash
108
+ end
109
+ def to_s
110
+ return @hobby
111
+ end
112
+ end
113
+
114
+ class LastSeat
115
+ def initialize(seat)
116
+ @seat = seat
117
+ end
118
+ attr_reader :seat
119
+ def to_s
120
+ return "[LastSeat seat=#{@seat.to_s}]"
121
+ end
122
+ end
123
+
124
+ class Path
125
+ def initialize(id,seat,guestName)
126
+ @id = id
127
+ @guestName = guestName
128
+ @seat = seat
129
+ end
130
+ attr_reader :id, :guestName, :seat
131
+ def to_s
132
+ "[Path id=#{@id.to_s}, name=#{@guestName.to_s}, seat=#{@seat.to_s}]"
133
+ end
134
+ end
135
+
136
+ class Seating
137
+ def initialize(id,pid,path,leftSeat,leftGuestName,rightSeat,rightGuestName)
138
+ @id = id
139
+ @pid = pid
140
+ @path = path
141
+ @leftSeat = leftSeat
142
+ @leftGuestName = leftGuestName
143
+ @rightSeat = rightSeat
144
+ @rightGuestName = rightGuestName
145
+ end
146
+ attr :path, true
147
+ attr_reader :id,:pid,:leftSeat,:leftGuestName,:rightSeat,:rightGuestName
148
+ def to_s
149
+ return "[Seating id=#{@id.to_s} , pid=#{pid.to_s} , pathDone=#{@path.to_s} , leftSeat=#{@leftSeat.to_s}, leftGuestName=#{@leftGuestName.to_s}, rightSeat=#{@rightSeat.to_s}, rightGuestName=#{@rightGuestName.to_s}]";
150
+ end
151
+ end
152
+
153
+ class Sex
154
+ STRING_M = 'm'
155
+ STRING_F = 'f'
156
+ SEX_LIST = [ STRING_M, STRING_F ]
157
+ def initialize(sex)
158
+ @sex = sex
159
+ end
160
+ M = Sex.new( 0 )
161
+ F = Sex.new( 1 )
162
+ def sex
163
+ return SEX_LIST[sex]
164
+ end
165
+ def resolve(sex)
166
+ if STRING_M == sex
167
+ return M
168
+ elsif STRING_F == sex
169
+ return F
170
+ else
171
+ raise "Sex '#{@sex.to_s}' does not exist for Sex Enum"
172
+ end
173
+ end
174
+ def to_s
175
+ return @sex.to_s
176
+ end
177
+ def to_hash
178
+ return @sex.to_hash
179
+ end
180
+ end
181
+
182
+ end