Class: Procedo::Engine::Intervention::Quantified

Inherits:
ProductParameter show all
Defined in:
lib/procedo/engine/intervention/quantified.rb

Direct Known Subclasses

Input, Output

Instance Attribute Summary collapse

Attributes inherited from ProductParameter

#product, #readings, #working_zone

Attributes inherited from Parameter

#group, #id, #intervention, #name, #reference, #type

Instance Method Summary collapse

Methods inherited from ProductParameter

#add_reading, #compute_attribute, #compute_reading, #impact_on_attributes, #impact_on_parameters, #impact_on_readings, #impact_with, #product_id, #product_id=, #reading, #usable_attribute?, #usable_handler?, #usable_reading?

Methods inherited from Parameter

#impact, #impact_with, #param_name, #root?

Constructor Details

#initialize(intervention, id, attributes = {}) ⇒ Quantified

Returns a new instance of Quantified.


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

def initialize(intervention, id, attributes = {})
  super(intervention, id, attributes)
  @quantity_handler = attributes[:quantity_handler]
  @quantity_value = attributes[:quantity_value].to_d if attributes[:quantity_value]
  @quantity_population = attributes[:quantity_population].to_d if attributes[:quantity_population]
end

Instance Attribute Details

#quantity_handlerObject

Returns the value of attribute quantity_handler


5
6
7
# File 'lib/procedo/engine/intervention/quantified.rb', line 5

def quantity_handler
  @quantity_handler
end

#quantity_populationObject

Returns the value of attribute quantity_population


5
6
7
# File 'lib/procedo/engine/intervention/quantified.rb', line 5

def quantity_population
  @quantity_population
end

#quantity_valueObject

Returns the value of attribute quantity_value


5
6
7
# File 'lib/procedo/engine/intervention/quantified.rb', line 5

def quantity_value
  @quantity_value
end

Instance Method Details

#compute_populationObject


88
89
90
91
# File 'lib/procedo/engine/intervention/quantified.rb', line 88

def compute_population
  ref = quantity_handler_reference
  intervention.interpret(ref.forward_tree, env).round(4)
end

#compute_valueObject


83
84
85
86
# File 'lib/procedo/engine/intervention/quantified.rb', line 83

def compute_value
  ref = quantity_handler_reference
  intervention.interpret(ref.backward_tree, env).round(4)
end

#envObject


93
94
95
96
# File 'lib/procedo/engine/intervention/quantified.rb', line 93

def env
  { self: self, product: product, working_zone: working_zone,
    value: quantity_value, population: quantity_population }
end

#handlers_statesObject


22
23
24
25
26
# File 'lib/procedo/engine/intervention/quantified.rb', line 22

def handlers_states
  reference.handlers.each_with_object({}) do |handler, hash|
    hash[handler.name] = usable_handler?(handler)
  end
end

#impact_dependencies!(field) ⇒ Object


69
70
71
72
# File 'lib/procedo/engine/intervention/quantified.rb', line 69

def impact_dependencies!(field)
  super(field)
  impact_on_handlers(field)
end

#impact_on_handlers(_field) ⇒ Object

Checks that handler is always valid and fix it if possible


75
76
77
78
79
80
81
# File 'lib/procedo/engine/intervention/quantified.rb', line 75

def impact_on_handlers(_field)
  rh = reference.handler(@quantity_handler) unless @quantity_handler.blank?
  unless @quantity_handler && usable_handler?(rh)
    rh = reference.handlers.detect { |h| usable_handler?(h) }
    self.quantity_handler = rh.name.to_s if rh
  end
end

#quantity_handler_referenceObject


28
29
30
# File 'lib/procedo/engine/intervention/quantified.rb', line 28

def quantity_handler_reference
  @quantity_handler ? reference.handler(@quantity_handler) : nil
end

#to_hashObject


14
15
16
17
18
19
20
# File 'lib/procedo/engine/intervention/quantified.rb', line 14

def to_hash
  hash = super
  hash[:quantity_handler] = @quantity_handler
  hash[:quantity_value] = @quantity_value.to_s.to_f
  hash[:quantity_population] = @quantity_population.to_s.to_f
  hash
end