1
0

Quantity computing from formula looks done, savepoint

This commit is contained in:
cryptogopher 2019-11-06 15:59:39 +01:00
parent d70a17f58b
commit eff672318b
3 changed files with 49 additions and 34 deletions

View File

@ -201,30 +201,27 @@ class IngredientsController < ApplicationController
def prepare_nutrients def prepare_nutrients
@quantities = @project.quantities.where(primary: true) @quantities = @project.quantities.where(primary: true)
compute_nutrients(filter_ingredients, @quantities) ingredients, nutrients = compute_nutrients(filter_ingredients, @quantities)
@primary_nutrients = {} @nutrients = {}
@extra_nutrients = {} @extra_nutrients = {}
ingredients.each do |i| ingredients.each do |i|
@primary_nutrients[i] = {} @nutrients[i] = []
@extra_nutrients[i] = {} @extra_nutrients[i] = {}
i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| @quantities.each do |q|
if @primary_quantities.include?(n.quantity) @nutrients[i] << [q.name, "%d [%s]" % nutrients[i][q.name]]
@primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" #@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
else
@extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]"
end
end end
end end
end end
def compute_nutrients(ingredients, requested_q) def compute_nutrients(ingredients, requested_q)
unchecked_q = requested_q.map { |q| [q, nil] }.to_h unchecked_q = requested_q.map { |q| [q, nil] }
nutrients = Hash.new { |h,k| h[k] = {} } nutrients = Hash.new { |h,k| h[k] = {} }
Nutrient.joins(:ingredient).where(ingredient: ingredients) Nutrient.where(ingredient: ingredients).includes(:quantity, :unit)
.pluck(:quantity_id, :ingredient_id, :amount, :unit_id) .pluck('quantities.name', :ingredient_id, :amount, 'units.shortname')
.each { |[q_id, i_id, a, u_id]| nutrients[q_id][i_id] = [a, u_id] } .each { |q_name, i_id, a, u_id| nutrients[q_name][i_id] = [a, u_id] }
completed_q = {} completed_q = {}
# FIXME: loop should finish unless there is circular dependency in formulas # FIXME: loop should finish unless there is circular dependency in formulas
@ -232,26 +229,38 @@ class IngredientsController < ApplicationController
while !unchecked_q.empty? while !unchecked_q.empty?
q, deps = unchecked_q.shift q, deps = unchecked_q.shift
if q.formula.blank? || (nutrients[q.id].length == ingredients.length) if q.formula.blank? || (nutrients[q.name].length == ingredients.length)
completed_q[q] = nutrients.delete(q.id) completed_q[q.name] = nutrients.delete(q.name)
next next
end end
if deps.nil? || !deps.empty? if deps.nil? || !deps.empty?
formula_deps = deps || q.formula_quantities deps ||= q.formula_quantities
unchecked_q[q] = formula_deps.delete_if { |q| completed_q.has_key?(q) } deps.reject! { |q| completed_q.has_key?(q.name) }
unchecked_q[q].each { |q| unchecked_q[q] = nil unless unchecked_q.has_key?(q) } deps.each { |q| unchecked_q << [q, nil] unless unchecked_q.index { |u| u[0] == q } }
deps = unchecked_q[q]
end end
if deps.empty? if deps.empty?
input_q = q.formula_quantities
ingredients.each do |i| ingredients.each do |i|
inputs = nutrients.map { |q_name, *| [q_name, nutrients[q_name][i.id][0]] }.to_h next if !nutrients[q.name][i.id].nil?
inputs = input_q.map do |i_q|
default_data = [nil, nil]
nutrient_data = (completed_q[i_q.name] || nutrients[i_q.name])[i.id]
[i_q.name, (nutrient_data || default_data)[0]]
end
nutrients[q.name][i.id] = q.calculate(inputs) nutrients[q.name][i.id] = q.calculate(inputs)
end end
unchecked_q.unshift([q, deps])
else
unchecked_q << [q, deps]
end end
unchecked_q[q] = deps
end end
all_q = nutrients.merge!(completed_q)
results = Hash.new { |h,k| h[k] = {} }
requested_q.each { |q| ingredients.each { |i| results[i][q.name] = all_q[q.name][i.id] } }
[ingredients, results]
end end
def filter_ingredients def filter_ingredients

View File

@ -1,5 +1,6 @@
class Quantity < ActiveRecord::Base class Quantity < ActiveRecord::Base
require 'ripper' require 'ripper'
QUANTITY_TTYPES = [:on_ident, :on_tstring_content, :on_const]
enum domain: { enum domain: {
diet: 0, diet: 0,
@ -26,7 +27,7 @@ class Quantity < ActiveRecord::Base
identifiers = [] identifiers = []
Ripper.lex(formula).each do |location, ttype, token| Ripper.lex(formula).each do |location, ttype, token|
case case
when [:on_ident, :on_tstring_content, :on_const].include?(ttype) when QUANTITY_TTYPES.include?(ttype)
identifiers << token identifiers << token
when [:on_sp, :on_int, :on_rational, :on_float, :on_tstring_beg, :on_tstring_end, when [:on_sp, :on_int, :on_rational, :on_float, :on_tstring_beg, :on_tstring_end,
:on_lparen, :on_rparen].include?(ttype) :on_lparen, :on_rparen].include?(ttype)
@ -62,8 +63,13 @@ class Quantity < ActiveRecord::Base
end end
def formula_quantities def formula_quantities
q_names = Ripper.lex(formula).each do |*, ttype, token|
token if QUANTITY_TTYPES.include?(ttype)
end.compact
self.project.quantities.where(name: q_names).to_a
end end
def calculate(inputs) def calculate(inputs)
[1.0, nil]
end end
end end

View File

@ -1,24 +1,24 @@
<% if @primary_nutrients.any? %> <% if @nutrients.any? %>
<%= render :partial => 'ingredients/options' %> <%= render :partial => 'ingredients/options' %>
<table class="nutrients list odd-even"> <table class="nutrients list odd-even">
<thead> <thead>
<tr> <tr>
<% total_width = 3 + @primary_quantities.length %> <% total_width = 3 + @quantities.length %>
<th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th> <th style="width:<%= 3 * 100/total_width%>%"><%= l(:field_name) %></th>
<% @primary_quantities.each do |q| %> <% @quantities.each do |q| %>
<th style="width:<%= 100/total_width %>%"><%= q.name %></th> <th style="width:<%= 100/total_width %>%"><%= q.name %></th>
<% end %> <% end %>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @primary_nutrients.each do |i, values| %> <% @nutrients.each do |i, values| %>
<% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %> <% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %>
<tr id="ingredient-<%= i.id %>" class="primary <%= row_class %>"> <tr id="ingredient-<%= i.id %>" class="primary <%= row_class %>">
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;"> <td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span> <span class="icon icon-bullet-closed"><%= i.name %></span>
</td> </td>
<% @primary_quantities.each do |q| %> <% @quantities.each do |q| %>
<td class="primary value"> <td class="primary value">
<%= values[q.id] || '-' %> <%= values[q.id] || '-' %>
</td> </td>
@ -29,7 +29,7 @@
<td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;"> <td class="name" style="cursor: pointer;" onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', 'tr').toggle(); return false;">
<span class="icon icon-bullet-closed"><%= i.name %></span> <span class="icon icon-bullet-closed"><%= i.name %></span>
</td> </td>
<% @primary_quantities.each do |q| %> <% @quantities.each do |q| %>
<td class="primary quantity"> <td class="primary quantity">
<%= q.name %> <%= q.name %>
</td> </td>
@ -37,7 +37,7 @@
</tr> </tr>
<tr class="<%= row_class %>" style="display:none"> <tr class="<%= row_class %>" style="display:none">
<td class="space"></td> <td class="space"></td>
<% @primary_quantities.each do |q| %> <% @quantities.each do |q| %>
<td class="primary value"> <td class="primary value">
<%= values[q.id] || '-' %> <%= values[q.id] || '-' %>
</td> </td>
@ -45,7 +45,7 @@
</tr> </tr>
<% extras = @extra_nutrients[i].keys %> <% extras = @extra_nutrients[i].keys %>
<% extras.each_slice(@primary_quantities.length).with_index do |names, index| %> <% extras.each_slice(@quantities.length).with_index do |names, index| %>
<tr class="extra <%= row_class %>" style="display:none"> <tr class="extra <%= row_class %>" style="display:none">
<td class="space"></td> <td class="space"></td>
<% names.each do |name| %> <% names.each do |name| %>
@ -53,8 +53,8 @@
<%= name %> <%= name %>
</td> </td>
<% end %> <% end %>
<% if @primary_quantities.length > names.length %> <% if @quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td> <td class="space" colspan="<%= @quantities.length-names.length %>"></td>
<% end %> <% end %>
</tr> </tr>
<tr class="extra <%= row_class %>" style="display:none"> <tr class="extra <%= row_class %>" style="display:none">
@ -64,8 +64,8 @@
<%= @extra_nutrients[i][name] %> <%= @extra_nutrients[i][name] %>
</td> </td>
<% end %> <% end %>
<% if @primary_quantities.length > names.length %> <% if @quantities.length > names.length %>
<td class="space" colspan="<%= @primary_quantities.length-names.length %>"></td> <td class="space" colspan="<%= @quantities.length-names.length %>"></td>
<% end %> <% end %>
</tr> </tr>
<% end %> <% end %>