Fixes after introducing Formula model
This commit is contained in:
parent
de395ad1e0
commit
08ac719566
@ -17,11 +17,14 @@ class Formula < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def calculate(inputs)
|
def calculate(inputs)
|
||||||
|
raise(InvalidInputs, 'No inputs') if inputs.empty?
|
||||||
|
|
||||||
quantities = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h
|
quantities = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h
|
||||||
length = quantities.values.first.length
|
length = quantities.values.first.length
|
||||||
|
|
||||||
raise(InvalidFormula, 'Invalid formula') unless self.valid?
|
raise(InvalidFormula, 'Invalid formula') unless self.valid?
|
||||||
raise InvalidInputs unless quantities.values.all? { |v| v.length == length }
|
raise(InvalidInputs, 'Inputs lengths differ') unless
|
||||||
|
quantities.values.all? { |v| v.length == length }
|
||||||
|
|
||||||
args = []
|
args = []
|
||||||
@parts.each do |p|
|
@parts.each do |p|
|
||||||
|
@ -66,7 +66,8 @@ module BodyTracking
|
|||||||
while !unchecked_q.empty?
|
while !unchecked_q.empty?
|
||||||
q, deps = unchecked_q.shift
|
q, deps = unchecked_q.shift
|
||||||
|
|
||||||
# quantity not computable (no formula) or not requiring calculation/computed
|
# quantity not computable: no formula/invalid formula (syntax error/runtime error)
|
||||||
|
# or not requiring calculation/computed
|
||||||
if !q.formula || q.formula.errors.any? || !q.formula.valid? ||
|
if !q.formula || q.formula.errors.any? || !q.formula.valid? ||
|
||||||
(subitems[q.name].length == items.count)
|
(subitems[q.name].length == items.count)
|
||||||
completed_q[q.name] = subitems.delete(q.name) { {} }
|
completed_q[q.name] = subitems.delete(q.name) { {} }
|
||||||
@ -76,9 +77,9 @@ module BodyTracking
|
|||||||
|
|
||||||
# quantity with formula requires refresh of dependencies availability
|
# quantity with formula requires refresh of dependencies availability
|
||||||
if deps.nil? || !deps.empty?
|
if deps.nil? || !deps.empty?
|
||||||
deps ||= q.formula.quantities
|
deps ||= q.formula.quantities.clone
|
||||||
deps.reject! { |q| completed_q.has_key?(q.name) }
|
deps.reject! { |d| completed_q.has_key?(d.name) }
|
||||||
deps.each { |q| unchecked_q << [q, nil] unless unchecked_q.index { |u| u[0] == q } }
|
deps.each { |d| unchecked_q << [d, nil] unless unchecked_q.index { |u| u[0] == d } }
|
||||||
end
|
end
|
||||||
|
|
||||||
# quantity with formula has all dependencies satisfied, requires calculation
|
# quantity with formula has all dependencies satisfied, requires calculation
|
||||||
@ -90,8 +91,14 @@ module BodyTracking
|
|||||||
calculated = q.formula.calculate(inputs.to_h)
|
calculated = q.formula.calculate(inputs.to_h)
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
output_ids.each { |oid| subitems[q.name][oid] = [BigDecimal::NAN, nil] }
|
output_ids.each { |oid| subitems[q.name][oid] = [BigDecimal::NAN, nil] }
|
||||||
q.formula.errors.add(:code, :computation_failed,
|
q.formula.errors.add(
|
||||||
{quantity: q.name, description: e.message, count: output_ids.size})
|
:code, :computation_failed,
|
||||||
|
{
|
||||||
|
quantity: q.name,
|
||||||
|
description: e.message,
|
||||||
|
count: output_ids.size == subitems[q.name].size ? 'all' : output_ids.size
|
||||||
|
}
|
||||||
|
)
|
||||||
else
|
else
|
||||||
output_ids.each_with_index { |oid, idx| subitems[q.name][oid] = calculated[idx] }
|
output_ids.each_with_index { |oid, idx| subitems[q.name][oid] = calculated[idx] }
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user