Module: Procedo::Engine::Functions

Defined in:
lib/procedo/engine/functions.rb

Overview

This module all functions accessible through formula language

Class Method Summary collapse

Class Method Details

.area(shape) ⇒ Object


91
92
93
94
95
# File 'lib/procedo/engine/functions.rb', line 91

def area(shape)
  return shape.area.in(:square_meter).to_f(:square_meter)
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.children(group, set) ⇒ Object


87
88
89
# File 'lib/procedo/engine/functions.rb', line 87

def children(group, set)
  set.build(group.children(set.parameter_name))
end

.contents_count(container) ⇒ Object


115
116
117
118
119
# File 'lib/procedo/engine/functions.rb', line 115

def contents_count(container)
  return container.actor.containeds.count(&:available?)
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.father_of(vial) ⇒ Object


154
155
156
157
158
# File 'lib/procedo/engine/functions.rb', line 154

def father_of(vial)
  return vial.mother.last_transplantation.input.father || vial.mother.last_insemination.input.producer
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.first(set) ⇒ Object


75
76
77
# File 'lib/procedo/engine/functions.rb', line 75

def first(set)
  set.first
end

.first_product_of(set) ⇒ Object


79
80
81
# File 'lib/procedo/engine/functions.rb', line 79

def first_product_of(set)
  set.parameters.first.product if set.parameters.first
end

.intersection(shape, other_shape) ⇒ Object


97
98
99
100
101
# File 'lib/procedo/engine/functions.rb', line 97

def intersection(shape, other_shape)
  return shape.intersection(other_shape)
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.intervention_started_at(set) ⇒ Object

return first date as Datetime object


167
168
169
# File 'lib/procedo/engine/functions.rb', line 167

def intervention_started_at(set)
  set.collect { |h| DateTime.parse(h[:started_at]) }.min
end

.intervention_stopped_at(set) ⇒ Object

return last date as Datetime object


172
173
174
# File 'lib/procedo/engine/functions.rb', line 172

def intervention_stopped_at(set)
  set.collect { |h| DateTime.parse(h[:stopped_at]) }.max
end

.members_count(set) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/procedo/engine/functions.rb', line 103

def members_count(set)
  group = (set.is_a?(Procedo::Engine::Set) ? first_product_of(set) : set)
  if group.present?
    value = group.members_at.count.to_i
    return (value > 0 ? value : 0)
  else
    return 0
  end
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.merge_working_zones(set) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'lib/procedo/engine/functions.rb', line 59

def merge_working_zones(set)
  zone = nil
  set.each do |parameter|
    unless parameter.is_a?(Procedo::Engine::Intervention::ProductParameter)
      raise 'Invalid parameter. Only product_parameter wanted. Got: ' + parameter.class.name
    end
    zone = zone.nil? ? parameter.working_zone : zone.merge(parameter.working_zone)
  end
  zone
end

.miscibility(set) ⇒ Object


6
7
8
9
10
11
12
# File 'lib/procedo/engine/functions.rb', line 6

def miscibility(set)
  products = set.map do |parameter|
    next parameter.variant if parameter.respond_to? :variant
    parameter.product
  end
  PhytosanitaryMiscibility.new(products.compact).validity
end

.mother_of(vial) ⇒ Object


160
161
162
163
164
# File 'lib/procedo/engine/functions.rb', line 160

def mother_of(vial)
  return vial.mother.last_transplantation.input.mother || vial.mother
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.output_computed_name(variant, working_periods) ⇒ Object

compute a name from given variant


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/procedo/engine/functions.rb', line 122

def output_computed_name(variant, working_periods) # working_periods
  # last_day = working_periods.last[:value]
  end_of_period = working_periods.last[:stopped_at].to_time

  # get product born on the same day
  products = []
  ps = Product.of_variant(variant).at(end_of_period).order(:born_at)
  ps.each do |p|
    products << p if p.born_at.to_date == end_of_period.to_date
  end

  # build variables
  ordered = products.compact.count + 1
  name = variant.name
  born_at = end_of_period.strftime('%d/%m/%Y')

  "#{name}#{ordered} #{born_at}"
end

.parent(parameter) ⇒ Object


83
84
85
# File 'lib/procedo/engine/functions.rb', line 83

def parent(parameter)
  parameter.group
end

.population_count(set) ⇒ Object

return a sum of population


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/procedo/engine/functions.rb', line 20

def population_count(set)
  list = set.map do |parameter|
    unless parameter.is_a?(Procedo::Engine::Intervention::ProductParameter)
      raise 'Invalid parameter. Only product_parameter wanted. Got: ' + parameter.class.name
    end
    (parameter.product ? parameter.product.population : nil)
  end
  list.compact!
  return 0.0 if list.empty?
  list.sum
end

.population_counting_is(product, expected) ⇒ Object

Test if population counting is as specified for given product


15
16
17
# File 'lib/procedo/engine/functions.rb', line 15

def population_counting_is(product, expected)
  ((product && product.population_counting.to_sym) == expected ? 1 : 0)
end

.siblings(parameter, set) ⇒ Object

Returns a set composed of sibling parameter


71
72
73
# File 'lib/procedo/engine/functions.rb', line 71

def siblings(parameter, set)
  children(parent(parameter), set)
end

.sum(set, indicator_name, unit = nil) ⇒ Object

Sums indicator values for a set of product


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/procedo/engine/functions.rb', line 33

def sum(set, indicator_name, unit = nil)
  indicator = Nomen::Indicator.find!(indicator_name)
  raise 'Only measure indicator can use this function' unless indicator.datatype == :measure
  list = set.map do |parameter|
    unless parameter.is_a?(Procedo::Engine::Intervention::ProductParameter)
      raise 'Invalid parameter. Only product_parameter wanted. Got: ' + parameter.class.name
    end
    (parameter.product ? parameter.product.get(indicator.name) : nil)
  end
  list.compact!
  return 0.0 if list.empty?
  list.sum.to_d(unit ? unit : indicator.unit)
end

.sum_working_zone_areas(set, unit = nil) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/procedo/engine/functions.rb', line 47

def sum_working_zone_areas(set, unit = nil)
  list = set.map do |parameter|
    unless parameter.is_a?(Procedo::Engine::Intervention::ProductParameter)
      raise 'Invalid parameter. Only product_parameter wanted. Got: ' + parameter.class.name
    end
    parameter.working_zone ? parameter.working_zone.area : nil
  end
  list.compact!
  return 0.0 if list.empty?
  list.sum.in(:square_meter).to_d(unit || :square_meter)
end

.variant_of(product) ⇒ Object


147
148
149
150
151
152
# File 'lib/procedo/engine/functions.rb', line 147

def variant_of(product)
  return product.member_variant unless product.nil?
  nil
rescue
  raise Procedo::Errors::FailedFunctionCall
end

.variety_of(product) ⇒ Object


141
142
143
144
145
# File 'lib/procedo/engine/functions.rb', line 141

def variety_of(product)
  return product.variety
rescue
  raise Procedo::Errors::FailedFunctionCall
end