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

#assembly, #component, #product, #read_at, #readings, #working_zone

Attributes inherited from Parameter

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

Instance Method Summary collapse

Methods inherited from ProductParameter

#add_reading, #assembly?, #assembly_id, #assembly_id=, #component?, #component_id, #component_id=, #compute_attribute, #compute_reading, #impact_on_attributes, #impact_on_components, #impact_on_parameters, #impact_on_readings, #impact_with, #product?, #product_id, #product_id=, #read_at?, #reading, #usable_attribute?, #usable_handler?, #usable_reading?, #working_zone?

Methods inherited from Parameter

#dependents, #impact_with, #param_name, #root?

Methods included from Reassignable

#assign, #assign!, #reassign, #reassign!

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


96
97
98
99
# File 'lib/procedo/engine/intervention/quantified.rb', line 96

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

#compute_valueObject


91
92
93
94
# File 'lib/procedo/engine/intervention/quantified.rb', line 91

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

#envObject


101
102
103
# File 'lib/procedo/engine/intervention/quantified.rb', line 101

def env
  super.merge(value: quantity_value, population: quantity_population)
end

#handlers_statesObject


30
31
32
33
34
# File 'lib/procedo/engine/intervention/quantified.rb', line 30

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

#impact_dependencies!(field) ⇒ Object


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

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


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

def impact_on_handlers(_field)
  rh = reference.handler(@quantity_handler) if @quantity_handler.present?
  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


36
37
38
# File 'lib/procedo/engine/intervention/quantified.rb', line 36

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

#to_attributesObject


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

def to_attributes
  hash = super
  hash[:quantity_handler] = @quantity_handler if @quantity_handler
  hash[:quantity_value] = @quantity_value.to_s.to_f unless @quantity_value.nil?
  hash[:quantity_population] = @quantity_population.to_s.to_f unless @quantity_population.nil?
  hash
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 if @quantity_handler
  hash[:quantity_value] = @quantity_value.to_s.to_f unless @quantity_value.nil?
  hash[:quantity_population] = @quantity_population.to_s.to_f unless @quantity_population.nil?
  hash
end