@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,631 @@
1
+ /**
2
+ * An {@link RdfStore} that reports, and can record, every change to the model.
3
+ *
4
+ * Two things in the application need to know exactly which triples an
5
+ * operation changed:
6
+ *
7
+ * - **Undo/redo.** A canvas command runs a model operation once, records the
8
+ * quads it actually added and removed, and from then on undo applies the
9
+ * inverse and redo re-applies the same diff — never re-running the operation,
10
+ * which would mint new IRIs and break everything that points at the old ones.
11
+ * - **Collaboration.** The CRDT binding forwards each local change to the
12
+ * shared document and applies remote changes with a different origin, so it
13
+ * does not echo them back.
14
+ *
15
+ * Only the default graph — the model — is observed. Metamodel graphs are loaded
16
+ * and dropped wholesale by the metamodel loader and are nobody's edit.
17
+ *
18
+ * A diff records only effective changes: adding a quad that is already there,
19
+ * or removing one that is not, is not part of it, so its inverse never deletes
20
+ * a triple the operation did not create. And it records them as the store holds
21
+ * them, which for a typed literal may not be as they were added.
22
+ *
23
+ * It also keeps a copy of the model graph in JavaScript (Task 22): a read of the
24
+ * model — the model whole, or a pattern in the default graph — is answered from
25
+ * it, without a crossing into Oxigraph's WebAssembly and a conversion of every
26
+ * quad back. The inner store keeps everything, for SPARQL and the metamodels —
27
+ * but a large addition (opening a model) reaches it in the background, in
28
+ * slices shorter than a frame, its terms checked at once as the inner store
29
+ * would; until it is there, a read of every graph is the copy's model and the
30
+ * inner store's named graphs, and SPARQL over the model waits for it.
31
+ *
32
+ * And it lets a local edit be stamped inside itself (`setStamper`, the provenance of edits): once at the end of
33
+ * each outermost write — a {@link record}, a {@link batch}, or a write outside both — what the local edit
34
+ * changed is given to the stamper, whose own writes join the edit, its undo step and its notification. Undo,
35
+ * redo ({@link applyDiff}), a collaborator's changes (another origin) and what runs {@link withoutStamps} are
36
+ * not stamped.
37
+ */
38
+
39
+ import { DataFactory, Store as N3Store, Writer } from 'n3';
40
+ import type { NamedNode, Quad, Term } from '@rdfjs/types';
41
+ import type { RdfStore, SparqlResult } from './interfaces.js';
42
+
43
+ /** Quads an operation added and removed, in the default graph. */
44
+ export interface RdfDiff {
45
+ added: Quad[];
46
+ removed: Quad[];
47
+ }
48
+
49
+ function appendDiff(target: RdfDiff, added: readonly Quad[], removed: readonly Quad[]): void {
50
+ // A load or undo can contain enough quads to exceed the function argument limit (KReq 1.6).
51
+ for (const quad of added) target.added.push(quad);
52
+ for (const quad of removed) target.removed.push(quad);
53
+ }
54
+
55
+ /** Who made a change: the local user, a remote collaborator, a file load… */
56
+ export type ChangeOrigin = 'local' | 'remote' | 'load' | 'undo' | 'redo' | (string & {});
57
+
58
+ /** A batch of effective changes to the model. */
59
+ export interface StoreChange extends RdfDiff {
60
+ origin: ChangeOrigin;
61
+ }
62
+
63
+ /** Listener for {@link ObservableRdfStore.subscribe}. */
64
+ export type StoreChangeListener = (change: StoreChange) => void;
65
+
66
+ const DEFAULT_GRAPH = DataFactory.defaultGraph();
67
+ /** A model addition this large is given to the inner store in the background. */
68
+ const IN_BACKGROUND_FROM = 5000;
69
+ /** How long one background slice may run, in ms: well under a frame. */
70
+ const SLICE_MS = 12;
71
+ /** Quads the inner store is given at a time. */
72
+ const STEP = 1000;
73
+ /** Subject and predicate of the quad a literal pattern is put in its held form through. */
74
+ const SCRATCH = DataFactory.namedNode('urn:la-gram:pattern');
75
+
76
+ function now(): number {
77
+ return typeof performance !== 'undefined' ? performance.now() : Date.now();
78
+ }
79
+
80
+ /** Native document loading can reassign blank node labels. Keep those terms on the quad path. */
81
+ function canLoadAsTriples(term: Term): boolean {
82
+ if (term.termType === 'BlankNode') return false;
83
+ if (term.termType === 'Literal') return !term.direction;
84
+ if (term.termType === 'Quad') {
85
+ return term.graph.termType === 'DefaultGraph' && canLoadAsTriples(term.subject) && canLoadAsTriples(term.object);
86
+ }
87
+ return term.termType === 'NamedNode';
88
+ }
89
+
90
+ /** Whether a quad is in the default graph (the model). */
91
+ function inDefaultGraph(q: Quad): boolean {
92
+ return q.graph.termType === 'DefaultGraph';
93
+ }
94
+
95
+ /**
96
+ * An update that drops or clears one named graph, and nothing else: `DROP SILENT GRAPH <…>`, `CLEAR GRAPH <…>`. Never
97
+ * the default graph, which `DEFAULT`, `ALL` and `NAMED` name, and never one of several operations.
98
+ */
99
+ const NAMED_GRAPH_DROP = /^\s*(?:DROP|CLEAR)\s+(?:SILENT\s+)?GRAPH\s+<[^<>"{}|^`\\\s]*>\s*;?\s*$/i;
100
+
101
+ /** Wraps a store and observes its default graph. */
102
+ export class ObservableRdfStore implements RdfStore {
103
+ private listeners = new Set<StoreChangeListener>();
104
+ private writeListeners = new Set<(diff: RdfDiff) => void>();
105
+ private recording: RdfDiff[] = [];
106
+ private origin: ChangeOrigin = 'local';
107
+ /** Changes made inside {@link batch}, delivered once at the end. */
108
+ private pending: RdfDiff | null = null;
109
+ /** The model graph, as the inner store holds it. */
110
+ private model = new N3Store();
111
+ /** Model quads the inner store is still to be given, in order, and how far it has got. */
112
+ private backlog: Quad[] = [];
113
+ private given = 0;
114
+ private backlogTimer: ReturnType<typeof setTimeout> | null = null;
115
+ /** What stamps a local edit; none, nothing is stamped. */
116
+ private stamper: ((diff: RdfDiff) => void) | null = null;
117
+ /** The local edit not yet stamped. */
118
+ private unstamped: RdfDiff = { added: [], removed: [] };
119
+ /** Open {@link record} and {@link batch} scopes. */
120
+ private depth = 0;
121
+ private stamping = false;
122
+ /** Open {@link withoutStamps} scopes. */
123
+ private stampsOff = 0;
124
+
125
+ constructor(readonly inner: RdfStore & { has?(q: Quad): boolean }) {
126
+ this.model.addQuads(this.defaultGraph() as never);
127
+ }
128
+
129
+ /** End a host-owned session, including pending WASM writes (workspace Req 1.5). */
130
+ dispose(): void {
131
+ this.dropBacklog(); this.listeners.clear(); this.writeListeners.clear(); this.stamper = null;
132
+ this.model = new N3Store();
133
+ (this.inner as RdfStore & { dispose?(): void }).dispose?.();
134
+ }
135
+
136
+ // ── Observation ────────────────────────────────────────────────────────
137
+
138
+ /** Be told about every effective change to the model. Returns an unsubscribe. */
139
+ subscribe(listener: StoreChangeListener): () => void {
140
+ this.listeners.add(listener);
141
+ return () => this.listeners.delete(listener);
142
+ }
143
+
144
+ /**
145
+ * Be told of each write to the model as it is made — inside a {@link batch}
146
+ * too, where {@link subscribe}rs hear only at its end — for a cache of what
147
+ * was read from the store, which must not outlive it. Returns an unsubscribe.
148
+ */
149
+ onEveryWrite(listener: (diff: RdfDiff) => void): () => void {
150
+ this.writeListeners.add(listener);
151
+ return () => this.writeListeners.delete(listener);
152
+ }
153
+
154
+ /**
155
+ * Run `fn` and return what it changed. Nested recordings each see their own
156
+ * changes and their callers' recordings see them too.
157
+ */
158
+ record<T>(fn: () => T): { result: T } & RdfDiff {
159
+ const diff: RdfDiff = { added: [], removed: [] };
160
+ this.recording.push(diff);
161
+ try {
162
+ const result = this.scope(fn);
163
+ return { result, ...diff };
164
+ } finally {
165
+ this.recording.splice(this.recording.indexOf(diff), 1);
166
+ }
167
+ }
168
+
169
+ /**
170
+ * What stamps a local edit (`model-manager/provenance.ts`): given what the edit changed, once at its end,
171
+ * while it is still being recorded and batched.
172
+ */
173
+ setStamper(stamper: ((diff: RdfDiff) => void) | null): void {
174
+ this.stamper = stamper;
175
+ this.unstamped = { added: [], removed: [] };
176
+ }
177
+
178
+ /** Run `fn` without stamping what it changes: opening, merging, restoring — no edit of the user's. */
179
+ withoutStamps<T>(fn: () => T): T {
180
+ this.stampsOff++;
181
+ try {
182
+ return fn();
183
+ } finally {
184
+ this.stampsOff--;
185
+ }
186
+ }
187
+
188
+ /** A write scope: the outermost stamps the local edit before it closes. */
189
+ private scope<T>(fn: () => T): T {
190
+ this.depth++;
191
+ try {
192
+ const result = fn();
193
+ if (this.depth === 1) this.stamp();
194
+ return result;
195
+ } finally {
196
+ this.depth--;
197
+ }
198
+ }
199
+
200
+ /** Give the stamper the local edit not yet stamped; its writes are not stamped again. */
201
+ private stamp(): void {
202
+ if (!this.stamper || this.stamping) return;
203
+ const diff = this.unstamped;
204
+ if (diff.added.length === 0 && diff.removed.length === 0) return;
205
+ this.unstamped = { added: [], removed: [] };
206
+ this.stamping = true;
207
+ try {
208
+ this.stamper(diff);
209
+ } catch (err) {
210
+ console.warn('[la-gram] Recording the provenance of an edit failed', err);
211
+ } finally {
212
+ this.stamping = false;
213
+ }
214
+ }
215
+
216
+ /** Run `fn` with changes attributed to `origin` (e.g. `remote`). */
217
+ withOrigin<T>(origin: ChangeOrigin, fn: () => T): T {
218
+ const previous = this.origin;
219
+ this.origin = origin;
220
+ try {
221
+ return fn();
222
+ } finally {
223
+ this.origin = previous;
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Run `fn` and notify listeners once, with everything it changed, instead of
229
+ * once per `addQuads`/`removeQuads` call. Nested batches fold into the outer.
230
+ */
231
+ batch<T>(fn: () => T): T {
232
+ if (this.pending) return this.scope(fn);
233
+ this.pending = { added: [], removed: [] };
234
+ try {
235
+ return this.scope(fn);
236
+ } finally {
237
+ const change = this.pending;
238
+ this.pending = null;
239
+ if (change.added.length > 0 || change.removed.length > 0) this.notify(change);
240
+ }
241
+ }
242
+
243
+ /** Apply a recorded diff, or its inverse. */
244
+ applyDiff(diff: RdfDiff, inverse = false): void {
245
+ // A diff replayed — undo, redo — is no new edit: its stamps, if it had any, are in it.
246
+ this.withoutStamps(() => this.batch(() => {
247
+ const [remove, add] = inverse ? [diff.added, diff.removed] : [diff.removed, diff.added];
248
+ if (remove.length > 0) this.removeQuads(remove);
249
+ if (add.length > 0) this.addQuads(add);
250
+ }));
251
+ }
252
+
253
+ // ── RdfStore ───────────────────────────────────────────────────────────
254
+
255
+ addQuads(quads: Quad[]): void {
256
+ const named = quads.filter((q) => !inDefaultGraph(q));
257
+ if (named.length > 0) {
258
+ this.namedChanged();
259
+ this.inner.addQuads(named);
260
+ }
261
+ // What the model did not have: n3 says whether a quad was new as it adds it.
262
+ const added = this.held(quads.filter(inDefaultGraph)).filter((q) => this.model.addQuad(q as never));
263
+ if (added.length === 0) return;
264
+ try {
265
+ if (added.length >= IN_BACKGROUND_FROM && this.inner.check) {
266
+ this.inner.check(added); // refused now, as adding them would be
267
+ this.giveLater(added);
268
+ } else {
269
+ this.inner.addQuads(added);
270
+ }
271
+ } catch (err) {
272
+ // What the inner store refused is not the model's.
273
+ for (const q of added) if (!this.innerHas(q)) this.model.removeQuad(q as never);
274
+ throw err;
275
+ }
276
+ this.track(added, []);
277
+ }
278
+
279
+ removeQuads(quads: Quad[]): void {
280
+ const named = quads.filter((q) => !inDefaultGraph(q));
281
+ if (named.length > 0) {
282
+ this.namedChanged();
283
+ this.inner.removeQuads(named);
284
+ }
285
+ const removed = this.held(quads.filter(inDefaultGraph)).filter((q) => this.model.removeQuad(q as never));
286
+ if (removed.length === 0) return;
287
+ // One still waiting in the backlog is passed over when it comes up.
288
+ try {
289
+ if (this.model.size === 0 && removed.length >= IN_BACKGROUND_FROM && this.inner.update) {
290
+ // The whole model removed — as opening another does: cleared in the
291
+ // inner store in one go, not converted into it quad by quad to be
292
+ // deleted (3.8 s for 347 000), and what it was still to be given dropped.
293
+ this.dropBacklog();
294
+ this.inner.update('CLEAR SILENT DEFAULT');
295
+ } else {
296
+ this.inner.removeQuads(removed);
297
+ }
298
+ } catch (err) {
299
+ for (const q of removed) if (this.innerHas(q)) this.model.addQuad(q as never);
300
+ throw err;
301
+ }
302
+ this.track([], removed);
303
+ }
304
+
305
+ getQuads(
306
+ subject?: NamedNode | null,
307
+ predicate?: NamedNode | null,
308
+ object?: NamedNode | null,
309
+ graph?: NamedNode | null,
310
+ ): Quad[] {
311
+ const termType = (term: unknown) => (term as { termType?: string } | null | undefined)?.termType;
312
+ // A literal asked for, in the form held: the inner store matches any of its
313
+ // forms (`"010"^^xsd:int` finds `"10"^^xsd:integer`), and so must the copy.
314
+ const pattern = termType(object) === 'Literal' ? this.heldTerm(object as unknown as Quad['object']) : object;
315
+ // The model, from the copy.
316
+ if (termType(graph) === 'DefaultGraph') {
317
+ return this.model.getQuads(subject ?? null, predicate ?? null, (pattern ?? null) as never, DEFAULT_GRAPH) as unknown as Quad[];
318
+ }
319
+ // Everything: the model from the copy, the inner store's named graphs one by one.
320
+ if (!subject && !predicate && !object && !graph && this.inner.namedGraphs) {
321
+ const quads = this.model.getQuads(null, null, null, DEFAULT_GRAPH) as unknown as Quad[];
322
+ for (const named of this.namedGraphs()) {
323
+ for (const q of this.inner.getQuads(null, null, null, named)) quads.push(q);
324
+ }
325
+ return quads;
326
+ }
327
+ // One named graph; or every graph, once the inner store has the whole model.
328
+ if (graph || !this.behind) return this.inner.getQuads(subject, predicate, object, graph);
329
+ // Every graph while the inner store is being given the model: the model
330
+ // from the copy, the rest from the inner store.
331
+ const quads = this.model.getQuads(subject ?? null, predicate ?? null, (pattern ?? null) as never, DEFAULT_GRAPH) as unknown as Quad[];
332
+ for (const q of this.inner.getQuads(subject, predicate, object, null)) if (!inDefaultGraph(q)) quads.push(q);
333
+ return quads;
334
+ }
335
+
336
+ /** How many quads the model has. */
337
+ modelSize(): number {
338
+ return this.model.size;
339
+ }
340
+
341
+ /** The model graph, to read from without a copy — never to write to. */
342
+ modelGraph(): N3Store {
343
+ return this.model;
344
+ }
345
+
346
+ /**
347
+ * The named graphs — the metamodels' documents — as last listed, until one
348
+ * is written to: listing them was a scan of every metamodel quad, asked for
349
+ * at every relationship lookup (Task 22).
350
+ */
351
+ namedGraphs(): NamedNode[] {
352
+ if (!this.namedList) this.namedList = this.inner.namedGraphs?.() ?? [];
353
+ return this.namedList;
354
+ }
355
+
356
+ private namedList: NamedNode[] | undefined;
357
+
358
+ /** A named graph may have been written to: its list is to be read again. */
359
+ private namedChanged(): void {
360
+ this.namedList = undefined;
361
+ }
362
+
363
+ /** Every quad of the named graphs: the metamodels. */
364
+ namedGraphQuads(): Quad[] {
365
+ const quads: Quad[] = [];
366
+ const graphs = this.inner.namedGraphs ? this.namedGraphs() : [];
367
+ for (const graph of graphs) for (const q of this.inner.getQuads(null, null, null, graph)) quads.push(q);
368
+ if (!this.inner.namedGraphs) for (const q of this.inner.getQuads()) if (!inDefaultGraph(q)) quads.push(q);
369
+ return quads;
370
+ }
371
+
372
+ /** SPARQL over everything — the model the inner store is still to be given, given first. */
373
+ query(sparql: string): SparqlResult {
374
+ this.giveAll();
375
+ return this.inner.query(sparql);
376
+ }
377
+
378
+ /**
379
+ * SPARQL about the metamodels — viewpoints, relationship tables, the class
380
+ * hierarchy — which reads the default graph only for metamodel data put
381
+ * there. It does not wait for the model: right after a model is opened, the
382
+ * inner store may hold only part of it (Task 22).
383
+ */
384
+ queryMetamodels(sparql: string): SparqlResult {
385
+ return this.inner.query(sparql);
386
+ }
387
+
388
+ /**
389
+ * SPARQL Update is diffed by reading the default graph after it against the
390
+ * copy — the only way to know what an arbitrary update touched. But for one
391
+ * that drops or clears a named graph alone — a metamodel's document
392
+ * unloaded — which cannot touch the model: reading the model back cost about
393
+ * 3 s a document with a model of 347 000 triples, and unloading a metamodel
394
+ * of ten documents held the page for 34 s (the kernel's KD-23).
395
+ */
396
+ update(sparqlUpdate: string): void {
397
+ if (NAMED_GRAPH_DROP.test(sparqlUpdate)) {
398
+ this.namedChanged();
399
+ this.inner.update(sparqlUpdate);
400
+ return;
401
+ }
402
+ this.giveAll();
403
+ this.namedChanged();
404
+ this.guarded(() => this.inner.update(sparqlUpdate));
405
+ const { added, removed } = this.resync();
406
+ this.track(added, removed);
407
+ }
408
+
409
+ load(data: string, options: { format: string; graph?: string; baseIri?: string }): number {
410
+ if (!this.inner.load) throw new Error('The underlying store cannot bulk-load');
411
+ // Bulk loads target metamodel graphs; into the default graph they are rare
412
+ // and are reported as one change computed by snapshot.
413
+ if (options.graph) {
414
+ this.namedChanged();
415
+ return this.inner.load(data, options);
416
+ }
417
+ this.giveAll();
418
+ const count = this.guarded(() => (this.inner.load as NonNullable<RdfStore['load']>)(data, options));
419
+ this.track(this.resync().added, []);
420
+ return count;
421
+ }
422
+
423
+ size(): number {
424
+ this.giveAll();
425
+ return this.inner.size ? this.inner.size() : this.inner.getQuads().length;
426
+ }
427
+
428
+ /** Whether the inner store is still to be given part of the model. */
429
+ get behind(): boolean {
430
+ return this.given < this.backlog.length;
431
+ }
432
+
433
+ /** Give the inner store, now, the model quads it is still to have. */
434
+ giveAll(): void {
435
+ if (this.backlogTimer) {
436
+ clearTimeout(this.backlogTimer);
437
+ this.backlogTimer = null;
438
+ }
439
+ if (!this.giveAsTriples()) this.give(Infinity);
440
+ }
441
+
442
+ // ── Internals ──────────────────────────────────────────────────────────
443
+
444
+ /**
445
+ * Quads as the store holds them: what it gives back — what the copy keeps,
446
+ * undo removes, a peer keys and validation sees — is their form, not
447
+ * necessarily the one added (`"010"^^xsd:int` kept as `"10"^^xsd:integer`).
448
+ */
449
+ private held(quads: Quad[]): Quad[] {
450
+ if (quads.length === 0 || !this.inner.canonical) return quads;
451
+ return this.inner.canonical(quads);
452
+ }
453
+
454
+ /** Nothing more to give the inner store. */
455
+ private dropBacklog(): void {
456
+ if (this.backlogTimer) {
457
+ clearTimeout(this.backlogTimer);
458
+ this.backlogTimer = null;
459
+ }
460
+ this.backlog = [];
461
+ this.given = 0;
462
+ }
463
+
464
+ /** The inner store is to have these model quads, in the background. */
465
+ private giveLater(quads: Quad[]): void {
466
+ this.backlog = this.behind ? this.backlog.slice(this.given).concat(quads) : quads;
467
+ this.given = 0;
468
+ this.scheduleGiving();
469
+ }
470
+
471
+ private scheduleGiving(): void {
472
+ if (this.backlogTimer || !this.behind) return;
473
+ this.backlogTimer = setTimeout(() => {
474
+ this.backlogTimer = null;
475
+ this.give(SLICE_MS);
476
+ this.scheduleGiving();
477
+ }, 0);
478
+ }
479
+
480
+ /** Give the inner store backlogged quads the model still has, for about `budgetMs`. */
481
+ private give(budgetMs: number): void {
482
+ const started = now();
483
+ while (this.behind) {
484
+ const end = Math.min(this.backlog.length, this.given + STEP);
485
+ const step: Quad[] = [];
486
+ for (let i = this.given; i < end; i++) if (this.model.has(this.backlog[i] as never)) step.push(this.backlog[i]);
487
+ this.given = end;
488
+ this.giveStep(step);
489
+ if (now() - started >= budgetMs) break;
490
+ }
491
+ if (!this.behind) {
492
+ this.backlog = [];
493
+ this.given = 0;
494
+ }
495
+ }
496
+
497
+ /** Finish a large checked write in one native insertion, keeping the model copy and its order. */
498
+ private giveAsTriples(): boolean {
499
+ if (!this.inner.load || this.backlog.length - this.given < IN_BACKGROUND_FROM) return false;
500
+ const quads = this.backlog.slice(this.given).filter(quad => this.model.has(quad as never));
501
+ if (quads.some(({ subject, object }) => !canLoadAsTriples(subject) || !canLoadAsTriples(object))) return false;
502
+ try {
503
+ const writer = new Writer({ format: 'N-Triples' });
504
+ this.inner.load(writer.quadsToString(quads as never), { format: 'application/n-triples' });
505
+ } catch {
506
+ // Replay through giveStep on a native-load failure. Inserting an already loaded statement is safe.
507
+ return false;
508
+ }
509
+ this.dropBacklog();
510
+ return true;
511
+ }
512
+
513
+ /**
514
+ * Give the inner store one step of the backlog. Its terms were checked when
515
+ * added; should it refuse one even so, that quad leaves the model — said, and
516
+ * told to listeners as a removal — rather than the copy and the store differ.
517
+ */
518
+ private giveStep(step: Quad[]): void {
519
+ try {
520
+ this.inner.addQuads(step);
521
+ return;
522
+ } catch {
523
+ // One at a time, to find the one refused.
524
+ }
525
+ const refused: Quad[] = [];
526
+ for (const q of step) {
527
+ try {
528
+ this.inner.addQuads([q]);
529
+ } catch (err) {
530
+ console.warn('[la-gram] The store refused a quad of the model', err);
531
+ if (this.model.removeQuad(q as never)) refused.push(q);
532
+ }
533
+ }
534
+ this.track([], refused);
535
+ }
536
+
537
+ /** Whether the inner store has a quad — not one it cannot even hold. */
538
+ private innerHas(q: Quad): boolean {
539
+ try {
540
+ if (this.inner.has) return this.inner.has(q);
541
+ return this.inner.getQuads(q.subject as NamedNode, q.predicate as NamedNode, q.object as NamedNode, q.graph as NamedNode).length > 0;
542
+ } catch {
543
+ return false;
544
+ }
545
+ }
546
+
547
+ /** A term in the form the store holds it (a literal canonical, as Oxigraph keeps it). */
548
+ private heldTerm(term: Quad['object']): Quad['object'] {
549
+ if (!this.inner.canonical) return term;
550
+ return this.inner.canonical([DataFactory.quad(SCRATCH, SCRATCH, term as never) as unknown as Quad])[0].object;
551
+ }
552
+
553
+ /** Run a write to the inner store; if it fails part-way, the copy is read again from it. */
554
+ private guarded<T>(write: () => T): T {
555
+ try {
556
+ return write();
557
+ } catch (err) {
558
+ this.resync();
559
+ throw err;
560
+ }
561
+ }
562
+
563
+ /** Read the model graph again from the inner store into the copy: what that added and removed. */
564
+ private resync(): RdfDiff {
565
+ this.giveAll();
566
+ const before = this.model;
567
+ const after = new N3Store();
568
+ after.addQuads(this.defaultGraph() as never);
569
+ const added = (after.getQuads(null, null, null, null) as unknown as Quad[]).filter((q) => !before.has(q as never));
570
+ const removed = (before.getQuads(null, null, null, null) as unknown as Quad[]).filter((q) => !after.has(q as never));
571
+ this.model = after;
572
+ return { added, removed };
573
+ }
574
+
575
+ private defaultGraph(): Quad[] {
576
+ return this.inner.getQuads(null, null, null, { termType: 'DefaultGraph', value: '' } as unknown as NamedNode);
577
+ }
578
+
579
+ private track(added: Quad[], removed: Quad[]): void {
580
+ if (added.length === 0 && removed.length === 0) return;
581
+ const stamped = this.stamper !== null && !this.stamping && this.stampsOff === 0 && this.origin === 'local';
582
+ if (stamped) appendDiff(this.unstamped, added, removed);
583
+ for (const listener of this.writeListeners) listener({ added, removed });
584
+ for (const diff of this.recording) appendDiff(diff, added, removed);
585
+ if (this.pending) {
586
+ appendDiff(this.pending, added, removed);
587
+ return;
588
+ }
589
+ this.notify({ added, removed });
590
+ // A write outside any scope is an edit of its own: stamped now, as one more change.
591
+ if (stamped && this.depth === 0) this.batch(() => undefined);
592
+ }
593
+
594
+ private notify(diff: RdfDiff): void {
595
+ const change: StoreChange = { ...diff, origin: this.origin };
596
+ for (const listener of this.listeners) listener(change);
597
+ }
598
+ }
599
+
600
+ /**
601
+ * A canonical string for a quad — N-Quads-like, stable across stores. Used as a
602
+ * map key here and as the CRDT key in the collaboration binding.
603
+ */
604
+ export function quadKey(q: Quad): string {
605
+ return `${termKey(q.subject)} ${termKey(q.predicate)} ${termKey(q.object)}${
606
+ q.graph.termType === 'DefaultGraph' ? '' : ` ${termKey(q.graph)}`
607
+ }`;
608
+ }
609
+
610
+ function termKey(term: Quad['object']): string {
611
+ switch (term.termType) {
612
+ case 'NamedNode':
613
+ return `<${term.value}>`;
614
+ case 'BlankNode':
615
+ return `_:${term.value}`;
616
+ case 'Literal': {
617
+ const lit = term as { value: string; language?: string; datatype?: { value: string } };
618
+ const escaped = JSON.stringify(lit.value);
619
+ if (lit.language) return `${escaped}@${lit.language}`;
620
+ const dt = lit.datatype?.value;
621
+ return dt && dt !== 'http://www.w3.org/2001/XMLSchema#string' ? `${escaped}^^<${dt}>` : escaped;
622
+ }
623
+ case 'Quad': {
624
+ // An RDF 1.2 triple term, as N-Triples 1.2 writes it: `<<( s p o )>>`.
625
+ const triple = term as Quad;
626
+ return `<<( ${termKey(triple.subject)} ${termKey(triple.predicate)} ${termKey(triple.object)} )>>`;
627
+ }
628
+ default:
629
+ return `?${term.value}`;
630
+ }
631
+ }