lutaml-model 0.8.203 → 0.8.205

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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_guides/native-engines.adoc +10 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  11. data/lib/lutaml/model/lml/bundle.rb +171 -0
  12. data/lib/lutaml/model/lml/document.rb +46 -10
  13. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  14. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  15. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  16. data/lib/lutaml/model/lml/exporter.rb +223 -87
  17. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  18. data/lib/lutaml/model/lml/importer.rb +180 -0
  19. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  20. data/lib/lutaml/model/lml/materialization.rb +257 -0
  21. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  22. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  23. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  24. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  25. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  26. data/lib/lutaml/model/lml.rb +61 -0
  27. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  28. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  29. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  30. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  31. data/lib/lutaml/model/sequence.rb +7 -0
  32. data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
  33. data/lib/lutaml/model/serialize/initialization.rb +2 -0
  34. data/lib/lutaml/model/version.rb +1 -1
  35. data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
  36. data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
  37. data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
  38. data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
  39. data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
  40. data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
  41. data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
  42. data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
  43. data/schema/lml/document.schema.json +179 -0
  44. data/scripts/lml/export_choices.rb +57 -0
  45. data/scripts/lml/export_conformance.rb +23 -0
  46. data/scripts/lml/export_mml.rb +253 -0
  47. data/scripts/lml/export_preservation.rb +421 -0
  48. data/scripts/lml/verify_artifacts.rb +31 -0
  49. data/spec/fixtures/lml_artifacts/model.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  51. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  52. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  55. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  57. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  58. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  61. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  63. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  65. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  66. data/spec/fixtures/lml_contract/bundle.json +81 -0
  67. data/spec/fixtures/lml_contract/manifest.json +30 -0
  68. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  69. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  71. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  72. data/spec/fixtures/lml_mml/inventory.json +1 -0
  73. data/spec/fixtures/lml_mml/manifest.json +1 -0
  74. data/spec/fixtures/lml_mml/oracle.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  76. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  78. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  79. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  110. data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
  111. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  112. data/spec/support/lml_conformance.rb +292 -0
  113. data/spec/support/lml_corpus.rb +75 -0
  114. data/spec/support/lml_export_fixtures.rb +190 -0
  115. data/spec/support/lml_graph_fixtures.rb +108 -0
  116. data/spec/support/lml_import_fixtures.rb +52 -0
  117. data/spec/support/lml_mapping_fixtures.rb +170 -0
  118. data/spec/support/lml_materialization_fixtures.rb +53 -0
  119. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  120. data/spec/support/lml_order_fixtures.rb +151 -0
  121. metadata +94 -3
  122. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -20,6 +20,7 @@ module Lutaml
20
20
  document = ::Leptris::XML.parse(xml)
21
21
  root = document.root
22
22
  return nil unless root && root.name == plan[:tree][:name]
23
+ return nil if PlanHydrator.xsi_nil_below_root?(root)
23
24
 
24
25
  PlanHydrator.call(model_class, plan,
25
26
  plan[:descriptor].walk(root),
@@ -30,7 +30,11 @@ module Lutaml
30
30
  yield(@builder) if block
31
31
  end
32
32
 
33
- # Generate the schema XML string
33
+ # Generate the schema XML string in the Nokogiri wrapper's byte
34
+ # shape: leptris indents only when indent_text rides along with
35
+ # indent, and the moxml backends expand empty elements (<x></x>)
36
+ # unless told otherwise. Generated schemas are artifacts consumers
37
+ # diff and commit, so the spelling must not follow the engine.
34
38
  # @param options [Hash] formatting options
35
39
  # @option options [Boolean] :pretty Pretty print with indentation
36
40
  # @option options [Integer] :indent Number of spaces (default: 2)
@@ -39,9 +43,9 @@ module Lutaml
39
43
  doc = @builder.document
40
44
  if options[:pretty]
41
45
  indent = options[:indent] || 2
42
- doc.to_xml(indent: indent)
46
+ doc.to_xml(indent: indent, indent_text: " ", expand_empty: false)
43
47
  else
44
- doc.to_xml
48
+ doc.to_xml(expand_empty: false)
45
49
  end
46
50
  end
47
51
  end
@@ -173,6 +173,10 @@ module Lutaml
173
173
  collection_element_count(value, rule)
174
174
  end
175
175
 
176
+ # Public: the plan lanes' coverage pre-check measures a parsed
177
+ # order against the same count before deciding to reconcile.
178
+ public :expected_element_count
179
+
176
180
  def collection_element_count(value, rule)
177
181
  # Mirror the applier's own dispatch. A value it will not iterate
178
182
  # — nil, or a String — never reaches process_collection_item; it
@@ -188,13 +188,12 @@ model_class, register_id)
188
188
  end
189
189
  end
190
190
 
191
- private
192
-
193
- # Whether an element_order entry is something the ordered path can
194
- # interpret. element_order is a plain public accessor, so arrays
195
- # holding foreign objects (Integers, strings, ...) exist in the
196
- # wild. Anything without #type cannot be matched to a rule or
197
- # emitted, and must be skipped rather than crash serialization.
191
+ # Whether an element_order entry is something the ordered path
192
+ # can interpret. element_order is a plain public accessor, so
193
+ # arrays holding foreign objects (Integers, strings, ...) exist
194
+ # in the wild. Anything without #type cannot be matched to a
195
+ # rule or emitted, and must be skipped rather than crash
196
+ # serialization. Shared with the plan lanes' order walks.
198
197
  #
199
198
  # @param object [Object] An entry from element_order
200
199
  # @return [Boolean] true when the entry is a usable order entry
@@ -202,6 +201,8 @@ model_class, register_id)
202
201
  object.respond_to?(:type)
203
202
  end
204
203
 
204
+ private
205
+
205
206
  # Match by name AND namespace when object has a namespace URI.
206
207
  # Falls back to name-only match for backward compatibility.
207
208
  # Also checks uri_aliases for namespace alias support (e.g., ReqIF versions).
@@ -0,0 +1,179 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/lutaml/lutaml-model/schema/lml/document.schema.json",
4
+ "title": "LML document, bundle and manifest (lutaml-model#1033)",
5
+ "description": "Structural schema for the agreed contract. Cross-reference rules (dangling references, duplicate IDs, discriminator targets, capability coverage, sorted arrays) are enforced by Lutaml::Model::Lml::DocumentValidator and reported with the named codes in $defs.errorCodes.",
6
+ "oneOf": [
7
+ {"$ref": "#/$defs/documentV1"},
8
+ {"$ref": "#/$defs/documentV2"},
9
+ {"$ref": "#/$defs/bundle"},
10
+ {"$ref": "#/$defs/manifest"}
11
+ ],
12
+ "$defs": {
13
+ "errorCodes": {
14
+ "enum": ["unsupported_version", "invalid_document", "duplicate_id", "dangling_reference", "unknown_type", "unsupported_type", "missing_type_binding", "unsupported_format", "unsupported_mapping", "non_serializable_default", "lambda_default", "custom_initialize", "imperative_element_order", "invalid_ordered_content", "reserved_member", "nonterminating_rational", "unknown_discriminator", "round_trip_mismatch", "checksum_mismatch", "capability_missing", "not_serializable"]
15
+ },
16
+ "sha256": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
17
+ "builtinType": {
18
+ "enum": ["string", "token", "uri", "email", "hostname", "ip", "boolean", "integer", "decimal", "float", "rational", "date", "time", "date_time", "time_without_date", "duration", "qname", "base64_binary", "hex_binary", "symbol", "hash", "email_address", "ip_v4_address", "ip_v6_address", "uuid", "reference", "uri_reference", "date_with_timezone", "date_time_with_timezone", "day_time_duration", "year_month_duration", "positive_integer", "non_negative_integer", "g_year", "g_year_month", "g_month", "g_month_day", "g_day", "idrefs", "nmtokens"]
19
+ },
20
+ "formats": {"enum": ["xml", "json", "yaml", "toml"]},
21
+ "documentV1": {
22
+ "type": "object",
23
+ "required": ["lml", "model"],
24
+ "properties": {
25
+ "lml": {"const": 1},
26
+ "model": {"type": "string", "minLength": 1},
27
+ "register": {"type": ["string", "null"]},
28
+ "attributes": {"type": "array", "items": {"type": "object"}},
29
+ "mappings": {"type": "object"}
30
+ }
31
+ },
32
+ "typeRef": {
33
+ "oneOf": [
34
+ {"type": "object", "required": ["kind", "name"], "additionalProperties": false,
35
+ "properties": {"kind": {"const": "builtin"}, "name": {"$ref": "#/$defs/builtinType"}}},
36
+ {"type": "object", "required": ["kind", "id"], "additionalProperties": false,
37
+ "properties": {"kind": {"enum": ["model", "value"]}, "id": {"type": "string", "minLength": 1}}}
38
+ ]
39
+ },
40
+ "namespace": {
41
+ "type": "object", "required": ["mode"],
42
+ "properties": {
43
+ "mode": {"enum": ["inherit", "unqualified", "uri"]},
44
+ "uri": {"type": "string", "minLength": 1},
45
+ "prefix": {"type": "string"}
46
+ },
47
+ "additionalProperties": false
48
+ },
49
+ "discriminator": {
50
+ "type": "object", "required": ["attribute", "cases"], "additionalProperties": false,
51
+ "properties": {
52
+ "attribute": {"type": "string", "minLength": 1},
53
+ "cases": {"type": "object", "minProperties": 1, "additionalProperties": {"type": "string", "minLength": 1}}
54
+ }
55
+ },
56
+ "attribute": {
57
+ "type": "object", "required": ["name", "type"], "additionalProperties": false,
58
+ "properties": {
59
+ "name": {"type": "string", "minLength": 1, "not": {"const": "__lml_content"}},
60
+ "type": {"$ref": "#/$defs/typeRef"},
61
+ "required": {"type": "boolean"},
62
+ "collection": {
63
+ "oneOf": [
64
+ {"const": false},
65
+ {"type": "object", "required": ["min", "max"], "additionalProperties": false,
66
+ "properties": {"min": {"type": "integer", "minimum": 0}, "max": {"type": ["integer", "null"]}, "class": {"type": "string", "minLength": 1}}}
67
+ ]
68
+ },
69
+ "default": {
70
+ "oneOf": [
71
+ {"type": "object", "required": ["present"], "additionalProperties": false, "properties": {"present": {"const": false}}},
72
+ {"type": "object", "required": ["present", "value"], "additionalProperties": false, "properties": {"present": {"const": true}, "value": {}}}
73
+ ]
74
+ },
75
+ "initialize_empty": {"type": "boolean"},
76
+ "values": {"type": "array"},
77
+ "discriminator": {"$ref": "#/$defs/discriminator"}
78
+ }
79
+ },
80
+ "rule": {
81
+ "type": "object", "additionalProperties": false,
82
+ "properties": {
83
+ "kind": {"enum": ["attribute", "element", "content", "any_attribute", "sequence", "choice"]},
84
+ "name": {"type": "array", "items": {"type": "string"}},
85
+ "key": {"type": "array", "items": {"type": "string"}},
86
+ "to": {"type": "string"},
87
+ "namespace": {"$ref": "#/$defs/namespace"},
88
+ "prefix": {"type": "string"},
89
+ "cdata": {"type": "boolean"},
90
+ "render": {"type": "object", "additionalProperties": false,
91
+ "properties": {"nil": {"type": ["string", "boolean"]}, "empty": {"type": ["string", "boolean"]}, "default": {"type": ["string", "boolean"]}}},
92
+ "discriminator": {"$ref": "#/$defs/discriminator"},
93
+ "members": {"type": "array"},
94
+ "min": {"type": "integer", "minimum": 0},
95
+ "max": {"type": ["integer", "null"]},
96
+ "child_mappings": {},
97
+ "root_mappings": {},
98
+ "polymorphic_map": {"type": "object", "additionalProperties": {"type": "string"}}
99
+ }
100
+ },
101
+ "compositor": {
102
+ "type": "object", "additionalProperties": false, "required": ["kind", "members"],
103
+ "properties": {
104
+ "kind": {"enum": ["sequence", "choice"]},
105
+ "min": {"type": "integer", "minimum": 0},
106
+ "max": {"type": ["integer", "null"]},
107
+ "members": {"type": "array", "items": {"oneOf": [{"$ref": "#/$defs/compositor"}, {"$ref": "#/$defs/rule"}]}}
108
+ }
109
+ },
110
+ "mapping": {
111
+ "type": "object", "additionalProperties": false,
112
+ "properties": {
113
+ "root": {},
114
+ "namespace": {"$ref": "#/$defs/namespace"},
115
+ "rules": {"type": "array", "items": {"$ref": "#/$defs/rule"}},
116
+ "ordered": {"type": "boolean"},
117
+ "mixed_content": {"type": "boolean"},
118
+ "sequence": {"type": "array", "items": {"$ref": "#/$defs/compositor"}},
119
+ "root_mappings": {},
120
+ "child_mappings": {}
121
+ }
122
+ },
123
+ "documentV2": {
124
+ "type": "object", "additionalProperties": false,
125
+ "required": ["lml", "model", "register", "attributes"],
126
+ "properties": {
127
+ "lml": {"const": 2},
128
+ "model": {"type": "string", "minLength": 1},
129
+ "register": {"type": "string", "pattern": "^[A-Za-z0-9_.:-]+$"},
130
+ "attributes": {"type": "array", "items": {"$ref": "#/$defs/attribute"}},
131
+ "mappings": {"type": "object", "propertyNames": {"$ref": "#/$defs/formats"}, "additionalProperties": {"$ref": "#/$defs/mapping"}},
132
+ "ordered_content": {
133
+ "type": "object", "required": ["member", "version"], "additionalProperties": false,
134
+ "properties": {"member": {"const": "__lml_content"}, "version": {"const": 1}}
135
+ }
136
+ }
137
+ },
138
+ "bundle": {
139
+ "type": "object", "additionalProperties": false,
140
+ "required": ["lml_bundle", "roots", "models", "value_types", "capabilities"],
141
+ "properties": {
142
+ "lml_bundle": {"const": 1},
143
+ "roots": {"type": "array", "uniqueItems": true, "items": {"type": "string"}},
144
+ "models": {"type": "object", "additionalProperties": {"$ref": "#/$defs/documentV2"}},
145
+ "value_types": {
146
+ "type": "object",
147
+ "additionalProperties": {
148
+ "type": "object", "required": ["base", "facets"], "additionalProperties": false,
149
+ "properties": {"base": {"$ref": "#/$defs/builtinType"}, "facets": {"type": "object"}}
150
+ }
151
+ },
152
+ "capabilities": {"type": "array", "uniqueItems": true, "items": {"type": "string"}},
153
+ "provenance": {"type": "object"}
154
+ }
155
+ },
156
+ "pinned": {
157
+ "type": "object", "required": ["sha256"],
158
+ "properties": {"sha256": {"$ref": "#/$defs/sha256"}}
159
+ },
160
+ "manifest": {
161
+ "type": "object", "additionalProperties": false,
162
+ "required": ["manifest", "lml_checksum", "schema_version", "generator_version", "target", "capabilities", "corpora", "files", "dependencies"],
163
+ "properties": {
164
+ "manifest": {"const": 1},
165
+ "lml_checksum": {"$ref": "#/$defs/sha256"},
166
+ "schema_version": {"const": 2},
167
+ "generator_version": {"type": "string", "minLength": 1},
168
+ "target": {"enum": ["ruby", "ts", "rust", "wasm"]},
169
+ "capabilities": {"type": "array", "uniqueItems": true, "items": {"type": "string"}},
170
+ "corpora": {"type": "array", "items": {"type": "object", "required": ["generation", "sha256"], "additionalProperties": false,
171
+ "properties": {"generation": {}, "sha256": {"$ref": "#/$defs/sha256"}}}},
172
+ "files": {"type": "array", "items": {"type": "object", "required": ["path", "sha256"], "additionalProperties": false,
173
+ "properties": {"path": {"type": "string", "pattern": "^(?!/)(?!.*(^|/)\\.\\.?(/|$)).+"}, "sha256": {"$ref": "#/$defs/sha256"}}}},
174
+ "dependencies": {"type": "array", "items": {"type": "object", "required": ["name", "revision", "sha256"], "additionalProperties": false,
175
+ "properties": {"name": {"type": "string"}, "revision": {"type": "string"}, "sha256": {"$ref": "#/$defs/sha256"}}}}
176
+ }
177
+ }
178
+ }
179
+ }
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ruby -Ilib scripts/lml/export_choices.rb OUTPUT [Plain Nested ...]
4
+ # Omitting names exports every source fixture, including eager field failures.
5
+ require "json"
6
+ require "digest"
7
+ require_relative "../../spec/support/lml_model_choices_fixtures"
8
+
9
+ Lutaml::Model::Config.xml_adapter_type = :nokogiri
10
+ output = ARGV.shift || abort("usage: export_choices.rb OUTPUT [fixture names]")
11
+ selected = ARGV.empty? ? LmlModelChoicesFixtures::CASES.keys : ARGV
12
+ sources = selected.to_h do |name|
13
+ [name, LmlModelChoicesFixtures.const_get(name, false)]
14
+ end
15
+ codes = lambda do |value|
16
+ value.validate.map do |error|
17
+ case error
18
+ when Lutaml::Model::ChoiceLowerBoundError then "choice_lower_bound"
19
+ when Lutaml::Model::ChoiceUpperBoundError then "choice_upper_bound"
20
+ else error.class.name
21
+ end
22
+ end
23
+ end
24
+ phase = lambda do |&block|
25
+ codes.call(block.call)
26
+ rescue Lutaml::Model::CollectionCountOutOfRangeError
27
+ ["eager_collection_count_out_of_range"]
28
+ end
29
+ rows = sources.flat_map do |name, klass|
30
+ LmlModelChoicesFixtures::CASES.fetch(name).each_with_index.map do |input, index|
31
+ value = klass.new(input)
32
+ json = value.to_json
33
+ xml = value.to_xml
34
+ {
35
+ id: "#{name}-#{index}", model_id: "#{klass.name}@default", input: input,
36
+ expected: {
37
+ construct: codes.call(value),
38
+ json_input: phase.call { klass.from_json(JSON.generate(input)) },
39
+ json_wire: JSON.parse(json),
40
+ json_roundtrip: phase.call { klass.from_json(json) },
41
+ xml_wire: xml,
42
+ xml_roundtrip: phase.call { klass.from_xml(xml) },
43
+ }
44
+ }
45
+ end
46
+ end
47
+ root = File.expand_path("../..", __dir__)
48
+ producer_files = %w[lib/lutaml/model/choice.rb lib/lutaml/model/validation.rb
49
+ spec/support/lml_model_choices_fixtures.rb]
50
+ producer = producer_files.to_h { |file| [file, Digest::SHA256.file(File.join(root, file)).hexdigest] }
51
+ report = {
52
+ source_semantics: "native Choice/Validation and format-specific I/O; group validation unchanged",
53
+ ruby: RUBY_VERSION, producer: producer,
54
+ bundle: Lutaml::Model::Lml.export_bundle(sources.values).to_h, cases: rows
55
+ }
56
+ File.write(output, "#{JSON.pretty_generate(report)}\n")
57
+ puts JSON.generate(models: report[:bundle].fetch("models").length, cases: rows.length)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "fileutils"
5
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
6
+ require_relative "../../spec/support/lml_conformance"
7
+
8
+ options = { check: false, package: File.expand_path("../../spec/fixtures/lml_conformance/core", __dir__) }
9
+ OptionParser.new do |parser|
10
+ parser.banner = "Usage: ruby scripts/lml/export_conformance.rb [--check] [--package DIRECTORY]"
11
+ parser.on("--check", "Check pinned files without rewriting") { options[:check] = true }
12
+ parser.on("--package DIRECTORY", "Explicit core package directory") { |path| options[:package] = path }
13
+ end.parse!
14
+
15
+ unless options[:check]
16
+ FileUtils.mkdir_p(options[:package])
17
+ LmlConformance.files.each do |path, bytes|
18
+ File.binwrite(File.join(options[:package], path), bytes)
19
+ end
20
+ end
21
+ report = LmlConformance.check(options[:package])
22
+ puts LmlConformance.json(report)
23
+ exit(report.fetch("errors").empty? ? 0 : 1)
@@ -0,0 +1,253 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "export_preservation"
4
+
5
+ # Real source-model oracles; the original MML classes are loaded only on request.
6
+ module LmlMml
7
+ SOURCE_REVISION = "d826d80f6b1477569c8320ccd945268a6ce1062f"
8
+ SOURCE_VERSION = "2.4.7"
9
+ SOURCE_SPECS = {
10
+ "ordered_content_spec.rb" => "sha256:b776a2a40a2ad8fcfdeb0363571b8638437868cdcb169492d9a61d3e036235ab",
11
+ "v3_spec.rb" => "sha256:0221940af4e7aae3226885bd789ae66a632b500ae77fef77c289ecfff4ab9ac9",
12
+ "v4_spec.rb" => "sha256:4fa8b613b26c7d1d435309df5d1b76fae727aad232f859d38a974e69bdfbe201",
13
+ }.freeze
14
+ SOURCE_DIGEST = "sha256:271bd0ed0256a02784b35aaa99e7f0aaf0ba11abc64ed974d24025375b4f9829"
15
+ FRAMEWORK_REVISION = "4e75c92b7d9ab3e3aa5f4f90af4a9ce860c76f72"
16
+ PRESERVATION_REVISION = "9716740b6b7feb755b8557a2bfab89d066e855bd"
17
+ NAMESPACE = "http://www.w3.org/1998/Math/MathML"
18
+ PACKAGE = File.expand_path("../../spec/fixtures/lml_mml", __dir__)
19
+
20
+ module_function
21
+
22
+ def source_root
23
+ ENV["LML_MML_ROOT"] || Gem.path.map { |path| File.join(path, "gems", "mml-#{SOURCE_VERSION}") }
24
+ .find { |path| File.directory?(path) } || raise(ArgumentError, "set LML_MML_ROOT to the pinned mml source")
25
+ end
26
+
27
+ def source_digest(root)
28
+ digest = Digest::SHA256.new
29
+ Dir.glob(File.join(root, "lib/**/*.rb"), sort: false).sort.each do |path|
30
+ digest.update("#{path.delete_prefix("#{root}/")}\0#{File.binread(path)}\0")
31
+ end
32
+ "sha256:#{digest.hexdigest}"
33
+ end
34
+
35
+ def load_source(root = source_root)
36
+ root = File.expand_path(root)
37
+ unless source_digest(root) == SOURCE_DIGEST
38
+ raise ArgumentError, "mml source digest differs from #{SOURCE_REVISION} (#{SOURCE_VERSION})"
39
+ end
40
+
41
+ $LOAD_PATH.unshift(File.join(root, "lib"))
42
+ require "mml"
43
+ require "mml/version"
44
+ raise ArgumentError, "expected mml #{SOURCE_VERSION}" unless Mml::VERSION == SOURCE_VERSION
45
+
46
+ [Mml::V2, Mml::V3, Mml::V4].each do |version|
47
+ configuration = version::Configuration
48
+ configuration.populate_context! unless configuration.context
49
+ end
50
+ loaded = $LOADED_FEATURES.find { |path| path.end_with?("/mml.rb") }
51
+ unless loaded == File.join(root, "lib/mml.rb")
52
+ raise ArgumentError, "mml was already loaded from a different source"
53
+ end
54
+
55
+ root
56
+ end
57
+
58
+ def cases
59
+ mixed = %(<mi xmlns="#{NAMESPACE}" mathvariant="bold">x<malignmark/>y<mglyph src="g.png" alt="g"/>z</mi>)
60
+ nested = %(<math xmlns="#{NAMESPACE}" display="block"><mrow><mi>a</mi><mo>+</mo><mfrac><mi>x<malignmark/>y</mi><mrow><mn>1</mn><mo>+</mo><mn>2</mn></mrow></mfrac><mi>b</mi></mrow><mi>c</mi></math>)
61
+ [3, 4].flat_map do |version|
62
+ [["v#{version}-mi-mixed", "Mi", mixed], ["v#{version}-math-nested", "Math", nested]].map do |id, name, input|
63
+ { "id" => id, "model_id" => "Mml::V#{version}::#{name}@mml_v#{version}",
64
+ "register" => "mml_v#{version}", "input" => input }
65
+ end
66
+ end
67
+ end
68
+
69
+ def normalize(value, register, identities = {})
70
+ case value
71
+ when Lutaml::Model::UninitializedClass then { "state" => "absent" }
72
+ when Array then value.map { |item| normalize(item, register, identities) }
73
+ when Lutaml::Model::Serialize
74
+ klass = value.class
75
+ model_id = identities.fetch(klass) { "#{klass.name}@#{register}" }
76
+ attributes = klass.attributes(register).to_h do |name, _attribute|
77
+ [name.to_s, normalize(value.public_send(name), register, identities)]
78
+ end
79
+ descriptor = Lutaml::Model::Lml::OrderedContent.descriptor_for(
80
+ klass, mapping: klass.mappings_for(:xml, register), register: register
81
+ )
82
+ attributes["__lml_content"] = Lutaml::Model::Lml::OrderedContent.capture(descriptor, value) if descriptor
83
+ attributes.merge("__lml_model" => model_id)
84
+ when Integer then value.to_s
85
+ else value
86
+ end
87
+ end
88
+
89
+ def replay(entry, klass, identities = {})
90
+ register = entry.fetch("register").to_sym
91
+ Lutaml::Model::Config.with_adapter(xml: :nokogiri) do
92
+ record = klass.from_xml(entry.fetch("input"), register: register)
93
+ output = record.to_xml(register: register)
94
+ { "data" => normalize(record, register, identities), "output" => output,
95
+ "tree" => LmlPreservation.xml_tree(output) }
96
+ end
97
+ end
98
+
99
+ def refusal(error)
100
+ { "reason" => error.reason.to_s, "model" => error.klass.to_s,
101
+ "detail" => error.detail, "location" => error.location }.compact
102
+ end
103
+
104
+ def inventory
105
+ [Mml::V2, Mml::V3, Mml::V4].map do |version|
106
+ register = version::Configuration.context_id
107
+ registry = version::Configuration.context.registry
108
+ classes = registry.names.map { |name| registry.lookup(name) }.uniq.sort_by(&:name)
109
+ declarations = classes.map do |klass|
110
+ mapping = klass.mappings_for(:xml, register)
111
+ source = { "model" => klass.name, "source_ordered" => mapping.ordered?,
112
+ "source_mixed_content" => mapping.mixed_content?,
113
+ "source_ordered_content" => !!(mapping.ordered? || mapping.mixed_content?),
114
+ "float_attributes" => klass.attributes(register).values.select { |attribute| attribute.unresolved_type == :float }.map { |attribute| attribute.name.to_s } }
115
+ begin
116
+ document = Lutaml::Model::Lml::Exporter.project(klass, register: register)
117
+ source.merge("ordered_content" => !!document["ordered_content"])
118
+ rescue Lutaml::Model::Lml::RejectedError => e
119
+ source.merge(refusal(e))
120
+ end
121
+ end
122
+ { "version" => version.name, "register" => register.to_s,
123
+ "registered_count" => classes.size, "exported_count" => declarations.count { |entry| !entry.key?("reason") },
124
+ "source_ordered_count" => declarations.count { |entry| entry["source_ordered_content"] },
125
+ "exported_ordered_count" => declarations.count { |entry| entry["ordered_content"] },
126
+ "float_ordered_count" => declarations.count do |entry|
127
+ entry["ordered_content"] && entry["float_attributes"].any?
128
+ end,
129
+ "declarations" => declarations }
130
+ end
131
+ end
132
+
133
+ def source_oracles
134
+ cases.map do |entry|
135
+ klass = Object.const_get(entry.fetch("model_id").split("@").first)
136
+ entry.merge("expected" => replay(entry, klass))
137
+ end
138
+ end
139
+
140
+ def source_graph(root, register)
141
+ seen = {}
142
+ queue = [root]
143
+ until queue.empty?
144
+ klass = queue.shift
145
+ next if seen.key?(klass)
146
+
147
+ children = klass.attributes(register).values.filter_map do |attribute|
148
+ type = attribute.type(register)
149
+ type if type.is_a?(Class) && type.include?(Lutaml::Model::Serialize)
150
+ end.uniq
151
+ seen[klass] = children
152
+ queue.concat(children)
153
+ end
154
+ seen.to_h do |klass, children|
155
+ ["#{klass.name}@#{register}", children.map { |child| "#{child.name}@#{register}" }.sort]
156
+ end.sort.to_h
157
+ end
158
+
159
+ def graph_exports
160
+ cases.to_h do |entry|
161
+ klass = Object.const_get(entry.fetch("model_id").split("@").first)
162
+ register = entry.fetch("register").to_sym
163
+ references = source_graph(klass, register)
164
+ bundle = Lutaml::Model::Lml.export_bundle([klass], register: register)
165
+ [entry.fetch("id"), { "bundle" => bundle.to_h, "checksum" => bundle.checksum,
166
+ "model_count" => bundle.models.size, "source_references" => references }]
167
+ rescue Lutaml::Model::Lml::RejectedError => e
168
+ [entry.fetch("id"), { "refusal" => refusal(e), "source_references" => references }]
169
+ end
170
+ end
171
+
172
+ def existing_exclusions(root, versions)
173
+ { "reported_historical_ordered_refusals" => 191, "historical_ordered_model_count_matched" => versions.sum { |version| version.fetch("source_ordered_count") } == 191,
174
+ "source_specs" => SOURCE_SPECS.map do |name, digest|
175
+ path = File.join(root, "spec/mml", name)
176
+ if File.file?(path) && LmlPreservation.digest(File.binread(path)) != digest
177
+ raise ArgumentError, "mml source expectation differs: #{name}"
178
+ end
179
+
180
+ { "path" => "spec/mml/#{name}", "sha256" => digest }
181
+ end,
182
+ "v3" => { "existing_filters" => ["share requires XInclude", "HTML span", "mathml entity", "foreign background/color attributes", "XML comments", "ErrorHandling"],
183
+ "negative_tests_pending" => "Parser does not validate MathML schema compliance",
184
+ "available_fixture_count" => Dir.glob(File.join(root, "spec/fixtures/mml3-testsuite/Math/testsuite/build/main/**/*.mml")).size },
185
+ "v4" => { "existing_filters" => ["HTML elements", "unknown namespace elements", "nonempty none/mprescripts/mspace", "mtd outside table", "br inside mtext"],
186
+ "available_fixture_count" => Dir.glob(File.join(root, "spec/fixtures/mmlcore-testsuite/mathml/**/*.mml")).size } }
187
+ end
188
+
189
+ def files(root = source_root)
190
+ root = load_source(root)
191
+ exports = graph_exports
192
+ versions = inventory
193
+ payloads = {
194
+ "oracle.json" => LmlPreservation.json("cases" => source_oracles),
195
+ "inventory.json" => LmlPreservation.json("versions" => versions, "existing_exclusions" => existing_exclusions(root, versions)),
196
+ }
197
+ exports.each do |id, result|
198
+ next unless result.key?("bundle")
199
+
200
+ payloads["#{id}.json"] = LmlPreservation.json(result.fetch("bundle"))
201
+ payloads["#{id}.yaml"] = YAML.safe_dump(LmlPreservation.canonical(result.fetch("bundle")))
202
+ end
203
+ manifest = {
204
+ "generation" => "mml-pilot-v1", "source_revision" => SOURCE_REVISION, "source_version" => SOURCE_VERSION,
205
+ "source_digest" => SOURCE_DIGEST, "framework_revision" => FRAMEWORK_REVISION,
206
+ "preservation_revision" => PRESERVATION_REVISION, "case_count" => cases.size,
207
+ "graphs" => exports.transform_values { |result| result.except("bundle") },
208
+ "files" => payloads.sort.map { |path, bytes| { "path" => path, "sha256" => LmlPreservation.digest(bytes) } }
209
+ }
210
+ payloads.merge("manifest.json" => LmlPreservation.json(manifest))
211
+ end
212
+
213
+ # Package drift is always an error. A refused closed graph is reported as
214
+ # "refused" and becomes an error only with require_complete.
215
+ def check(directory, payloads, require_complete: false)
216
+ errors = payloads.filter_map do |path, bytes|
217
+ full_path = File.join(directory, path)
218
+ { "id" => path, "error" => "missing or changed file" } unless File.file?(full_path) && File.binread(full_path) == bytes.b
219
+ end
220
+ (Dir.children(directory) - payloads.keys).each { |path| errors << { "id" => path, "error" => "unknown file" } }
221
+ manifest = JSON.parse(payloads.fetch("manifest.json"))
222
+ refused = manifest.fetch("graphs").select { |_id, graph| graph.key?("refusal") }
223
+ refused.each do |id, graph|
224
+ errors << { "id" => id, "error" => "closed graph refused", "refusal" => graph.fetch("refusal") } if require_complete
225
+ end
226
+ { "expected_count" => cases.size, "exported_count" => manifest.fetch("graphs").count { |_id, graph| graph.key?("checksum") },
227
+ "refused" => refused.transform_values { |graph| graph.fetch("refusal") }, "errors" => errors }
228
+ end
229
+
230
+ def main(arguments = ARGV)
231
+ options = { check: false, complete: false, package: PACKAGE, root: nil }
232
+ OptionParser.new do |parser|
233
+ parser.on("--check") { options[:check] = true }
234
+ parser.on("--require-complete") { options[:complete] = true }
235
+ parser.on("--package DIRECTORY") { |path| options[:package] = path }
236
+ parser.on("--mml-root DIRECTORY") { |path| options[:root] = path }
237
+ end.parse!(arguments)
238
+ payloads = files(options[:root] || source_root)
239
+ unless options[:check]
240
+ FileUtils.mkdir_p(options[:package])
241
+ payloads.each { |path, bytes| File.binwrite(File.join(options[:package], path), bytes) }
242
+ end
243
+ # The large payloads are not carried in the working tree; a check
244
+ # reads them from the corpus first (see spec/support/lml_corpus.rb).
245
+ require_relative "../../spec/support/lml_corpus"
246
+ LmlCorpus.materialize!(options[:package]) if options[:check]
247
+ report = check(options[:package], payloads, require_complete: options[:complete])
248
+ puts LmlPreservation.json(report)
249
+ report.fetch("errors").empty? ? 0 : 1
250
+ end
251
+ end
252
+
253
+ exit LmlMml.main if $PROGRAM_NAME == __FILE__