Class: Procedo::Procedure::ProductParameter

Inherits:
Parameter
  • Object
show all
Includes:
Codeable
Defined in:
lib/procedo/procedure/product_parameter.rb

Overview

A parameter is used to defined which are the operators, targets, inputs, outputs and tools in procedure.

Constant Summary collapse

TYPES =
%i[target tool doer input output].freeze

Instance Attribute Summary collapse

Attributes inherited from Parameter

#cardinality, #group, #name

Instance Method Summary collapse

Methods inherited from Parameter

#human_name, #inspect, #position, type

Constructor Details

#initialize(procedure, name, type, options = {}) ⇒ ProductParameter

Returns a new instance of ProductParameter.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/procedo/procedure/product_parameter.rb', line 21

def initialize(procedure, name, type, options = {})
  super(procedure, name, options)
  @type = type
  unless ProductParameter::TYPES.include?(@type)
    raise ArgumentError, "Unknown parameter type: #{@type.inspect}"
  end
  if options[:filter]
    @filter = options[:filter]
    # # Check filter syntax
    # WorkingSet.parse(@filter)
  end
  if output?
    self.variety = options[:variety]
    self.derivative_of = options[:derivative_of]
  end
  if input? || target?
    self.component_of = options[:component_of] if options[:component_of]
    @display_status = options[:display_status] if options[:display_status]
  end
  @handlers = {}
  @attributes = {}
  @readings = {}
end

Instance Attribute Details

#birth_natureObject (readonly)

Returns the value of attribute birth_nature


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def birth_nature
  @birth_nature
end

#default_actorObject (readonly)

Returns the value of attribute default_actor


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def default_actor
  @default_actor
end

#default_nameObject (readonly)

Returns the value of attribute default_name


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def default_name
  @default_name
end

#default_variantObject (readonly)

Returns the value of attribute default_variant


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def default_variant
  @default_variant
end

#derivative_ofObject

Returns the value of attribute derivative_of


15
16
17
# File 'lib/procedo/procedure/product_parameter.rb', line 15

def derivative_of
  @derivative_of
end

#destinationsObject (readonly)

Returns the value of attribute destinations


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def destinations
  @destinations
end

#display_statusObject (readonly)

Returns the value of attribute display_status


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def display_status
  @display_status
end

#filterObject (readonly)

Returns the value of attribute filter


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def filter
  @filter
end

#new_valueObject (readonly)

Returns the value of attribute new_value


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def new_value
  @new_value
end

#procedureObject (readonly)

Returns the value of attribute procedure


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def procedure
  @procedure
end

#producer_nameObject (readonly)

Returns the value of attribute producer_name


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def producer_name
  @producer_name
end

#rolesObject (readonly)

Returns the value of attribute roles


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def roles
  @roles
end

#typeObject (readonly)

Returns the value of attribute type


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value


10
11
12
# File 'lib/procedo/procedure/product_parameter.rb', line 10

def value
  @value
end

#varietyObject

Returns the value of attribute variety


15
16
17
# File 'lib/procedo/procedure/product_parameter.rb', line 15

def variety
  @variety
end

Instance Method Details

#add_attribute(name, options = {}) ⇒ Object

Adds a new attribute


63
64
65
66
67
68
69
# File 'lib/procedo/procedure/product_parameter.rb', line 63

def add_attribute(name, options = {})
  attribute = Procedo::Procedure::Attribute.new(self, name, options)
  if @attributes.key?(attribute.name)
    raise ArgumentError, "Attribute name already taken: #{name}"
  end
  @attributes[attribute.name] = attribute
end

#add_handler(name, options = {}) ⇒ Object

Adds a new handler


54
55
56
57
58
59
60
# File 'lib/procedo/procedure/product_parameter.rb', line 54

def add_handler(name, options = {})
  handler = Procedo::Procedure::Handler.new(self, name, options)
  if @handlers.key?(handler.name)
    raise ArgumentError, "Handler name already taken: #{name}"
  end
  @handlers[handler.name] = handler
end

#add_reading(name, options = {}) ⇒ Object

Adds a new reading


72
73
74
75
76
77
78
# File 'lib/procedo/procedure/product_parameter.rb', line 72

def add_reading(name, options = {})
  reading = Procedo::Procedure::Reading.new(self, name, options)
  if @readings.key?(reading.name)
    raise ArgumentError, "Reading name already taken: #{name}"
  end
  @readings[reading.name] = reading
end

#attribute(name) ⇒ Object

Returns an attribute by its name


123
124
125
126
# File 'lib/procedo/procedure/product_parameter.rb', line 123

def attribute(name)
  raise 'Invalid attribute: ' + name.inspect unless Procedo::Procedure::Attribute::TYPES.include?(name)
  @attributes[name]
end

#attributesObject


84
85
86
# File 'lib/procedo/procedure/product_parameter.rb', line 84

def attributes
  @attributes.values
end

#backward_converters_from(destination) ⇒ Object

Returns backward converters from a given destination


247
248
249
250
251
252
253
# File 'lib/procedo/procedure/product_parameter.rb', line 247

def backward_converters_from(destination)
  handlers.collect do |handler|
    handler.converters.select do |converter|
      converter.destination == destination && converter.backward?
    end
  end.flatten.compact
end

#best_handler_for(quantity) ⇒ Object

Find best handler for given quantity


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/procedo/procedure/product_parameter.rb', line 134

def best_handler_for(quantity)
  if quantity.is_a?(Measure)
    candidates = handlers.select do |h|
      h.measure? && h.dimension_name == quantity.dimension
    end
    return nil unless candidates.any?
    return candidates.first if candidates.count == 1
    best = candidates.find { |h| h.unit.name.to_s == quantity.unit.to_s }
    (best || candidates.first)
  elsif quantity.is_a?(Numeric)
    candidates = handlers.select { |h| h.indicator.datatype == :decimal }
    return nil unless candidates.any?
    candidates.first
  elsif quantity.is_a?(Charta::Geometry)
    candidates = handlers.select { |h| h.indicator.datatype == :multi_polygon }
    return nil unless candidates.any?
    candidates.first
  end
end

#beta?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/procedo/procedure/product_parameter.rb', line 49

def beta?
  !@display_status.nil?
end

#componentsObject

Return parameters where component-of dependent on current parameter


165
166
167
168
169
170
# File 'lib/procedo/procedure/product_parameter.rb', line 165

def components
  procedure.product_parameters(true).select do |p|
    next unless p.component_of?
    p.component_of? && p.component_of_with_parameter?(name, p == self)
  end
end

#computed_attr(attribute_name) ⇒ Object


184
185
186
187
188
189
190
191
192
193
194
# File 'lib/procedo/procedure/product_parameter.rb', line 184

def computed_attr(attribute_name)
  attribute = instance_variable_get(:"@#{attribute_name}")
  return nil unless attribute
  return attribute unless attribute =~ /\:/
  attr, other = attribute.split(/\:/)[0..1].map(&:strip)
  attr = attribute_name.to_s.underscore if attr.blank?
  unless parameter = @procedure.parameters[other]
    raise Procedo::Errors::MissingParameter, "Parameter #{other.inspect} can not be found"
  end
  parameter.send("computed_#{attr}")
end

#computed_derivative_ofObject


180
181
182
# File 'lib/procedo/procedure/product_parameter.rb', line 180

def computed_derivative_of
  computed_attr(:derivative_of)
end

#computed_varietyObject


176
177
178
# File 'lib/procedo/procedure/product_parameter.rb', line 176

def computed_variety
  computed_attr(:variety)
end

#default(destination) ⇒ Object

Returns default given destination if exists


242
243
244
# File 'lib/procedo/procedure/product_parameter.rb', line 242

def default(destination)
  @default_destinations[destination]
end

#default_name?Boolean

Returns:

  • (Boolean)

154
155
156
# File 'lib/procedo/procedure/product_parameter.rb', line 154

def default_name?
  @default_name.present?
end

#depend_on?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)

263
264
265
266
267
268
269
# File 'lib/procedo/procedure/product_parameter.rb', line 263

def depend_on?(parameter_name)
  return false if parameter_name == name
  @attributes.values.any? { |a| a.depend_on? parameter_name } ||
    @readings.values.any? { |r| r.depend_on? parameter_name } ||
    @handlers.values.any? { |h| h.depend_on? parameter_name } ||
    (component_of? && component_of_with_parameter?(parameter_name))
end

#dependent_variablesObject

Returns dependent parameters. Parameters that point on me


256
257
258
259
260
261
# File 'lib/procedo/procedure/product_parameter.rb', line 256

def dependent_variables
  procedure.parameters.select do |v|
    # v.producer == self or
    v.variety =~ /\:\s*#{name}\z/ || v.derivative_of =~ /\:\s*#{name}\z/
  end
end

#depending_variablesObject

Returns dependings parameters. Parameters that I point


272
273
274
275
# File 'lib/procedo/procedure/product_parameter.rb', line 272

def depending_variables
  # self.producer
  [procedure.parameters[variety.split(/\:\s*/)], procedure.parameters[derivative_of.split(/\:\s*/)]].compact
end

#fulfilled_by?(actor) ⇒ Boolean

Checks if a given actor might fulfill the procedure's parameter. Returns true if all information provided by the parameter (variety, derivative_of and/or abilities) match with actor's ones, false if at least one does not fit or is missing

Returns:

  • (Boolean)

284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/procedo/procedure/product_parameter.rb', line 284

def fulfilled_by?(actor)
  # do not test created parameters
  return false if new?
  expr = []
  expr << "is #{computed_variety}" if @variety.present?
  if @derivative_of.present? && actor.derivative_of.present?
    expr << "derives from #{computed_derivative_of}"
  end
  if @abilities.present?
    expr << @abilities.map { |a| "can #{a}" }.join(' and ')
  end
  return false if expr.empty?
  actor.of_expression(expr.join(' and '))
end

#handled?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'lib/procedo/procedure/product_parameter.rb', line 113

def handled?
  @handlers.any?
end

#handler(name) ⇒ Object

Returns an handler by its name


118
119
120
# File 'lib/procedo/procedure/product_parameter.rb', line 118

def handler(name)
  @handlers[name.to_sym]
end

#handlersObject


80
81
82
# File 'lib/procedo/procedure/product_parameter.rb', line 80

def handlers
  @handlers.values
end

#killable_questionObject


92
93
94
95
96
97
98
99
100
101
# File 'lib/procedo/procedure/product_parameter.rb', line 92

def killable_question
  "is_#{name}_completely_destroyed_by_#{procedure.name}".t(
    scope: [:procedure_killable_parameters],
    default: [
      "is_#{name}_completely_destroyed_by_intervention".to_sym,
      "is_this_completely_destroyed_by_#{procedure.name}".to_sym,
      'is_this_completely_destroyed_by_this_intervention'.to_sym
    ]
  )
end

#known_variant?Boolean

Returns:

  • (Boolean)

206
207
208
# File 'lib/procedo/procedure/product_parameter.rb', line 206

def known_variant?
  !@variant.nil? || parted?
end

#othersObject


108
109
110
# File 'lib/procedo/procedure/product_parameter.rb', line 108

def others
  @procedure.parameters.reject { |v| v == self }
end

#possible_matching_for(*actors) ⇒ Array<Product>

Matches actors to parameter. Returns an array of actors fulfilling parameter

Parameters:

  • actors, (Array<Product>)

    a list of actors to check

Returns:


302
303
304
305
306
307
308
309
# File 'lib/procedo/procedure/product_parameter.rb', line 302

def possible_matching_for(*actors)
  actors.flatten!
  result = []
  actors.each do |actor|
    result << actor if fulfilled_by?(actor)
  end
  result
end

#producerObject


172
173
174
# File 'lib/procedo/procedure/product_parameter.rb', line 172

def producer
  @producer ||= @procedure.parameters[@producer_name]
end

#quantified?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/procedo/procedure/product_parameter.rb', line 45

def quantified?
  input? || output?
end

#reading(name) ⇒ Object

Returns an reading by its name


129
130
131
# File 'lib/procedo/procedure/product_parameter.rb', line 129

def reading(name)
  @readings[name.to_sym]
end

#readingsObject


88
89
90
# File 'lib/procedo/procedure/product_parameter.rb', line 88

def readings
  @readings.values
end

#reflection_nameObject

Returns reflection name for an intervention object


104
105
106
# File 'lib/procedo/procedure/product_parameter.rb', line 104

def reflection_name
  @type.to_s.pluralize.to_sym
end

#scope_hashObject

Returns scope hash for unroll


197
198
199
200
201
202
203
204
# File 'lib/procedo/procedure/product_parameter.rb', line 197

def scope_hash
  hash = {}
  hash[:of_expression] = @filter if @filter.present?
  # hash[:can_each] = @abilities.join(',') unless @abilities.empty?
  hash[:of_variety] = computed_variety if computed_variety
  hash[:derivative_of] = computed_derivative_of if computed_derivative_of
  hash
end

#variant(intervention) ⇒ Object

Return a ProductNatureVariant based on given informations


211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/procedo/procedure/product_parameter.rb', line 211

def variant(intervention)
  if @variant.start_with?(':')
    other = @variant[1..-1]
    return intervention.product_parameters.find_by(parameter: other).variant
  elsif Nomen::ProductNatureVariant[@variant]
    unless variant = ProductNatureVariant.find_by(nomen: @variant.to_s)
      variant = ProductNatureVariant.import_from_nomenclature(@variant)
    end
    return variant
  end
  nil
end

#variant_indicationObject


234
235
236
237
238
239
# File 'lib/procedo/procedure/product_parameter.rb', line 234

def variant_indication
  if v = variant_parameter
    return 'same_variant_as_x'.tl(x: v.human_name)
  end
  'unknown_variant'.tl
end

#variant_parameterObject


224
225
226
227
228
229
230
231
232
# File 'lib/procedo/procedure/product_parameter.rb', line 224

def variant_parameter
  if parted?
    return producer
  elsif @variant.start_with?(':')
    other = @variant[1..-1]
    return @procedure.parameters[other]
  end
  nil
end