Class: Ruleby::Ferrari::RuleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/ferrari.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, pattern = nil, action = nil, priority = 0) ⇒ RuleBuilder

Returns a new instance of RuleBuilder.



205
206
207
208
209
210
211
212
213
214
# File 'lib/dsl/ferrari.rb', line 205

def initialize(name, pattern=nil, action=nil, priority=0) 
  @name = name
  @pattern = pattern
  @action = action  
  @priority = priority            

  @tags = {}
  @methods = {}
  @when_counter = 0
end

Instance Method Details

#build_ruleObject



295
296
297
# File 'lib/dsl/ferrari.rb', line 295

def build_rule
  Core::Rule.new @name, @pattern, @action, @priority
end

#priorityObject



286
287
288
# File 'lib/dsl/ferrari.rb', line 286

def priority
  return @priority
end

#priority=(p) ⇒ Object



290
291
292
293
# File 'lib/dsl/ferrari.rb', line 290

def priority=(p)
  @priority = p
  @action.priority = @priority
end

#then(&block) ⇒ Object



280
281
282
283
284
# File 'lib/dsl/ferrari.rb', line 280

def then(&block)
  @action = Core::Action.new(&block)  
  @action.name = @name
  @action.priority = @priority
end

#when(*args) ⇒ Object



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
# File 'lib/dsl/ferrari.rb', line 216

def when(*args)      
  clazz = AtomBuilder === args[0] ? nil : args.shift
  is_not = false
  is_collect = 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 == :collect
      is_collect = true
    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::Template.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.kind_of? FunctionBuilder
      atoms.push arg.build_atom(GeneratedTag.new, deftemplate)
    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) : 
                          is_collect ? Core::CollectPattern.new(head, atoms) :
                                       Core::ObjectPattern.new(head, atoms)
  end
  @pattern = @pattern ? Core::AndPattern.new(@pattern, p) : p
end