-
Notifications
You must be signed in to change notification settings - Fork 42
/
ferrari.rb
executable file
·291 lines (245 loc) · 8.23 KB
/
ferrari.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# This file is part of the Ruleby project (http://ruleby.org)
#
# This application is free software; you can redistribute it and/or
# modify it under the terms of the Ruby license defined in the
# LICENSE.txt file.
#
# Copyright (c) 2007 Joe Kutner and Matt Smith. All rights reserved.
#
# * Authors: Joe Kutner
#
module Ruleby
module Ferrari
class RulebookHelper
def initialize(engine)
@engine = engine
end
attr_reader :engine
def rule(name, *args, &block)
options = args[0].kind_of?(Hash) ? args.shift : {}
r = RuleBuilder.new name
args.each do |arg|
if arg.kind_of? Array
r.when(*arg)
else
raise 'Invalid condition. All or none must be Arrays.'
end
end
r.then(&block)
r.priority = options[:priority] if options[:priority]
@engine.assert_rule(r.build_rule)
end
end
class RuleBuilder
def initialize(name, pattern=nil, action=nil, priority=0)
@name = name
@pattern = pattern
@action = action
@priority = priority
@tags = {}
@methods = {}
@when_counter = 0
end
def when(*args)
clazz = AtomBuilder === args[0] ? nil : args.shift
is_not = false
mode = :equals
while clazz.is_a? Symbol
if clazz == :not || clazz == :~
is_not = true
elsif clazz == :is_a? || clazz == :kind_of? || clazz == :instance_of?
mode = :inherits
elsif clazz == :exists?
raise 'The \'exists\' quantifier is not yet supported.'
end
clazz = args.empty? ? nil : args.shift
end
if clazz == nil
clazz = Object
mode = :inherits
end
deftemplate = Core::DefTemplate.new clazz, mode
atoms = []
@when_counter += 1
htag = Symbol === args[0] ? args.shift : GeneratedTag.new
head = Core::HeadAtom.new htag, deftemplate
@tags[htag] = @when_counter
args.each do |arg|
if arg.kind_of? Hash
arg.each do |ab,tag|
ab.tag = tag
ab.deftemplate = deftemplate
@tags[tag] = @when_counter
@methods[tag] = ab.name
atoms.push *ab.build_atoms(@tags, @methods, @when_counter)
end
elsif arg.kind_of? AtomBuilder
arg.tag = GeneratedTag.new
arg.deftemplate = deftemplate
@methods[arg.tag] = arg.name
atoms.push *arg.build_atoms(@tags, @methods, @when_counter)
elsif arg == false
raise 'The != operator is not allowed.'
else
raise "Invalid condition: #{arg}"
end
end
if is_not
p = mode==:inherits ? Core::NotInheritsPattern.new(head, atoms) :
Core::NotPattern.new(head, atoms)
else
p = mode==:inherits ? Core::InheritsPattern.new(head, atoms) :
Core::ObjectPattern.new(head, atoms)
end
@pattern = @pattern ? Core::AndPattern.new(@pattern, p) : p
return nil
end
def then(&block)
@action = Core::Action.new(&block)
@action.name = @name
@action.priority = @priority
end
def priority
return @priority
end
def priority=(p)
@priority = p
@action.priority = @priority
end
def build_rule
Core::Rule.new @name, @pattern, @action, @priority
end
end
class MethodBuilder
public_instance_methods.each do |m|
a = [:method_missing, :new, :public_instance_methods, :__send__, :__id__]
undef_method m.to_sym unless a.include? m.to_sym
end
def method_missing(method_id, *args, &block)
ab = AtomBuilder.new method_id
if block_given?
args.each do |arg|
ab.bindings.push BindingBuilder.new(arg, method_id)
end
ab.block = block
elsif args.size > 0
puts args.class.to_s + ' --- ' + args.to_s
raise 'Arguments not supported for short-hand conditions'
end
return ab
end
end
class BindingBuilder
attr_accessor :tag, :method
def initialize(tag,method=nil)
@tag = tag
@method = method
end
def +(arg)
raise 'Cannot use operators in short-hand mode!'
end
def -(arg)
raise 'Cannot use operators in short-hand mode!'
end
def /(arg)
raise 'Cannot use operators in short-hand mode!'
end
def *(arg)
raise 'Cannot use operators in short-hand mode!'
end
def to_s
"BindingBuilder @tag=#{@tag}, @method=#{@method}"
end
end
class AtomBuilder
attr_accessor :tag, :name, :bindings, :deftemplate, :block
def initialize(method_id)
@name = method_id
@deftemplate = nil
@tag = GeneratedTag.new
@bindings = []
@block = lambda {|x| true}
@child_atom_builders = []
end
def method_missing(method_id, *args, &block)
if method_id == :not
return NotOperatorBuilder.new(@name)
end
end
def ==(value)
@atom_type = :equals
@value = value
create_block value, lambda {|x,y| x == y}, lambda {|x| x == value}; self
end
def >(value)
create_block value, lambda {|x,y| x > y}, lambda {|x| x > value}; self
end
def <(value)
create_block value, lambda {|x,y| x < y}, lambda {|x| x < value}; self
end
def =~(value)
create_block value, lambda {|x,y| x =~ y}, lambda {|x| x =~ value}; self
end
def <=(value)
create_block value, lambda {|x,y| x <= y}, lambda {|x| x <= value}; self
end
def >=(value)
create_block value, lambda {|x,y| x >= y}, lambda {|x| x >= value}; self
end
def =~(value)
create_block value, lambda {|x,y| x =~ y}, lambda {|x| x =~ value}; self
end
def build_atoms(tags,methods,when_id)
atoms = @child_atom_builders.map { |atom_builder|
tags[atom_builder.tag] = when_id
methods[atom_builder.tag] = atom_builder.name
atom_builder.build_atoms(tags,methods,when_id)
}.flatten || []
if @bindings.empty?
if @atom_type == :equals
return atoms << Core::EqualsAtom.new(@tag, @name, @deftemplate, @value)
else
return atoms << Core::PropertyAtom.new(@tag, @name, @deftemplate, &@block)
end
end
if references_self?(tags,when_id)
bind_methods = @bindings.collect{ |bb| methods[bb.tag] }
atoms << Core::SelfReferenceAtom.new(@tag,@name,bind_methods,@deftemplate,&@block)
else
bind_tags = @bindings.collect{ |bb| bb.tag }
atoms << Core::ReferenceAtom.new(@tag,@name,bind_tags,@deftemplate,&@block)
end
end
private
def references_self?(tags,when_id)
ref_self = 0
@bindings.each do |bb|
if (tags[bb.tag] == when_id)
ref_self += 1
end
end
if ref_self > 0 and ref_self != @bindings.size
raise 'Binding to self and another pattern in the same condition is not yet supported.'
end
return ref_self > 0
end
def create_block(value, ref_block, basic_block)
if value && value.kind_of?(BindingBuilder)
@bindings = [value]
@block = ref_block
elsif value && value.kind_of?(AtomBuilder)
@child_atom_builders << value
@bindings = [BindingBuilder.new(value.tag)]
@block = ref_block
else
@block = basic_block
end
end
end
class NotOperatorBuilder < AtomBuilder
def ==(value)
create_block value, lambda {|x,y| x != y}, lambda {|x| x != value}; self
end
end
end
end