@linked.archi/kernel 0.0.0-stage → 0.1.5

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 (365) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +11 -0
  3. package/README.md +96 -2
  4. package/assets/arch-decision/arch-decision-metamodel.ttl +44 -0
  5. package/assets/arch-decision/arch-decision-onto.ttl +1566 -0
  6. package/assets/arch-decision/arch-decision-shapes.ttl +582 -0
  7. package/assets/arch-decision/arch-decision-tax.ttl +129 -0
  8. package/assets/arch-processes/arch-processes-metamodel.ttl +42 -0
  9. package/assets/arch-processes/arch-processes-tax.ttl +136 -0
  10. package/assets/arch-processes/archProcesses-shapes.ttl +189 -0
  11. package/assets/arch-processes/archProcesses.ttl +953 -0
  12. package/assets/archimate3/archimate3.2-element-shapes.ttl +379 -0
  13. package/assets/archimate3/archimate3.2-metamodel.ttl +205 -0
  14. package/assets/archimate3/archimate3.2-notation.ttl +1503 -0
  15. package/assets/archimate3/archimate3.2-onto.ttl +1579 -0
  16. package/assets/archimate3/archimate3.2-presentation-contexts.ttl +90 -0
  17. package/assets/archimate3/archimate3.2-principle-shapes.ttl +923 -0
  18. package/assets/archimate3/archimate3.2-questions.ttl +1125 -0
  19. package/assets/archimate3/archimate3.2-reference-data.ttl +74 -0
  20. package/assets/archimate3/archimate3.2-relationship-shapes.ttl +30956 -0
  21. package/assets/archimate3/archimate3.2-relationship-tables.ttl +5992 -0
  22. package/assets/archimate3/archimate3.2-tax.ttl +724 -0
  23. package/assets/archimate3/archimate3.2-viewpoint-shapes.ttl +59 -0
  24. package/assets/archimate3/archimate3.2-viewpoints.ttl +793 -0
  25. package/assets/archimate3/icons/and-junction.svg +3 -0
  26. package/assets/archimate3/icons/application-collaboration.svg +22 -0
  27. package/assets/archimate3/icons/application-component.svg +18 -0
  28. package/assets/archimate3/icons/application-event.svg +15 -0
  29. package/assets/archimate3/icons/application-function.svg +14 -0
  30. package/assets/archimate3/icons/application-interaction.svg +17 -0
  31. package/assets/archimate3/icons/application-interface.svg +16 -0
  32. package/assets/archimate3/icons/application-process.svg +14 -0
  33. package/assets/archimate3/icons/application-service.svg +14 -0
  34. package/assets/archimate3/icons/artifact.svg +16 -0
  35. package/assets/archimate3/icons/assessment.svg +17 -0
  36. package/assets/archimate3/icons/business-actor.svg +22 -0
  37. package/assets/archimate3/icons/business-collaboration.svg +21 -0
  38. package/assets/archimate3/icons/business-event.svg +15 -0
  39. package/assets/archimate3/icons/business-function.svg +14 -0
  40. package/assets/archimate3/icons/business-interaction.svg +18 -0
  41. package/assets/archimate3/icons/business-interface.svg +16 -0
  42. package/assets/archimate3/icons/business-object.svg +14 -0
  43. package/assets/archimate3/icons/business-process.svg +15 -0
  44. package/assets/archimate3/icons/business-role.svg +16 -0
  45. package/assets/archimate3/icons/business-service.svg +14 -0
  46. package/assets/archimate3/icons/capability.svg +40 -0
  47. package/assets/archimate3/icons/communication-network.svg +18 -0
  48. package/assets/archimate3/icons/constraint.svg +18 -0
  49. package/assets/archimate3/icons/contract.svg +13 -0
  50. package/assets/archimate3/icons/course-of-action.svg +20 -0
  51. package/assets/archimate3/icons/data-object.svg +14 -0
  52. package/assets/archimate3/icons/deliverable.svg +11 -0
  53. package/assets/archimate3/icons/device.svg +18 -0
  54. package/assets/archimate3/icons/distribution-network.svg +19 -0
  55. package/assets/archimate3/icons/driver.svg +24 -0
  56. package/assets/archimate3/icons/equipment.svg +31 -0
  57. package/assets/archimate3/icons/facility.svg +16 -0
  58. package/assets/archimate3/icons/gap.svg +22 -0
  59. package/assets/archimate3/icons/glyphs/activity.svg +3 -0
  60. package/assets/archimate3/icons/glyphs/actor.svg +14 -0
  61. package/assets/archimate3/icons/glyphs/application-component.svg +12 -0
  62. package/assets/archimate3/icons/glyphs/artifact.svg +9 -0
  63. package/assets/archimate3/icons/glyphs/assessment.svg +9 -0
  64. package/assets/archimate3/icons/glyphs/capability.svg +32 -0
  65. package/assets/archimate3/icons/glyphs/cloud-gear.svg +3 -0
  66. package/assets/archimate3/icons/glyphs/collaboration.svg +15 -0
  67. package/assets/archimate3/icons/glyphs/communication-network.svg +12 -0
  68. package/assets/archimate3/icons/glyphs/constraint.svg +10 -0
  69. package/assets/archimate3/icons/glyphs/contract.svg +5 -0
  70. package/assets/archimate3/icons/glyphs/course-of-action.svg +12 -0
  71. package/assets/archimate3/icons/glyphs/data-object.svg +4 -0
  72. package/assets/archimate3/icons/glyphs/deliverable.svg +3 -0
  73. package/assets/archimate3/icons/glyphs/device.svg +10 -0
  74. package/assets/archimate3/icons/glyphs/distribution-network.svg +11 -0
  75. package/assets/archimate3/icons/glyphs/driver.svg +16 -0
  76. package/assets/archimate3/icons/glyphs/equipment.svg +22 -0
  77. package/assets/archimate3/icons/glyphs/event.svg +6 -0
  78. package/assets/archimate3/icons/glyphs/facility.svg +7 -0
  79. package/assets/archimate3/icons/glyphs/function.svg +6 -0
  80. package/assets/archimate3/icons/glyphs/gap.svg +14 -0
  81. package/assets/archimate3/icons/glyphs/goal.svg +10 -0
  82. package/assets/archimate3/icons/glyphs/grouping.svg +4 -0
  83. package/assets/archimate3/icons/glyphs/interaction.svg +10 -0
  84. package/assets/archimate3/icons/glyphs/interface.svg +8 -0
  85. package/assets/archimate3/icons/glyphs/location.svg +6 -0
  86. package/assets/archimate3/icons/glyphs/material.svg +12 -0
  87. package/assets/archimate3/icons/glyphs/meaning.svg +3 -0
  88. package/assets/archimate3/icons/glyphs/node.svg +8 -0
  89. package/assets/archimate3/icons/glyphs/object.svg +4 -0
  90. package/assets/archimate3/icons/glyphs/outcome.svg +16 -0
  91. package/assets/archimate3/icons/glyphs/path.svg +10 -0
  92. package/assets/archimate3/icons/glyphs/person-silhouette.svg +3 -0
  93. package/assets/archimate3/icons/glyphs/plateau.svg +8 -0
  94. package/assets/archimate3/icons/glyphs/principle.svg +12 -0
  95. package/assets/archimate3/icons/glyphs/process.svg +6 -0
  96. package/assets/archimate3/icons/glyphs/product.svg +4 -0
  97. package/assets/archimate3/icons/glyphs/representation.svg +4 -0
  98. package/assets/archimate3/icons/glyphs/requirement.svg +6 -0
  99. package/assets/archimate3/icons/glyphs/resource.svg +14 -0
  100. package/assets/archimate3/icons/glyphs/role.svg +6 -0
  101. package/assets/archimate3/icons/glyphs/service.svg +6 -0
  102. package/assets/archimate3/icons/glyphs/stakeholder.svg +6 -0
  103. package/assets/archimate3/icons/glyphs/system-software.svg +14 -0
  104. package/assets/archimate3/icons/glyphs/value-stream.svg +3 -0
  105. package/assets/archimate3/icons/glyphs/value.svg +3 -0
  106. package/assets/archimate3/icons/glyphs/work-package.svg +4 -0
  107. package/assets/archimate3/icons/goal.svg +18 -0
  108. package/assets/archimate3/icons/grouping.svg +12 -0
  109. package/assets/archimate3/icons/implementation-event.svg +15 -0
  110. package/assets/archimate3/icons/location.svg +14 -0
  111. package/assets/archimate3/icons/material.svg +21 -0
  112. package/assets/archimate3/icons/meaning.svg +11 -0
  113. package/assets/archimate3/icons/node.svg +14 -0
  114. package/assets/archimate3/icons/or-junction.svg +3 -0
  115. package/assets/archimate3/icons/outcome.svg +25 -0
  116. package/assets/archimate3/icons/path.svg +18 -0
  117. package/assets/archimate3/icons/plateau.svg +16 -0
  118. package/assets/archimate3/icons/principle.svg +21 -0
  119. package/assets/archimate3/icons/product.svg +12 -0
  120. package/assets/archimate3/icons/representation.svg +12 -0
  121. package/assets/archimate3/icons/requirement.svg +14 -0
  122. package/assets/archimate3/icons/resource.svg +23 -0
  123. package/assets/archimate3/icons/stakeholder.svg +15 -0
  124. package/assets/archimate3/icons/system-software.svg +22 -0
  125. package/assets/archimate3/icons/technology-collaboration.svg +22 -0
  126. package/assets/archimate3/icons/technology-event.svg +15 -0
  127. package/assets/archimate3/icons/technology-function.svg +14 -0
  128. package/assets/archimate3/icons/technology-interaction.svg +18 -0
  129. package/assets/archimate3/icons/technology-interface.svg +16 -0
  130. package/assets/archimate3/icons/technology-process.svg +14 -0
  131. package/assets/archimate3/icons/technology-service.svg +14 -0
  132. package/assets/archimate3/icons/value-stream.svg +11 -0
  133. package/assets/archimate3/icons/value.svg +11 -0
  134. package/assets/archimate3/icons/work-package.svg +12 -0
  135. package/assets/archimate4/archimate4-element-shapes.ttl +404 -0
  136. package/assets/archimate4/archimate4-metamodel.ttl +179 -0
  137. package/assets/archimate4/archimate4-notation.ttl +1147 -0
  138. package/assets/archimate4/archimate4-onto.ttl +1559 -0
  139. package/assets/archimate4/archimate4-presentation-contexts.ttl +60 -0
  140. package/assets/archimate4/archimate4-principle-shapes.ttl +893 -0
  141. package/assets/archimate4/archimate4-questions.ttl +1224 -0
  142. package/assets/archimate4/archimate4-reference-data.ttl +56 -0
  143. package/assets/archimate4/archimate4-relationship-shapes.ttl +16920 -0
  144. package/assets/archimate4/archimate4-relationship-tables.ttl +3200 -0
  145. package/assets/archimate4/archimate4-tax.ttl +749 -0
  146. package/assets/archimate4/archimate4-viewpoint-shapes.ttl +55 -0
  147. package/assets/archimate4/archimate4-viewpoints.ttl +777 -0
  148. package/assets/archimate4/icons/and-junction.svg +3 -0
  149. package/assets/archimate4/icons/application-component.svg +18 -0
  150. package/assets/archimate4/icons/application-interface.svg +16 -0
  151. package/assets/archimate4/icons/artifact.svg +16 -0
  152. package/assets/archimate4/icons/assessment.svg +17 -0
  153. package/assets/archimate4/icons/business-actor.svg +22 -0
  154. package/assets/archimate4/icons/business-interface.svg +16 -0
  155. package/assets/archimate4/icons/business-object.svg +14 -0
  156. package/assets/archimate4/icons/capability.svg +40 -0
  157. package/assets/archimate4/icons/collaboration.svg +21 -0
  158. package/assets/archimate4/icons/communication-network.svg +18 -0
  159. package/assets/archimate4/icons/course-of-action.svg +20 -0
  160. package/assets/archimate4/icons/data-object.svg +14 -0
  161. package/assets/archimate4/icons/deliverable.svg +11 -0
  162. package/assets/archimate4/icons/device.svg +18 -0
  163. package/assets/archimate4/icons/distribution-network.svg +19 -0
  164. package/assets/archimate4/icons/driver.svg +24 -0
  165. package/assets/archimate4/icons/equipment.svg +31 -0
  166. package/assets/archimate4/icons/event.svg +15 -0
  167. package/assets/archimate4/icons/facility.svg +16 -0
  168. package/assets/archimate4/icons/function.svg +13 -0
  169. package/assets/archimate4/icons/glyphs/activity.svg +3 -0
  170. package/assets/archimate4/icons/glyphs/actor.svg +14 -0
  171. package/assets/archimate4/icons/glyphs/application-component.svg +12 -0
  172. package/assets/archimate4/icons/glyphs/artifact.svg +9 -0
  173. package/assets/archimate4/icons/glyphs/assessment.svg +9 -0
  174. package/assets/archimate4/icons/glyphs/capability.svg +32 -0
  175. package/assets/archimate4/icons/glyphs/cloud-gear.svg +3 -0
  176. package/assets/archimate4/icons/glyphs/collaboration.svg +15 -0
  177. package/assets/archimate4/icons/glyphs/communication-network.svg +12 -0
  178. package/assets/archimate4/icons/glyphs/constraint.svg +10 -0
  179. package/assets/archimate4/icons/glyphs/contract.svg +5 -0
  180. package/assets/archimate4/icons/glyphs/course-of-action.svg +12 -0
  181. package/assets/archimate4/icons/glyphs/data-object.svg +4 -0
  182. package/assets/archimate4/icons/glyphs/deliverable.svg +3 -0
  183. package/assets/archimate4/icons/glyphs/device.svg +10 -0
  184. package/assets/archimate4/icons/glyphs/distribution-network.svg +11 -0
  185. package/assets/archimate4/icons/glyphs/driver.svg +16 -0
  186. package/assets/archimate4/icons/glyphs/equipment.svg +22 -0
  187. package/assets/archimate4/icons/glyphs/event.svg +6 -0
  188. package/assets/archimate4/icons/glyphs/facility.svg +7 -0
  189. package/assets/archimate4/icons/glyphs/function.svg +6 -0
  190. package/assets/archimate4/icons/glyphs/gap.svg +14 -0
  191. package/assets/archimate4/icons/glyphs/goal.svg +10 -0
  192. package/assets/archimate4/icons/glyphs/grouping.svg +4 -0
  193. package/assets/archimate4/icons/glyphs/interaction.svg +10 -0
  194. package/assets/archimate4/icons/glyphs/interface.svg +8 -0
  195. package/assets/archimate4/icons/glyphs/location.svg +6 -0
  196. package/assets/archimate4/icons/glyphs/material.svg +12 -0
  197. package/assets/archimate4/icons/glyphs/meaning.svg +3 -0
  198. package/assets/archimate4/icons/glyphs/node.svg +8 -0
  199. package/assets/archimate4/icons/glyphs/object.svg +4 -0
  200. package/assets/archimate4/icons/glyphs/outcome.svg +16 -0
  201. package/assets/archimate4/icons/glyphs/path.svg +10 -0
  202. package/assets/archimate4/icons/glyphs/person-silhouette.svg +3 -0
  203. package/assets/archimate4/icons/glyphs/plateau.svg +8 -0
  204. package/assets/archimate4/icons/glyphs/principle.svg +12 -0
  205. package/assets/archimate4/icons/glyphs/process.svg +6 -0
  206. package/assets/archimate4/icons/glyphs/product.svg +4 -0
  207. package/assets/archimate4/icons/glyphs/representation.svg +4 -0
  208. package/assets/archimate4/icons/glyphs/requirement.svg +6 -0
  209. package/assets/archimate4/icons/glyphs/resource.svg +14 -0
  210. package/assets/archimate4/icons/glyphs/role.svg +6 -0
  211. package/assets/archimate4/icons/glyphs/service.svg +6 -0
  212. package/assets/archimate4/icons/glyphs/stakeholder.svg +6 -0
  213. package/assets/archimate4/icons/glyphs/system-software.svg +14 -0
  214. package/assets/archimate4/icons/glyphs/value-stream.svg +3 -0
  215. package/assets/archimate4/icons/glyphs/value.svg +3 -0
  216. package/assets/archimate4/icons/glyphs/work-package.svg +4 -0
  217. package/assets/archimate4/icons/goal.svg +18 -0
  218. package/assets/archimate4/icons/grouping.svg +12 -0
  219. package/assets/archimate4/icons/location.svg +14 -0
  220. package/assets/archimate4/icons/material.svg +21 -0
  221. package/assets/archimate4/icons/meaning.svg +11 -0
  222. package/assets/archimate4/icons/node.svg +14 -0
  223. package/assets/archimate4/icons/or-junction.svg +3 -0
  224. package/assets/archimate4/icons/outcome.svg +25 -0
  225. package/assets/archimate4/icons/path.svg +18 -0
  226. package/assets/archimate4/icons/plateau.svg +16 -0
  227. package/assets/archimate4/icons/principle.svg +21 -0
  228. package/assets/archimate4/icons/process.svg +15 -0
  229. package/assets/archimate4/icons/product.svg +12 -0
  230. package/assets/archimate4/icons/requirement.svg +14 -0
  231. package/assets/archimate4/icons/resource.svg +23 -0
  232. package/assets/archimate4/icons/role.svg +16 -0
  233. package/assets/archimate4/icons/service.svg +13 -0
  234. package/assets/archimate4/icons/stakeholder.svg +15 -0
  235. package/assets/archimate4/icons/system-software.svg +22 -0
  236. package/assets/archimate4/icons/technology-interface.svg +16 -0
  237. package/assets/archimate4/icons/value-stream.svg +11 -0
  238. package/assets/archimate4/icons/value.svg +11 -0
  239. package/assets/archimate4/icons/work-package.svg +12 -0
  240. package/assets/basic/basic-metamodel.ttl +100 -0
  241. package/assets/basic/basic-notation-bubble.ttl +175 -0
  242. package/assets/basic/basic-notation.ttl +172 -0
  243. package/assets/basic/basic-onto.ttl +171 -0
  244. package/assets/basic/basic-presentation-contexts.ttl +70 -0
  245. package/assets/basic/basic-tax.ttl +130 -0
  246. package/assets/basic/icons/aggregation.svg +7 -0
  247. package/assets/basic/icons/association.svg +6 -0
  248. package/assets/basic/icons/element.svg +5 -0
  249. package/assets/basic/icons/glyphs/aggregation.svg +6 -0
  250. package/assets/basic/icons/glyphs/association.svg +5 -0
  251. package/assets/basic/icons/glyphs/element.svg +5 -0
  252. package/assets/basic/icons/glyphs/relationship.svg +5 -0
  253. package/assets/basic/icons/relationship.svg +7 -0
  254. package/assets/basic/notation-bubble/icons/aggregation.svg +7 -0
  255. package/assets/basic/notation-bubble/icons/association.svg +6 -0
  256. package/assets/basic/notation-bubble/icons/element.svg +5 -0
  257. package/assets/basic/notation-bubble/icons/glyphs/aggregation.svg +6 -0
  258. package/assets/basic/notation-bubble/icons/glyphs/association.svg +5 -0
  259. package/assets/basic/notation-bubble/icons/glyphs/element.svg +5 -0
  260. package/assets/basic/notation-bubble/icons/glyphs/relationship.svg +5 -0
  261. package/assets/basic/notation-bubble/icons/relationship.svg +7 -0
  262. package/assets/catalog.ttl +7302 -0
  263. package/assets/core/core-notation.ttl +131 -0
  264. package/assets/core/core-onto.ttl +2811 -0
  265. package/assets/core/core-questions.ttl +957 -0
  266. package/assets/core/core-shapes.ttl +915 -0
  267. package/assets/core/core-vis-onto.ttl +1408 -0
  268. package/assets/core/core-vis-shapes.ttl +840 -0
  269. package/assets/core-vis/markers/circle-dot.svg +3 -0
  270. package/assets/core-vis/markers/closed-arrowhead.svg +3 -0
  271. package/assets/core-vis/markers/filled-diamond.svg +3 -0
  272. package/assets/core-vis/markers/half-arrowhead.svg +3 -0
  273. package/assets/core-vis/markers/hollow-arrowhead.svg +3 -0
  274. package/assets/core-vis/markers/hollow-diamond.svg +3 -0
  275. package/assets/core-vis/markers/open-arrowhead.svg +3 -0
  276. package/fixtures/interoperability/archimate4-demo.trig +259 -0
  277. package/fixtures/interoperability/expected-findings.json +26 -0
  278. package/fixtures/interoperability/shop-seed.ttl +225 -0
  279. package/package.json +136 -4
  280. package/scripts/sync-assets.mjs +252 -0
  281. package/src/conformance.ts +66 -0
  282. package/src/import-export/model-io.ts +426 -0
  283. package/src/index.ts +11 -0
  284. package/src/metamodel-manager/asset-sources.ts +754 -0
  285. package/src/metamodel-manager/bundled-assets.ts +599 -0
  286. package/src/metamodel-manager/catalogue.ts +114 -0
  287. package/src/metamodel-manager/class-hierarchy.ts +155 -0
  288. package/src/metamodel-manager/index.ts +65 -0
  289. package/src/metamodel-manager/interfaces.ts +422 -0
  290. package/src/metamodel-manager/metamodel-completeness.ts +162 -0
  291. package/src/metamodel-manager/metamodel-loader.ts +864 -0
  292. package/src/metamodel-manager/notation-resolver.ts +284 -0
  293. package/src/metamodel-manager/notation.ts +659 -0
  294. package/src/metamodel-manager/palette-generator.ts +882 -0
  295. package/src/metamodel-manager/property-shapes.ts +290 -0
  296. package/src/metamodel-manager/taxonomy.ts +904 -0
  297. package/src/metamodel-manager/type-guide.ts +241 -0
  298. package/src/metamodel-manager/viewpoint-manager.ts +147 -0
  299. package/src/model-manager/appearance.ts +389 -0
  300. package/src/model-manager/cascade.ts +246 -0
  301. package/src/model-manager/clipboard.ts +566 -0
  302. package/src/model-manager/context-names.ts +108 -0
  303. package/src/model-manager/custom-properties.ts +549 -0
  304. package/src/model-manager/derivations.ts +166 -0
  305. package/src/model-manager/direct-permissions.ts +182 -0
  306. package/src/model-manager/drawing-order.ts +119 -0
  307. package/src/model-manager/element-manager.ts +69 -0
  308. package/src/model-manager/element-pictures.ts +118 -0
  309. package/src/model-manager/field-names.ts +42 -0
  310. package/src/model-manager/folders.ts +354 -0
  311. package/src/model-manager/grid.ts +6 -0
  312. package/src/model-manager/index.ts +43 -0
  313. package/src/model-manager/interfaces.ts +417 -0
  314. package/src/model-manager/iri-conformance.ts +183 -0
  315. package/src/model-manager/label-expression.ts +233 -0
  316. package/src/model-manager/legacy-migration.ts +200 -0
  317. package/src/model-manager/missing-references.ts +86 -0
  318. package/src/model-manager/model-diff-text.ts +150 -0
  319. package/src/model-manager/model-diff.ts +534 -0
  320. package/src/model-manager/model-merge.ts +1160 -0
  321. package/src/model-manager/model-namespace.ts +199 -0
  322. package/src/model-manager/model-operations.ts +132 -0
  323. package/src/model-manager/model-queries.ts +616 -0
  324. package/src/model-manager/model-session.ts +759 -0
  325. package/src/model-manager/multiplicity.ts +74 -0
  326. package/src/model-manager/nested-resources.ts +145 -0
  327. package/src/model-manager/own-metamodel.ts +157 -0
  328. package/src/model-manager/placement-copy.ts +116 -0
  329. package/src/model-manager/property-catalog.ts +787 -0
  330. package/src/model-manager/proposal-order.ts +203 -0
  331. package/src/model-manager/provenance.ts +396 -0
  332. package/src/model-manager/questions.ts +470 -0
  333. package/src/model-manager/rdf-scope.ts +97 -0
  334. package/src/model-manager/relationship-graph.ts +462 -0
  335. package/src/model-manager/relationship-manager.ts +962 -0
  336. package/src/model-manager/relationship-table.ts +189 -0
  337. package/src/model-manager/relationship-validity.ts +261 -0
  338. package/src/model-manager/resource-edits.ts +28 -0
  339. package/src/model-manager/short-hash.ts +104 -0
  340. package/src/model-manager/snapshot-lines.ts +88 -0
  341. package/src/model-manager/version-history.ts +205 -0
  342. package/src/model-manager/view-from-results.ts +81 -0
  343. package/src/model-manager/view-manager.ts +1410 -0
  344. package/src/model-manager/vocabulary.ts +279 -0
  345. package/src/rdf-engine/finding-shape.ts +23 -0
  346. package/src/rdf-engine/graph-scope.ts +163 -0
  347. package/src/rdf-engine/index.ts +7 -0
  348. package/src/rdf-engine/interfaces.ts +144 -0
  349. package/src/rdf-engine/iri.ts +203 -0
  350. package/src/rdf-engine/localized-text.ts +13 -0
  351. package/src/rdf-engine/namespaces.ts +41 -0
  352. package/src/rdf-engine/observable-store.ts +631 -0
  353. package/src/rdf-engine/rdf-store.ts +508 -0
  354. package/src/rdf-engine/shacl-validator.ts +602 -0
  355. package/src/rdf-engine/shape-reach.ts +140 -0
  356. package/src/rdf-engine/shape-switches.ts +165 -0
  357. package/src/rdf-engine/single-valued.ts +219 -0
  358. package/src/rdf-engine/sparql-constraints.ts +334 -0
  359. package/src/rdf-engine/term-codec.ts +51 -0
  360. package/src/rdf-engine/ttl-parser.ts +51 -0
  361. package/src/rdf-engine/ttl-serializer.ts +79 -0
  362. package/src/rdf-engine/turtle-writer.ts +284 -0
  363. package/src/rdf-engine/validation-engine.ts +682 -0
  364. package/src/rdf-engine/validation-service.ts +184 -0
  365. package/src/rdf-engine/validation.worker.ts +47 -0
@@ -0,0 +1,754 @@
1
+ import type { BundledAssetOptions } from './bundled-assets.js';
2
+ /**
3
+ * Where metamodel documents come from.
4
+ *
5
+ * The loader follows a manifest's `arch:*` references and `owl:imports` and asks
6
+ * an {@link AssetSource} for each document IRI it discovers. Three sources exist,
7
+ * and the application chains them:
8
+ *
9
+ * - {@link UploadedAssetSource} — documents the user supplied (file upload or a
10
+ * pasted IRI whose content was already fetched). Consulted first, so a local
11
+ * edit of a published file overrides the published one. An upload of several
12
+ * files — a folder — is indexed by every IRI each file can be referred to by,
13
+ * and an uploaded manifest's namespace is not asked of the network (KD-27).
14
+ * - {@link RemoteAssetSource} — dereferences the IRI over HTTP with
15
+ * `Accept: text/turtle`. `meta.linked.archi` serves every document with
16
+ * `Access-Control-Allow-Origin: *`, so the browser needs no proxy.
17
+ * - {@link BundledAssetSource} — the copies under `assets/`, which mirror the
18
+ * published site. The offline fallback, and the only source in tests.
19
+ *
20
+ * ## Why the Accept header is a single type
21
+ *
22
+ * `meta.linked.archi` content-negotiates on the literal header value; a
23
+ * multi-valued header with quality parameters (`text/turtle;q=1, *\/*;q=0.1`)
24
+ * is answered with a 404 rather than the best match. One exact type is sent.
25
+ */
26
+
27
+ import { Parser as N3Parser } from 'n3';
28
+ import type { Quad } from '@rdfjs/types';
29
+ import * as oxigraph from 'oxigraph';
30
+ import {
31
+ BUNDLED_CATALOGUE_PATH,
32
+ CATALOGUE_IRI,
33
+ fetchAssetText,
34
+ getAssetByIri,
35
+ type BundledAssetRole,
36
+ } from './bundled-assets.js';
37
+
38
+ /** Serialization of a fetched document, as far as the loader cares. */
39
+ export type AssetFormat =
40
+ | 'text/turtle'
41
+ | 'application/trig'
42
+ | 'application/n-triples'
43
+ | 'application/n-quads'
44
+ | 'application/ld+json'
45
+ | 'application/rdf+xml';
46
+
47
+ /** Where a document was found. */
48
+ export type AssetOrigin = 'bundled' | 'remote' | 'uploaded';
49
+
50
+ /** A document as returned by a source. */
51
+ export interface AssetDocument {
52
+ /**
53
+ * The document's name as the source has it — the named-graph key. The IRI asked for, except from an upload, which
54
+ * answers with the file's own name: `<…/onto>` asked for as `<…/onto#>` (KD-27).
55
+ */
56
+ documentIri: string;
57
+ /** Raw text. */
58
+ text: string;
59
+ /** Serialization of {@link text}. */
60
+ format: AssetFormat;
61
+ /** Which source answered. */
62
+ origin: AssetOrigin;
63
+ /** The URL actually read, when it differs from the IRI (bundled path, redirect). */
64
+ location?: string;
65
+ /** Role, when the source knows it (the bundle registry does). */
66
+ role?: BundledAssetRole;
67
+ }
68
+
69
+ /** Something that can turn a document IRI into its content. */
70
+ export interface AssetSource {
71
+ /** A short name for status reporting. */
72
+ readonly kind: AssetOrigin | 'chain';
73
+ /**
74
+ * The document behind `documentIri`, or `null` when this source does not
75
+ * have it. Throws only for a real failure (the source *should* have it but
76
+ * could not read it), so a chain can tell "absent" from "broken".
77
+ */
78
+ fetch(documentIri: string): Promise<AssetDocument | null>;
79
+ /**
80
+ * Whether this source stands for every document under the IRI's namespace, so that one it does not have is not to
81
+ * be asked of the network ({@link ChainedAssetSource}): an uploaded metamodel's (KReq 3.7).
82
+ */
83
+ claims?(documentIri: string): boolean;
84
+ }
85
+
86
+ /** The URL to dereference for a document IRI: the IRI without its fragment. */
87
+ export function dereferenceUrl(documentIri: string): string {
88
+ const hash = documentIri.indexOf('#');
89
+ return hash === -1 ? documentIri : documentIri.slice(0, hash);
90
+ }
91
+
92
+ /**
93
+ * Guess a format from a media type and, failing that, a file name.
94
+ *
95
+ * Returns `text/turtle` when nothing matches: Turtle is what every Linked.Archi
96
+ * document is published as, and N-Triples is a subset of it.
97
+ */
98
+ export function detectFormat(contentType: string | null | undefined, name?: string): AssetFormat {
99
+ const type = (contentType ?? '').split(';')[0].trim().toLowerCase();
100
+ if (type === 'application/trig') return 'application/trig';
101
+ if (type === 'application/n-quads') return 'application/n-quads';
102
+ if (type === 'application/n-triples') return 'application/n-triples';
103
+ if (type === 'application/ld+json' || type === 'application/json') return 'application/ld+json';
104
+ if (type === 'application/rdf+xml' || type === 'application/xml' || type === 'text/xml') {
105
+ return 'application/rdf+xml';
106
+ }
107
+ const lower = (name ?? '').toLowerCase();
108
+ if (lower.endsWith('.trig')) return 'application/trig';
109
+ if (lower.endsWith('.nq')) return 'application/n-quads';
110
+ if (lower.endsWith('.nt')) return 'application/n-triples';
111
+ if (lower.endsWith('.jsonld') || lower.endsWith('.json')) return 'application/ld+json';
112
+ if (lower.endsWith('.rdf') || lower.endsWith('.owl') || lower.endsWith('.xml')) {
113
+ return 'application/rdf+xml';
114
+ }
115
+ return 'text/turtle';
116
+ }
117
+
118
+ /**
119
+ * Parse any supported serialization into quads.
120
+ *
121
+ * Turtle-family formats go through n3 (fast, keeps prefixes available to the
122
+ * caller); JSON-LD and RDF/XML go through a throw-away Oxigraph store, which
123
+ * parses both natively. Terms come back as n3 terms either way.
124
+ */
125
+ export function parseRdf(text: string, format: AssetFormat, baseIri?: string): Quad[] {
126
+ if (format === 'application/ld+json' || format === 'application/rdf+xml') {
127
+ const scratch = new oxigraph.Store();
128
+ scratch.load(text, { format, ...(baseIri ? { base_iri: baseIri } : {}) });
129
+ const nquads = scratch.dump({ format: 'application/n-quads' });
130
+ return new N3Parser({ format: 'application/n-quads' }).parse(nquads) as unknown as Quad[];
131
+ }
132
+ const parser = new N3Parser({ format, ...(baseIri ? { baseIRI: baseIri } : {}) });
133
+ return parser.parse(text) as unknown as Quad[];
134
+ }
135
+
136
+ // ── Bundled ─────────────────────────────────────────────────────────────
137
+
138
+ /** Reads the copies shipped under `assets/` — the registry's files, and the catalogue. */
139
+ export class BundledAssetSource implements AssetSource {
140
+ readonly kind = 'bundled' as const;
141
+
142
+ constructor(private readonly options: BundledAssetOptions = {}) {}
143
+
144
+ async fetch(documentIri: string): Promise<AssetDocument | null> {
145
+ if (documentIri === CATALOGUE_IRI) {
146
+ const text = await fetchAssetText(BUNDLED_CATALOGUE_PATH, this.options);
147
+ return { documentIri, text, format: 'text/turtle', origin: 'bundled', location: BUNDLED_CATALOGUE_PATH };
148
+ }
149
+ const asset = getAssetByIri(documentIri);
150
+ if (!asset) return null;
151
+ const text = await fetchAssetText(asset, this.options);
152
+ return {
153
+ documentIri,
154
+ text,
155
+ format: 'text/turtle',
156
+ origin: 'bundled',
157
+ location: asset.path,
158
+ role: asset.role,
159
+ };
160
+ }
161
+ }
162
+
163
+ // ── Remote ──────────────────────────────────────────────────────────────
164
+
165
+ /** The part of `fetch` this module uses; injectable for tests. */
166
+ export type FetchLike = (
167
+ url: string,
168
+ init?: { headers?: Record<string, string>; signal?: AbortSignal; cache?: 'no-cache' },
169
+ ) => Promise<{
170
+ ok: boolean;
171
+ status: number;
172
+ statusText: string;
173
+ url?: string;
174
+ headers: { get(name: string): string | null };
175
+ text(): Promise<string>;
176
+ }>;
177
+
178
+ /** Options for {@link RemoteAssetSource}. */
179
+ export interface RemoteAssetSourceOptions {
180
+ /** Defaults to the global `fetch`. */
181
+ fetchImpl?: FetchLike;
182
+ /** Per-request timeout. A slow network must not stall startup indefinitely. */
183
+ timeoutMs?: number;
184
+ /**
185
+ * Only dereference IRIs under these prefixes. Empty means "any http(s) IRI".
186
+ * Keeps a manifest that `owl:imports` the whole web from fanning out.
187
+ */
188
+ allowedPrefixes?: readonly string[];
189
+ }
190
+
191
+ /** Dereferences document IRIs over HTTP. */
192
+ export class RemoteAssetSource implements AssetSource {
193
+ readonly kind = 'remote' as const;
194
+ private readonly fetchImpl: FetchLike | undefined;
195
+ private readonly timeoutMs: number;
196
+ private readonly allowedPrefixes: readonly string[];
197
+
198
+ constructor(options: RemoteAssetSourceOptions = {}) {
199
+ this.fetchImpl =
200
+ options.fetchImpl ??
201
+ (typeof fetch === 'function' ? (fetch.bind(globalThis) as unknown as FetchLike) : undefined);
202
+ this.timeoutMs = options.timeoutMs ?? 10_000;
203
+ this.allowedPrefixes = options.allowedPrefixes ?? [];
204
+ }
205
+
206
+ async fetch(documentIri: string): Promise<AssetDocument | null> {
207
+ if (!this.fetchImpl) return null;
208
+ if (!/^https?:\/\//.test(documentIri)) return null;
209
+ if (
210
+ this.allowedPrefixes.length > 0 &&
211
+ !this.allowedPrefixes.some((prefix) => documentIri.startsWith(prefix))
212
+ ) {
213
+ return null;
214
+ }
215
+
216
+ const url = dereferenceUrl(documentIri);
217
+ const controller = typeof AbortController === 'function' ? new AbortController() : undefined;
218
+ const timer = controller ? setTimeout(() => controller.abort(), this.timeoutMs) : undefined;
219
+ try {
220
+ const response = await this.fetchImpl(url, {
221
+ headers: { Accept: 'text/turtle' },
222
+ // Always ask the server: meta.linked.archi sends no Cache-Control, so a
223
+ // browser would otherwise keep a "fresh" copy for hours after a publish.
224
+ // An unchanged document costs a 304.
225
+ cache: 'no-cache',
226
+ ...(controller ? { signal: controller.signal } : {}),
227
+ });
228
+ if (!response.ok) {
229
+ // A 404 is "not here", not "broken": the chain moves on to the bundle.
230
+ if (response.status === 404 || response.status === 410) return null;
231
+ throw new Error(`${response.status} ${response.statusText} for <${url}>`);
232
+ }
233
+ const text = await response.text();
234
+ const format = detectFormat(response.headers.get('content-type'), url);
235
+ if (/^\s*<!doctype html|^\s*<html/i.test(text)) {
236
+ // A site answering an unknown path with its HTML 404 page.
237
+ return null;
238
+ }
239
+ return {
240
+ documentIri,
241
+ text,
242
+ format,
243
+ origin: 'remote',
244
+ location: response.url || url,
245
+ };
246
+ } finally {
247
+ if (timer !== undefined) clearTimeout(timer);
248
+ }
249
+ }
250
+ }
251
+
252
+ // ── Uploaded ────────────────────────────────────────────────────────────
253
+
254
+ /** The extensions of the RDF files an upload reads (KReq 3.6). */
255
+ export const UPLOAD_RDF_EXTENSIONS: readonly string[] = ['.ttl', '.trig', '.jsonld', '.json', '.rdf', '.owl', '.nt', '.nq'];
256
+
257
+ /** The extensions of the images an upload keeps, by their path, as possible icons (KReq 3.8). */
258
+ export const UPLOAD_IMAGE_EXTENSIONS: readonly string[] = ['.svg', '.png', '.jpg', '.jpeg', '.gif', '.webp'];
259
+
260
+ /**
261
+ * What an upload does with a file, by its path: reads it (`rdf`), keeps its path (`image`) or leaves it out
262
+ * (`other`) — as it does a hidden one, a name starting with `.` anywhere in the path.
263
+ */
264
+ export function uploadFileKind(path: string): 'rdf' | 'image' | 'other' {
265
+ if (path.split('/').some((segment) => segment.startsWith('.'))) return 'other';
266
+ const lower = path.toLowerCase();
267
+ if (UPLOAD_RDF_EXTENSIONS.some((ext) => lower.endsWith(ext))) return 'rdf';
268
+ if (UPLOAD_IMAGE_EXTENSIONS.some((ext) => lower.endsWith(ext))) return 'image';
269
+ return 'other';
270
+ }
271
+
272
+ /** A file of an upload. */
273
+ export interface UploadedFile {
274
+ /** Its path in the upload, `/`-separated: `acme/onto.ttl` from a folder, a bare name otherwise. */
275
+ path: string;
276
+ /** Its text; absent for an image, which is kept by its path alone. */
277
+ text?: string;
278
+ }
279
+
280
+ /** An RDF file an upload read. */
281
+ export interface UploadedDocumentInfo {
282
+ path: string;
283
+ /** The graph it loads into: its `owl:Ontology`, else its `skos:ConceptScheme`, else its first `arch:Metamodel`'s
284
+ * document, else the namespace most of what it types is in; `null` when it types nothing. */
285
+ documentIri: string | null;
286
+ /** The IRIs it says it is, as written. */
287
+ names: string[];
288
+ format: AssetFormat;
289
+ /** The upload it came with. */
290
+ batch: number;
291
+ }
292
+
293
+ /** An `arch:Metamodel` an upload declares. */
294
+ export interface UploadedMetamodelInfo {
295
+ iri: string;
296
+ /** Its `skos:prefLabel`, `rdfs:label` or `dcterms:title`, English first; its IRI's local name when none. */
297
+ label: string;
298
+ path: string;
299
+ batch: number;
300
+ }
301
+
302
+ /** What an upload held, file by file (KReq 3.6). */
303
+ export interface UploadReport {
304
+ batch: number;
305
+ documents: UploadedDocumentInfo[];
306
+ metamodels: UploadedMetamodelInfo[];
307
+ /** Images kept, by path. */
308
+ images: string[];
309
+ /** RDF files that declare an `arch:Model`: models, not metamodel documents. */
310
+ models: string[];
311
+ /** Hidden files, other kinds of file, and RDF files that hold no statement. */
312
+ leftOut: string[];
313
+ unreadable: Array<{ path: string; message: string }>;
314
+ /** An IRI two files of the upload say they are; the first path is the one used. */
315
+ conflicts: Array<{ iri: string; paths: string[] }>;
316
+ }
317
+
318
+ const RDF_TYPE_IRI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
319
+ const OWL_NS = 'http://www.w3.org/2002/07/owl#';
320
+ const SKOS_NS = 'http://www.w3.org/2004/02/skos/core#';
321
+ const ARCH_NS = 'https://meta.linked.archi/core#';
322
+ /** The published site: never claimed, and never matched by what a file merely types (KD-27). */
323
+ const PUBLISHED_BASE = 'https://meta.linked.archi/';
324
+ const LABEL_PREDICATES = [
325
+ SKOS_NS + 'prefLabel',
326
+ 'http://www.w3.org/2000/01/rdf-schema#label',
327
+ 'http://purl.org/dc/terms/title',
328
+ ];
329
+
330
+ /** An IRI as an upload's index keys it: without a trailing `#` or `/`. */
331
+ function uploadKey(iri: string): string {
332
+ return iri.replace(/[#/]+$/, '');
333
+ }
334
+
335
+ /** An absolute IRI: one with a scheme. A file with `<onto#>` and no `@base` names nothing it can be found by. */
336
+ function isAbsoluteIri(iri: string): boolean {
337
+ return /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(iri);
338
+ }
339
+
340
+ /** Up to and including the last `#` or `/`: the namespace an IRI is in. */
341
+ function namespaceOf(iri: string): string {
342
+ const at = Math.max(iri.lastIndexOf('#'), iri.lastIndexOf('/'));
343
+ return at === -1 ? iri : iri.slice(0, at + 1);
344
+ }
345
+
346
+ /** Up to and including the last `/` of the IRI without its fragment (`:` for a URN): where its document sits. */
347
+ function baseOf(iri: string): string {
348
+ const hash = iri.indexOf('#');
349
+ const document = hash === -1 ? iri : iri.slice(0, hash);
350
+ const authority = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^/]*/.exec(document);
351
+ if (authority) {
352
+ const slash = document.lastIndexOf('/');
353
+ return slash < authority[0].length ? `${authority[0]}/` : document.slice(0, slash + 1);
354
+ }
355
+ return document.slice(0, document.lastIndexOf(':') + 1);
356
+ }
357
+
358
+ /**
359
+ * The IRI base and the folder that stand for each other, judged from one file: the IRI's path segments (fragment and
360
+ * trailing `/` dropped) and the file's (extension dropped) matched from the end; what is left of each is the pair. When
361
+ * nothing matches, the IRI's last segment and the file's name are taken to correspond (KD-27).
362
+ */
363
+ function basePair(iri: string, path: string): { iriBase: string; folder: string } | null {
364
+ const hash = iri.indexOf('#');
365
+ const url = (hash === -1 ? iri : iri.slice(0, hash)).replace(/\/+$/, '');
366
+ const match = /^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^/]+\/)(.*)$/.exec(url.includes('/', url.indexOf('//') + 2) ? url : `${url}/`);
367
+ if (!match) return null;
368
+ const iriSegments = match[2] ? match[2].split('/') : [];
369
+ const fileSegments = path.split('/');
370
+ fileSegments[fileSegments.length - 1] = fileSegments[fileSegments.length - 1].replace(/\.[^.]*$/, '');
371
+ let common = 0;
372
+ while (
373
+ common < iriSegments.length &&
374
+ common < fileSegments.length &&
375
+ iriSegments[iriSegments.length - 1 - common] === fileSegments[fileSegments.length - 1 - common]
376
+ ) {
377
+ common++;
378
+ }
379
+ if (common === 0 && iriSegments.length > 0) common = 1;
380
+ const join = (segments: string[]) => segments.map((s) => `${s}/`).join('');
381
+ return {
382
+ iriBase: match[1] + join(iriSegments.slice(0, iriSegments.length - common)),
383
+ folder: join(fileSegments.slice(0, fileSegments.length - common)),
384
+ };
385
+ }
386
+
387
+ /** An RDF file of an upload, indexed. */
388
+ interface IndexedFile {
389
+ batch: number;
390
+ path: string;
391
+ text: string;
392
+ format: AssetFormat;
393
+ documentIri: string | null;
394
+ /** Each kept by {@link uploadKey}: what it says it is; what it types; their namespaces. */
395
+ names: Set<string>;
396
+ typed: Set<string>;
397
+ namespaces: Set<string>;
398
+ }
399
+
400
+ /** One upload. */
401
+ interface Batch {
402
+ id: number;
403
+ files: IndexedFile[];
404
+ images: Set<string>;
405
+ metamodels: UploadedMetamodelInfo[];
406
+ /** The bases of its manifests, outside the published site: what it claims (KD-27). */
407
+ claimed: string[];
408
+ /** IRI base ↔ folder, longest IRI base first, for its images. */
409
+ pairs: Array<{ iriBase: string; folder: string }>;
410
+ }
411
+
412
+ /**
413
+ * Documents the user handed over: one at a time under an IRI the caller names ({@link add}), or an upload of several
414
+ * files — a folder — each found by every IRI it can be referred to by ({@link addFiles}, KReq 3.6, KD-27).
415
+ */
416
+ export class UploadedAssetSource implements AssetSource {
417
+ readonly kind = 'uploaded' as const;
418
+ private readonly documents = new Map<string, AssetDocument>();
419
+ private readonly batchList: Batch[] = [];
420
+ private nextBatch = 1;
421
+
422
+ /** Register a document under an IRI (normally the file's `owl:Ontology`). */
423
+ add(documentIri: string, text: string, format: AssetFormat = 'text/turtle', name?: string): void {
424
+ this.documents.set(documentIri, {
425
+ documentIri,
426
+ text,
427
+ format,
428
+ origin: 'uploaded',
429
+ ...(name ? { location: name } : {}),
430
+ });
431
+ }
432
+
433
+ /**
434
+ * Take an upload: every file is read and indexed before any is looked up, so the order they come in does not
435
+ * matter. A later upload stands for an IRI in place of an earlier one; within one, the first file by path does.
436
+ */
437
+ addFiles(files: readonly UploadedFile[]): UploadReport {
438
+ const batch: Batch = { id: this.nextBatch++, files: [], images: new Set(), metamodels: [], claimed: [], pairs: [] };
439
+ const report: UploadReport = {
440
+ batch: batch.id,
441
+ documents: [],
442
+ metamodels: [],
443
+ images: [],
444
+ models: [],
445
+ leftOut: [],
446
+ unreadable: [],
447
+ conflicts: [],
448
+ };
449
+ const firstByName = new Map<string, { iri: string; paths: string[] }>();
450
+ const typedByFile: Array<{ file: IndexedFile; typed: string[] }> = [];
451
+ const manifestBases: Array<{ base: string; path: string }> = [];
452
+
453
+ for (const file of [...files].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))) {
454
+ const kind = uploadFileKind(file.path);
455
+ if (kind === 'image') {
456
+ batch.images.add(file.path);
457
+ report.images.push(file.path);
458
+ continue;
459
+ }
460
+ if (kind === 'other' || file.text === undefined) {
461
+ report.leftOut.push(file.path);
462
+ continue;
463
+ }
464
+ const format = detectFormat(null, file.path);
465
+ let quads: Quad[];
466
+ try {
467
+ quads = parseRdf(file.text, format);
468
+ } catch (err: unknown) {
469
+ report.unreadable.push({ path: file.path, message: err instanceof Error ? err.message : String(err) });
470
+ continue;
471
+ }
472
+ if (quads.length === 0) {
473
+ report.leftOut.push(file.path);
474
+ continue;
475
+ }
476
+
477
+ const ontologies: string[] = [];
478
+ const schemes: string[] = [];
479
+ const metamodels: string[] = [];
480
+ const versions: string[] = [];
481
+ const typed: string[] = [];
482
+ let model = false;
483
+ for (const q of quads) {
484
+ if (q.subject.termType !== 'NamedNode') continue;
485
+ const subject = q.subject.value;
486
+ if (q.predicate.value === OWL_NS + 'versionIRI' && q.object.termType === 'NamedNode') versions.push(q.object.value);
487
+ if (q.predicate.value !== RDF_TYPE_IRI) continue;
488
+ typed.push(subject);
489
+ const type = q.object.value;
490
+ if (type === OWL_NS + 'Ontology') ontologies.push(subject);
491
+ else if (type === SKOS_NS + 'ConceptScheme') schemes.push(subject);
492
+ else if (type === ARCH_NS + 'Metamodel') metamodels.push(subject);
493
+ else if (type === ARCH_NS + 'Model') model = true;
494
+ }
495
+ if (model && metamodels.length === 0) {
496
+ report.models.push(file.path);
497
+ continue;
498
+ }
499
+
500
+ const names = [...new Set([...ontologies, ...versions, ...schemes, ...metamodels])].filter(isAbsoluteIri);
501
+ const documentIri =
502
+ ontologies.find(isAbsoluteIri) ??
503
+ schemes.find(isAbsoluteIri) ??
504
+ (metamodels.find(isAbsoluteIri) ? namespaceOf(metamodels.find(isAbsoluteIri) as string) : undefined) ??
505
+ mostCommonNamespace(typed.filter(isAbsoluteIri));
506
+ const indexed: IndexedFile = {
507
+ batch: batch.id,
508
+ path: file.path,
509
+ text: file.text,
510
+ format,
511
+ documentIri,
512
+ names: new Set(names.map(uploadKey)),
513
+ typed: new Set(),
514
+ namespaces: new Set(),
515
+ };
516
+ batch.files.push(indexed);
517
+ typedByFile.push({ file: indexed, typed: typed.filter(isAbsoluteIri) });
518
+ report.documents.push({ path: file.path, documentIri, names, format, batch: batch.id });
519
+
520
+ for (const name of names) {
521
+ const key = uploadKey(name);
522
+ const seen = firstByName.get(key);
523
+ if (!seen) firstByName.set(key, { iri: name, paths: [file.path] });
524
+ else if (!seen.paths.includes(file.path)) seen.paths.push(file.path);
525
+ }
526
+ for (const iri of metamodels.filter(isAbsoluteIri)) {
527
+ const info: UploadedMetamodelInfo = { iri, label: labelIn(quads, iri), path: file.path, batch: batch.id };
528
+ batch.metamodels.push(info);
529
+ report.metamodels.push(info);
530
+ const base = baseOf(iri);
531
+ if (!base.startsWith(PUBLISHED_BASE)) manifestBases.push({ base, path: file.path });
532
+ }
533
+ // By the file's own name only: a version IRI (`…/onto/4.0#`) does not end as the file's path does.
534
+ const pair = documentIri ? basePair(documentIri, file.path) : null;
535
+ if (pair && !batch.pairs.some((p) => p.iriBase === pair.iriBase && p.folder === pair.folder)) batch.pairs.push(pair);
536
+ }
537
+
538
+ // What a file types is indexed only under the bases the upload's own names are in, and never on the published
539
+ // site: a file that also declares `skos:prefLabel` an annotation property must not stand for SKOS (KD-27).
540
+ const bases = new Set(
541
+ batch.files
542
+ .flatMap((f) => [...f.names, ...(f.documentIri ? [uploadKey(f.documentIri)] : [])])
543
+ .map((key) => baseOf(key))
544
+ .filter((base) => base !== '' && !base.startsWith(PUBLISHED_BASE)),
545
+ );
546
+ for (const { file, typed } of typedByFile) {
547
+ for (const iri of typed) {
548
+ if (iri.startsWith(PUBLISHED_BASE) || ![...bases].some((base) => iri.startsWith(base))) continue;
549
+ file.typed.add(uploadKey(iri));
550
+ file.namespaces.add(uploadKey(namespaceOf(iri)));
551
+ }
552
+ }
553
+ // A manifest claims its base when the upload brings another of its documents with it: a manifest uploaded alone
554
+ // still has its parts from the usual sources (KD-27).
555
+ for (const { base, path } of manifestBases) {
556
+ const brought = batch.files.some((f) => f.path !== path && f.documentIri?.startsWith(base));
557
+ if (brought && !batch.claimed.includes(base)) batch.claimed.push(base);
558
+ }
559
+ batch.pairs.sort((a, b) => b.iriBase.length - a.iriBase.length);
560
+ report.conflicts = [...firstByName.values()].filter((c) => c.paths.length > 1);
561
+ this.batchList.push(batch);
562
+ return report;
563
+ }
564
+
565
+ /** Forget a document registered with {@link add}. */
566
+ remove(documentIri: string): void {
567
+ this.documents.delete(documentIri);
568
+ }
569
+
570
+ /** Forget everything: the documents registered with {@link add} and every upload. */
571
+ clear(): void {
572
+ this.documents.clear();
573
+ this.batchList.length = 0;
574
+ }
575
+
576
+ /** IRIs currently registered: with {@link add}, and the name of each uploaded file that has one. */
577
+ list(): string[] {
578
+ const names = [...this.documents.keys()];
579
+ for (const file of this.batchList.flatMap((b) => b.files)) {
580
+ if (file.documentIri && !names.includes(file.documentIri)) names.push(file.documentIri);
581
+ }
582
+ return names;
583
+ }
584
+
585
+ /** The RDF files of every upload, oldest upload first, each by path. */
586
+ files(): Array<{ path: string; documentIri: string | null; batch: number }> {
587
+ return this.batchList.flatMap((b) => b.files.map((f) => ({ path: f.path, documentIri: f.documentIri, batch: b.id })));
588
+ }
589
+
590
+ /** The metamodels the uploads declare, newest upload's first; each IRI once. */
591
+ metamodels(): UploadedMetamodelInfo[] {
592
+ const seen = new Set<string>();
593
+ const result: UploadedMetamodelInfo[] = [];
594
+ for (const batch of [...this.batchList].reverse()) {
595
+ for (const m of batch.metamodels) {
596
+ if (seen.has(m.iri)) continue;
597
+ seen.add(m.iri);
598
+ result.push(m);
599
+ }
600
+ }
601
+ return result;
602
+ }
603
+
604
+ /**
605
+ * Whether an IRI is under the base of an uploaded manifest — its document IRI up to the last `/` — that came with
606
+ * another document under that base, and so not to be asked of the network: the metamodel is here, and what is not
607
+ * among its files is not anywhere (KReq 3.7).
608
+ */
609
+ claims(documentIri: string): boolean {
610
+ return this.batchList.some((b) => b.claimed.some((base) => documentIri.startsWith(base)));
611
+ }
612
+
613
+ /**
614
+ * The path of the uploaded image an IRI stands for (KReq 3.8): under an uploaded document's IRI base, the image at
615
+ * the matching path in its folder; failing that, the one uploaded image of that file name. `null` when none.
616
+ */
617
+ resourcePathFor(iri: string): string | null {
618
+ const clean = iri.replace(/[?#].*$/, '');
619
+ for (const batch of [...this.batchList].reverse()) {
620
+ for (const pair of batch.pairs) {
621
+ if (!clean.startsWith(pair.iriBase)) continue;
622
+ const candidate = pair.folder + clean.slice(pair.iriBase.length);
623
+ if (batch.images.has(candidate)) return candidate;
624
+ }
625
+ }
626
+ const name = clean.slice(clean.lastIndexOf('/') + 1);
627
+ if (!name) return null;
628
+ const named = new Set(
629
+ this.batchList.flatMap((b) => [...b.images]).filter((path) => path === name || path.endsWith(`/${name}`)),
630
+ );
631
+ return named.size === 1 ? [...named][0] : null;
632
+ }
633
+
634
+ async fetch(documentIri: string): Promise<AssetDocument | null> {
635
+ const registered = this.documents.get(documentIri);
636
+ if (registered) return registered;
637
+ const file = this.find(documentIri);
638
+ if (!file) return null;
639
+ return {
640
+ documentIri: file.documentIri ?? documentIri,
641
+ text: file.text,
642
+ format: file.format,
643
+ origin: 'uploaded',
644
+ location: file.path,
645
+ };
646
+ }
647
+
648
+ /** The file an IRI stands for: by what files say they are, then what they type, then its namespace (KD-27). */
649
+ private find(iri: string): IndexedFile | undefined {
650
+ const key = uploadKey(iri);
651
+ const newestFirst = [...this.batchList].reverse();
652
+ for (const level of ['names', 'typed', 'namespaces'] as const) {
653
+ for (const batch of newestFirst) {
654
+ const file = batch.files.find((f) => f[level].has(key));
655
+ if (file) return file;
656
+ }
657
+ }
658
+ return undefined;
659
+ }
660
+ }
661
+
662
+ /** The namespace most of these IRIs are in; `null` for none. */
663
+ function mostCommonNamespace(iris: readonly string[]): string | null {
664
+ const counts = new Map<string, number>();
665
+ for (const iri of iris) counts.set(namespaceOf(iri), (counts.get(namespaceOf(iri)) ?? 0) + 1);
666
+ let best: string | null = null;
667
+ let most = 0;
668
+ for (const [namespace, count] of counts) {
669
+ if (count > most) {
670
+ best = namespace;
671
+ most = count;
672
+ }
673
+ }
674
+ return best;
675
+ }
676
+
677
+ /** A subject's label in a file: English first, then any; its local name when it has none. */
678
+ function labelIn(quads: readonly Quad[], subject: string): string {
679
+ for (const predicate of LABEL_PREDICATES) {
680
+ const literals = quads.filter(
681
+ (q) => q.subject.value === subject && q.predicate.value === predicate && q.object.termType === 'Literal',
682
+ );
683
+ const english = literals.find((q) => (q.object as { language?: string }).language?.toLowerCase().startsWith('en'));
684
+ const chosen = english ?? literals[0];
685
+ if (chosen) return chosen.object.value;
686
+ }
687
+ return subject.slice(Math.max(subject.lastIndexOf('#'), subject.lastIndexOf('/')) + 1) || subject;
688
+ }
689
+
690
+ // ── Chain ───────────────────────────────────────────────────────────────
691
+
692
+ /**
693
+ * Asks each source in turn; the first non-null answer wins.
694
+ *
695
+ * A source that *throws* is recorded and skipped, so a flaky network falls back
696
+ * to the bundle. When every source either lacks the document or failed, the
697
+ * last error is rethrown (so the loader reports `error`), or `null` is returned
698
+ * when nobody failed (so the loader reports `not-bundled`/`unavailable`).
699
+ *
700
+ * An IRI a source claims ({@link AssetSource.claims}) is not asked of a remote
701
+ * source: it is asked of the others — the bundle still stands behind a local copy
702
+ * of a published file (KReq 3.7, KD-27).
703
+ */
704
+ export class ChainedAssetSource implements AssetSource {
705
+ readonly kind = 'chain' as const;
706
+
707
+ constructor(readonly sources: readonly AssetSource[]) {}
708
+
709
+ claims(documentIri: string): boolean {
710
+ return this.sources.some((source) => source.claims?.(documentIri) ?? false);
711
+ }
712
+
713
+ async fetch(documentIri: string): Promise<AssetDocument | null> {
714
+ let lastError: unknown;
715
+ const claimed = this.claims(documentIri);
716
+ for (const source of this.sources) {
717
+ if (claimed && source.kind === 'remote') continue;
718
+ try {
719
+ const doc = await source.fetch(documentIri);
720
+ if (doc) return doc;
721
+ } catch (err) {
722
+ lastError = err;
723
+ }
724
+ }
725
+ if (lastError !== undefined) throw lastError;
726
+ return null;
727
+ }
728
+ }
729
+
730
+ /** How the application should resolve metamodel documents. */
731
+ export type AssetMode = 'bundled-first' | 'remote-first' | 'offline';
732
+
733
+ /**
734
+ * The application's source chain.
735
+ *
736
+ * - `remote-first` (default when online): the published version wins, the
737
+ * bundle covers outages. What a user of a live metamodel expects.
738
+ * - `bundled-first`: the bundle wins, remote covers what is not bundled. Faster
739
+ * and deterministic; what tests and demos want.
740
+ * - `offline`: bundle only.
741
+ */
742
+ export function createAssetSource(
743
+ mode: AssetMode,
744
+ uploaded: UploadedAssetSource = new UploadedAssetSource(),
745
+ remoteOptions?: RemoteAssetSourceOptions,
746
+ bundledOptions?: BundledAssetOptions,
747
+ ): ChainedAssetSource {
748
+ const bundled = new BundledAssetSource(bundledOptions);
749
+ if (mode === 'offline') return new ChainedAssetSource([uploaded, bundled]);
750
+ const remote = new RemoteAssetSource(remoteOptions);
751
+ return mode === 'remote-first'
752
+ ? new ChainedAssetSource([uploaded, remote, bundled])
753
+ : new ChainedAssetSource([uploaded, bundled, remote]);
754
+ }