Class: OpenEHR::AM::Archetype::ConstraintModel::CComplexObject

Inherits:
CDefinedObject show all
Defined in:
lib/openehr/am/archetype/constraint_model.rb

Direct Known Subclasses

CArchetypeRoot

Instance Attribute Summary collapse

Attributes inherited from CDefinedObject

#assumed_value

Attributes inherited from CObject

#node_id, #occurrences, #rm_type_name

Attributes inherited from ArchetypeConstraint

#parent, #path

Instance Method Summary collapse

Methods inherited from CDefinedObject

#has_assumed_value?

Methods inherited from CObject

#node_conforms_to?, #path, #to_rm

Methods inherited from ArchetypeConstraint

#congruent?, #has_path?, #node_conforms_to?

Constructor Details

#initialize(args = { }) ⇒ CComplexObject

Returns a new instance of CComplexObject.



291
292
293
294
# File 'lib/openehr/am/archetype/constraint_model.rb', line 291

def initialize(args = { })
  super
  self.attributes = args[:attributes]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



289
290
291
# File 'lib/openehr/am/archetype/constraint_model.rb', line 289

def attributes
  @attributes
end

Instance Method Details

#any_allowed?Boolean

Returns:

  • (Boolean)


308
309
310
# File 'lib/openehr/am/archetype/constraint_model.rb', line 308

def any_allowed?
  return (@attributes.nil? or @attributes.empty?)
end

#default_valueObject

Builds the minimal RM instance satisfying this node's mandatory constraints: only attributes with existence lower >= 1 are populated, each from its first child alternative's own default_value (recursively), and CMultipleAttribute gets exactly cardinality.interval.lower (or 1) copies of it. Returns nil - rather than raising - whenever a mandatory field has no derivable default, since that means no minimal instance can be built at all.



424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/openehr/am/archetype/constraint_model.rb', line 424

def default_value
  return nil if any_allowed?

  klass = OpenEHR::RM.class_for(rm_type_name)
  return nil if klass.nil?

  params = mandatory_attribute_params
  return nil if params.nil?

  klass.new(node_id.nil? ? params : params.merge(:archetype_node_id => node_id))
rescue ArgumentError, NotImplementedError
  nil
end

#find_violation(value) ⇒ Object

Diagnostic counterpart to valid_value?, for building path-annotated instance-validation reports: walks the same rules but returns the first [failing_constraint_node, failing_rm_value] pair found (depth-first) instead of a bare boolean, or nil when value fully conforms.



333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/openehr/am/archetype/constraint_model.rb', line 333

def find_violation(value)
  if value.nil? || !node_id_matches?(value) || !OpenEHR::RM.subtype_of?(value, rm_type_name)
    return [self, value]
  end
  return nil if any_allowed?

  attributes.each do |attribute|
    violation = attribute_violation(attribute, value)
    return violation if violation
  end
  nil
end

#has_attributes?Boolean

Returns:

  • (Boolean)


304
305
306
# File 'lib/openehr/am/archetype/constraint_model.rb', line 304

def has_attributes?
  !attributes.nil? and !attributes.empty?
end

#valid_value?(value) ⇒ Boolean

Recursively checks value (an RM instance) against this node's constraints: rm_type_name conformance (RM subtypes allowed), archetype_node_id (when this node specifies one), then each attribute's existence/cardinality and its children constraints. ArchetypeSlot/ArchetypeInternalRef/ ConstraintRef children are accepted permissively (v1 does not resolve slot-fillers or internal references).

Returns:

  • (Boolean)


319
320
321
322
323
324
325
326
# File 'lib/openehr/am/archetype/constraint_model.rb', line 319

def valid_value?(value)
  return false if value.nil?
  return false unless node_id_matches?(value)
  return false unless OpenEHR::RM.subtype_of?(value, rm_type_name)
  return true if any_allowed?

  attributes.all? { |attribute| attribute_conforms?(attribute, attribute_values(attribute, value)) }
end