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


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

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

.children(group, set) ⇒ Object


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

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

.contents_count(container) ⇒ Object


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

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

.intersection(shape, other_shape) ⇒ Object


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

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

.members_count(group) ⇒ Object


87
88
89
90
91
92
93
94
95
96
# File 'lib/procedo/engine/functions.rb', line 87

def members_count(group)
  if group.present?
    value = group.actor.members_at(group.now).count.to_i
    return (value > 0 ? value : 0)
  else
    return 0
  end
rescue
  raise Procedo::FailedFunctionCall
end

.merge_working_zones(set) ⇒ Object


51
52
53
54
55
56
57
58
59
60
# File 'lib/procedo/engine/functions.rb', line 51

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

.parent(parameter) ⇒ Object


67
68
69
# File 'lib/procedo/engine/functions.rb', line 67

def parent(parameter)
  parameter.group
end

.population_count(set) ⇒ Object

return a sum of population


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/procedo/engine/functions.rb', line 12

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


7
8
9
# File 'lib/procedo/engine/functions.rb', line 7

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


63
64
65
# File 'lib/procedo/engine/functions.rb', line 63

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

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

Sums indicator values for a set of product


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/procedo/engine/functions.rb', line 25

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


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/procedo/engine/functions.rb', line 39

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.to_d(unit || :square_meter)
end