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.



109
110
111
112
113
114
115
116
117
118
# File 'lib/dsl/ferrari.rb', line 109

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



193
194
195
# File 'lib/dsl/ferrari.rb', line 193

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

#priorityObject



184
185
186
# File 'lib/dsl/ferrari.rb', line 184

def priority
  return @priority
end

#priority=(p) ⇒ Object



188
189
190
191
# File 'lib/dsl/ferrari.rb', line 188

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

#then(&block) ⇒ Object



178
179
180
181
182
# File 'lib/dsl/ferrari.rb', line 178

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

#when(*args) ⇒ Object



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

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
end