@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,682 @@
1
+ /**
2
+ * Validation of the model, the same wherever it runs (Task 22): in a Web Worker
3
+ * in the browser and the desktop app (`validation.worker.ts`), on the calling
4
+ * thread in Node and the tests (`validation-service.ts`).
5
+ *
6
+ * It keeps its own copy of the model, given whole once ({@link load}) and then
7
+ * kept up to date by the store's changes ({@link apply}) — so a validation does
8
+ * not copy the model out of the store on the UI thread, and does not copy its
9
+ * own either: SHACL and the tables read the engine's store as it is — and what
10
+ * validation needs of the metamodels ({@link setup}): the shapes, the class
11
+ * hierarchy, the types of resources outside the model, and the relationship
12
+ * tables.
13
+ *
14
+ * What it validates is what `App.validationDataGraph` describes: the model, the
15
+ * `rdfs:subClassOf` axioms, and the types of resources the model refers to from
16
+ * outside it (a view's viewpoint) — what is found of such a resource itself is
17
+ * left out of the report: only its types are there to validate. How: the relationship shapes the tables cover
18
+ * by lookup in the tables (`relationship-validity.ts`), the rest by SHACL; the
19
+ * shapes' SPARQL-based constraints (`sh:sparql`), which the SHACL library cannot
20
+ * run, with Oxigraph (`sparql-constraints.ts`) on a copy of the same data and
21
+ * the metamodels' statements they read; and the references to what the model
22
+ * does not have (`missing-references.ts`).
23
+ *
24
+ * The Oxigraph copy is made only when the shapes have SPARQL-based
25
+ * constraints, and only where Oxigraph is ready (its WebAssembly loaded in the
26
+ * worker): about 0.7 KB a quad (78 MB at 100 000 triples, measured in Node).
27
+ */
28
+ import { DataFactory, Store as N3Store, termFromId, termToId } from 'n3';
29
+ import * as oxigraph from 'oxigraph';
30
+ import type { Quad, Term } from '@rdfjs/types';
31
+ import { RdfShaclValidator } from './shacl-validator.js';
32
+ import { isOxigraphReady, toOxQuad } from './rdf-store.js';
33
+ import { SparqlRunner, predicatesNamedBy, sparqlConstraintsOf, type ShapeTargets } from './sparql-constraints.js';
34
+ import { REFERENCE_PREDICATES, missingReferences } from '../model-manager/missing-references.js';
35
+ import { predicatesRead, shapesReach, type ShapesReach } from './shape-reach.js';
36
+ import type { ValidationReport, ValidationResult } from './interfaces.js';
37
+ import { NAMESPACES } from './namespaces.js';
38
+ import { decodeQuads, type EncodedQuad } from './term-codec.js';
39
+ import { NO_TABLE_PERMISSIONS, TablePermissions, type PlainTablePermissions } from '../model-manager/direct-permissions.js';
40
+ import { checkRelationshipsByTables, noTableShapes, splitTableShapes, type TableShapes } from '../model-manager/relationship-validity.js';
41
+ import type { TypeClosure } from '../model-manager/model-queries.js';
42
+
43
+ const RDF_TYPE = NAMESPACES.rdf + 'type';
44
+ const RDFS_SUBCLASS_OF = NAMESPACES.rdfs + 'subClassOf';
45
+ /** What the table check reads besides the predicates the tables cover: the types, and a qualified relationship's ends. */
46
+ const TABLES_READ = [RDF_TYPE, NAMESPACES.arch + 'source', NAMESPACES.arch + 'target'];
47
+ const PREF_LABEL = NAMESPACES.skos + 'prefLabel';
48
+ const QUALIFIED_RELATIONSHIP = NAMESPACES.arch + 'QualifiedRelationship';
49
+
50
+ /** What validation needs of the loaded metamodels. */
51
+ export interface EngineSetup {
52
+ /** Preferred language for labels quoted in generated SHACL findings. */
53
+ preferredLanguage?: string;
54
+ /** The whole shapes graph; the engine splits off what the tables check. */
55
+ shapes: EncodedQuad[];
56
+ /** Every `rdfs:subClassOf` axiom. */
57
+ hierarchy: EncodedQuad[];
58
+ /** `rdf:type` statements about resources outside the model (in the metamodel graphs). */
59
+ outsideTypes: EncodedQuad[];
60
+ /** Relationship class → its `arch:unqualifiedForm`. */
61
+ unqualifiedForms: Array<[string, string]>;
62
+ /** What the relationship tables permit (`permittedBy`); `null` when none are loaded. */
63
+ tables: PlainTablePermissions | null;
64
+ /**
65
+ * The metamodels' statements the SPARQL-based constraints read, besides types
66
+ * and the class hierarchy (a viewpoint's `arch:includesConcept`).
67
+ */
68
+ sparqlContext?: EncodedQuad[];
69
+ }
70
+
71
+ /** A result as plain data: IRIs as strings. */
72
+ export interface PlainResult {
73
+ focusNode: string;
74
+ resultPath: string;
75
+ sourceShape: string;
76
+ message: string;
77
+ severity: ValidationResult['severity'];
78
+ sourceConstraintComponent?: string;
79
+ /** The named shape an inline source shape is written in. */
80
+ withinShape?: string;
81
+ /** The value it is about (`sh:value`), as its n3 id: the IRI, or `"text"@en`. */
82
+ value?: string;
83
+ }
84
+
85
+ /** A report as plain data. */
86
+ export interface PlainReport {
87
+ conforms: boolean;
88
+ results: PlainResult[];
89
+ /**
90
+ * How long it took, in ms: SHACL, the tables, the SPARQL-based constraints,
91
+ * the references; and how many nodes were validated again — `all`, or after
92
+ * an edit only those it can change.
93
+ */
94
+ timing: { shacl: number; tables: number; sparql?: number; references?: number; nodes: number | 'all' };
95
+ }
96
+
97
+ /** After this many validations of what edits changed, the whole model is validated again. */
98
+ const FULL_EVERY = 100;
99
+ /** When an edit can change the findings of more nodes than one in this many of the model's quads, the whole model is validated. */
100
+ const FULL_SHARE = 20;
101
+ /** …but not fewer than this many nodes. */
102
+ const FULL_FLOOR = 256;
103
+
104
+ /** The graph the SPARQL-based constraints' metamodel statements are kept in, in the Oxigraph copy. */
105
+ const CONTEXT_GRAPH = 'urn:la-gram:sparql-context';
106
+ /** Hops around what changed whose nodes the SPARQL-based constraints validate again — both ways. */
107
+ const SPARQL_HOPS = 1;
108
+
109
+ /** The class hierarchy as a closure function, from `rdfs:subClassOf` axioms. */
110
+ export function closureOf(hierarchy: readonly Quad[]): TypeClosure {
111
+ const parents = new Map<string, string[]>();
112
+ for (const q of hierarchy) {
113
+ if (q.subject.termType !== 'NamedNode' || q.object.termType !== 'NamedNode') continue;
114
+ const list = parents.get(q.subject.value);
115
+ if (list) list.push(q.object.value);
116
+ else parents.set(q.subject.value, [q.object.value]);
117
+ }
118
+ const cache = new Map<string, string[]>();
119
+ return (type: string) => {
120
+ const cached = cache.get(type);
121
+ if (cached) return cached;
122
+ const seen = new Set<string>([type]);
123
+ const stack = [...(parents.get(type) ?? [])];
124
+ while (stack.length > 0) {
125
+ const current = stack.pop() as string;
126
+ if (seen.has(current)) continue;
127
+ seen.add(current);
128
+ stack.push(...(parents.get(current) ?? []));
129
+ }
130
+ const closure = [...seen];
131
+ cache.set(type, closure);
132
+ return closure;
133
+ };
134
+ }
135
+
136
+ /** Validates a model it keeps a copy of. */
137
+ export class ValidationEngine {
138
+ /**
139
+ * What is validated, in one store, so that a validation copies nothing: the
140
+ * model, and its extras — the class hierarchy and the types of the outside
141
+ * resources the model refers to. A quad both the model and the extras hold
142
+ * stays until neither does.
143
+ */
144
+ private data = new N3Store();
145
+ /** The extras, by key. */
146
+ private readonly extras = new Map<string, Quad>();
147
+ /** The extras the model holds too. */
148
+ private readonly alsoModel = new Set<string>();
149
+ /** An outside resource's types, from the metamodel graphs. */
150
+ private outsideTypes = new Map<string, Quad[]>();
151
+ /** How many of the model's quads refer to each outside resource with types. */
152
+ private readonly references = new Map<string, number>();
153
+ private readonly validator = new RdfShaclValidator();
154
+ private preferredLanguage?: string;
155
+ private hierarchy: Quad[] = [];
156
+ private split: TableShapes = noTableShapes([]);
157
+ private tables: TablePermissions = NO_TABLE_PERMISSIONS;
158
+ private closure: TypeClosure = (t) => [t];
159
+ private reach: ShapesReach = { hops: 0, inverse: false, recursive: false, valuesAsFocus: false };
160
+ private ready = false;
161
+ /** The same data in Oxigraph, with the metamodels' statements the SPARQL-based constraints read; none without such constraints. */
162
+ private sparqlData?: oxigraph.Store;
163
+ private sparqlRunner?: SparqlRunner;
164
+ private sparqlContext: Quad[] = [];
165
+ /**
166
+ * The predicates validation reads (KD-21): those the shapes, the table check and the reference check read — every
167
+ * one, until the shapes are given or when a shape is closed — and those the SPARQL-based constraints' queries name.
168
+ */
169
+ private reads: { checks: ReadonlySet<string> | 'every'; queries: ReadonlySet<string> } = { checks: 'every', queries: new Set() };
170
+
171
+ /** The findings of the last validation, by focus node: SHACL's, the tables', the SPARQL-based constraints', the references'. */
172
+ private findings: {
173
+ shacl: Map<string, ValidationResult[]>;
174
+ tables: Map<string, ValidationResult[]>;
175
+ sparql: Map<string, ValidationResult[]>;
176
+ references: Map<string, ValidationResult[]>;
177
+ } | null = null;
178
+ /**
179
+ * What changed since — or so much that the whole model is validated — by the triples added and removed: the
180
+ * subjects and objects of those the shapes and the engine's checks read; the ends of those the SPARQL-based
181
+ * constraints' queries name; and the ends of the rest, validated alone (KD-21).
182
+ */
183
+ private changed = {
184
+ subjects: new Map<string, Term>(),
185
+ objects: new Map<string, Term>(),
186
+ around: new Map<string, Term>(),
187
+ alone: new Map<string, Term>(),
188
+ all: true,
189
+ };
190
+ /** Validations of what edits changed since the whole model was last validated. */
191
+ private sinceWhole = 0;
192
+
193
+ /**
194
+ * @param options.sparql Whether to run the SPARQL-based constraints (on by default).
195
+ */
196
+ constructor(private readonly options: { sparql?: boolean } = {}) {}
197
+
198
+ /** Why SPARQL-based constraints could not be run, by shape; what Oxigraph refused. */
199
+ get sparqlFailures(): ReadonlyMap<string, string> {
200
+ return this.sparqlRunner?.failures ?? new Map();
201
+ }
202
+
203
+ /** Take what validation needs of the metamodels; splits the shapes. */
204
+ setup(setup: EngineSetup): void {
205
+ this.preferredLanguage = setup.preferredLanguage;
206
+ for (const q of [...this.extras.values()]) this.removeExtra(q);
207
+ this.references.clear();
208
+ this.hierarchy = decodeQuads(setup.hierarchy).map(inDefaultGraph);
209
+ this.outsideTypes = new Map();
210
+ for (const q of decodeQuads(setup.outsideTypes).map(inDefaultGraph)) {
211
+ const list = this.outsideTypes.get(q.subject.value);
212
+ if (list) list.push(q);
213
+ else this.outsideTypes.set(q.subject.value, [q]);
214
+ }
215
+ this.addExtras();
216
+ this.closure = closureOf(this.hierarchy);
217
+ this.tables = setup.tables ? TablePermissions.fromPlain(setup.tables) : NO_TABLE_PERMISSIONS;
218
+ const formOf = new Map(setup.unqualifiedForms);
219
+ const closure = this.closure;
220
+ const shapes = decodeQuads(setup.shapes);
221
+ this.split = splitTableShapes(shapes, this.tables, formOf, (c) => closure(c).includes(QUALIFIED_RELATIONSHIP));
222
+ this.reach = shapesReach(this.split.kept);
223
+ const constraints = this.options.sparql === false ? [] : sparqlConstraintsOf(shapes).constraints;
224
+ const checks = predicatesRead(shapes);
225
+ this.reads = {
226
+ checks: checks === 'every' ? 'every' : new Set([...checks, ...TABLES_READ, ...REFERENCE_PREDICATES]),
227
+ // A class target reads the types too.
228
+ queries: new Set([RDF_TYPE, ...constraints.flatMap(predicatesNamedBy)]),
229
+ };
230
+ this.sparqlContext = decodeQuads(setup.sparqlContext ?? []);
231
+ this.sparqlRunner = undefined;
232
+ if (constraints.length > 0 && isOxigraphReady()) {
233
+ this.sparqlRunner = new SparqlRunner(
234
+ constraints,
235
+ { select: (query) => (this.sparqlData?.query(query, { use_default_graph_as_union: true }) ?? []) as Array<Map<string, Term>> },
236
+ (iri) => this.nameOf(iri),
237
+ );
238
+ }
239
+ this.rebuildSparqlData();
240
+ this.ready = true;
241
+ this.changed.all = true;
242
+ }
243
+
244
+ /** Replace the model with these quads (default graph). */
245
+ load(quads: readonly EncodedQuad[]): void {
246
+ this.data = new N3Store();
247
+ this.extras.clear();
248
+ this.alsoModel.clear();
249
+ this.references.clear();
250
+ this.data.addQuads(decodeQuads(quads) as never);
251
+ this.addExtras();
252
+ this.rebuildSparqlData();
253
+ this.changed.all = true;
254
+ }
255
+
256
+ /** Apply a change to the model. */
257
+ apply(added: readonly EncodedQuad[], removed: readonly EncodedQuad[]): void {
258
+ for (const q of decodeQuads(removed)) {
259
+ const k = extraKey(q);
260
+ if (k !== undefined && this.extras.has(k)) this.alsoModel.delete(k);
261
+ else this.drop(q);
262
+ this.refer(q, -1);
263
+ this.touch(q);
264
+ }
265
+ for (const q of decodeQuads(added)) {
266
+ const k = extraKey(q);
267
+ if (k !== undefined && this.extras.has(k)) this.alsoModel.add(k);
268
+ else this.put(q);
269
+ this.refer(q, 1);
270
+ this.touch(q);
271
+ }
272
+ }
273
+
274
+ /** How many quads the model has. */
275
+ get size(): number {
276
+ return this.data.size - this.extras.size + this.alsoModel.size;
277
+ }
278
+
279
+ /**
280
+ * Validate the model as it is now: the whole of it after it was loaded, the
281
+ * metamodels changed, or now and then; else only the nodes whose findings
282
+ * what changed can change — the subjects of the changed triples and the
283
+ * nodes up to the shapes' reach before them, and the objects (a target of
284
+ * `sh:targetObjectsOf`); of a triple by a predicate nothing reads, its ends
285
+ * alone (KD-21) — their findings replacing theirs of the last time.
286
+ */
287
+ validate(): PlainReport {
288
+ if (!this.ready || this.size === 0) {
289
+ this.findings = { shacl: new Map(), tables: new Map(), sparql: new Map(), references: new Map() };
290
+ this.clearChanged();
291
+ return { conforms: true, results: [], timing: { shacl: 0, tables: 0, nodes: 0 } };
292
+ }
293
+ const nodes = this.findings && !this.changed.all ? this.affected() : undefined;
294
+ // The SPARQL-based constraints reach as their queries do, not as the shapes say: around what changed, both ways.
295
+ const sparqlNodes = nodes && this.sparqlRunner ? this.around(SPARQL_HOPS) : undefined;
296
+ const timing = nodes ? this.validateNodes(nodes, sparqlNodes) : this.validateWhole();
297
+ this.clearChanged();
298
+ return this.report(timing);
299
+ }
300
+
301
+ private validateWhole(): PlainReport['timing'] {
302
+ const t0 = now();
303
+ const options = { recursionGuard: this.reach.recursive, preferredLanguage: this.preferredLanguage };
304
+ const shacl = this.split.kept.length > 0 ? this.validator.validateStore(this.data, this.split.kept, options).results : [];
305
+ const t1 = now();
306
+ const byTables = checkRelationshipsByTables(this.data as unknown as Iterable<Quad>, this.split, this.tables, this.closure);
307
+ const t2 = now();
308
+ const sparql = this.sparqlRunner?.run() ?? [];
309
+ const t3 = now();
310
+ const references = missingReferences(this.data as never);
311
+ const t4 = now();
312
+ this.findings = { shacl: byFocus(shacl), tables: byFocus(byTables), sparql: byFocus(sparql), references: byFocus(references) };
313
+ this.sinceWhole = 0;
314
+ return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: 'all' };
315
+ }
316
+
317
+ private validateNodes(nodes: Map<string, Term>, sparqlNodes?: Map<string, Term>): PlainReport['timing'] {
318
+ const findings = this.findings as NonNullable<ValidationEngine['findings']>;
319
+ const focus = [...nodes.values()];
320
+ const t0 = now();
321
+ const options = { recursionGuard: this.reach.recursive, preferredLanguage: this.preferredLanguage };
322
+ const shacl = this.split.kept.length > 0 ? this.validator.validateNodes(this.data, this.split.kept, focus, options).results : [];
323
+ const t1 = now();
324
+ const byTables = checkRelationshipsByTables(this.triplesOf(focus), this.split, this.tables, this.closure);
325
+ const t2 = now();
326
+ // Too many nodes around what changed: the SPARQL-based constraints run on the whole model.
327
+ const sparql = this.sparqlRunner
328
+ ? sparqlNodes
329
+ ? this.sparqlRunner.run([...sparqlNodes.values()], this.targetTest())
330
+ : this.sparqlRunner.run()
331
+ : [];
332
+ const t3 = now();
333
+ const references = missingReferences(this.data as never, focus);
334
+ const t4 = now();
335
+ for (const key of nodes.keys()) {
336
+ findings.shacl.delete(key);
337
+ findings.tables.delete(key);
338
+ findings.references.delete(key);
339
+ }
340
+ if (sparqlNodes) for (const key of sparqlNodes.keys()) findings.sparql.delete(key);
341
+ else findings.sparql.clear();
342
+ for (const [key, results] of byFocus(shacl)) findings.shacl.set(key, results);
343
+ for (const [key, results] of byFocus(byTables)) findings.tables.set(key, results);
344
+ for (const [key, results] of byFocus(sparql)) findings.sparql.set(key, results);
345
+ for (const [key, results] of byFocus(references)) findings.references.set(key, results);
346
+ this.sinceWhole++;
347
+ return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: nodes.size };
348
+ }
349
+
350
+ /** The report from the findings kept — none about a resource the model only refers to. */
351
+ private report(timing: PlainReport['timing']): PlainReport {
352
+ const findings = this.findings as NonNullable<ValidationEngine['findings']>;
353
+ const keep = (r: ValidationResult) => !this.outsideOnly(r.focusNode.value);
354
+ const shacl = [...findings.shacl.values()].flat().filter(keep);
355
+ const others = [...findings.tables.values(), ...findings.sparql.values(), ...findings.references.values()].flat().filter(keep);
356
+ return {
357
+ // As SHACL reports it: conforming with no result at all; the rest with no violation.
358
+ conforms: shacl.length === 0 && !others.some((r) => r.severity === 'Violation'),
359
+ results: [...shacl, ...others].map(plain),
360
+ timing,
361
+ };
362
+ }
363
+
364
+ /**
365
+ * The nodes whose findings what changed can change, by key — or `undefined`
366
+ * when the whole model is to be validated: the class hierarchy changed, the
367
+ * shapes reach without bound or report one node's findings as another's, it
368
+ * is time to, or the nodes are too many.
369
+ */
370
+ private affected(): Map<string, Term> | undefined {
371
+ if (this.sinceWhole >= FULL_EVERY || !Number.isFinite(this.reach.hops) || this.reach.valuesAsFocus) return undefined;
372
+ const { subjects, objects, alone } = this.changed;
373
+ // The table check reads a relationship's ends: one hop at least.
374
+ const hops = Math.max(1, this.reach.hops);
375
+ const from = this.reach.inverse ? [...subjects.values(), ...objects.values()] : [...subjects.values()];
376
+ // A shape reaches a node only along the predicates its paths name; the table check and the reference check, by
377
+ // theirs.
378
+ const nodes = this.near(from, hops, this.reach.inverse, (p) => this.checksRead(p));
379
+ if (!nodes) return undefined;
380
+ // Validated without what leads to them: the objects (a target of `sh:targetObjectsOf`), and the ends of what no
381
+ // shape reads. Added after the walk, so that one of them the walk comes to is walked on from.
382
+ for (const [key, term] of objects) nodes.set(key, term);
383
+ for (const [key, term] of alone) nodes.set(key, term);
384
+ return nodes.size > this.limit ? undefined : nodes;
385
+ }
386
+
387
+ /**
388
+ * The nodes within `hops` of the triples the SPARQL-based constraints' queries
389
+ * name, both ways, and the ends of the rest — or `undefined` when they are too
390
+ * many and the whole model is to be validated.
391
+ */
392
+ private around(hops: number): Map<string, Term> | undefined {
393
+ // Along any predicate: a query that matches any predicate of `$this` reaches its neighbours by every one.
394
+ const nodes = this.near([...this.changed.around.values()], hops, true, () => true);
395
+ if (!nodes) return undefined;
396
+ for (const [key, term] of this.changed.alone) nodes.set(key, term);
397
+ return nodes.size > this.limit ? undefined : nodes;
398
+ }
399
+
400
+ /**
401
+ * `from`, and the nodes up to `hops` before it — and after it too, `both` —
402
+ * along the predicates that `follows`, by key; `undefined` when they are more
403
+ * than a share of the model.
404
+ */
405
+ private near(from: readonly Term[], hops: number, both: boolean, follows: (predicate: string) => boolean): Map<string, Term> | undefined {
406
+ const limit = this.limit;
407
+ const nodes = new Map<string, Term>();
408
+ for (const term of from) nodes.set(keyOf(term), term);
409
+ let frontier = [...nodes.values()];
410
+ for (let hop = 0; hop < hops && frontier.length > 0; hop++) {
411
+ const next: Term[] = [];
412
+ const add = (term: Term) => {
413
+ if (term.termType !== 'NamedNode' && term.termType !== 'BlankNode') return;
414
+ const key = keyOf(term);
415
+ if (nodes.has(key)) return;
416
+ nodes.set(key, term);
417
+ next.push(term);
418
+ };
419
+ for (const node of frontier) {
420
+ for (const q of this.data.getQuads(null, null, node as never, null)) if (follows(q.predicate.value)) add(q.subject as unknown as Term);
421
+ if (both) for (const q of this.data.getQuads(node as never, null, null, null)) if (follows(q.predicate.value)) add(q.object as unknown as Term);
422
+ if (nodes.size > limit) return undefined;
423
+ }
424
+ frontier = next;
425
+ }
426
+ return nodes.size > limit ? undefined : nodes;
427
+ }
428
+
429
+ /** The most nodes validated after an edit: past it, the whole model is (FULL_SHARE, FULL_FLOOR). */
430
+ private get limit(): number {
431
+ return Math.max(FULL_FLOOR, this.size / FULL_SHARE);
432
+ }
433
+
434
+ /**
435
+ * Whether a node is one of a SPARQL-based constraint's targets, as its query finds them in the same data
436
+ * (`targetPattern`): one of its nodes, a subject or an object of its predicates, or of one of its classes — by
437
+ * `rdf:type/rdfs:subClassOf*`. Each node's classes are read once, for one validation.
438
+ */
439
+ private targetTest(): (targets: ShapeTargets, node: Term) => boolean {
440
+ const type = DataFactory.namedNode(RDF_TYPE);
441
+ const subClassOf = DataFactory.namedNode(RDFS_SUBCLASS_OF);
442
+ const classes = new Map<string, Set<string>>();
443
+ const classesOf = (node: Term): Set<string> => {
444
+ let found = classes.get(keyOf(node));
445
+ if (found) return found;
446
+ found = new Set<string>();
447
+ const stack = this.data.getQuads(node as never, type, null, null).map((q) => q.object as unknown as Term);
448
+ while (stack.length > 0) {
449
+ const c = stack.pop() as Term;
450
+ if (c.termType !== 'NamedNode' || found.has(c.value)) continue;
451
+ found.add(c.value);
452
+ for (const q of this.data.getQuads(c as never, subClassOf, null, null)) stack.push(q.object as unknown as Term);
453
+ }
454
+ classes.set(keyOf(node), found);
455
+ return found;
456
+ };
457
+ return (targets, node) =>
458
+ (node.termType === 'NamedNode' && targets.nodes.includes(node.value)) ||
459
+ targets.subjectsOf.some((p) => this.data.countQuads(node as never, DataFactory.namedNode(p), null, null) > 0) ||
460
+ targets.objectsOf.some((p) => this.data.countQuads(null, DataFactory.namedNode(p), node as never, null) > 0) ||
461
+ (targets.classes.length > 0 && targets.classes.some((c) => classesOf(node).has(c)));
462
+ }
463
+
464
+ /** What the table check reads of these nodes: their triples, and the types of what they point to. */
465
+ private triplesOf(nodes: readonly Term[]): Quad[] {
466
+ const quads: Quad[] = [];
467
+ const typed = new Set<string>();
468
+ const type = DataFactory.namedNode(RDF_TYPE);
469
+ for (const node of nodes) {
470
+ for (const q of this.data.getQuads(node as never, null, null, null) as unknown as Quad[]) {
471
+ quads.push(q);
472
+ const o = q.object;
473
+ if ((o.termType === 'NamedNode' || o.termType === 'BlankNode') && !typed.has(keyOf(o))) {
474
+ typed.add(keyOf(o));
475
+ quads.push(...(this.data.getQuads(o as never, type, null, null) as unknown as Quad[]));
476
+ }
477
+ }
478
+ }
479
+ return quads;
480
+ }
481
+
482
+ /**
483
+ * Note a changed triple (KD-21). A change to the class hierarchy changes everything. One the shapes, the table
484
+ * check or the reference check read: its subject, followed back by the shapes' reach, and its object, when a node;
485
+ * one the SPARQL-based constraints' queries name: its ends, and what is around them. One that nothing names — an
486
+ * edit's stamps on the model, `dcterms:modified` and the rest — can change no finding but its ends' own: a query
487
+ * reads a predicate it does not name only by a variable, and does so next to `$this`, as every published rule
488
+ * does. Unless it is its subject's first triple or its last: what refers to the subject is validated again too,
489
+ * the subject being in the model, or no longer.
490
+ */
491
+ private touch(q: Quad): void {
492
+ if (this.changed.all) return;
493
+ const p = q.predicate.value;
494
+ if (p === RDFS_SUBCLASS_OF) {
495
+ this.changed.all = true;
496
+ return;
497
+ }
498
+ let byChecks = this.checksRead(p);
499
+ let byQueries = this.reads.queries.has(p);
500
+ if ((!byChecks || !byQueries) && this.firstOrLast(q)) byChecks = byQueries = true;
501
+ const s = q.subject as Term;
502
+ const o = q.object as Term;
503
+ const ends = o.termType === 'NamedNode' || o.termType === 'BlankNode' ? [s, o] : [s];
504
+ if (byChecks) {
505
+ this.changed.subjects.set(keyOf(s), s);
506
+ if (ends.length > 1) this.changed.objects.set(keyOf(o), o);
507
+ }
508
+ if (byQueries) for (const t of ends) this.changed.around.set(keyOf(t), t);
509
+ if (!byChecks || !byQueries) for (const t of ends) this.changed.alone.set(keyOf(t), t);
510
+ }
511
+
512
+ /** Whether the shapes, the table check or the reference check read triples by this predicate. */
513
+ private checksRead(p: string): boolean {
514
+ const { checks } = this.reads;
515
+ return checks === 'every' || checks.has(p) || this.tables.covers(p);
516
+ }
517
+
518
+ /** Whether a triple just added or removed is the only one its subject has, or had. */
519
+ private firstOrLast(q: Quad): boolean {
520
+ return this.data.countQuads(q.subject as never, null, null, null) === (this.data.has(q as never) ? 1 : 0);
521
+ }
522
+
523
+ private clearChanged(): void {
524
+ this.changed = { subjects: new Map(), objects: new Map(), around: new Map(), alone: new Map(), all: false };
525
+ }
526
+
527
+ /** The extras for the model the store holds (and nothing else): the hierarchy, the types of what it refers to. */
528
+ private addExtras(): void {
529
+ for (const q of this.hierarchy) this.addExtra(q);
530
+ const type = DataFactory.namedNode(RDF_TYPE);
531
+ for (const [iri, types] of this.outsideTypes) {
532
+ const object = DataFactory.namedNode(iri);
533
+ const n = this.data.countQuads(null, null, object, null) - this.data.countQuads(null, type, object, null);
534
+ if (n <= 0) continue;
535
+ this.references.set(iri, n);
536
+ for (const q of types) this.addExtra(q);
537
+ }
538
+ }
539
+
540
+ /** A model quad added (+1) or removed (−1): the outside resource it refers to gains or loses a reference — and its types with the first or the last. */
541
+ private refer(q: Quad, delta: 1 | -1): void {
542
+ if (q.predicate.value === RDF_TYPE || q.object.termType !== 'NamedNode') return;
543
+ const types = this.outsideTypes.get(q.object.value);
544
+ if (!types) return;
545
+ const before = this.references.get(q.object.value) ?? 0;
546
+ const after = before + delta;
547
+ if (after > 0) this.references.set(q.object.value, after);
548
+ else this.references.delete(q.object.value);
549
+ if (before === 0 && after > 0) for (const t of types) this.addExtra(t);
550
+ if (before > 0 && after <= 0) for (const t of types) this.removeExtra(t);
551
+ }
552
+
553
+ private addExtra(q: Quad): void {
554
+ const k = extraKey(q) as string;
555
+ if (this.extras.has(k)) return;
556
+ this.extras.set(k, q);
557
+ if (this.data.has(q as never)) this.alsoModel.add(k);
558
+ else this.put(q);
559
+ this.touch(q);
560
+ }
561
+
562
+ private removeExtra(q: Quad): void {
563
+ const k = extraKey(q) as string;
564
+ if (!this.extras.delete(k)) return;
565
+ this.touch(q);
566
+ if (this.alsoModel.delete(k)) return; // the model holds it
567
+ this.drop(q);
568
+ }
569
+
570
+ /**
571
+ * Whether a node is a resource the model only refers to: outside it, its types
572
+ * among the extras, nothing else said of it in the data. What the shapes find
573
+ * of it is not the model's to fix — and not true either: only its types are
574
+ * validated, not the rest of what its metamodel says (a catalogue's viewpoint
575
+ * found to declare no concepts).
576
+ */
577
+ private outsideOnly(iri: string): boolean {
578
+ if (!this.references.has(iri)) return false;
579
+ for (const q of this.data.getQuads(DataFactory.namedNode(iri), null, null, null)) {
580
+ const k = extraKey(q as unknown as Quad);
581
+ if (k === undefined || !this.extras.has(k) || this.alsoModel.has(k)) return false;
582
+ }
583
+ return true;
584
+ }
585
+
586
+ /** A node's name in the model (`skos:prefLabel`, English first), for the SPARQL-based constraints' messages. */
587
+ private nameOf(iri: string): string | undefined {
588
+ const names = this.data.getQuads(DataFactory.namedNode(iri), DataFactory.namedNode(PREF_LABEL), null, null);
589
+ const pick = names.find((q) => (q.object as { language?: string }).language === 'en') ?? names[0];
590
+ return pick?.object.value;
591
+ }
592
+
593
+ /** A quad into the data — and its Oxigraph copy. */
594
+ private put(q: Quad): void {
595
+ this.data.addQuad(q as never);
596
+ this.sparqlData?.add(toOxQuad(q));
597
+ }
598
+
599
+ /** A quad out of the data — and its Oxigraph copy. */
600
+ private drop(q: Quad): void {
601
+ this.data.removeQuad(q as never);
602
+ this.sparqlData?.delete(toOxQuad(q));
603
+ }
604
+
605
+ /** The Oxigraph copy made again from the data and the context — or none, without SPARQL-based constraints to run. */
606
+ private rebuildSparqlData(): void {
607
+ if (!this.sparqlRunner) {
608
+ this.sparqlData = undefined;
609
+ return;
610
+ }
611
+ const store = new oxigraph.Store();
612
+ for (const q of this.data) store.add(toOxQuad(q as unknown as Quad));
613
+ const context = oxigraph.namedNode(CONTEXT_GRAPH);
614
+ for (const q of this.sparqlContext) {
615
+ const ox = toOxQuad(q);
616
+ store.add(oxigraph.quad(ox.subject, ox.predicate, ox.object, context));
617
+ }
618
+ this.sparqlData = store;
619
+ }
620
+ }
621
+
622
+ /** A node's key: its value — as a result's focus node carries it. */
623
+ function keyOf(term: Term): string {
624
+ return term.value;
625
+ }
626
+
627
+ /** Results by their focus node's key. */
628
+ function byFocus(results: readonly ValidationResult[]): Map<string, ValidationResult[]> {
629
+ const map = new Map<string, ValidationResult[]>();
630
+ for (const r of results) {
631
+ const list = map.get(r.focusNode.value);
632
+ if (list) list.push(r);
633
+ else map.set(r.focusNode.value, [r]);
634
+ }
635
+ return map;
636
+ }
637
+
638
+ /** The quad in the default graph, where the engine keeps everything. */
639
+ function inDefaultGraph(q: Quad): Quad {
640
+ return q.graph.termType === 'DefaultGraph' ? q : (DataFactory.quad(q.subject as never, q.predicate as never, q.object as never) as unknown as Quad);
641
+ }
642
+
643
+ /** A key for a quad that can be an extra — `rdfs:subClassOf` or `rdf:type` — else `undefined`. */
644
+ function extraKey(q: Quad): string | undefined {
645
+ const p = q.predicate.value;
646
+ if (p !== RDF_TYPE && p !== RDFS_SUBCLASS_OF) return undefined;
647
+ return `${termToId(q.subject as never)} ${p} ${termToId(q.object as never)}`;
648
+ }
649
+
650
+ function plain(r: ValidationResult): PlainResult {
651
+ return {
652
+ focusNode: r.focusNode.value,
653
+ resultPath: r.resultPath.value,
654
+ sourceShape: r.sourceShape.value,
655
+ message: r.message,
656
+ severity: r.severity,
657
+ ...(r.sourceConstraintComponent ? { sourceConstraintComponent: r.sourceConstraintComponent.value } : {}),
658
+ ...(r.withinShape ? { withinShape: r.withinShape.value } : {}),
659
+ ...(r.value ? { value: termToId(r.value as never) } : {}),
660
+ };
661
+ }
662
+
663
+ function now(): number {
664
+ return typeof performance !== 'undefined' ? performance.now() : Date.now();
665
+ }
666
+
667
+ /** The report back from plain data, its IRIs as terms. */
668
+ export function fromPlainReport(report: Pick<PlainReport, 'conforms' | 'results'>, namedNode: (iri: string) => unknown): ValidationReport {
669
+ return {
670
+ conforms: report.conforms,
671
+ results: report.results.map((r) => ({
672
+ focusNode: namedNode(r.focusNode) as ValidationResult['focusNode'],
673
+ resultPath: namedNode(r.resultPath) as ValidationResult['resultPath'],
674
+ sourceShape: namedNode(r.sourceShape) as ValidationResult['sourceShape'],
675
+ message: r.message,
676
+ severity: r.severity,
677
+ ...(r.sourceConstraintComponent ? { sourceConstraintComponent: namedNode(r.sourceConstraintComponent) as ValidationResult['focusNode'] } : {}),
678
+ ...(r.withinShape ? { withinShape: namedNode(r.withinShape) as ValidationResult['focusNode'] } : {}),
679
+ ...(r.value ? { value: termFromId(r.value, DataFactory as never) as unknown as Term } : {}),
680
+ })),
681
+ };
682
+ }