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/agent.rb,
lib/procedo/engine/intervention/input.rb,
lib/procedo/engine/intervention/output.rb,
lib/procedo/engine/intervention/target.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.


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

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

#working_periodsObject (readonly)

Returns the value of attribute working_periods


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

def working_periods
  @working_periods
end

Instance Method Details

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


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

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

#handlers_statesObject


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

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

Impact changes


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/procedo/engine/intervention.rb', line 105

def impact_with!(steps)
  steps = steps.to_s.split(/[\[\]]+/) unless steps.is_a?(Array)
  unless steps.size > 1
    raise ArgumentError, 'Invalid steps: got ' + steps.inspect
  end
  if steps.first == 'working_periods'
    @working_periods[steps[1]].impact_with(steps[2..-1])
  else
    @root_group.impact_with(steps)
  end
end

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


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

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

#parameter_set(name) ⇒ Object


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

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

#parameters_of_name(name) ⇒ Object


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

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

#procedure_statesObject


70
71
72
73
74
75
76
77
78
79
# File 'lib/procedo/engine/intervention.rb', line 70

def procedure_states
  hash = {}
  @procedure.parameters.each do |parameter|
    param_name = parameter.name
    concerned_params = @root_group.parameters_of_name(param_name)
    display_method = parameter.display_status if parameter.respond_to? :display_status
    hash[param_name] = { display: Functions.send(display_method, concerned_params) } if display_method
  end
  hash
end

#to_attributesObject


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

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

#to_hashObject


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

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


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

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

#working_periods_parameterObject


117
118
119
# File 'lib/procedo/engine/intervention.rb', line 117

def working_periods_parameter
  @working_periods.values.collect(&:to_hash)
end