Formula#calculate is using QuantityInput
Updated FormulaBuilder to properly index parts[]
This commit is contained in:
parent
c402fe8353
commit
fa9c329a81
@ -20,7 +20,7 @@ class Formula < ActiveRecord::Base
|
|||||||
def calculate(inputs)
|
def calculate(inputs)
|
||||||
raise(InvalidInputs, 'No inputs') if inputs.empty?
|
raise(InvalidInputs, 'No inputs') if inputs.empty?
|
||||||
|
|
||||||
deps = inputs.map { |q, v| [q.name, v.transpose.first] }.to_h
|
deps = inputs.map { |q, v| [q.name, QuantityInput.new(q, v.transpose.first)] }.to_h
|
||||||
length = deps.values.first.length
|
length = deps.values.first.length
|
||||||
|
|
||||||
raise(InvalidFormula, 'Invalid formula') unless self.valid?
|
raise(InvalidFormula, 'Invalid formula') unless self.valid?
|
||||||
@ -47,6 +47,10 @@ class Formula < ActiveRecord::Base
|
|||||||
super(*args)
|
super(*args)
|
||||||
@quantity = q
|
@quantity = q
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def lastBefore(timepoints)
|
||||||
|
self.map{ BigDecimal(2000) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse
|
def parse
|
||||||
@ -77,7 +81,7 @@ class Formula < ActiveRecord::Base
|
|||||||
errors
|
errors
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_binding(quantities, args, length)
|
def get_binding(quantities, parts, length)
|
||||||
binding
|
binding
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -177,7 +177,14 @@ module BodyTracking
|
|||||||
[
|
[
|
||||||
:bt_expression,
|
:bt_expression,
|
||||||
[left, right].map do |side|
|
[left, right].map do |side|
|
||||||
side[0] == :bt_quantity ? "quantities['#{side[1]}'][_index]" : "#{side[1]}"
|
case side[0]
|
||||||
|
when :bt_quantity
|
||||||
|
"quantities['#{side[1]}'][_index]"
|
||||||
|
when :bt_quantity_method_call
|
||||||
|
"#{side[1]}[_index]"
|
||||||
|
else
|
||||||
|
"#{side[1]}"
|
||||||
|
end
|
||||||
end.join(op.to_s)
|
end.join(op.to_s)
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
@ -65,13 +65,20 @@ class FormulaTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
# Model method calls
|
# Model method calls
|
||||||
'100*Energy/RM.lastBefore(Meal.eaten_at||Meal.created_at)', Set['Energy', 'RM', 'Meal'],
|
'100*Energy/RM.lastBefore(Meal.eaten_at||Meal.created_at)', Set['Energy', 'RM', 'Meal'],
|
||||||
# TODO: fill parts
|
[
|
||||||
[]
|
{type: :indexed, content: "quantities['Meal'][_index].eaten_at||" \
|
||||||
|
"quantities['Meal'][_index].created_at"},
|
||||||
|
{type: :unindexed, content: "quantities['RM'].lastBefore(parts[0])"},
|
||||||
|
{type: :indexed, content: "100*quantities['Energy'][_index]/parts[1][_index]"}
|
||||||
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
d_methods = ['nil?', 'abs']
|
d_methods = ['nil?', 'abs']
|
||||||
|
q_methods = Hash.new(['all', 'lastBefore'])
|
||||||
|
q_methods['Meal'] = ['created_at', 'eaten_at']
|
||||||
|
|
||||||
vector.each_slice(3) do |formula, identifiers, parts|
|
vector.each_slice(3) do |formula, identifiers, parts|
|
||||||
parser = FormulaBuilder.new(formula, d_methods: d_methods)
|
parser = FormulaBuilder.new(formula, d_methods: d_methods, q_methods: q_methods)
|
||||||
i, p = parser.parse
|
i, p = parser.parse
|
||||||
assert_empty parser.errors
|
assert_empty parser.errors
|
||||||
assert_equal identifiers, i
|
assert_equal identifiers, i
|
||||||
|
Reference in New Issue
Block a user