lutaml-model 0.8.186 → 0.8.188

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 (308) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependent-repos-tools-informational.json +8 -0
  3. data/.github/workflows/dependent-repos-tools.json +1 -5
  4. data/.github/workflows/dependent-tests.yml +26 -2
  5. data/Gemfile +2 -0
  6. data/Rakefile +6 -0
  7. data/exe/lutaml-lml-lsp +11 -0
  8. data/lib/compat/opal/generate_boot.rb +12 -5
  9. data/lib/compat/opal/lutaml_model_boot.rb +96 -4
  10. data/lib/lutaml/lml/association_label_resolver.rb +52 -0
  11. data/lib/lutaml/lml/data_processor/attribute_processing.rb +133 -0
  12. data/lib/lutaml/lml/data_processor/collection_processing.rb +37 -0
  13. data/lib/lutaml/lml/data_processor/instance_processing.rb +77 -0
  14. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +70 -0
  15. data/lib/lutaml/lml/data_processor/value_processing.rb +103 -0
  16. data/lib/lutaml/lml/data_processor/view_processing.rb +26 -0
  17. data/lib/lutaml/lml/data_processor.rb +66 -0
  18. data/lib/lutaml/lml/diagnostic.rb +149 -0
  19. data/lib/lutaml/lml/document_builder.rb +486 -0
  20. data/lib/lutaml/lml/entity_types.rb +31 -0
  21. data/lib/lutaml/lml/executor/adapter_helpers.rb +45 -0
  22. data/lib/lutaml/lml/executor/condition_evaluator.rb +175 -0
  23. data/lib/lutaml/lml/executor/csv_adapter.rb +89 -0
  24. data/lib/lutaml/lml/executor/format_adapter.rb +54 -0
  25. data/lib/lutaml/lml/executor/xml_adapter.rb +102 -0
  26. data/lib/lutaml/lml/executor.rb +96 -0
  27. data/lib/lutaml/lml/format/adapter/document.rb +11 -0
  28. data/lib/lutaml/lml/format/adapter/mapping.rb +31 -0
  29. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +164 -0
  30. data/lib/lutaml/lml/format/adapter/transform.rb +11 -0
  31. data/lib/lutaml/lml/format.rb +34 -0
  32. data/lib/lutaml/lml/formatter/base.rb +81 -0
  33. data/lib/lutaml/lml/formatter/canonical.rb +738 -0
  34. data/lib/lutaml/lml/formatter.rb +11 -0
  35. data/lib/lutaml/lml/grammar/lml.artifact.json +14993 -0
  36. data/lib/lutaml/lml/grammar/lml.parg +389 -0
  37. data/lib/lutaml/lml/grammar.rb +60 -0
  38. data/lib/lutaml/lml/has_attributes.rb +14 -0
  39. data/lib/lutaml/lml/import_resolver.rb +96 -0
  40. data/lib/lutaml/lml/layout/engine.rb +17 -0
  41. data/lib/lutaml/lml/layout.rb +9 -0
  42. data/lib/lutaml/lml/literal_value.rb +32 -0
  43. data/lib/lutaml/lml/lsp/conversions.rb +78 -0
  44. data/lib/lutaml/lml/lsp/keywords.rb +43 -0
  45. data/lib/lutaml/lml/lsp/server.rb +380 -0
  46. data/lib/lutaml/lml/lsp/store.rb +68 -0
  47. data/lib/lutaml/lml/lsp/transport.rb +53 -0
  48. data/lib/lutaml/lml/lsp.rb +16 -0
  49. data/lib/lutaml/lml/model_compiler/enums.rb +170 -0
  50. data/lib/lutaml/lml/model_compiler/hydration.rb +122 -0
  51. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +188 -0
  52. data/lib/lutaml/lml/model_compiler/string_formats.rb +42 -0
  53. data/lib/lutaml/lml/model_compiler/validation.rb +65 -0
  54. data/lib/lutaml/lml/model_compiler.rb +329 -0
  55. data/lib/lutaml/lml/models/action.rb +10 -0
  56. data/lib/lutaml/lml/models/association.rb +31 -0
  57. data/lib/lutaml/lml/models/cardinality.rb +10 -0
  58. data/lib/lutaml/lml/models/collection.rb +11 -0
  59. data/lib/lutaml/lml/models/constraint.rb +25 -0
  60. data/lib/lutaml/lml/models/data_type.rb +43 -0
  61. data/lib/lutaml/lml/models/derived_field.rb +14 -0
  62. data/lib/lutaml/lml/models/diagram.rb +22 -0
  63. data/lib/lutaml/lml/models/document.rb +86 -0
  64. data/lib/lutaml/lml/models/enum.rb +40 -0
  65. data/lib/lutaml/lml/models/fidelity.rb +10 -0
  66. data/lib/lutaml/lml/models/group.rb +11 -0
  67. data/lib/lutaml/lml/models/instance.rb +28 -0
  68. data/lib/lutaml/lml/models/instance_collection.rb +18 -0
  69. data/lib/lutaml/lml/models/instances_export.rb +10 -0
  70. data/lib/lutaml/lml/models/instances_import.rb +11 -0
  71. data/lib/lutaml/lml/models/mapping.rb +15 -0
  72. data/lib/lutaml/lml/models/mapping_rule.rb +16 -0
  73. data/lib/lutaml/lml/models/member_field.rb +16 -0
  74. data/lib/lutaml/lml/models/namespace.rb +18 -0
  75. data/lib/lutaml/lml/models/operation.rb +28 -0
  76. data/lib/lutaml/lml/models/operation_parameter.rb +12 -0
  77. data/lib/lutaml/lml/models/package.rb +28 -0
  78. data/lib/lutaml/lml/models/primitive_type.rb +35 -0
  79. data/lib/lutaml/lml/models/reference.rb +23 -0
  80. data/lib/lutaml/lml/models/serialization_mapping.rb +21 -0
  81. data/lib/lutaml/lml/models/string_format.rb +16 -0
  82. data/lib/lutaml/lml/models/top_element_attribute.rb +118 -0
  83. data/lib/lutaml/lml/models/uml_class.rb +87 -0
  84. data/lib/lutaml/lml/models/value.rb +17 -0
  85. data/lib/lutaml/lml/models/view_filter.rb +11 -0
  86. data/lib/lutaml/lml/models/view_import.rb +11 -0
  87. data/lib/lutaml/lml/parser.rb +19 -0
  88. data/lib/lutaml/lml/pipeline.rb +73 -0
  89. data/lib/lutaml/lml/positioned.rb +13 -0
  90. data/lib/lutaml/lml/preprocessor.rb +132 -0
  91. data/lib/lutaml/lml/quoting.rb +60 -0
  92. data/lib/lutaml/lml/source.rb +49 -0
  93. data/lib/lutaml/lml/source_position.rb +20 -0
  94. data/lib/lutaml/lml/symbol_index.rb +202 -0
  95. data/lib/lutaml/lml/transform.rb +119 -0
  96. data/lib/lutaml/lml/types/text_type.rb +28 -0
  97. data/lib/lutaml/lml/types.rb +10 -0
  98. data/lib/lutaml/lml/validator.rb +460 -0
  99. data/lib/lutaml/lml/view_resolution.rb +32 -0
  100. data/lib/lutaml/lml/view_resolver.rb +48 -0
  101. data/lib/lutaml/lml/yaml_parser.rb +17 -0
  102. data/lib/lutaml/lml.rb +130 -0
  103. data/lib/lutaml/model/adapter_resolver.rb +21 -2
  104. data/lib/lutaml/model/config.rb +22 -0
  105. data/lib/lutaml/model/serialize/format_conversion.rb +8 -6
  106. data/lib/lutaml/model/version.rb +1 -1
  107. data/lib/lutaml/xml/adapter/base_adapter.rb +26 -0
  108. data/lib/lutaml/xml/adapter/moxml_adapter.rb +49 -0
  109. data/lib/lutaml/xml/adapter/xml_parser.rb +4 -4
  110. data/lib/lutaml/xml/adapter/xml_serializer.rb +1 -1
  111. data/lib/lutaml/xml/adapter.rb +5 -0
  112. data/lib/lutaml/xml/adapter_loader.rb +5 -0
  113. data/lib/lutaml/xml/format.rb +23 -2
  114. data/lib/lutaml/xml/leptris/native_emission.rb +6 -1
  115. data/lib/lutaml/xml/leptris/plan_serializer.rb +5 -1
  116. data/lib/lutaml/xml/parse_session.rb +8 -1
  117. data/lib/lutaml/xml/schema/builder/moxml.rb +1 -1
  118. data/lib/lutaml/xml/schema/builder.rb +1 -1
  119. data/lib/lutaml/xml/schema/relaxng_schema.rb +1 -1
  120. data/lib/lutaml/xml/schema/vc_namespace.rb +24 -0
  121. data/lib/lutaml/xml/schema/xsd/any.rb +4 -0
  122. data/lib/lutaml/xml/schema/xsd/any_attribute.rb +4 -0
  123. data/lib/lutaml/xml/schema/xsd/base.rb +7 -0
  124. data/lib/lutaml/xml/schema/xsd/open_content.rb +2 -0
  125. data/lib/lutaml/xml/schema/xsd/restriction_simple_type.rb +3 -0
  126. data/lib/lutaml/xml/schema/xsd/schema.rb +38 -0
  127. data/lib/lutaml/xml/schema/xsd/schema_validator.rb +1 -1
  128. data/lib/lutaml/xml/schema/xsd.rb +1 -0
  129. data/lib/lutaml/xml/schema/xsd_schema.rb +1 -1
  130. data/lib/lutaml/xml/serialization/format_conversion.rb +6 -0
  131. data/lib/lutaml/xml/xsd_validator.rb +1 -1
  132. data/lutaml-model.gemspec +2 -0
  133. data/spec/lml/fixtures/dsl/broken_diagram.lutaml +34 -0
  134. data/spec/lml/fixtures/dsl/diagram.lutaml +4 -0
  135. data/spec/lml/fixtures/dsl/diagram_attributes.lutaml +6 -0
  136. data/spec/lml/fixtures/dsl/diagram_blank_definion.lutaml +6 -0
  137. data/spec/lml/fixtures/dsl/diagram_blank_entities.lutaml +8 -0
  138. data/spec/lml/fixtures/dsl/diagram_class_assocation.lutaml +29 -0
  139. data/spec/lml/fixtures/dsl/diagram_class_fields.lutaml +25 -0
  140. data/spec/lml/fixtures/dsl/diagram_commented_includes.lutaml +5 -0
  141. data/spec/lml/fixtures/dsl/diagram_comments.lutaml +28 -0
  142. data/spec/lml/fixtures/dsl/diagram_concept_model.lutaml +132 -0
  143. data/spec/lml/fixtures/dsl/diagram_data_types.lutaml +44 -0
  144. data/spec/lml/fixtures/dsl/diagram_definitions.lutaml +25 -0
  145. data/spec/lml/fixtures/dsl/diagram_includes.lutaml +6 -0
  146. data/spec/lml/fixtures/dsl/diagram_multiply_classes.lutaml +7 -0
  147. data/spec/lml/fixtures/dsl/diagram_non_existing_include.lutaml +6 -0
  148. data/spec/lml/fixtures/dsl/shared.lutaml +5 -0
  149. data/spec/lml/fixtures/dsl/shared1.lutaml +4 -0
  150. data/spec/lml/fixtures/generated_dot/AddressClassProfile.dot +170 -0
  151. data/spec/lml/fixtures/generated_dot/AddressProfile.dot +34 -0
  152. data/spec/lml/fixtures/generated_dot/address_class_profile_with_associations.dot +127 -0
  153. data/spec/lml/fixtures/generated_dot/address_profile_with_associations.dot +28 -0
  154. data/spec/lml/fixtures/generated_dot/document.dot +28 -0
  155. data/spec/lml/fixtures/generated_dot/document.dot.backup +33 -0
  156. data/spec/lml/fixtures/generated_dot/document_with_fidelity.dot +27 -0
  157. data/spec/lml/fixtures/lml/data_s102_check.lml +79 -0
  158. data/spec/lml/fixtures/lml/data_s158_metadata.lml +28 -0
  159. data/spec/lml/fixtures/lml/iho_data_models.lml +59 -0
  160. data/spec/lml/fixtures/lml/iho_s102_check.lml +101 -0
  161. data/spec/lml/fixtures/mixed_lml/diagram.lml +26 -0
  162. data/spec/lml/fixtures/mixed_lml/instances.lml +72 -0
  163. data/spec/lml/fixtures/mixed_lml/model.lml +30 -0
  164. data/spec/lml/fixtures/real_world/basicdoc-models/AdmonitionType.lutaml +32 -0
  165. data/spec/lml/fixtures/real_world/basicdoc-models/BasicBlock.lutaml +11 -0
  166. data/spec/lml/fixtures/real_world/basicdoc-models/BasicDocument.lutaml +41 -0
  167. data/spec/lml/fixtures/real_world/basicdoc-models/BasicSection.lutaml +38 -0
  168. data/spec/lml/fixtures/real_world/basicdoc-models/BibliographicItem.lutaml +6 -0
  169. data/spec/lml/fixtures/real_world/basicdoc-models/FigureBlock.lutaml +65 -0
  170. data/spec/lml/fixtures/real_world/basicdoc-models/FormattedString.lutaml +12 -0
  171. data/spec/lml/fixtures/real_world/basicdoc-models/List.lutaml +12 -0
  172. data/spec/lml/fixtures/real_world/basicdoc-models/OrderedListType.lutaml +32 -0
  173. data/spec/lml/fixtures/real_world/basicdoc-models/Signature.lutaml +22 -0
  174. data/spec/lml/fixtures/real_world/basicdoc-models/String.lutaml +2 -0
  175. data/spec/lml/fixtures/real_world/basicdoc-models/TableBlock.lutaml +60 -0
  176. data/spec/lml/fixtures/real_world/basicdoc-models/TextElement.lutaml +13 -0
  177. data/spec/lml/fixtures/real_world/basicdoc-models/Uri.lutaml +7 -0
  178. data/spec/lml/fixtures/real_world/metanorma-model-iso/BibliographicItem.lutaml +4 -0
  179. data/spec/lml/fixtures/real_world/metanorma-model-iso/IsoAdmonitionType.lutaml +31 -0
  180. data/spec/lml/fixtures/real_world/metanorma-model-iso/IsoBibDataExtensionType.lutaml +40 -0
  181. data/spec/lml/fixtures/real_world/metanorma-model-iso/IsoSections.lutaml +30 -0
  182. data/spec/lml/fixtures/real_world/metanorma-model-iso/IsoStandardDocument.lutaml +46 -0
  183. data/spec/lml/fixtures/real_world/metanorma-model-iso/IsoTerm.lutaml +20 -0
  184. data/spec/lml/fixtures/real_world/metanorma-model-iso/StandardBibDataExtensionType.lutaml +2 -0
  185. data/spec/lml/fixtures/real_world/metanorma-model-iso/StandardDocument.lutaml +2 -0
  186. data/spec/lml/fixtures/rs3001/01_class_tail_comment.lml +5 -0
  187. data/spec/lml/fixtures/rs3001/02_definition_block.lml +9 -0
  188. data/spec/lml/fixtures/rs3001/03_package_ceramics.lml +20 -0
  189. data/spec/lml/fixtures/rs3001/04_attribute_definition_form.lml +10 -0
  190. data/spec/lml/fixtures/rs3001/05_attribute_cardinality_unbounded.lml +10 -0
  191. data/spec/lml/fixtures/rs3001/06_attribute_values_set.lml +10 -0
  192. data/spec/lml/fixtures/rs3001/07_attribute_pattern.lml +10 -0
  193. data/spec/lml/fixtures/rs3001/08_attribute_default_colon.lml +15 -0
  194. data/spec/lml/fixtures/rs3001/09_value_porcelain.lml +9 -0
  195. data/spec/lml/fixtures/rs3001/10_enum_glaze_technique.lml +19 -0
  196. data/spec/lml/fixtures/rs3001/11_enum_values_as_instances.lml +29 -0
  197. data/spec/lml/fixtures/rs3001/12_instance_named.lml +13 -0
  198. data/spec/lml/fixtures/rs3001/13_instances_collection_refs.lml +11 -0
  199. data/spec/lml/fixtures/rs3001/14_require_models.lml +7 -0
  200. data/spec/lml/fixtures/rs3001/15_comment_line.lml +1 -0
  201. data/spec/lml/fixtures/rs3001/16_comment_tail.lml +5 -0
  202. data/spec/lml/fixtures/rs3001/17_comment_documented_class.lml +5 -0
  203. data/spec/lml/fixtures/rs3001/18_comment_block.lml +4 -0
  204. data/spec/lml/fixtures/rs3001/19_top_level_instance_definition.lml +21 -0
  205. data/spec/lml/fixtures/rs3001/20_instance_typed_definition.lml +22 -0
  206. data/spec/lml/fixtures/rs3001/21_instance_collection_studios.lml +19 -0
  207. data/spec/lml/fixtures/rs3001/22_instance_collection_tiles.lml +15 -0
  208. data/spec/lml/fixtures/rs3001/23_reference_cross_collection.lml +11 -0
  209. data/spec/lml/fixtures/rs3001/24_null_and_empty.lml +15 -0
  210. data/spec/lml/fixtures/rs3001/25_string_escapes.lml +23 -0
  211. data/spec/lml/fixtures/rs3001/26_quoted_keys.lml +7 -0
  212. data/spec/lml/fixtures/test.lutaml +20 -0
  213. data/spec/lml/fixtures/test_data/components.csv +4 -0
  214. data/spec/lml/fixtures/test_data/products.xml +18 -0
  215. data/spec/lml/fixtures/uml/address_class_profile_with_associations.yml +58 -0
  216. data/spec/lml/fixtures/uml/address_profile_with_associations.yml +31 -0
  217. data/spec/lml/fixtures/uml/association.yml +11 -0
  218. data/spec/lml/fixtures/uml/class.yml +28 -0
  219. data/spec/lml/fixtures/uml/data_type.yml +30 -0
  220. data/spec/lml/fixtures/uml/diagram.yml +7 -0
  221. data/spec/lml/fixtures/uml/document.yml +44 -0
  222. data/spec/lml/fixtures/uml/document_with_fidelity.yml +47 -0
  223. data/spec/lml/fixtures/uml/enum.yml +14 -0
  224. data/spec/lml/fixtures/uml/operation.yml +8 -0
  225. data/spec/lml/fixtures/uml/package.yml +10 -0
  226. data/spec/lml/fixtures/uml/top_element.yml +11 -0
  227. data/spec/lml/fixtures/uml/top_element_attribute.yml +10 -0
  228. data/spec/lml/fixtures/uml/trigger.yml +12 -0
  229. data/spec/lml/fixtures/uml/value.yml +8 -0
  230. data/spec/lml/fixtures/view/cycle_a.lutaml +3 -0
  231. data/spec/lml/fixtures/view/cycle_b.lutaml +3 -0
  232. data/spec/lml/fixtures/view/import_model.lutaml +3 -0
  233. data/spec/lml/fixtures/view/import_view.lutaml +3 -0
  234. data/spec/lml/fixtures/view/import_with_hide.lutaml +4 -0
  235. data/spec/lml/fixtures/view/import_with_show.lutaml +4 -0
  236. data/spec/lml/fixtures/view/models/bar.lutaml +3 -0
  237. data/spec/lml/fixtures/view/models/baz.lutaml +3 -0
  238. data/spec/lml/fixtures/view/models/foo.lutaml +4 -0
  239. data/spec/lml/integration/instance_import_export_spec.rb +102 -0
  240. data/spec/lml/lml_spec_setup.rb +25 -0
  241. data/spec/lml/lutaml/lml/association_label_resolver_spec.rb +139 -0
  242. data/spec/lml/lutaml/lml/data_processor_spec.rb +274 -0
  243. data/spec/lml/lutaml/lml/declarations_spec.rb +337 -0
  244. data/spec/lml/lutaml/lml/document_builder_registry_spec.rb +52 -0
  245. data/spec/lml/lutaml/lml/document_builder_spec.rb +99 -0
  246. data/spec/lml/lutaml/lml/element_visibility_spec.rb +128 -0
  247. data/spec/lml/lutaml/lml/entity_types_spec.rb +56 -0
  248. data/spec/lml/lutaml/lml/enum_spec.rb +77 -0
  249. data/spec/lml/lutaml/lml/executor/adapter_helpers_spec.rb +96 -0
  250. data/spec/lml/lutaml/lml/executor/condition_evaluator_spec.rb +393 -0
  251. data/spec/lml/lutaml/lml/executor/csv_adapter_spec.rb +151 -0
  252. data/spec/lml/lutaml/lml/executor/format_adapter_spec.rb +31 -0
  253. data/spec/lml/lutaml/lml/executor/xml_adapter_spec.rb +201 -0
  254. data/spec/lml/lutaml/lml/executor_spec.rb +71 -0
  255. data/spec/lml/lutaml/lml/format_adapter_spec.rb +272 -0
  256. data/spec/lml/lutaml/lml/format_autoload_spec.rb +31 -0
  257. data/spec/lml/lutaml/lml/grammar_spec.rb +581 -0
  258. data/spec/lml/lutaml/lml/has_attributes_spec.rb +54 -0
  259. data/spec/lml/lutaml/lml/import_resolver_spec.rb +116 -0
  260. data/spec/lml/lutaml/lml/instance_hydration_spec.rb +178 -0
  261. data/spec/lml/lutaml/lml/lsp/server_spec.rb +285 -0
  262. data/spec/lml/lutaml/lml/model_compiler_spec.rb +406 -0
  263. data/spec/lml/lutaml/lml/models/association_spec.rb +40 -0
  264. data/spec/lml/lutaml/lml/models/collection_spec.rb +27 -0
  265. data/spec/lml/lutaml/lml/models/data_type_spec.rb +34 -0
  266. data/spec/lml/lutaml/lml/models/diagram_spec.rb +18 -0
  267. data/spec/lml/lutaml/lml/models/document_spec.rb +64 -0
  268. data/spec/lml/lutaml/lml/models/enum_spec.rb +42 -0
  269. data/spec/lml/lutaml/lml/models/inheritance_spec.rb +77 -0
  270. data/spec/lml/lutaml/lml/models/instance_collection_spec.rb +62 -0
  271. data/spec/lml/lutaml/lml/models/instance_spec.rb +81 -0
  272. data/spec/lml/lutaml/lml/models/instances_export_spec.rb +23 -0
  273. data/spec/lml/lutaml/lml/models/instances_import_spec.rb +28 -0
  274. data/spec/lml/lutaml/lml/models/package_spec.rb +26 -0
  275. data/spec/lml/lutaml/lml/models/primitive_type_spec.rb +34 -0
  276. data/spec/lml/lutaml/lml/models/top_element_attribute_spec.rb +153 -0
  277. data/spec/lml/lutaml/lml/models/uml_class_spec.rb +70 -0
  278. data/spec/lml/lutaml/lml/models_version_spec.rb +27 -0
  279. data/spec/lml/lutaml/lml/operations_spec.rb +59 -0
  280. data/spec/lml/lutaml/lml/parser_spec.rb +348 -0
  281. data/spec/lml/lutaml/lml/pipeline_spec.rb +93 -0
  282. data/spec/lml/lutaml/lml/preprocessor_spec.rb +174 -0
  283. data/spec/lml/lutaml/lml/property_laws_spec.rb +344 -0
  284. data/spec/lml/lutaml/lml/real_world_spec.rb +191 -0
  285. data/spec/lml/lutaml/lml/reference_spec.rb +64 -0
  286. data/spec/lml/lutaml/lml/round_trip_spec.rb +457 -0
  287. data/spec/lml/lutaml/lml/rs3001_corpus_spec.rb +83 -0
  288. data/spec/lml/lutaml/lml/rs3001_syntax_spec.rb +77 -0
  289. data/spec/lml/lutaml/lml/serialization_mapping_spec.rb +127 -0
  290. data/spec/lml/lutaml/lml/source_spec.rb +62 -0
  291. data/spec/lml/lutaml/lml/transform_spec.rb +37 -0
  292. data/spec/lml/lutaml/lml/types/text_type_spec.rb +43 -0
  293. data/spec/lml/lutaml/lml/validator_spec.rb +238 -0
  294. data/spec/lml/lutaml/lml/view_resolver_spec.rb +78 -0
  295. data/spec/lml/lutaml/lml/view_spec.rb +112 -0
  296. data/spec/lml/lutaml/parsers/dsl_spec.rb +275 -0
  297. data/spec/lml/lutaml/parsers/yaml_spec.rb +52 -0
  298. data/spec/lml/lutaml/qualified_type_spec.rb +33 -0
  299. data/spec/lutaml/model/adapter_type_unset_restore_spec.rb +61 -0
  300. data/spec/lutaml/model/custom_bibtex_adapter_spec.rb +4 -1
  301. data/spec/lutaml/xml/adapter/moxml_lane_spec.rb +112 -0
  302. data/spec/lutaml/xml/adapter_metadata_default_spec.rb +21 -2
  303. data/spec/lutaml/xml/adapter_moxml_engine_spec.rb +57 -0
  304. data/spec/lutaml/xml/leptris/native_emission_plain_model_spec.rb +50 -0
  305. data/spec/lutaml/xml/plain_model_mapping_resolution_spec.rb +88 -0
  306. data/spec/lutaml/xml/schema/xsd/vc_versioning_spec.rb +49 -0
  307. data/spec/lutaml/xml/schema/xsd/xsd_11_constructs_spec.rb +83 -0
  308. metadata +286 -2
@@ -0,0 +1,738 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/lml/quoting"
4
+
5
+ module Lutaml
6
+ module Formatter
7
+ # The canonical LML emitter (RS 3001 informative "Canonical form"):
8
+ # a deterministic rendering of the document model. Canonicality is a
9
+ # formatting operation, not a validity constraint. The rules live in
10
+ # TODO.next/sota/canon.md.
11
+ #
12
+ # Laws: format(format(x)) == format(x) (idempotence), and
13
+ # parse(format(parse(x))) equals parse(x) under the document-model
14
+ # comparison.
15
+ class Canonical
16
+ include Lutaml::Lml::Quoting
17
+
18
+ GROUPS = %i[
19
+ namespaces packages classes enums data_types primitives
20
+ serialization_mappings associations diagrams
21
+ ].freeze
22
+
23
+ VISIBILITY_SYMBOLS = {
24
+ "private" => "-", "protected" => "#", "package" => "~"
25
+ }.freeze
26
+
27
+ VISIBILITY_WORDS = %w[private protected package].freeze
28
+
29
+ def initialize(document)
30
+ @document = document
31
+ @lines = []
32
+ @level = 0
33
+ @bodies = []
34
+ end
35
+
36
+ def render
37
+ render_document(@document)
38
+ trim_trailing_blanks
39
+ return "" if @lines.empty?
40
+
41
+ "#{@lines.join("\n")}\n"
42
+ end
43
+
44
+ private
45
+
46
+ # ---- document ----
47
+
48
+ def render_document(doc)
49
+ unless doc.lml_version.to_s.empty?
50
+ line("lml #{quote_always(doc.lml_version)}")
51
+ blank_line
52
+ end
53
+ Array(doc.requires).each { |path| line("require #{quote_always(path)}") }
54
+ blank_line if Array(doc.requires).any?
55
+
56
+ Array(doc.comments).each { |comment| top { render_comment(comment) } }
57
+
58
+ render_scope(doc)
59
+ top { render_instance(doc.instance) } if doc.instance
60
+ top { render_instance_collection(doc.instances) } if doc.instances
61
+ render_views(doc)
62
+ end
63
+
64
+ def render_scope(doc)
65
+ return render_model_members(doc) if doc.name.to_s.empty?
66
+
67
+ top do
68
+ render_block("models #{quote_scalar(doc.name)}#{version_suffix(doc)}") do
69
+ render_model_members(doc)
70
+ end
71
+ end
72
+ end
73
+
74
+ def version_suffix(doc)
75
+ version = doc.model_versions[doc.name]
76
+ version.nil? ? "" : " version #{quote_always(version)}"
77
+ end
78
+
79
+ def render_model_members(doc)
80
+ %i[title caption fontname].each do |attribute|
81
+ value = doc.public_send(attribute)
82
+ next if value.to_s.empty?
83
+
84
+ top { line("#{attribute} #{quote_always(value)}") }
85
+ end
86
+
87
+ GROUPS.each do |collection|
88
+ Array(doc.public_send(collection)).each do |item|
89
+ top { render_group_member(collection, item) }
90
+ end
91
+ end
92
+ end
93
+
94
+ def render_group_member(collection, item)
95
+ case collection
96
+ when :namespaces then render_namespace(item)
97
+ when :packages then render_package(item)
98
+ when :classes then render_class(item)
99
+ when :enums then render_enum(item)
100
+ when :data_types then render_data_type(item)
101
+ when :primitives then render_primitive(item)
102
+ when :serialization_mappings then render_mapping(item)
103
+ when :associations then render_association(item)
104
+ when :diagrams then render_diagram(item)
105
+ end
106
+ end
107
+
108
+ def render_views(doc)
109
+ imports = Array(doc.view_imports)
110
+ show = doc.show_filter
111
+ hide = doc.hide_filter
112
+ return if imports.empty? && show.nil? && hide.nil?
113
+
114
+ top do
115
+ render_block("view #{quote_scalar(doc.name)}") do
116
+ imports.each { |entry| member { line("import #{quote_always(entry.path)}") } }
117
+ member { line("show #{show.entity_names.join(', ')}") } if show
118
+ member { line("hide #{hide.entity_names.join(', ')}") } if hide
119
+ end
120
+ end
121
+ end
122
+
123
+ # ---- declarations ----
124
+
125
+ def render_namespace(namespace)
126
+ render_block("namespace #{namespace.name}") do
127
+ line("uri #{quote_scalar(namespace.uri)}") if namespace.uri
128
+ line("prefix #{quote_scalar(namespace.prefix)}") if namespace.prefix
129
+ end
130
+ end
131
+
132
+ def render_package(package)
133
+ render_block("#{declaration_visibility(package.visibility)}package #{package.name}") do
134
+ render_collection(package, :classes) { |item| render_class(item) }
135
+ render_collection(package, :enums) { |item| render_enum(item) }
136
+ render_collection(package, :data_types) { |item| render_data_type(item) }
137
+ render_collection(package, :packages) { |item| render_package(item) }
138
+ end
139
+ end
140
+
141
+ def render_class(klass)
142
+ render_block(class_header(klass)) do
143
+ render_comments(klass.comments)
144
+ member { render_definition(klass.definition) } if definition_present?(klass.definition)
145
+ member { render_class_namespace(klass.xml_namespace) } if klass.xml_namespace&.uri
146
+ Array(klass.attributes).each { |attribute| member { render_attribute(attribute) } }
147
+ Array(klass.operations).each { |operation| member { render_operation(operation) } }
148
+ Array(klass.derived_fields).each do |field|
149
+ member { line("derive #{field.name}: #{field.type}") }
150
+ end
151
+ Array(klass.string_formats).each { |format| member { render_string_format(format) } }
152
+ Array(klass.serialization_mappings).each do |mapping|
153
+ member { render_mapping(mapping, with_target: false) }
154
+ end
155
+ end
156
+ end
157
+
158
+ def render_class_namespace(namespace)
159
+ text = "namespace #{quote_always(namespace.uri)}"
160
+ text += " prefix #{quote_always(namespace.prefix)}" if namespace.prefix
161
+ line(text)
162
+ end
163
+
164
+ def class_header(klass)
165
+ text = "#{declaration_visibility(klass.visibility)}#{class_modifier(klass)}"
166
+ text += "class #{klass.name}"
167
+ parent = klass.parent_class.to_s.empty? ? klass.type : klass.parent_class
168
+ text += " < #{parent}" if parent.to_s != ""
169
+ text + stereotype_suffix(klass.stereotype)
170
+ end
171
+
172
+ def class_modifier(klass)
173
+ return "#{klass.modifier} " if klass.modifier.to_s != ""
174
+
175
+ klass.is_abstract ? "abstract " : ""
176
+ end
177
+
178
+ def render_enum(enum)
179
+ render_block("#{declaration_visibility(enum.visibility)}enum #{enum.name}#{stereotype_suffix(enum.stereotype)}") do
180
+ member { render_definition(enum.definition) } if definition_present?(enum.definition)
181
+ member { line("abstract true") } if enum.is_abstract
182
+ member { render_member_fields(enum.member_fields) } if Array(enum.member_fields).any?
183
+ member { render_from_table(enum) } if enum.from_table_name
184
+ Array(enum.attributes).each { |value| member { render_enum_member(value) } }
185
+ end
186
+ end
187
+
188
+ def render_member_fields(fields)
189
+ render_block("member_fields") do
190
+ Array(fields).each { |field| line("#{field.name} #{field.type}") }
191
+ end
192
+ end
193
+
194
+ def render_from_table(enum)
195
+ header = "from_table #{quote_scalar(enum.from_table_name)}"
196
+ if enum.from_table_artifact.to_s.empty?
197
+ line(header)
198
+ else
199
+ render_block(header) do
200
+ line("artifact #{quote_scalar(enum.from_table_artifact)}")
201
+ end
202
+ end
203
+ end
204
+
205
+ def render_enum_member(value)
206
+ return render_attribute(value) if attribute_member?(value)
207
+
208
+ description = Array(value.attributes).find { |attribute| attribute.name == "description" }
209
+ properties = Array(value.properties)
210
+ instances = Array(value.instances)
211
+ payload = description || properties.any? || instances.any?
212
+
213
+ unless payload
214
+ line("value #{quote_scalar(value.name)}")
215
+ return
216
+ end
217
+
218
+ render_block("value #{quote_scalar(value.name)}") do
219
+ member { render_definition(description.type) } if description
220
+ properties.each do |property|
221
+ member { line("#{property.name} = #{render_data_value(unwrap(property.value))}") }
222
+ end
223
+ instances.each { |instance| member { render_instance(instance) } }
224
+ end
225
+ end
226
+
227
+ # A member carrying the attribute shape (type, visibility, direct
228
+ # definition, cardinality, options) came from a bare attribute
229
+ # declaration, not from the `value` construct. Rendering it as a
230
+ # `value` block would move a direct definition into the nested
231
+ # description payload and change the model.
232
+ def attribute_member?(value)
233
+ value.type.to_s != "" ||
234
+ !public_visibility?(value.visibility) ||
235
+ definition_present?(value.definition) ||
236
+ value.cardinality || Array(value.value_set).any? ||
237
+ value.pattern || value.default ||
238
+ Array(value.stereotype).any? || value.literal
239
+ end
240
+
241
+ def render_data_type(data_type)
242
+ render_block("#{declaration_visibility(data_type.visibility)}data_type #{data_type.name}#{stereotype_suffix(data_type.stereotype)}") do
243
+ member { render_definition(data_type.definition) } if definition_present?(data_type.definition)
244
+ Array(data_type.attributes).each { |attribute| member { render_attribute(attribute) } }
245
+ end
246
+ end
247
+
248
+ def render_primitive(primitive)
249
+ line("#{declaration_visibility(primitive.visibility)}primitive #{primitive.name}")
250
+ end
251
+
252
+ def render_mapping(mapping, with_target: true)
253
+ target = with_target && mapping.target.to_s != "" ? " #{mapping.target}" : ""
254
+ render_block("mapping #{mapping.format}#{target}") do
255
+ line("element #{quote_always(mapping.element_name)}") if mapping.element_name
256
+ line("namespace #{mapping.namespace_ref}") if mapping.namespace_ref
257
+ line("mixed_content") if mapping.mixed_content
258
+ Array(mapping.rules).each do |rule|
259
+ text = mapping_rule(rule)
260
+ line(text) if text
261
+ end
262
+ end
263
+ end
264
+
265
+ def mapping_rule(rule)
266
+ case rule.kind.to_s
267
+ when "element" then "map_element #{wire_and_field(rule)}"
268
+ when "attribute" then "map_attribute #{wire_and_field(rule)}"
269
+ when "content" then "map_content #{rule.field}"
270
+ when "namespace" then "namespace #{rule.field || rule.wire}"
271
+ when "key" then "map_key to_instance #{rule.field}"
272
+ when "value"
273
+ mode = rule.mode.to_s.empty? ? "to_instance" : rule.mode
274
+ "map_value #{mode} #{rule.field}"
275
+ else "map #{wire_and_field(rule)}"
276
+ end
277
+ end
278
+
279
+ def wire_and_field(rule)
280
+ parts = []
281
+ parts << quote_always(rule.wire) if rule.wire
282
+ parts << rule.field if rule.field
283
+ parts.join(" ")
284
+ end
285
+
286
+ def render_association(association)
287
+ name = association.name.to_s.empty? ? "" : " #{association.name}"
288
+ render_block("association#{name}") do
289
+ member { line("owner_type #{association.owner_end_type}") } if association.owner_end_type
290
+ member { line("member_type #{association.member_end_type}") } if association.member_end_type
291
+ member { line(association_end("owner", association.owner_end, association.owner_end_attribute_name, association.owner_end_cardinality)) } if association.owner_end
292
+ member { line(association_end("member", association.member_end, association.member_end_attribute_name, association.member_end_cardinality)) } if association.member_end
293
+ end
294
+ end
295
+
296
+ def association_end(keyword, name, attribute_name, cardinality)
297
+ text = "#{keyword} #{quote_scalar(name)}"
298
+ text += "##{attribute_name}" if attribute_name.to_s != ""
299
+ text += " [#{cardinality_token(cardinality)}]" if cardinality
300
+ text
301
+ end
302
+
303
+ def render_diagram(diagram)
304
+ return if diagram.name.to_s.empty?
305
+
306
+ render_block("diagram #{diagram.name}") do
307
+ member { render_definition(diagram.definition) } if definition_present?(diagram.definition)
308
+ end
309
+ end
310
+
311
+ # ---- attributes ----
312
+
313
+ def render_attribute(attribute)
314
+ return if attribute.name.to_s.empty?
315
+
316
+ return render_attribute_block(attribute) if attribute.xml_role && attribute_block_options?(attribute)
317
+
318
+ if terse_attribute?(attribute)
319
+ line("attribute #{attribute.name}, #{attribute.type}")
320
+ elsif naked_attribute?(attribute)
321
+ line("#{visibility_symbol(attribute.visibility)}#{attribute_name_text(attribute.name)}")
322
+ elsif bare_attribute?(attribute)
323
+ render_bare_attribute(attribute)
324
+ else
325
+ render_attribute_block(attribute)
326
+ end
327
+ end
328
+
329
+ def attribute_block_options?(attribute)
330
+ Array(attribute.value_set).any? || Array(attribute.properties).any? ||
331
+ !attribute.pattern.nil? || !attribute.default.nil?
332
+ end
333
+
334
+ def terse_attribute?(attribute)
335
+ public_visibility?(attribute.visibility) &&
336
+ bare_name?(attribute.name) &&
337
+ attribute.type.to_s != "" &&
338
+ attribute_cardinality(attribute).nil? &&
339
+ Array(attribute.value_set).empty? &&
340
+ Array(attribute.properties).empty? &&
341
+ attribute.pattern.nil? && attribute.default.nil? &&
342
+ Array(attribute.stereotype).empty? && !attribute.literal &&
343
+ attribute.xml_role.nil? && !definition_present?(attribute.definition)
344
+ end
345
+
346
+ def naked_attribute?(attribute)
347
+ public_visibility?(attribute.visibility) &&
348
+ attribute.type.to_s.empty? &&
349
+ attribute_cardinality(attribute).nil? &&
350
+ Array(attribute.value_set).empty? &&
351
+ Array(attribute.properties).empty? &&
352
+ attribute.pattern.nil? && attribute.default.nil? &&
353
+ Array(attribute.stereotype).empty? && !attribute.literal &&
354
+ attribute.xml_role.nil? && !definition_present?(attribute.definition)
355
+ end
356
+
357
+ def bare_attribute?(attribute)
358
+ !public_visibility?(attribute.visibility) ||
359
+ !bare_name?(attribute.name) ||
360
+ Array(attribute.stereotype).any? || attribute.literal ||
361
+ !attribute.xml_role.nil? || definition_present?(attribute.definition)
362
+ end
363
+
364
+ def render_bare_attribute(attribute)
365
+ header = "#{visibility_symbol(attribute.visibility)}#{attribute_name_text(attribute.name)}"
366
+ type = attribute_type_text(attribute)
367
+ header += ": #{type}" if type != ""
368
+ cardinality = attribute_cardinality(attribute)
369
+ header += " [#{cardinality}]" if cardinality
370
+ header += " as :#{attribute.xml_role}" if attribute.xml_role
371
+
372
+ if definition_present?(attribute.definition)
373
+ render_block(header) { render_definition(attribute.definition) }
374
+ else
375
+ line(header)
376
+ end
377
+ end
378
+
379
+ def bare_name?(name)
380
+ name.to_s.match?(%r{\A[A-Za-z0-9_/+]+\z})
381
+ end
382
+
383
+ def attribute_name_text(name)
384
+ bare_name?(name) ? name.to_s : quote_always(name)
385
+ end
386
+
387
+ def attribute_type_text(attribute)
388
+ return "" if attribute.type.to_s.empty?
389
+
390
+ stereotypes = Array(attribute.stereotype)
391
+ type = attribute.literal ? quote_always(attribute.type) : attribute.type.to_s
392
+ stereotypes.any? ? "<<#{stereotypes.first}>> #{type}" : type
393
+ end
394
+
395
+ def render_attribute_block(attribute)
396
+ header = "attribute #{attribute_name_text(attribute.name)}"
397
+ header += " as :#{attribute.xml_role}" if attribute.xml_role
398
+ render_block(header) do
399
+ line("type #{attribute.type}") if attribute.type.to_s != ""
400
+ cardinality = attribute_cardinality(attribute)
401
+ line("cardinality #{cardinality}") if cardinality
402
+ if Array(attribute.value_set).any?
403
+ values = attribute.value_set.map { |value| quote_scalar(value) }.join(", ")
404
+ line("values { #{values} }")
405
+ end
406
+ line("default #{render_data_value(attribute.default)}") if attribute.default
407
+
408
+ properties = Array(attribute.properties)
409
+ defaults, others = properties.partition { |property| property.name == "default" }
410
+ defaults.each { |property| line(property_line(property)) }
411
+ line("pattern /#{attribute.pattern}/") if attribute.pattern
412
+ others.sort_by { |property| property.name.to_s }
413
+ .each { |property| line(property_line(property)) }
414
+ end
415
+ end
416
+
417
+ def property_line(property)
418
+ "#{property.name} = #{render_data_value(unwrap(property.value))}"
419
+ end
420
+
421
+ def render_operation(operation)
422
+ parameters = Array(operation.owned_parameter).map do |parameter|
423
+ operation_parameter(parameter)
424
+ end.join(", ")
425
+ text = "#{visibility_symbol(operation.visibility)}#{operation.name}(#{parameters})"
426
+ text += ": #{operation.return_type}" if operation.return_type.to_s != ""
427
+ stereotypes = Array(operation.stereotype)
428
+ text += " { #{stereotypes.join(', ')} }" if stereotypes.any?
429
+ line(text)
430
+ end
431
+
432
+ def operation_parameter(parameter)
433
+ parts = []
434
+ direction = parameter.direction.to_s
435
+ parts << direction if !direction.empty? && direction != "in"
436
+ parts << parameter.name
437
+ parts << ": #{parameter.type}" if parameter.type.to_s != ""
438
+ parts << "= #{render_data_value(parameter.default)}" unless parameter.default.nil?
439
+ parts.join(" ")
440
+ end
441
+
442
+ def render_string_format(format)
443
+ render_block("string_format #{quote_scalar(format.format)}") do
444
+ line("artifact #{quote_scalar(format.artifact)}") if format.artifact
445
+ line("root #{quote_scalar(format.root)}") if format.root
446
+ end
447
+ end
448
+
449
+ # ---- instances ----
450
+
451
+ def render_instance_collection(collection)
452
+ name = collection.name.to_s.empty? ? "" : " #{quote_scalar(collection.name)}"
453
+ render_block("instances#{name}") do
454
+ Array(collection.imports).each { |entry| member { render_import(entry) } }
455
+ Array(collection.collections).each { |entry| member { render_collection_decl(entry) } }
456
+ Array(collection.exports).each { |entry| member { render_export(entry) } }
457
+ Array(collection.instances).each { |entry| member { render_instance(entry) } }
458
+ end
459
+ end
460
+
461
+ def render_import(entry)
462
+ render_block("import") do
463
+ member do
464
+ render_block("#{entry.format_type} #{quote_always(entry.file)}") do
465
+ Array(entry.attributes).each do |attribute|
466
+ member { render_instance_attribute(attribute) }
467
+ end
468
+ end
469
+ end
470
+ end
471
+ end
472
+
473
+ def render_export(entry)
474
+ render_block("export") do
475
+ member do
476
+ render_block("format #{entry.format_type}") do
477
+ Array(entry.attributes).each do |attribute|
478
+ member { render_instance_attribute(attribute) }
479
+ end
480
+ end
481
+ end
482
+ end
483
+ end
484
+
485
+ def render_collection_decl(collection)
486
+ render_block("collection #{quote_always(collection.name)}") do
487
+ if Array(collection.includes).any?
488
+ values = collection.includes.map { |value| quote_always(value) }.join(", ")
489
+ member { line("includes [#{values}]") }
490
+ end
491
+ next if Array(collection.validations).empty?
492
+
493
+ member do
494
+ render_block("validation") do
495
+ Array(collection.validations).each do |condition|
496
+ line("condition #{quote_always(condition)}")
497
+ end
498
+ end
499
+ end
500
+ end
501
+ end
502
+
503
+ def render_instance(instance, prefix = "")
504
+ render_block("#{prefix}#{instance_header(instance)}") do
505
+ member { line("isa #{quote_scalar(instance.isa)}") } if instance.isa.to_s != ""
506
+ if Array(instance.template).any?
507
+ member { render_template(instance.template) }
508
+ else
509
+ Array(instance.attributes).each do |attribute|
510
+ member { render_instance_attribute(attribute) }
511
+ end
512
+ end
513
+ member { render_instance(instance.instance) } if instance.instance
514
+ end
515
+ end
516
+
517
+ def instance_header(instance)
518
+ label =
519
+ if instance.name.to_s.empty?
520
+ instance.type.to_s.empty? ? "instance" : "instance #{instance.type}"
521
+ elsif instance.type.to_s.empty?
522
+ "instance #{quote_always(instance.name)}"
523
+ else
524
+ "#{instance.type} #{quote_always(instance.name)}"
525
+ end
526
+ label += " extends #{quote_always(instance.parent)}" if instance.parent.to_s != ""
527
+ label
528
+ end
529
+
530
+ def render_instance_attribute(attribute)
531
+ if attribute.name.to_s.empty?
532
+ render_definition(attribute.definition, force_block: true) if definition_present?(attribute.definition)
533
+ return
534
+ end
535
+
536
+ # `# text` inside an instance body parses as an attribute named
537
+ # Comment with no type. Emitting it as a normal assignment would
538
+ # reintroduce a String type on the next parse.
539
+ if attribute.name == "Comment" && attribute.type.to_s.empty?
540
+ line("# #{unwrap(attribute.value)}")
541
+ return
542
+ end
543
+
544
+ if Array(attribute.instances).any?
545
+ render_instance_values(attribute)
546
+ else
547
+ line("#{assignment_prefix(attribute)}#{render_data_value(unwrap(attribute.value))}")
548
+ end
549
+ end
550
+
551
+ # `name += value` extends an inherited value; the grammar stores
552
+ # that as the `extended` flag.
553
+ def assignment_prefix(attribute)
554
+ marker = attribute.extended ? "+" : ""
555
+ "#{quote_scalar(attribute.name)} #{marker}= "
556
+ end
557
+
558
+ def render_instance_values(attribute)
559
+ values = Array(attribute.instances)
560
+ if values.one?
561
+ render_instance(values.first, assignment_prefix(attribute))
562
+ return
563
+ end
564
+
565
+ line("#{assignment_prefix(attribute)}[")
566
+ @level += 1
567
+ values.each { |instance| render_instance(instance) }
568
+ @level -= 1
569
+ line("]")
570
+ end
571
+
572
+ def render_template(entries)
573
+ render_block("template") do
574
+ Array(entries).each do |entry|
575
+ member { render_instance_attribute(entry) }
576
+ end
577
+ end
578
+ end
579
+
580
+ # ---- values ----
581
+
582
+ def render_data_value(value)
583
+ value = unwrap(value)
584
+ case value
585
+ when nil then "null"
586
+ when Float then number_scalar(value)
587
+ when true, false, Integer then value.to_s
588
+ when String then quote_always(value)
589
+ when Array
590
+ "[#{value.map { |item| render_data_value(item) }.join(', ')}]"
591
+ when Hash
592
+ reference = value[:reference] || value["reference"]
593
+ return "ref:(#{reference})" if reference
594
+
595
+ pairs = value.map do |key, item|
596
+ "#{quote_scalar(key)} = #{render_data_value(item)}"
597
+ end
598
+ "{ #{pairs.join(' ')} }"
599
+ else
600
+ quote_always(value.to_s)
601
+ end
602
+ end
603
+
604
+ def unwrap(value)
605
+ value = value.value while value.is_a?(Lutaml::Lml::LiteralValue)
606
+ value
607
+ end
608
+
609
+ def cardinality_token(cardinality)
610
+ min = cardinality.min.to_s
611
+ max = cardinality.max.to_s
612
+ max = "n" if max == "*"
613
+ max.empty? ? min : "#{min}..#{max}"
614
+ end
615
+
616
+ def attribute_cardinality(attribute)
617
+ cardinality = attribute.cardinality
618
+ return nil if cardinality.nil?
619
+
620
+ token = cardinality_token(cardinality)
621
+ token == "0..1" ? nil : token
622
+ end
623
+
624
+ # ---- definitions and comments ----
625
+
626
+ def definition_present?(definition)
627
+ text = definition_text(definition)
628
+ !(text.nil? || text.empty?)
629
+ end
630
+
631
+ def definition_text(definition)
632
+ definition&.to_s
633
+ end
634
+
635
+ def render_definition(definition, force_block: false)
636
+ text = definition_text(definition)
637
+ return if text.nil? || text.empty?
638
+
639
+ if force_block || text.include?("\n")
640
+ render_definition_block(text)
641
+ else
642
+ line("definition #{quote_always(text)}")
643
+ end
644
+ end
645
+
646
+ def render_definition_block(text)
647
+ line("definition {")
648
+ @level += 1
649
+ text.split("\n", -1).each do |part|
650
+ part.empty? ? (@lines << "") : line(escape_braces(part))
651
+ end
652
+ @level -= 1
653
+ trim_trailing_blanks
654
+ line("}")
655
+ end
656
+
657
+ def escape_braces(text)
658
+ text.gsub(/[{}]/) { |brace| "\\#{brace}" }
659
+ end
660
+
661
+ def render_comments(comments)
662
+ Array(comments).each { |comment| member { render_comment(comment) } }
663
+ end
664
+
665
+ def render_comment(comment)
666
+ text = comment.to_s
667
+ if text.include?("\n")
668
+ line("*|#{text}|*")
669
+ elsif text.empty?
670
+ line("**")
671
+ else
672
+ line("** #{text}")
673
+ end
674
+ end
675
+
676
+ # ---- headers ----
677
+
678
+ def declaration_visibility(visibility)
679
+ word = visibility.to_s
680
+ VISIBILITY_WORDS.include?(word) ? "#{word} " : ""
681
+ end
682
+
683
+ def visibility_symbol(visibility)
684
+ VISIBILITY_SYMBOLS.fetch(visibility.to_s, "")
685
+ end
686
+
687
+ def public_visibility?(visibility)
688
+ visibility.nil? || visibility.to_s.empty? || visibility.to_s == "public"
689
+ end
690
+
691
+ def stereotype_suffix(stereotypes)
692
+ values = Array(stereotypes)
693
+ values.empty? ? "" : " <<#{values.first}>>"
694
+ end
695
+
696
+ # ---- layout ----
697
+
698
+ def render_collection(owner, collection)
699
+ Array(owner.public_send(collection)).each do |item|
700
+ member { yield item }
701
+ end
702
+ end
703
+
704
+ def render_block(header)
705
+ line("#{header} {")
706
+ @level += 1
707
+ @bodies.push(@lines.length)
708
+ yield
709
+ @bodies.pop
710
+ @level -= 1
711
+ trim_trailing_blanks
712
+ line("}")
713
+ end
714
+
715
+ def member
716
+ blank_line if @bodies.any? && @lines.length > @bodies.last
717
+ yield
718
+ end
719
+
720
+ def top
721
+ blank_line if @bodies.empty? || @lines.length > @bodies.last
722
+ yield
723
+ end
724
+
725
+ def line(text)
726
+ @lines << "#{' ' * @level}#{text}"
727
+ end
728
+
729
+ def blank_line
730
+ @lines << "" unless @lines.empty? || @lines.last == ""
731
+ end
732
+
733
+ def trim_trailing_blanks
734
+ @lines.pop while @lines.last == ""
735
+ end
736
+ end
737
+ end
738
+ end