lib/fbe/award.rb in fbe-0.0.35 vs lib/fbe/award.rb in fbe-0.0.36
- old
+ new
@@ -45,22 +45,31 @@
#
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class Fbe::Award
- def initialize(query = J.pmp.hr.send($judge.gsub('-', '_')))
+ # Ctor.
+ # @param [String] query The query with the policy
+ # @param [String] judge The name of the judge
+ def initialize(query = nil, judge: $judge, global: $global, options: $options, loog: $loog)
+ query = Fbe.pmp(fb: Fbe.fb, global:, options:, loog:).hr.send(judge.gsub('-', '_')) if query.nil?
@query = query
end
+ # Build a bill object from this award query.
+ # @param [Hash] vars Hash of variables
+ # @return [Fbe::Award::Bill] The bill
def bill(vars = {})
term = Factbase::Syntax.new(@query, term: Fbe::Award::BTerm).to_term
bill = Bill.new
vars.each { |k, v| bill.set(k, v) }
term.bill_to(bill)
bill
end
+ # Build a policy object from this award query.
+ # @return [Fbe::Award::Policy] The policy
def policy
term = Factbase::Syntax.new(@query, term: Fbe::Award::PTerm).to_term
policy = Policy.new
term.publish_to(policy)
policy
@@ -120,10 +129,16 @@
case @op
when :total
bill.points
when :if
to_val(@operands[0], bill) ? to_val(@operands[1], bill) : to_val(@operands[2], bill)
+ when :and
+ @operands.all? { |o| to_val(o, bill) }
+ when :or
+ @operands.any? { |o| to_val(o, bill) }
+ when :not
+ !to_val(@operands[0], bill)
when :eq
to_val(@operands[0], bill) == to_val(@operands[1], bill)
when :lt
to_val(@operands[0], bill) < to_val(@operands[1], bill)
when :lte
@@ -148,11 +163,10 @@
v = to_val(@operands[0], bill)
a = to_val(@operands[1], bill)
b = to_val(@operands[2], bill)
min, max = [a, b].minmax
return 0 if (!v.negative? && v < min) || (!v.positive? && v > max)
-
v.clamp(min, max)
else
raise "Unknown term '#{@op}'"
end
end
@@ -169,10 +183,16 @@
case @op
when :total
'total'
when :if
"if #{to_p(@operands[0])} then #{to_p(@operands[1])} else #{to_p(@operands[2])}"
+ when :and
+ @operands.map(&:to_s).join(' and ')
+ when :or
+ @operands.map(&:to_s).join(' or ')
+ when :not
+ "not #{@operands[0]}"
when :eq
"#{to_p(@operands[0])} = #{to_p(@operands[1])}"
when :lt
"#{to_p(@operands[0])} < #{to_p(@operands[1])}"
when :lte
@@ -330,9 +350,9 @@
if @lines.size == 1
pars << "Just #{@lines.first}."
else
pars += @lines.each_with_index.map { |t, i| "#{i.zero? ? 'First' : 'Then'}, #{t}." }
end
- pars.join(' ').gsub('. Then, award ', ', and award ')
+ pars.join(' ').gsub('. Then, award ', ', and award ').gsub(/\s{2,}/, ' ')
end
end
end