Class: Fbe::Award::PTerm

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/award.rb

Overview

A term for policy.

Instance Method Summary collapse

Constructor Details

#initialize(operator, operands) ⇒ PTerm

Returns a new instance of PTerm.



163
164
165
166
# File 'lib/fbe/award.rb', line 163

def initialize(operator, operands)
  @op = operator
  @operands = operands
end

Instance Method Details

#publish_to(policy) ⇒ Object



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
# File 'lib/fbe/award.rb', line 203

def publish_to(policy)
  case @op
  when :award
    @operands.each do |o|
      o.publish_to(policy)
    rescue StandardError => e
      raise "Failure in #{o}: #{e.message}"
    end
  when :aka
    @operands[0..-2].each do |o|
      o.publish_to(policy)
    rescue StandardError => e
      raise "Failure in #{o}: #{e.message}"
    end
    policy.revert(@operands.size - 1)
    policy.line(to_p(@operands[@operands.size - 1]))
  when :explain
    policy.intro(to_p(@operands[0]))
  when :in
    policy.line("assume that #{to_p(@operands[0])} is #{to_p(@operands[1])}")
  when :let
    policy.line("let #{to_p(@operands[0])} be equal to #{to_p(@operands[1])}")
    policy.let(@operands[0], @operands[1])
  when :set
    policy.line("set #{to_p(@operands[0])} to #{to_p(@operands[1])}")
  when :give
    policy.line("award #{to_p(@operands[0])}")
  else
    raise "Unknown term '#{@op}'"
  end
end

#to_p(any) ⇒ Object



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
# File 'lib/fbe/award.rb', line 235

def to_p(any)
  case any
  when PTerm
    any.to_s
  when Symbol
    s = any.to_s
    subs = {
      0 => '',
      1 => '',
      2 => '',
      3 => '',
      4 => '',
      5 => '',
      6 => '',
      7 => '',
      8 => '',
      9 => ''
    }
    s.gsub!(/([a-z]+)([0-9])/) { |_| "#{Regexp.last_match[1]}#{subs[Regexp.last_match[2].to_i]}" }
    "_#{s.gsub('_', '-')}_"
  when Integer, Float
    "**#{any}**"
  else
    any
  end
end

#to_sObject



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
# File 'lib/fbe/award.rb', line 168

def to_s
  case @op
  when :total
    'total'
  when :if
    "if #{to_p(@operands[0])} then #{to_p(@operands[1])} else #{to_p(@operands[2])}"
  when :eq
    "#{to_p(@operands[0])} = #{to_p(@operands[1])}"
  when :lt
    "#{to_p(@operands[0])} < #{to_p(@operands[1])}"
  when :lte
    "#{to_p(@operands[0])}#{to_p(@operands[1])}"
  when :gt
    "#{to_p(@operands[0])} > #{to_p(@operands[1])}"
  when :gte
    "#{to_p(@operands[0])}#{to_p(@operands[1])}"
  when :div
    "#{to_p(@operands[0])} ÷ #{to_p(@operands[1])}"
  when :times
    "#{to_p(@operands[0])} × #{to_p(@operands[1])}"
  when :plus
    "#{to_p(@operands[0])} + #{to_p(@operands[1])}"
  when :minus
    "#{to_p(@operands[0])} - #{to_p(@operands[1])}"
  when :max
    "maximum of #{to_p(@operands[0])} and #{to_p(@operands[1])}"
  when :min
    "minimum of #{to_p(@operands[0])} and #{to_p(@operands[1])}"
  when :between
    "at least #{to_p(@operands[0])} and at most #{to_p(@operands[1])}"
  else
    raise "Unknown term '#{@op}'"
  end
end