expressir 2.4.39-x86_64-linux → 2.4.40-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/TODO.eengine/01-resolution-diagnostics.md +6 -0
  3. data/TODO.eengine/03-entity-computations.md +6 -0
  4. data/TODO.eengine/04-shtolo-rule-audit.md +6 -0
  5. data/TODO.eengine/05-shtolo-longform-flatten.md +7 -0
  6. data/TODO.eengine/08-official-corpus-gate.md +7 -0
  7. data/TODO.eengine/09-mim-mapping-parser.md +7 -0
  8. data/TODO.max-perf/01-restore-ci-green.md +4 -0
  9. data/TODO.max-perf/02-streaming-parse-path.md +4 -0
  10. data/TODO.max-perf/03-cli-parallel-opt-in.md +4 -0
  11. data/TODO.max-perf/04-benchmark-harness.md +4 -0
  12. data/TODO.max-perf/05-parallel-fidelity-specs.md +4 -0
  13. data/TODO.max-perf/06-builder-cpu-audit.md +4 -0
  14. data/TODO.max-perf/07-upstream-parsanol-roadmap.md +4 -0
  15. data/TODO.max-perf/08-builder-build-perf.md +4 -0
  16. data/TODO.max-perf/09-grammar-cold-start.md +4 -0
  17. data/TODO.max-perf/10-parser-facade-hygiene.md +4 -0
  18. data/TODO.max-perf/11-ci-green-closeout.md +4 -0
  19. data/TODO.max-perf/12-require-boot-profile.md +12 -0
  20. data/TODO.max-perf/13-key-conversion-specs.md +11 -0
  21. data/TODO.max-perf/14-builder-call-handler-audit.md +14 -0
  22. data/TODO.p28/00-overview.md +37 -0
  23. data/TODO.p28/01-uos-writer.md +54 -0
  24. data/TODO.p28/02-uos-reader.md +50 -0
  25. data/TODO.p28/03-default-xsd.md +68 -0
  26. data/TODO.p28/04-document-envelope.md +62 -0
  27. data/TODO.sota/02-evaluator.md +0 -1
  28. data/lib/expressir/p28/document.rb +138 -0
  29. data/lib/expressir/p28/uos.rb +452 -0
  30. data/lib/expressir/p28/xsd.rb +526 -0
  31. data/lib/expressir/p28.rb +29 -0
  32. data/lib/expressir/version.rb +1 -1
  33. data/lib/expressir.rb +1 -0
  34. metadata +10 -1
@@ -0,0 +1,452 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Expressir
4
+ module P28
5
+ # The unit of serialization faces (TODO.p28/01+02):
6
+ # `emit` writes a P21-shaped population as a clause 9.2.2 uos
7
+ # element and `parse` reads one back, both under the default
8
+ # binding (clause 7) encoding rules of clause 9, so the round
9
+ # trip is value-identical over the shared P21 value model.
10
+ #
11
+ # Encoding summary (clause ids cited at each rule):
12
+ # - every instance is a by-value entity instance element
13
+ # (9.3.1), named by the entity id with its first letter
14
+ # uppercased (8.5.5 identifier conversion; 9.2.6.1 worked
15
+ # example `car_model` -> `Car_model`), carrying `id="id<n>"`;
16
+ # - instance elements sit in the schema's target namespace;
17
+ # attribute accessor elements are unprefixed, matching the
18
+ # clause 9 worked example;
19
+ # - explicit attributes become child elements (9.3.1.1):
20
+ # booleans `true`/`false` (9.5.2), logicals
21
+ # `true`/`false`/`unknown` (9.5.4), integers per xs:integer
22
+ # (9.5.3), enumeration items as their declared string (9.6.1),
23
+ # binaries as hexBinary (9.5.1), entity references as a
24
+ # by-reference accessor with `ref` (9.3.3), `$` omits the
25
+ # element (the P21 validator owns mandatory-presence
26
+ # findings), aggregates of simple base as whitespace LIST
27
+ # text (9.8.1), aggregates of entity base as one child per
28
+ # component, each named and referenced by its target instance
29
+ # (9.2.6.1 worked example `Control_points`);
30
+ # - complex entities become `exp:complexEntity` (9.3.4) with
31
+ # one by-value child per supertype component.
32
+ module Uos
33
+ module_function
34
+
35
+ # @param population [P21::Population]
36
+ # @param schema [Model::Declarations::Schema]
37
+ # @param id [String] the uos id attribute
38
+ # @param target_namespace [String] the schema's target
39
+ # namespace URI (clause 7: preprocessor-chosen)
40
+ # @param prefix [String] the namespace prefix for instance
41
+ # elements (clause 7: preprocessor-chosen)
42
+ # @return [String] the serialized uos document
43
+ def emit(population, schema, id: "uos01", schema_ref: nil,
44
+ target_namespace: P28.default_target_namespace(schema.id),
45
+ prefix: default_prefix(schema))
46
+ ctx = Moxml::Context.new
47
+ doc = ctx.create_document
48
+ # 7.8: the uos element is a target-namespace global
49
+ uos = doc.create_element("#{prefix}:uos")
50
+ uos.set_attributes(
51
+ "id" => id,
52
+ "express" => schema.id,
53
+ "xmlns:#{prefix}" => target_namespace,
54
+ "xmlns:exp" => COMMON_NS,
55
+ )
56
+ # 9.2.6.2: the schema attribute references the express
57
+ # element's id within an iso_10303_28 document
58
+ uos.set_attributes("schema" => schema_ref) if schema_ref
59
+ doc.add_child(uos)
60
+ slots = Eval::Environment.new(schema: schema)
61
+ population.instances.each do |pid, record|
62
+ uos.add_child(instance_element(doc, schema, slots, prefix,
63
+ population, pid, record))
64
+ end
65
+ doc.to_xml
66
+ end
67
+
68
+ # A deterministic preprocessor-chosen prefix: the initials of
69
+ # the schema name's words (car_ownership -> "co").
70
+ def default_prefix(schema)
71
+ schema.id.to_s.split("_").filter_map { |word| word[0] }.join.downcase
72
+ end
73
+
74
+ # 8.5.5 identifier conversion: uppercase the first character.
75
+ def element_name(express_id)
76
+ express_id.to_s.sub(/\A[a-z]/, &:upcase)
77
+ end
78
+
79
+ # Part 21 keywords are stored uppercased; the XML element name
80
+ # derives from the EXPRESS declaration id (8.5.5), falling back
81
+ # to the downcased keyword when the schema has no declaration.
82
+ def instance_name(entity, keyword)
83
+ element_name(entity ? entity.id : keyword.to_s.downcase)
84
+ end
85
+
86
+ def instance_element(doc, schema, slots, prefix, population, pid, record)
87
+ if record.is_a?(P21::ComplexRecord)
88
+ return complex_entity_element(doc, schema, slots, prefix,
89
+ population, pid, record)
90
+ end
91
+
92
+ entity = find_entity(schema, record.keyword)
93
+ node = doc.create_element(
94
+ "#{prefix}:#{instance_name(entity, record.keyword)}",
95
+ )
96
+ node.set_attributes("id" => instance_id(pid))
97
+ record.params.each_with_index do |value, index|
98
+ name = entity ? attribute_name(slots, entity, index) : nil
99
+ element = attribute_value_element(doc, name, value, population,
100
+ prefix)
101
+ node.add_child(element) if element
102
+ end
103
+ node
104
+ end
105
+
106
+ # 9.3.4: one by-value child per supertype component; the id
107
+ # rides the complexEntity element.
108
+ def complex_entity_element(doc, schema, slots, prefix, population,
109
+ pid, record)
110
+ node = doc.create_element("exp:complexEntity")
111
+ node.set_attributes("id" => instance_id(pid))
112
+ record.records.each do |part|
113
+ entity = find_entity(schema, part.keyword)
114
+ child = doc.create_element(
115
+ "#{prefix}:#{instance_name(entity, part.keyword)}",
116
+ )
117
+ part.params.each_with_index do |value, index|
118
+ name = entity ? attribute_name(slots, entity, index) : nil
119
+ element = attribute_value_element(doc, name, value, population,
120
+ prefix)
121
+ child.add_child(element) if element
122
+ end
123
+ node.add_child(child)
124
+ end
125
+ node
126
+ end
127
+
128
+ def attribute_value_element(doc, name, value, population, prefix)
129
+ case value
130
+ when Array then aggregate_element(doc, name, value, population,
131
+ prefix)
132
+ when P21::TypedValue then typed_element(doc, value, population,
133
+ prefix)
134
+ when :unset, :derived then nil
135
+ when String then reference_or_string_element(doc, name, value,
136
+ population)
137
+ when P21::Enumeration
138
+ # Part 21 encodes logicals/booleans as .T./.F./.UNKNOWN.
139
+ # (Part 21 7.5.3); the parser hands them to us as
140
+ # Enumerations. Re-encode per 9.5.2/9.5.4; genuine
141
+ # enumeration items pass through as their text (9.6.1).
142
+ text_element(doc, name, logical_text(value.name))
143
+ when P21::Binary
144
+ text_element(doc, name, value.hex) # 9.5.1 hexBinary
145
+ else
146
+ # 9.5.2 booleans, 9.5.3/9.5.5 numerics, plain strings
147
+ text_element(doc, name, value.to_s)
148
+ end
149
+ end
150
+
151
+ # 9.3.3: a reference value is a by-reference accessor named
152
+ # after the attribute; a string value is a text accessor.
153
+ def reference_or_string_element(doc, name, value, _population)
154
+ return text_element(doc, name, value) unless value.start_with?("#")
155
+
156
+ node = doc.create_element(name ? element_name(name) : "Ref")
157
+ node.set_attributes("ref" => instance_id(value))
158
+ node
159
+ end
160
+
161
+ # 9.8.1: aggregates of simple base types are one whitespace
162
+ # LIST text value; entity-based aggregates carry one child per
163
+ # component, named by the component's entity type and
164
+ # referenced (9.2.6.1 worked example `Control_points`).
165
+ def aggregate_element(doc, name, values, population, prefix)
166
+ node = doc.create_element(name ? element_name(name) : "List")
167
+ if values.all? { |v| v.is_a?(String) && v.start_with?("#") }
168
+ values.each do |ref|
169
+ keyword = population.instances[ref]&.keyword
170
+ child = doc.create_element(
171
+ "#{prefix}:#{element_name(keyword ? keyword.to_s.downcase : 'ref')}",
172
+ )
173
+ child.set_attributes("ref" => instance_id(ref))
174
+ node.add_child(child)
175
+ end
176
+ else
177
+ node.content = values.map { |v| scalar_text(v) }.join(" ")
178
+ end
179
+ node
180
+ end
181
+
182
+ # A typed parameter (KEYWORD(values)) is emitted as the
183
+ # capitalized keyword element wrapping its values.
184
+ def typed_element(doc, value, population, prefix)
185
+ node = doc.create_element(element_name(value.keyword))
186
+ value.value.each do |item|
187
+ element = attribute_value_element(doc, nil, item, population,
188
+ prefix)
189
+ node.add_child(element) if element
190
+ end
191
+ node
192
+ end
193
+
194
+ def text_element(doc, name, text)
195
+ node = doc.create_element(name ? element_name(name) : "Value")
196
+ node.content = text.to_s
197
+ node
198
+ end
199
+
200
+ def logical_text(name)
201
+ case name.to_s.upcase
202
+ when "T" then "true"
203
+ when "F" then "false"
204
+ when "UNKNOWN" then "unknown"
205
+ else name.to_s
206
+ end
207
+ end
208
+
209
+ def scalar_text(value)
210
+ case value
211
+ when P21::Enumeration then logical_text(value.name)
212
+ when P21::Binary then value.hex
213
+ when :unset, :derived then ""
214
+ else value.to_s
215
+ end
216
+ end
217
+
218
+ def instance_id(pid)
219
+ "id#{pid.to_s.delete_prefix('#')}"
220
+ end
221
+
222
+ # The Part 21 slot's attribute name: inherited explicit
223
+ # attributes first (supertype closure order), the same
224
+ # contract the P21 validator maps values by.
225
+ def attribute_name(slots, entity, index)
226
+ name_for_slot = slots.slots_for(entity).find { |_, slot| slot == index }
227
+ name_for_slot&.first
228
+ end
229
+
230
+ def find_entity(schema, keyword)
231
+ Array(schema.entities).find do |entity|
232
+ entity.id&.safe_downcase == keyword&.safe_downcase
233
+ end
234
+ end
235
+
236
+ # ------------------------------------------------------------------
237
+ # The reader face (TODO.p28/02): a uos document back into a
238
+ # P21-shaped population, inverting the writer's encodings so
239
+ # the round trip is value-identical.
240
+ # ------------------------------------------------------------------
241
+
242
+ # @param xml [String] a uos document
243
+ # @param schema [Model::Declarations::Schema]
244
+ # @return [P21::Population]
245
+ def parse(xml, schema)
246
+ doc = Moxml::Context.new.parse(xml)
247
+ uos = doc.root
248
+ unless uos&.name == "uos"
249
+ raise Expressir::Error, "not a uos document (root '#{uos&.name}')"
250
+ end
251
+
252
+ slots = Eval::Environment.new(schema: schema)
253
+ instances = {}
254
+ elems(uos).each do |node|
255
+ pid = instance_pid(node)
256
+ instances[pid] = if node.name == "complexEntity"
257
+ P21::ComplexRecord.new(
258
+ elems(node).map do |role|
259
+ role_record(schema, slots, role)
260
+ end,
261
+ )
262
+ else
263
+ role_record(schema, slots, node)
264
+ end
265
+ end
266
+ P21::Population.new(instances: instances)
267
+ end
268
+
269
+ def instance_pid(node)
270
+ id = attrs(node)["id"]
271
+ unless id
272
+ raise Expressir::Error,
273
+ "instance element '#{node.name}' has no id"
274
+ end
275
+
276
+ "##{id.delete_prefix('id')}"
277
+ end
278
+
279
+ def role_record(schema, slots, node)
280
+ entity = Array(schema.entities).find do |candidate|
281
+ candidate.id&.safe_downcase == node.name.downcase
282
+ end
283
+ unless entity
284
+ raise Expressir::Error,
285
+ "element '#{node.name}' does not match any entity of " \
286
+ "schema '#{schema.id}'"
287
+ end
288
+
289
+ keyword = entity.id.upcase
290
+ mapped = {}
291
+ elems(node).each do |accessor|
292
+ slot_name = accessor.name.downcase
293
+ index = slots.slots_for(entity)[slot_name]
294
+ unless index
295
+ raise Expressir::Error,
296
+ "entity '#{keyword}' has no attribute " \
297
+ "'#{accessor.name}'"
298
+ end
299
+
300
+ mapped[index] = attribute_value(schema, entity, slot_name,
301
+ accessor)
302
+ end
303
+ size = slots.slots_for(entity).size
304
+ params = Array.new(size) { |i| mapped[i] || :unset }
305
+ P21::Record.new(keyword, params)
306
+ end
307
+
308
+ # Inverse of the writer's value encodings. Logical faces come
309
+ # from the DECLARED type: the 9.5.2/9.5.4 texts only re-encode
310
+ # to the Part 21 renderings when the attribute is
311
+ # BOOLEAN/LOGICAL.
312
+ def attribute_value(schema, entity, slot_name, accessor)
313
+ declaration = find_attribute_decl(entity, slot_name)
314
+ components = elems(accessor)
315
+ if components.any?
316
+ return components.map do |component|
317
+ ref = attrs(component)["ref"]
318
+ unless ref
319
+ raise Expressir::Error,
320
+ "component '#{component.name}' has no ref"
321
+ end
322
+
323
+ "##{ref.delete_prefix('id')}"
324
+ end
325
+ end
326
+
327
+ ref = attrs(accessor)["ref"] # 9.3.3 by-reference accessor
328
+ return "##{ref.delete_prefix('id')}" if ref
329
+
330
+ text = accessor.text
331
+ case value_kind(schema, declaration)
332
+ when :boolean then P21::Enumeration.new(logical_item(text, %w[T F]))
333
+ when :logical
334
+ P21::Enumeration.new(logical_item(text, %w[T F UNKNOWN]))
335
+ when :enumeration then P21::Enumeration.new(text)
336
+ when :binary then P21::Binary.new(text)
337
+ when :number then numeric(text)
338
+ when :list then split_list(schema, declaration, text)
339
+ else text
340
+ end
341
+ end
342
+
343
+ def logical_item(text, allowed)
344
+ item = { "true" => "T", "false" => "F",
345
+ "unknown" => "UNKNOWN" }[text]
346
+ raise Expressir::Error, "invalid logical value '#{text}'" unless
347
+ allowed.include?(item)
348
+
349
+ item
350
+ end
351
+
352
+ def value_kind(schema, declaration)
353
+ type = declaration.respond_to?(:type) ? declaration.type : nil
354
+ case type
355
+ when Model::DataTypes::Boolean then :boolean
356
+ when Model::DataTypes::Logical then :logical
357
+ when Model::DataTypes::Binary then :binary
358
+ when Model::DataTypes::Enumeration then :enumeration
359
+ when Model::DataTypes::Integer, Model::DataTypes::Real,
360
+ Model::DataTypes::Number
361
+ :number
362
+ else
363
+ if type.respond_to?(:base_type) && type.base_type
364
+ return :list # LIST/SET/BAG/ARRAY over any base (9.8.1)
365
+ end
366
+
367
+ resolve_defined_type(schema, type) || :string
368
+ end
369
+ end
370
+
371
+ # EXPRESS builtins may arrive as DataTypes or as name
372
+ # references; both faces resolve here.
373
+ BUILTIN_KINDS = {
374
+ "binary" => :binary,
375
+ "boolean" => :boolean,
376
+ "logical" => :logical,
377
+ "integer" => :number,
378
+ "real" => :number,
379
+ "number" => :number,
380
+ "string" => :string,
381
+ }.freeze
382
+
383
+ # A defined TYPE's underlying kind (the writer's enumeration
384
+ # and aggregate faces ride on these).
385
+ def resolve_defined_type(schema, type)
386
+ case type
387
+ when nil then nil
388
+ when Model::DataTypes::Enumeration then :enumeration
389
+ when Model::References::SimpleReference
390
+ id = base_id_of(type)
391
+ return BUILTIN_KINDS[id&.downcase] if BUILTIN_KINDS.key?(id&.downcase)
392
+
393
+ decl = id &&
394
+ Array(schema.types).find do |candidate|
395
+ candidate.id&.safe_downcase == id
396
+ end
397
+ return nil unless decl
398
+
399
+ resolve_defined_type(schema, decl.underlying_type)
400
+ end
401
+ end
402
+
403
+ def base_id_of(type)
404
+ if type.respond_to?(:base_path) && type.base_path
405
+ type.base_path.to_s.split(".").last
406
+ elsif type.respond_to?(:id)
407
+ type.id
408
+ end
409
+ end
410
+
411
+ def numeric(text)
412
+ text.match?(/\A-?\d+\z/) ? text.to_i : text.to_f
413
+ end
414
+
415
+ # 9.8.1: whitespace LIST text; STRING components never carry
416
+ # whitespace (clause-ruled), so splitting is lossless.
417
+ def split_list(schema, declaration, text)
418
+ return [] if text.empty?
419
+
420
+ type = declaration.respond_to?(:type) ? declaration.type : nil
421
+ base = type.respond_to?(:base_type) ? type.base_type : nil
422
+ kind = resolve_defined_type(schema, base) || :string
423
+ text.split(/\s+/).map do |item|
424
+ case kind
425
+ when :enumeration then P21::Enumeration.new(item)
426
+ when :number then numeric(item)
427
+ else item
428
+ end
429
+ end
430
+ end
431
+
432
+ # Direct or inherited attribute declaration by name.
433
+ def find_attribute_decl(entity, name)
434
+ key = name.downcase
435
+ (entity.supertype_closure.to_a + [entity])
436
+ .flat_map { |decl| Array(decl.attributes) }
437
+ .find do |attr|
438
+ attr.instance_of?(Model::Declarations::Attribute) &&
439
+ attr.id&.safe_downcase == key
440
+ end
441
+ end
442
+
443
+ def elems(node)
444
+ node.children.grep(Moxml::Element)
445
+ end
446
+
447
+ def attrs(node)
448
+ node.attributes.to_h { |a| [a.name, a.value] }
449
+ end
450
+ end
451
+ end
452
+ end