Class: Procedo::Engine::Intervention

Inherits:
Object
  • Object
show all
Defined in:
lib/procedo/engine/intervention.rb,
lib/procedo/engine/intervention/doer.rb,
lib/procedo/engine/intervention/tool.rb,
lib/procedo/engine/intervention/input.rb,
lib/procedo/engine/intervention/agent.rb,
lib/procedo/engine/intervention/target.rb,
lib/procedo/engine/intervention/output.rb,
lib/procedo/engine/intervention/reading.rb,
lib/procedo/engine/intervention/parameter.rb,
lib/procedo/engine/intervention/quantified.rb,
lib/procedo/engine/intervention/working_period.rb,
lib/procedo/engine/intervention/group_parameter.rb,
lib/procedo/engine/intervention/product_parameter.rb

Defined Under Namespace

Classes: Agent, Doer, GroupParameter, Input, Output, Parameter, ProductParameter, Quantified, Reading, Target, Tool, WorkingPeriod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Intervention

Returns a new instance of Intervention.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/procedo/engine/intervention.rb', line 12

def initialize(attributes = {})
  @attributes = attributes.deep_symbolize_keys
  @procedure = Procedo.find(@attributes[:procedure_name])
  unless @procedure
    raise "Cannot find procedure: #{@attributes[:procedure_name].inspect}"
  end
  @actions = (@attributes[:actions] || []).map(&:to_sym)
  @root_group = Procedo::Engine::Intervention::GroupParameter.new(self, Procedo::Procedure::ROOT_NAME)
  @working_periods = {}.with_indifferent_access
  if @attributes[:working_periods_attributes]
    @attributes[:working_periods_attributes].each do |id, attributes|
      add_working_period(id, attributes)
    end
  end
  # Parse doers, inputs...
  @root_group.parse_params(@attributes)
end

Instance Attribute Details

#procedureObject (readonly)

Returns the value of attribute procedure


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

def procedure
  @procedure
end

Instance Method Details

#add_working_period(id, attributes = {}) ⇒ Object


58
59
60
61
# File 'lib/procedo/engine/intervention.rb', line 58

def add_working_period(id, attributes = {})
  period = Procedo::Engine::Intervention::WorkingPeriod.new(id, attributes)
  @working_periods[period.id] = period
end

#handlers_statesObject


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/procedo/engine/intervention.rb', line 43

def handlers_states
  hash = {}
  @root_group.each_member do |parameter|
    param_name = parameter.param_name
    next unless parameter.respond_to? :handlers_states
    states = parameter.handlers_states
    next if states.empty?
    hash[param_name] ||= {}
    hash[param_name][parameter.id.to_s] = states
  end
  hash
end

#impact_with(steps) ⇒ Object

Find a working_period, or a parameters


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

def impact_with(steps)
  if steps.size > 1
    if steps.first == 'working_periods'
      @working_periods[steps[1]].impact_with(steps[2..-1])
    else
      @root_group.impact_with(steps)
    end
  elsif !steps.empty?
    field = steps.first
    send(field + '=', send(field))
  end
end

#impact_with!(updater_name) ⇒ Object

Impact changes


80
81
82
83
# File 'lib/procedo/engine/intervention.rb', line 80

def impact_with!(updater_name)
  steps = updater_name.to_s.split(/[\[\]]+/)
  impact_with(steps)
end

#interpret(tree, env = {}) ⇒ Object


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

def interpret(tree, env = {})
  Interpreter.interpret(self, tree, env)
end

#parameter_set(name) ⇒ Object


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

def parameter_set(name)
  Procedo::Engine::Set.new(self, procedure.find!(name))
end

#parameters_of_name(name) ⇒ Object


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

def parameters_of_name(name)
  @root_group.parameters_of_name(name.to_sym)
end

#to_hashObject


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/procedo/engine/intervention.rb', line 30

def to_hash
  hash = { procedure_name: @procedure.name, working_periods_attributes: {} }
  @working_periods.each do |id, period|
    hash[:working_periods_attributes][id] = period.to_hash
  end
  @root_group.each_member do |parameter|
    param_name = parameter.param_name
    hash[param_name] ||= {}
    hash[param_name][parameter.id.to_s] = parameter.to_hash
  end
  hash
end

#working_durationObject


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

def working_duration
  @working_periods.map(&:duration).sum || 0.0
end