Class: Opencdd::Model::YamlEntity

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/opencdd/model/yaml_entity.rb

Overview

CDD-native YAML entity. Every attribute uses the semantic name from the CDD ontology (preferred_name, superclass, class_type) rather than the IEC 62656-1 wire-format key (MDC_P004, MDC_P010, MDC_P011). Multilingual fields are Hash<String, String>; collection fields are Array.

This is the canonical YAML shape for a CDD entity:

---
irdi: 0112/2///61360_4#AAA001
type: class
code: AAA001
preferred_name:
en: Vehicle
fr: Véhicule
class_type: ITEM_CLASS
superclass: UNIVERSE
applicable_properties:
- vehicle_length
- vehicle_weight

Conversion between YamlEntity and the existing Entity model is via #from_entity and #to_entity, which bridge the semantic attributes to the raw properties Hash.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_entity(entity) ⇒ Object

── Conversion: Entity → YamlEntity ────────────────────



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/opencdd/model/yaml_entity.rb', line 82

def self.from_entity(entity)
  attrs = {
    irdi: entity.irdi&.to_s,
    type: entity.type&.to_s,
    code: entity.code,
    guid: entity[Opencdd::PropertyIds::MDC_P066],
    version: entity[Opencdd::PropertyIds::MDC_P002_1],
    revision: entity[Opencdd::PropertyIds::MDC_P002_2],
  }

  # Multilingual fields
  attrs[:preferred_name] = extract_ml(entity, Opencdd::PropertyIds::MDC_P004)
  attrs[:short_name]     = extract_ml(entity, Opencdd::PropertyIds::MDC_P005)
  attrs[:definition]     = extract_ml(entity, Opencdd::PropertyIds::MDC_P006)
  attrs[:note]           = extract_ml(entity, Opencdd::PropertyIds::MDC_P008)
  attrs[:remark]         = extract_ml(entity, Opencdd::PropertyIds::MDC_P009)

  # Class-specific
  if entity.type == :class
    ct = entity.read_field(:class_type)
    attrs[:class_type] = ct&.to_s
    attrs[:superclass] = entity.read_field(:superclass_irdi)&.to_s
    attrs[:is_case_of] = entity.read_field(:is_case_of_irdis)&.map(&:to_s) || []
    attrs[:applicable_properties] = entity.read_field(:applicable_property_irdis)&.map(&:to_s) || []
    attrs[:imported_properties] = entity.read_field(:imported_property_irdis)&.map(&:to_s) || []
    attrs[:sub_class_selection] = entity.read_field(:sub_class_selection_irdis)&.map(&:to_s) || []
  end

  # Property-specific
  if entity.type == :property
    dt = entity.read_field(:parsed_data_type)
    attrs[:data_type] = dt&.to_s
    attrs[:value_format] = entity.read_field(:parsed_value_format)&.to_s
    dc = entity.read_field(:definition_class_irdi)
    attrs[:definition_class] = dc&.to_s
    ui = entity.read_field(:unit_irdi)
    attrs[:unit] = ui&.to_s
    attrs[:condition] = entity.read_field(:condition)&.to_s
    attrs[:property_data_element_type] = entity.read_field(:property_data_element_type)&.to_s
  end

  new(**attrs.compact)
end

.merge_ml(props, pid, hash) ⇒ Object



184
185
186
187
# File 'lib/opencdd/model/yaml_entity.rb', line 184

def self.merge_ml(props, pid, hash)
  return unless hash&.any?
  hash.each { |lang, val| props["#{pid}.#{lang}"] = val }
end

Instance Method Details

#to_entity(database = nil) ⇒ Object

── Conversion: YamlEntity → Entity ────────────────────



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/opencdd/model/yaml_entity.rb', line 127

def to_entity(database = nil)
  props = {}

  props[Opencdd::PropertyIds::MDC_P066] = guid if guid
  props[Opencdd::PropertyIds::MDC_P002_1] = version if version
  props[Opencdd::PropertyIds::MDC_P002_2] = revision if revision

  # Multilingual
  merge_ml_into(props, Opencdd::PropertyIds::MDC_P004, preferred_name)
  merge_ml_into(props, Opencdd::PropertyIds::MDC_P005, short_name)
  merge_ml_into(props, Opencdd::PropertyIds::MDC_P006, definition)
  merge_ml_into(props, Opencdd::PropertyIds::MDC_P008, note)
  merge_ml_into(props, Opencdd::PropertyIds::MDC_P009, remark)

  # Class-specific
  props[Opencdd::PropertyIds::MDC_P011] = class_type if class_type
  props[Opencdd::PropertyIds::MDC_P010] = superclass if superclass
  props[Opencdd::PropertyIds::MDC_P013] = "{#{is_case_of.join(",")}}" if is_case_of&.any?
  props[Opencdd::PropertyIds::MDC_P014] = "{#{applicable_properties.join(",")}}" if applicable_properties&.any?
  props[Opencdd::PropertyIds::MDC_P090] = "{#{imported_properties.join(",")}}" if imported_properties&.any?
  props[Opencdd::PropertyIds::MDC_P016] = "{#{sub_class_selection.join(",")}}" if sub_class_selection&.any?

  # Property-specific
  props[Opencdd::PropertyIds::MDC_P022] = data_type if data_type
  props[Opencdd::PropertyIds::MDC_P024] = value_format if value_format
  props[Opencdd::PropertyIds::MDC_P021] = definition_class if definition_class
  props[Opencdd::PropertyIds::MDC_P041] = unit if unit
  props[Opencdd::PropertyIds::MDC_P028] = condition if condition
  props[Opencdd::PropertyIds::MDC_P020] = property_data_element_type if property_data_element_type

  # Determine entity class from type
  entity_class = Opencdd::MetaClasses.entity_class_for_type(type&.to_sym) || Opencdd::Klass
  meta_code = Opencdd::MetaClasses.meta_class_for_type(type&.to_sym) || "MDC_C002"

  parsed_irdi = irdi ? Opencdd::IRDI.parse(irdi) : nil
  parsed_meta = Opencdd::IRDI.parse(meta_code)

  entity_class.new(
    irdi: parsed_irdi,
    properties: props,
    meta_class_irdi: parsed_meta,
  )
end