Class: Procedo::Procedure::ProductParameter

Inherits:
Parameter
  • Object
show all
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 =
[: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.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/procedo/procedure/product_parameter.rb', line 16

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
  @handlers = {}
  @attributes = {}
  @readings = {}
end

Instance Attribute Details

#birth_natureObject (readonly)

Returns the value of attribute birth_nature


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

def birth_nature
  @birth_nature
end

#default_actorObject (readonly)

Returns the value of attribute default_actor


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

def default_actor
  @default_actor
end

#default_nameObject (readonly)

Returns the value of attribute default_name


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

def default_name
  @default_name
end

#default_variantObject (readonly)

Returns the value of attribute default_variant


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

def default_variant
  @default_variant
end

#derivative_ofObject (readonly)

Returns the value of attribute derivative_of


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

def derivative_of
  @derivative_of
end

#destinationsObject (readonly)

Returns the value of attribute destinations


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

def destinations
  @destinations
end

#filterObject (readonly)

Returns the value of attribute filter


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

def filter
  @filter
end

#new_valueObject (readonly)

Returns the value of attribute new_value


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

def new_value
  @new_value
end

#procedureObject (readonly)

Returns the value of attribute procedure


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

def procedure
  @procedure
end

#producer_nameObject (readonly)

Returns the value of attribute producer_name


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

def producer_name
  @producer_name
end

#rolesObject (readonly)

Returns the value of attribute roles


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

def roles
  @roles
end

#typeObject (readonly)

Returns the value of attribute type


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

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value


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

def value
  @value
end

#varietyObject (readonly)

Returns the value of attribute variety


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

def variety
  @variety
end

Instance Method Details

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

Adds a new attribute


46
47
48
49
50
51
52
# File 'lib/procedo/procedure/product_parameter.rb', line 46

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


37
38
39
40
41
42
43
# File 'lib/procedo/procedure/product_parameter.rb', line 37

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


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

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


95
96
97
98
# File 'lib/procedo/procedure/product_parameter.rb', line 95

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

#attributesObject


67
68
69
# File 'lib/procedo/procedure/product_parameter.rb', line 67

def attributes
  @attributes.values
end

#backward_converters_from(destination) ⇒ Object

Returns backward converters from a given destination


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

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


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/procedo/procedure/product_parameter.rb', line 106

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.select { |h| h.unit.name.to_s == quantity.unit.to_s }
    return (best ? best : candidates.first)
  elsif quantity.is_a?(Numeric)
    candidates = handlers.select { |h| h.indicator.datatype == :decimal }
    return nil unless candidates.any?
    return candidates.first
  elsif quantity.is_a?(Charta::Geometry)
    candidates = handlers.select { |h| h.indicator.datatype == :multi_polygon }
    return nil unless candidates.any?
    return candidates.first
  else
    return nil
  end
end

#computed_derivative_ofObject


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/procedo/procedure/product_parameter.rb', line 159

def computed_derivative_of
  if @derivative_of
    if @derivative_of =~ /\:/
      attr, other = @derivative_of.split(/\:/)[0..1].map(&:strip)
      attr = 'derivative_of' if attr.blank?
      attr.tr!('-', '_')
      unless parameter = @procedure.parameters[other]
        raise Procedo::Errors::MissingParameter, "Parameter #{other.inspect} can not be found"
      end
      return parameter.send("computed_#{attr}")
    else
      return @derivative_of
    end
  end
  nil
end

#computed_varietyObject


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/procedo/procedure/product_parameter.rb', line 142

def computed_variety
  if @variety
    if @variety =~ /\:/
      attr, other = @variety.split(/\:/)[0..1].map(&:strip)
      attr = 'variety' if attr.blank?
      attr.tr!('-', '_')
      unless parameter = @procedure.parameters[other]
        raise Procedo::Errors::MissingParameter, "Parameter #{other.inspect} can not be found"
      end
      return parameter.send("computed_#{attr}")
    else
      return @variety
    end
  end
  nil
end

#default(destination) ⇒ Object

Returns default given destination if exists


222
223
224
# File 'lib/procedo/procedure/product_parameter.rb', line 222

def default(destination)
  @default_destinations[destination]
end

#default_name?Boolean

Returns:

  • (Boolean)

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

def default_name?
  !@default_name.blank?
end

#depend_on?(parameter) ⇒ Boolean

Returns:

  • (Boolean)

268
269
270
271
272
# File 'lib/procedo/procedure/product_parameter.rb', line 268

def depend_on?(parameter)
  dependent_parameters.detect do |p|
    p == parameter.name
  end
end

#dependent_parameter_namesObject

Returns list of dependent parameter names


254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/procedo/procedure/product_parameter.rb', line 254

def dependent_parameter_names
  parameter_names = []
  @handlers.each do |_, handler|
    parameter_names += handler.dependent_parameters
  end
  @attributes.each do |_, attribute|
    parameter_names += attribute.dependent_parameters
  end
  @readings.each do |_, reading|
    parameter_names += reading.dependent_parameters
  end
  parameter_names.uniq
end

#dependent_parametersObject


249
250
251
# File 'lib/procedo/procedure/product_parameter.rb', line 249

def dependent_parameters
  @dependent_parameters ||= dependent_parameter_names.map { |p| procedure.find!(p) }
end

#dependent_variablesObject

Returns dependent parameters. Parameters that point on me


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

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


244
245
246
247
# File 'lib/procedo/procedure/product_parameter.rb', line 244

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)

281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/procedo/procedure/product_parameter.rb', line 281

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)

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

def handled?
  @handlers.any?
end

#handler(name) ⇒ Object

Returns an handler by its name


90
91
92
# File 'lib/procedo/procedure/product_parameter.rb', line 90

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

#handlersObject


63
64
65
# File 'lib/procedo/procedure/product_parameter.rb', line 63

def handlers
  @handlers.values
end

#known_variant?Boolean

Returns:

  • (Boolean)

186
187
188
# File 'lib/procedo/procedure/product_parameter.rb', line 186

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

#othersObject


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

def others
  @procedure.parameters.select { |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:


299
300
301
302
303
304
305
306
# File 'lib/procedo/procedure/product_parameter.rb', line 299

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

#producerObject


138
139
140
# File 'lib/procedo/procedure/product_parameter.rb', line 138

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

#quantified?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/procedo/procedure/product_parameter.rb', line 32

def quantified?
  input? || output?
end

#reading(name) ⇒ Object

Returns an reading by its name


101
102
103
# File 'lib/procedo/procedure/product_parameter.rb', line 101

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

#readingsObject


71
72
73
# File 'lib/procedo/procedure/product_parameter.rb', line 71

def readings
  @readings.values
end

#reflection_nameObject

Returns reflection name for an intervention object


76
77
78
# File 'lib/procedo/procedure/product_parameter.rb', line 76

def reflection_name
  @type.to_s.pluralize.to_sym
end

#scope_hashObject

Returns scope hash for unroll


177
178
179
180
181
182
183
184
# File 'lib/procedo/procedure/product_parameter.rb', line 177

def scope_hash
  hash = {}
  hash[:of_expression] = @filter unless @filter.blank?
  # 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


191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/procedo/procedure/product_parameter.rb', line 191

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


214
215
216
217
218
219
# File 'lib/procedo/procedure/product_parameter.rb', line 214

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

#variant_parameterObject


204
205
206
207
208
209
210
211
212
# File 'lib/procedo/procedure/product_parameter.rb', line 204

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