compute_quantities: allow indirect associations and model dependencies
This commit is contained in:
@@ -15,6 +15,7 @@ class Food < ActiveRecord::Base
|
||||
belongs_to :project, required: true
|
||||
belongs_to :ref_unit, class_name: 'Unit', required: true
|
||||
belongs_to :source, required: false
|
||||
has_many :ingredients, dependent: :restrict_with_error
|
||||
|
||||
has_many :nutrients, inverse_of: :food, dependent: :destroy, validate: true
|
||||
validates :nutrients, presence: true
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Formula < ActiveRecord::Base
|
||||
include BodyTracking::FormulaBuilder
|
||||
|
||||
attr_reader :parts, :quantities
|
||||
attr_reader :parts, :quantity_deps, :model_deps
|
||||
|
||||
belongs_to :quantity, inverse_of: :formula, required: true
|
||||
belongs_to :unit
|
||||
@@ -17,85 +17,29 @@ class Formula < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def self.resolve(quantities, items, subitems)
|
||||
unchecked_q = quantities.map { |q| [q, nil] }
|
||||
|
||||
# TODO: jesli wartosci nie ma w subitems to pytac w yield (jesli jest block)
|
||||
completed_q = {}
|
||||
# FIXME: loop should finish unless there is circular dependency in formulas
|
||||
# for now we don't guard against that
|
||||
while !unchecked_q.empty?
|
||||
q, deps = unchecked_q.shift
|
||||
|
||||
# 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? ||
|
||||
(subitems[q].length == items.count)
|
||||
completed_q[q] = subitems.delete(q) { {} }
|
||||
next
|
||||
end
|
||||
|
||||
# quantity with formula requires refresh of dependencies availability
|
||||
if deps.nil? || !deps.empty?
|
||||
deps ||= q.formula.quantities.clone
|
||||
deps.reject! { |d| completed_q.has_key?(d) }
|
||||
deps.each { |d| unchecked_q << [d, nil] unless unchecked_q.index { |u| u[0] == d } }
|
||||
end
|
||||
|
||||
# quantity with formula has all dependencies satisfied, requires calculation
|
||||
if deps.empty?
|
||||
output_items = items.select { |i| subitems[q][i].nil? }
|
||||
input_q = q.formula.quantities
|
||||
inputs = input_q.map do |i_q|
|
||||
values = completed_q[i_q].values_at(*output_items).map { |v| v || [nil, nil] }
|
||||
values.map! { |v, u| [v || BigDecimal(0), u] } if q.formula.zero_nil
|
||||
[i_q, values]
|
||||
end
|
||||
begin
|
||||
calculated = q.formula.calculate(inputs.to_h)
|
||||
rescue Exception => e
|
||||
output_items.each { |o_i| subitems[q][o_i] = nil }
|
||||
q.formula.errors.add(
|
||||
:code, :computation_failed,
|
||||
{
|
||||
quantity: q.name,
|
||||
description: e.message,
|
||||
count: output_items.size == subitems[q].size ? 'all' : output_items.size
|
||||
}
|
||||
)
|
||||
else
|
||||
output_items.each_with_index { |o_i, idx| subitems[q][o_i] = calculated[idx] }
|
||||
end
|
||||
unchecked_q.unshift([q, deps])
|
||||
next
|
||||
end
|
||||
|
||||
# quantity still has unsatisfied dependencies, move to the end of queue
|
||||
unchecked_q << [q, deps]
|
||||
end
|
||||
|
||||
completed_q
|
||||
end
|
||||
|
||||
def calculate(inputs)
|
||||
raise(InvalidInputs, 'No inputs') if inputs.empty?
|
||||
|
||||
quantities = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h
|
||||
length = quantities.values.first.length
|
||||
deps = inputs.map { |q, v| [q.name, v.transpose[0]] }.to_h
|
||||
length = deps.values.first.length
|
||||
|
||||
raise(InvalidFormula, 'Invalid formula') unless self.valid?
|
||||
raise(InvalidInputs, 'Inputs lengths differ') unless
|
||||
quantities.values.all? { |v| v.length == length }
|
||||
deps.values.all? { |v| v.length == length }
|
||||
|
||||
args = []
|
||||
@parts.each do |p|
|
||||
code = p[:type] == :indexed ?
|
||||
"length.times.map { |_index| #{p[:content]} }" : p[:content]
|
||||
args << get_binding(quantities, args, length).eval(code)
|
||||
args << get_binding(deps, args, length).eval(code)
|
||||
end
|
||||
args.last.map { |v| [v, self.unit] }
|
||||
end
|
||||
|
||||
def dependencies
|
||||
@quantity_deps + @model_deps
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse
|
||||
@@ -108,11 +52,13 @@ class Formula < ActiveRecord::Base
|
||||
errors = parser.errors
|
||||
|
||||
quantities = Quantity.where(project: self.quantity.project, name: identifiers.to_a)
|
||||
(identifiers - quantities.map(&:name) - q_methods.keys).each do |q|
|
||||
errors << [:unknown_quantity, {quantity: q}]
|
||||
identifiers -= quantities.map(&:name)
|
||||
models = identifiers.map(&:safe_constantize).compact || []
|
||||
(identifiers - models.map(&:class_name)).each do |i|
|
||||
errors << [:unknown_dependency, {identifier: i}]
|
||||
end
|
||||
|
||||
@parts, @quantities = parts, quantities.to_a if errors.empty?
|
||||
@parts, @quantity_deps, @model_deps = parts, quantities.to_a, models if errors.empty?
|
||||
errors
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user