Module: Chemicalml::Cml::Translator::ValueTranslations::ClassMethods

Included in:
Chemicalml::Cml::Translator
Defined in:
lib/chemicalml/cml/translator/value_translations.rb

Instance Method Summary collapse

Instance Method Details

#array_from_canonical(array, schema: :schema3) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 53

def array_from_canonical(array, schema: :schema3)
  return nil unless array

  wire_class_for(schema, Chemicalml::Cml::Role::Array).new(
    content: [*array.values].join(" "),
    data_type: array.data_type,
    units: array.units,
    size: array.size,
    id: array.id,
    title: array.title,
    dict_ref: array.dict_ref
  )
end

#array_to_canonical(cml_array) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 39

def array_to_canonical(cml_array)
  return nil unless cml_array

  Model::Array.new(
    values: cml_array.content.to_s.split,
    data_type: cml_array.data_type,
    units: cml_array.units,
    size: cml_array.size,
    id: cml_array.id,
    title: cml_array.title,
    dict_ref: cml_array.dict_ref
  )
end

#formula_from_canonical(formula, schema: :schema3) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 232

def formula_from_canonical(formula, schema: :schema3)
  return nil unless formula

  wire_class_for(schema, Chemicalml::Cml::Role::Formula).new(
    id: formula.id,
    concise: formula.concise,
    inline: formula.inline,
    formal_charge: formula.formal_charge,
    count: formula.count,
    title: formula.title,
    convention: formula.convention,
    dict_ref: formula.dict_ref
  )
end

#formula_to_canonical(cml_formula) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 217

def formula_to_canonical(cml_formula)
  return nil unless cml_formula

  Model::Formula.new(
    id: cml_formula.id,
    concise: cml_formula.concise,
    inline: cml_formula.inline,
    formal_charge: cml_formula.formal_charge,
    count: cml_formula.count,
    title: cml_formula.title,
    convention: cml_formula.convention,
    dict_ref: cml_formula.dict_ref
  )
end

#label_from_canonical(label, schema: :schema3) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 182

def label_from_canonical(label, schema: :schema3)
  return nil unless label

  wire_class_for(schema, Chemicalml::Cml::Role::Label).new(
    id: label.id,
    value: label.value,
    dict_ref: label.dict_ref,
    convention: label.convention
  )
end

#label_to_canonical(cml_label) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 171

def label_to_canonical(cml_label)
  return nil unless cml_label

  Model::Label.new(
    id: cml_label.id,
    value: cml_label.value,
    dict_ref: cml_label.dict_ref,
    convention: cml_label.convention
  )
end

#matrix_from_canonical(matrix, schema: :schema3) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 82

def matrix_from_canonical(matrix, schema: :schema3)
  return nil unless matrix

  wire_class_for(schema, Chemicalml::Cml::Role::Matrix).new(
    content: [*matrix.values].join(" "),
    rows: matrix.rows,
    columns: matrix.columns,
    data_type: matrix.data_type,
    units: matrix.units,
    id: matrix.id,
    title: matrix.title,
    dict_ref: matrix.dict_ref
  )
end

#matrix_to_canonical(cml_matrix) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 67

def matrix_to_canonical(cml_matrix)
  return nil unless cml_matrix

  Model::Matrix.new(
    values: cml_matrix.content.to_s.split,
    rows: cml_matrix.rows,
    columns: cml_matrix.columns,
    data_type: cml_matrix.data_type,
    units: cml_matrix.units,
    id: cml_matrix.id,
    title: cml_matrix.title,
    dict_ref: cml_matrix.dict_ref
  )
end

#metadata_from_canonical(md, schema: :schema3) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 205

def (md, schema: :schema3)
  return nil unless md

  wire_class_for(schema, Chemicalml::Cml::Role::Metadata).new(
    id: md.id,
    name: md.name,
    content: md.content,
    convention: md.convention,
    title: md.title
  )
end

#metadata_to_canonical(cml_md) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 193

def (cml_md)
  return nil unless cml_md

  Model::Metadata.new(
    id: cml_md.id,
    name: cml_md.name,
    content: cml_md.content,
    convention: cml_md.convention,
    title: cml_md.title
  )
end

#parameter_from_canonical(param, schema: :schema3) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 156

def parameter_from_canonical(param, schema: :schema3)
  return nil unless param

  value = value_container_from_canonical(param.value, schema: schema)
  wire_class_for(schema, Chemicalml::Cml::Role::Parameter).new(
    id: param.id,
    title: param.title,
    dict_ref: param.dict_ref,
    convention: param.convention,
    scalar: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Scalar)) ? value : nil,
    array: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Array)) ? value : nil,
    matrix: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Matrix)) ? value : nil
  )
end

#parameter_to_canonical(cml_param) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 144

def parameter_to_canonical(cml_param)
  return nil unless cml_param

  Model::Parameter.new(
    id: cml_param.id,
    title: cml_param.title,
    dict_ref: cml_param.dict_ref,
    convention: cml_param.convention,
    value: parameter_value_to_canonical(cml_param)
  )
end

#property_from_canonical(prop, schema: :schema3) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 129

def property_from_canonical(prop, schema: :schema3)
  return nil unless prop

  value = value_container_from_canonical(prop.value, schema: schema)
  wire_class_for(schema, Chemicalml::Cml::Role::Property).new(
    id: prop.id,
    title: prop.title,
    dict_ref: prop.dict_ref,
    convention: prop.convention,
    scalar: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Scalar)) ? value : nil,
    array: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Array)) ? value : nil,
    matrix: value.is_a?(wire_class_for(schema, Chemicalml::Cml::Role::Matrix)) ? value : nil
  )
end

#property_to_canonical(cml_prop) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 117

def property_to_canonical(cml_prop)
  return nil unless cml_prop

  Model::Property.new(
    id: cml_prop.id,
    title: cml_prop.title,
    dict_ref: cml_prop.dict_ref,
    convention: cml_prop.convention,
    value: property_value_to_canonical(cml_prop)
  )
end

#scalar_from_canonical(scalar, schema: :schema3) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 26

def scalar_from_canonical(scalar, schema: :schema3)
  return nil unless scalar

  wire_class_for(schema, Chemicalml::Cml::Role::Scalar).new(
    content: scalar.value,
    data_type: scalar.data_type,
    units: scalar.units,
    id: scalar.id,
    title: scalar.title,
    dict_ref: scalar.dict_ref
  )
end

#scalar_to_canonical(cml_scalar) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 13

def scalar_to_canonical(cml_scalar)
  return nil unless cml_scalar

  Model::Scalar.new(
    value: cml_scalar.content,
    data_type: cml_scalar.data_type,
    units: cml_scalar.units,
    id: cml_scalar.id,
    title: cml_scalar.title,
    dict_ref: cml_scalar.dict_ref
  )
end

#value_container_from_canonical(value, schema: :schema3) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 109

def value_container_from_canonical(value, schema: :schema3)
  case value
  when Model::Matrix then matrix_from_canonical(value, schema: schema)
  when Model::Array  then array_from_canonical(value, schema: schema)
  when Model::Scalar then scalar_from_canonical(value, schema: schema)
  end
end

#value_container_to_canonical(cml_value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/chemicalml/cml/translator/value_translations.rb', line 97

def value_container_to_canonical(cml_value)
  return nil unless cml_value

  if cml_value.is_a?(Chemicalml::Cml::Role::Matrix)
    matrix_to_canonical(cml_value)
  elsif cml_value.is_a?(Chemicalml::Cml::Role::Array)
    array_to_canonical(cml_value)
  elsif cml_value.is_a?(Chemicalml::Cml::Role::Scalar)
    scalar_to_canonical(cml_value)
  end
end