@linked.archi/kernel 0.0.0-stage → 0.1.6
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.
- package/LICENSE +202 -0
- package/NOTICE +11 -0
- package/README.md +102 -2
- package/assets/arch-decision/arch-decision-metamodel.ttl +44 -0
- package/assets/arch-decision/arch-decision-onto.ttl +1566 -0
- package/assets/arch-decision/arch-decision-shapes.ttl +582 -0
- package/assets/arch-decision/arch-decision-tax.ttl +129 -0
- package/assets/arch-processes/arch-processes-metamodel.ttl +42 -0
- package/assets/arch-processes/arch-processes-tax.ttl +136 -0
- package/assets/arch-processes/archProcesses-shapes.ttl +189 -0
- package/assets/arch-processes/archProcesses.ttl +953 -0
- package/assets/archimate3/archimate3.2-element-shapes.ttl +379 -0
- package/assets/archimate3/archimate3.2-metamodel.ttl +205 -0
- package/assets/archimate3/archimate3.2-notation.ttl +1503 -0
- package/assets/archimate3/archimate3.2-onto.ttl +1579 -0
- package/assets/archimate3/archimate3.2-presentation-contexts.ttl +90 -0
- package/assets/archimate3/archimate3.2-principle-shapes.ttl +917 -0
- package/assets/archimate3/archimate3.2-questions.ttl +1125 -0
- package/assets/archimate3/archimate3.2-reference-data.ttl +74 -0
- package/assets/archimate3/archimate3.2-relationship-shapes.ttl +30956 -0
- package/assets/archimate3/archimate3.2-relationship-tables.ttl +5992 -0
- package/assets/archimate3/archimate3.2-tax.ttl +724 -0
- package/assets/archimate3/archimate3.2-viewpoint-shapes.ttl +59 -0
- package/assets/archimate3/archimate3.2-viewpoints.ttl +793 -0
- package/assets/archimate3/icons/and-junction.svg +3 -0
- package/assets/archimate3/icons/application-collaboration.svg +22 -0
- package/assets/archimate3/icons/application-component.svg +18 -0
- package/assets/archimate3/icons/application-event.svg +15 -0
- package/assets/archimate3/icons/application-function.svg +14 -0
- package/assets/archimate3/icons/application-interaction.svg +17 -0
- package/assets/archimate3/icons/application-interface.svg +16 -0
- package/assets/archimate3/icons/application-process.svg +14 -0
- package/assets/archimate3/icons/application-service.svg +14 -0
- package/assets/archimate3/icons/artifact.svg +16 -0
- package/assets/archimate3/icons/assessment.svg +17 -0
- package/assets/archimate3/icons/business-actor.svg +22 -0
- package/assets/archimate3/icons/business-collaboration.svg +21 -0
- package/assets/archimate3/icons/business-event.svg +15 -0
- package/assets/archimate3/icons/business-function.svg +14 -0
- package/assets/archimate3/icons/business-interaction.svg +18 -0
- package/assets/archimate3/icons/business-interface.svg +16 -0
- package/assets/archimate3/icons/business-object.svg +14 -0
- package/assets/archimate3/icons/business-process.svg +15 -0
- package/assets/archimate3/icons/business-role.svg +16 -0
- package/assets/archimate3/icons/business-service.svg +14 -0
- package/assets/archimate3/icons/capability.svg +40 -0
- package/assets/archimate3/icons/communication-network.svg +18 -0
- package/assets/archimate3/icons/constraint.svg +18 -0
- package/assets/archimate3/icons/contract.svg +13 -0
- package/assets/archimate3/icons/course-of-action.svg +20 -0
- package/assets/archimate3/icons/data-object.svg +14 -0
- package/assets/archimate3/icons/deliverable.svg +11 -0
- package/assets/archimate3/icons/device.svg +18 -0
- package/assets/archimate3/icons/distribution-network.svg +19 -0
- package/assets/archimate3/icons/driver.svg +24 -0
- package/assets/archimate3/icons/equipment.svg +31 -0
- package/assets/archimate3/icons/facility.svg +16 -0
- package/assets/archimate3/icons/gap.svg +22 -0
- package/assets/archimate3/icons/glyphs/activity.svg +3 -0
- package/assets/archimate3/icons/glyphs/actor.svg +14 -0
- package/assets/archimate3/icons/glyphs/application-component.svg +12 -0
- package/assets/archimate3/icons/glyphs/artifact.svg +9 -0
- package/assets/archimate3/icons/glyphs/assessment.svg +9 -0
- package/assets/archimate3/icons/glyphs/capability.svg +32 -0
- package/assets/archimate3/icons/glyphs/cloud-gear.svg +3 -0
- package/assets/archimate3/icons/glyphs/collaboration.svg +15 -0
- package/assets/archimate3/icons/glyphs/communication-network.svg +12 -0
- package/assets/archimate3/icons/glyphs/constraint.svg +10 -0
- package/assets/archimate3/icons/glyphs/contract.svg +5 -0
- package/assets/archimate3/icons/glyphs/course-of-action.svg +12 -0
- package/assets/archimate3/icons/glyphs/data-object.svg +4 -0
- package/assets/archimate3/icons/glyphs/deliverable.svg +3 -0
- package/assets/archimate3/icons/glyphs/device.svg +10 -0
- package/assets/archimate3/icons/glyphs/distribution-network.svg +11 -0
- package/assets/archimate3/icons/glyphs/driver.svg +16 -0
- package/assets/archimate3/icons/glyphs/equipment.svg +22 -0
- package/assets/archimate3/icons/glyphs/event.svg +6 -0
- package/assets/archimate3/icons/glyphs/facility.svg +7 -0
- package/assets/archimate3/icons/glyphs/function.svg +6 -0
- package/assets/archimate3/icons/glyphs/gap.svg +14 -0
- package/assets/archimate3/icons/glyphs/goal.svg +10 -0
- package/assets/archimate3/icons/glyphs/grouping.svg +4 -0
- package/assets/archimate3/icons/glyphs/interaction.svg +10 -0
- package/assets/archimate3/icons/glyphs/interface.svg +8 -0
- package/assets/archimate3/icons/glyphs/location.svg +6 -0
- package/assets/archimate3/icons/glyphs/material.svg +12 -0
- package/assets/archimate3/icons/glyphs/meaning.svg +3 -0
- package/assets/archimate3/icons/glyphs/node.svg +8 -0
- package/assets/archimate3/icons/glyphs/object.svg +4 -0
- package/assets/archimate3/icons/glyphs/outcome.svg +16 -0
- package/assets/archimate3/icons/glyphs/path.svg +10 -0
- package/assets/archimate3/icons/glyphs/person-silhouette.svg +3 -0
- package/assets/archimate3/icons/glyphs/plateau.svg +8 -0
- package/assets/archimate3/icons/glyphs/principle.svg +12 -0
- package/assets/archimate3/icons/glyphs/process.svg +6 -0
- package/assets/archimate3/icons/glyphs/product.svg +4 -0
- package/assets/archimate3/icons/glyphs/representation.svg +4 -0
- package/assets/archimate3/icons/glyphs/requirement.svg +6 -0
- package/assets/archimate3/icons/glyphs/resource.svg +14 -0
- package/assets/archimate3/icons/glyphs/role.svg +6 -0
- package/assets/archimate3/icons/glyphs/service.svg +6 -0
- package/assets/archimate3/icons/glyphs/stakeholder.svg +6 -0
- package/assets/archimate3/icons/glyphs/system-software.svg +14 -0
- package/assets/archimate3/icons/glyphs/value-stream.svg +3 -0
- package/assets/archimate3/icons/glyphs/value.svg +3 -0
- package/assets/archimate3/icons/glyphs/work-package.svg +4 -0
- package/assets/archimate3/icons/goal.svg +18 -0
- package/assets/archimate3/icons/grouping.svg +12 -0
- package/assets/archimate3/icons/implementation-event.svg +15 -0
- package/assets/archimate3/icons/location.svg +14 -0
- package/assets/archimate3/icons/material.svg +21 -0
- package/assets/archimate3/icons/meaning.svg +11 -0
- package/assets/archimate3/icons/node.svg +14 -0
- package/assets/archimate3/icons/or-junction.svg +3 -0
- package/assets/archimate3/icons/outcome.svg +25 -0
- package/assets/archimate3/icons/path.svg +18 -0
- package/assets/archimate3/icons/plateau.svg +16 -0
- package/assets/archimate3/icons/principle.svg +21 -0
- package/assets/archimate3/icons/product.svg +12 -0
- package/assets/archimate3/icons/representation.svg +12 -0
- package/assets/archimate3/icons/requirement.svg +14 -0
- package/assets/archimate3/icons/resource.svg +23 -0
- package/assets/archimate3/icons/stakeholder.svg +15 -0
- package/assets/archimate3/icons/system-software.svg +22 -0
- package/assets/archimate3/icons/technology-collaboration.svg +22 -0
- package/assets/archimate3/icons/technology-event.svg +15 -0
- package/assets/archimate3/icons/technology-function.svg +14 -0
- package/assets/archimate3/icons/technology-interaction.svg +18 -0
- package/assets/archimate3/icons/technology-interface.svg +16 -0
- package/assets/archimate3/icons/technology-process.svg +14 -0
- package/assets/archimate3/icons/technology-service.svg +14 -0
- package/assets/archimate3/icons/value-stream.svg +11 -0
- package/assets/archimate3/icons/value.svg +11 -0
- package/assets/archimate3/icons/work-package.svg +12 -0
- package/assets/archimate4/archimate4-element-shapes.ttl +404 -0
- package/assets/archimate4/archimate4-metamodel.ttl +179 -0
- package/assets/archimate4/archimate4-notation.ttl +1147 -0
- package/assets/archimate4/archimate4-onto.ttl +1559 -0
- package/assets/archimate4/archimate4-presentation-contexts.ttl +60 -0
- package/assets/archimate4/archimate4-principle-shapes.ttl +895 -0
- package/assets/archimate4/archimate4-questions.ttl +1224 -0
- package/assets/archimate4/archimate4-reference-data.ttl +56 -0
- package/assets/archimate4/archimate4-relationship-shapes.ttl +16920 -0
- package/assets/archimate4/archimate4-relationship-tables.ttl +3200 -0
- package/assets/archimate4/archimate4-tax.ttl +749 -0
- package/assets/archimate4/archimate4-viewpoint-shapes.ttl +55 -0
- package/assets/archimate4/archimate4-viewpoints.ttl +777 -0
- package/assets/archimate4/icons/and-junction.svg +3 -0
- package/assets/archimate4/icons/application-component.svg +18 -0
- package/assets/archimate4/icons/application-interface.svg +16 -0
- package/assets/archimate4/icons/artifact.svg +16 -0
- package/assets/archimate4/icons/assessment.svg +17 -0
- package/assets/archimate4/icons/business-actor.svg +22 -0
- package/assets/archimate4/icons/business-interface.svg +16 -0
- package/assets/archimate4/icons/business-object.svg +14 -0
- package/assets/archimate4/icons/capability.svg +40 -0
- package/assets/archimate4/icons/collaboration.svg +21 -0
- package/assets/archimate4/icons/communication-network.svg +18 -0
- package/assets/archimate4/icons/course-of-action.svg +20 -0
- package/assets/archimate4/icons/data-object.svg +14 -0
- package/assets/archimate4/icons/deliverable.svg +11 -0
- package/assets/archimate4/icons/device.svg +18 -0
- package/assets/archimate4/icons/distribution-network.svg +19 -0
- package/assets/archimate4/icons/driver.svg +24 -0
- package/assets/archimate4/icons/equipment.svg +31 -0
- package/assets/archimate4/icons/event.svg +15 -0
- package/assets/archimate4/icons/facility.svg +16 -0
- package/assets/archimate4/icons/function.svg +13 -0
- package/assets/archimate4/icons/glyphs/activity.svg +3 -0
- package/assets/archimate4/icons/glyphs/actor.svg +14 -0
- package/assets/archimate4/icons/glyphs/application-component.svg +12 -0
- package/assets/archimate4/icons/glyphs/artifact.svg +9 -0
- package/assets/archimate4/icons/glyphs/assessment.svg +9 -0
- package/assets/archimate4/icons/glyphs/capability.svg +32 -0
- package/assets/archimate4/icons/glyphs/cloud-gear.svg +3 -0
- package/assets/archimate4/icons/glyphs/collaboration.svg +15 -0
- package/assets/archimate4/icons/glyphs/communication-network.svg +12 -0
- package/assets/archimate4/icons/glyphs/constraint.svg +10 -0
- package/assets/archimate4/icons/glyphs/contract.svg +5 -0
- package/assets/archimate4/icons/glyphs/course-of-action.svg +12 -0
- package/assets/archimate4/icons/glyphs/data-object.svg +4 -0
- package/assets/archimate4/icons/glyphs/deliverable.svg +3 -0
- package/assets/archimate4/icons/glyphs/device.svg +10 -0
- package/assets/archimate4/icons/glyphs/distribution-network.svg +11 -0
- package/assets/archimate4/icons/glyphs/driver.svg +16 -0
- package/assets/archimate4/icons/glyphs/equipment.svg +22 -0
- package/assets/archimate4/icons/glyphs/event.svg +6 -0
- package/assets/archimate4/icons/glyphs/facility.svg +7 -0
- package/assets/archimate4/icons/glyphs/function.svg +6 -0
- package/assets/archimate4/icons/glyphs/gap.svg +14 -0
- package/assets/archimate4/icons/glyphs/goal.svg +10 -0
- package/assets/archimate4/icons/glyphs/grouping.svg +4 -0
- package/assets/archimate4/icons/glyphs/interaction.svg +10 -0
- package/assets/archimate4/icons/glyphs/interface.svg +8 -0
- package/assets/archimate4/icons/glyphs/location.svg +6 -0
- package/assets/archimate4/icons/glyphs/material.svg +12 -0
- package/assets/archimate4/icons/glyphs/meaning.svg +3 -0
- package/assets/archimate4/icons/glyphs/node.svg +8 -0
- package/assets/archimate4/icons/glyphs/object.svg +4 -0
- package/assets/archimate4/icons/glyphs/outcome.svg +16 -0
- package/assets/archimate4/icons/glyphs/path.svg +10 -0
- package/assets/archimate4/icons/glyphs/person-silhouette.svg +3 -0
- package/assets/archimate4/icons/glyphs/plateau.svg +8 -0
- package/assets/archimate4/icons/glyphs/principle.svg +12 -0
- package/assets/archimate4/icons/glyphs/process.svg +6 -0
- package/assets/archimate4/icons/glyphs/product.svg +4 -0
- package/assets/archimate4/icons/glyphs/representation.svg +4 -0
- package/assets/archimate4/icons/glyphs/requirement.svg +6 -0
- package/assets/archimate4/icons/glyphs/resource.svg +14 -0
- package/assets/archimate4/icons/glyphs/role.svg +6 -0
- package/assets/archimate4/icons/glyphs/service.svg +6 -0
- package/assets/archimate4/icons/glyphs/stakeholder.svg +6 -0
- package/assets/archimate4/icons/glyphs/system-software.svg +14 -0
- package/assets/archimate4/icons/glyphs/value-stream.svg +3 -0
- package/assets/archimate4/icons/glyphs/value.svg +3 -0
- package/assets/archimate4/icons/glyphs/work-package.svg +4 -0
- package/assets/archimate4/icons/goal.svg +18 -0
- package/assets/archimate4/icons/grouping.svg +12 -0
- package/assets/archimate4/icons/location.svg +14 -0
- package/assets/archimate4/icons/material.svg +21 -0
- package/assets/archimate4/icons/meaning.svg +11 -0
- package/assets/archimate4/icons/node.svg +14 -0
- package/assets/archimate4/icons/or-junction.svg +3 -0
- package/assets/archimate4/icons/outcome.svg +25 -0
- package/assets/archimate4/icons/path.svg +18 -0
- package/assets/archimate4/icons/plateau.svg +16 -0
- package/assets/archimate4/icons/principle.svg +21 -0
- package/assets/archimate4/icons/process.svg +15 -0
- package/assets/archimate4/icons/product.svg +12 -0
- package/assets/archimate4/icons/requirement.svg +14 -0
- package/assets/archimate4/icons/resource.svg +23 -0
- package/assets/archimate4/icons/role.svg +16 -0
- package/assets/archimate4/icons/service.svg +13 -0
- package/assets/archimate4/icons/stakeholder.svg +15 -0
- package/assets/archimate4/icons/system-software.svg +22 -0
- package/assets/archimate4/icons/technology-interface.svg +16 -0
- package/assets/archimate4/icons/value-stream.svg +11 -0
- package/assets/archimate4/icons/value.svg +11 -0
- package/assets/archimate4/icons/work-package.svg +12 -0
- package/assets/basic/basic-metamodel.ttl +100 -0
- package/assets/basic/basic-notation-bubble.ttl +175 -0
- package/assets/basic/basic-notation.ttl +172 -0
- package/assets/basic/basic-onto.ttl +171 -0
- package/assets/basic/basic-presentation-contexts.ttl +70 -0
- package/assets/basic/basic-tax.ttl +130 -0
- package/assets/basic/icons/aggregation.svg +7 -0
- package/assets/basic/icons/association.svg +6 -0
- package/assets/basic/icons/element.svg +5 -0
- package/assets/basic/icons/glyphs/aggregation.svg +6 -0
- package/assets/basic/icons/glyphs/association.svg +5 -0
- package/assets/basic/icons/glyphs/element.svg +5 -0
- package/assets/basic/icons/glyphs/relationship.svg +5 -0
- package/assets/basic/icons/relationship.svg +7 -0
- package/assets/basic/notation-bubble/icons/aggregation.svg +7 -0
- package/assets/basic/notation-bubble/icons/association.svg +6 -0
- package/assets/basic/notation-bubble/icons/element.svg +5 -0
- package/assets/basic/notation-bubble/icons/glyphs/aggregation.svg +6 -0
- package/assets/basic/notation-bubble/icons/glyphs/association.svg +5 -0
- package/assets/basic/notation-bubble/icons/glyphs/element.svg +5 -0
- package/assets/basic/notation-bubble/icons/glyphs/relationship.svg +5 -0
- package/assets/basic/notation-bubble/icons/relationship.svg +7 -0
- package/assets/catalog.ttl +7406 -0
- package/assets/core/core-notation.ttl +131 -0
- package/assets/core/core-onto.ttl +2811 -0
- package/assets/core/core-questions.ttl +957 -0
- package/assets/core/core-shapes.ttl +915 -0
- package/assets/core/core-vis-onto.ttl +1408 -0
- package/assets/core/core-vis-shapes.ttl +851 -0
- package/assets/core-vis/markers/circle-dot.svg +3 -0
- package/assets/core-vis/markers/closed-arrowhead.svg +3 -0
- package/assets/core-vis/markers/filled-diamond.svg +3 -0
- package/assets/core-vis/markers/half-arrowhead.svg +3 -0
- package/assets/core-vis/markers/hollow-arrowhead.svg +3 -0
- package/assets/core-vis/markers/hollow-diamond.svg +3 -0
- package/assets/core-vis/markers/open-arrowhead.svg +3 -0
- package/fixtures/interoperability/archimate4-demo.trig +259 -0
- package/fixtures/interoperability/expected-findings.json +26 -0
- package/fixtures/interoperability/shop-seed.ttl +225 -0
- package/package.json +136 -4
- package/scripts/sync-assets.mjs +252 -0
- package/src/conformance.ts +66 -0
- package/src/import-export/model-io.ts +426 -0
- package/src/index.ts +11 -0
- package/src/metamodel-manager/asset-sources.ts +754 -0
- package/src/metamodel-manager/bundled-assets.ts +599 -0
- package/src/metamodel-manager/catalogue.ts +114 -0
- package/src/metamodel-manager/class-hierarchy.ts +155 -0
- package/src/metamodel-manager/index.ts +65 -0
- package/src/metamodel-manager/interfaces.ts +422 -0
- package/src/metamodel-manager/metamodel-completeness.ts +162 -0
- package/src/metamodel-manager/metamodel-loader.ts +864 -0
- package/src/metamodel-manager/notation-resolver.ts +284 -0
- package/src/metamodel-manager/notation.ts +659 -0
- package/src/metamodel-manager/palette-generator.ts +882 -0
- package/src/metamodel-manager/property-shapes.ts +290 -0
- package/src/metamodel-manager/taxonomy.ts +904 -0
- package/src/metamodel-manager/type-guide.ts +241 -0
- package/src/metamodel-manager/viewpoint-manager.ts +147 -0
- package/src/model-manager/appearance.ts +389 -0
- package/src/model-manager/cascade.ts +246 -0
- package/src/model-manager/clipboard.ts +566 -0
- package/src/model-manager/context-names.ts +108 -0
- package/src/model-manager/custom-properties.ts +549 -0
- package/src/model-manager/derivations.ts +166 -0
- package/src/model-manager/direct-permissions.ts +182 -0
- package/src/model-manager/drawing-order.ts +119 -0
- package/src/model-manager/element-manager.ts +69 -0
- package/src/model-manager/element-pictures.ts +118 -0
- package/src/model-manager/field-names.ts +42 -0
- package/src/model-manager/folders.ts +354 -0
- package/src/model-manager/grid.ts +6 -0
- package/src/model-manager/index.ts +43 -0
- package/src/model-manager/interfaces.ts +417 -0
- package/src/model-manager/iri-conformance.ts +183 -0
- package/src/model-manager/label-expression.ts +233 -0
- package/src/model-manager/legacy-migration.ts +200 -0
- package/src/model-manager/missing-references.ts +86 -0
- package/src/model-manager/model-diff-text.ts +150 -0
- package/src/model-manager/model-diff.ts +534 -0
- package/src/model-manager/model-merge.ts +1160 -0
- package/src/model-manager/model-namespace.ts +199 -0
- package/src/model-manager/model-operations.ts +132 -0
- package/src/model-manager/model-queries.ts +616 -0
- package/src/model-manager/model-session.ts +759 -0
- package/src/model-manager/multiplicity.ts +74 -0
- package/src/model-manager/nested-resources.ts +145 -0
- package/src/model-manager/own-metamodel.ts +157 -0
- package/src/model-manager/placement-copy.ts +116 -0
- package/src/model-manager/property-catalog.ts +787 -0
- package/src/model-manager/proposal-order.ts +203 -0
- package/src/model-manager/provenance.ts +396 -0
- package/src/model-manager/questions.ts +475 -0
- package/src/model-manager/rdf-scope.ts +97 -0
- package/src/model-manager/relationship-graph.ts +462 -0
- package/src/model-manager/relationship-manager.ts +962 -0
- package/src/model-manager/relationship-table.ts +189 -0
- package/src/model-manager/relationship-validity.ts +261 -0
- package/src/model-manager/resource-edits.ts +28 -0
- package/src/model-manager/short-hash.ts +104 -0
- package/src/model-manager/snapshot-lines.ts +88 -0
- package/src/model-manager/version-history.ts +205 -0
- package/src/model-manager/view-from-results.ts +81 -0
- package/src/model-manager/view-manager.ts +1410 -0
- package/src/model-manager/vocabulary.ts +279 -0
- package/src/rdf-engine/finding-shape.ts +23 -0
- package/src/rdf-engine/graph-scope.ts +163 -0
- package/src/rdf-engine/index.ts +7 -0
- package/src/rdf-engine/interfaces.ts +144 -0
- package/src/rdf-engine/iri.ts +203 -0
- package/src/rdf-engine/localized-text.ts +13 -0
- package/src/rdf-engine/namespaces.ts +41 -0
- package/src/rdf-engine/native-load.ts +37 -0
- package/src/rdf-engine/observable-store.ts +609 -0
- package/src/rdf-engine/rdf-store.ts +520 -0
- package/src/rdf-engine/shacl-validator.ts +602 -0
- package/src/rdf-engine/shape-reach.ts +140 -0
- package/src/rdf-engine/shape-switches.ts +165 -0
- package/src/rdf-engine/single-valued.ts +219 -0
- package/src/rdf-engine/sparql-constraints.ts +349 -0
- package/src/rdf-engine/term-codec.ts +51 -0
- package/src/rdf-engine/ttl-parser.ts +51 -0
- package/src/rdf-engine/ttl-serializer.ts +79 -0
- package/src/rdf-engine/turtle-writer.ts +284 -0
- package/src/rdf-engine/validation-engine.ts +747 -0
- package/src/rdf-engine/validation-service.ts +186 -0
- package/src/rdf-engine/validation.worker.ts +47 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHACL's SPARQL-based constraints (`sh:sparql` with `sh:select`), run with
|
|
3
|
+
* Oxigraph — the rules `RdfShaclValidator` takes out of the shapes, because
|
|
4
|
+
* rdf-validate-shacl cannot run them: ArchiMate's self-loops and principles,
|
|
5
|
+
* the viewpoints' rules, core-vis's nesting. Phase 3 of the Git collaboration
|
|
6
|
+
* plan: run in the validation engine, so the editor and CI report them alike.
|
|
7
|
+
*
|
|
8
|
+
* ## What is read of a shape
|
|
9
|
+
*
|
|
10
|
+
* - its targets: `sh:targetClass` (instances of the class and its subclasses),
|
|
11
|
+
* `sh:targetNode`, `sh:targetSubjectsOf`, `sh:targetObjectsOf`, and the
|
|
12
|
+
* implicit class target of a shape that is a class;
|
|
13
|
+
* - each `sh:sparql` constraint's `sh:select`, with the prefixes its
|
|
14
|
+
* `sh:prefixes` declare (`sh:declare`), its `sh:message` (else the shape's),
|
|
15
|
+
* and the shape's `sh:severity` (a violation when none);
|
|
16
|
+
* - `sh:deactivated true`, on the shape or the constraint, leaves it out.
|
|
17
|
+
*
|
|
18
|
+
* A property shape's constraint takes its `sh:path` as `$PATH` and as the
|
|
19
|
+
* result's path when the path is one property; other paths are not supported
|
|
20
|
+
* and the constraint is left out.
|
|
21
|
+
*
|
|
22
|
+
* ## How it is run
|
|
23
|
+
*
|
|
24
|
+
* One query per constraint: the shape's targets are joined into the query's
|
|
25
|
+
* outer `WHERE`, binding `$this` — among the nodes validated, when only some
|
|
26
|
+
* are (`VALUES $this { … }`). Each solution is a result: `$this` its
|
|
27
|
+
* focus node, `?path` (else the shape's path) its path, `?value` its value; the
|
|
28
|
+
* message's `{$this}`, `{?var}` take the solution's values.
|
|
29
|
+
*
|
|
30
|
+
* Pre-binding `$this` this way is what SHACL-SPARQL implementations do in
|
|
31
|
+
* practice; it differs from the specification's pre-binding only for a `$this`
|
|
32
|
+
* inside a sub-query, which no published Linked.Archi rule has.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { Quad, Term } from '@rdfjs/types';
|
|
36
|
+
import { NAMESPACES } from './namespaces.js';
|
|
37
|
+
import type { ValidationResult, ValidationSeverity } from './interfaces.js';
|
|
38
|
+
|
|
39
|
+
const SH = NAMESPACES.sh;
|
|
40
|
+
const RDF_TYPE = NAMESPACES.rdf + 'type';
|
|
41
|
+
const RDFS_CLASS = NAMESPACES.rdfs + 'Class';
|
|
42
|
+
const OWL_CLASS = NAMESPACES.owl + 'Class';
|
|
43
|
+
const SPARQL_COMPONENT = `${SH}SPARQLConstraintComponent`;
|
|
44
|
+
const SEVERITIES: Record<string, ValidationSeverity> = {
|
|
45
|
+
[`${SH}Violation`]: 'Violation',
|
|
46
|
+
[`${SH}Warning`]: 'Warning',
|
|
47
|
+
[`${SH}Info`]: 'Info',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** A shape's targets. */
|
|
51
|
+
export interface ShapeTargets {
|
|
52
|
+
classes: string[];
|
|
53
|
+
nodes: string[];
|
|
54
|
+
subjectsOf: string[];
|
|
55
|
+
objectsOf: string[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One SPARQL-based constraint of a shape, ready to run. */
|
|
59
|
+
export interface SparqlConstraint {
|
|
60
|
+
/** The shape (an IRI, or `_:label`). */
|
|
61
|
+
shape: string;
|
|
62
|
+
/** The query, prefixes declared, `$PATH` replaced. */
|
|
63
|
+
select: string;
|
|
64
|
+
/** The message template: the constraint's, else the shape's; `''` for none. */
|
|
65
|
+
message: string;
|
|
66
|
+
severity: ValidationSeverity;
|
|
67
|
+
targets: ShapeTargets;
|
|
68
|
+
/** A property shape's path, when it is one property. */
|
|
69
|
+
path?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A constraint the shapes have that is not run, and why. */
|
|
73
|
+
export interface SkippedConstraint {
|
|
74
|
+
shape: string;
|
|
75
|
+
reason: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** What {@link SparqlRunner} asks of a store: a SPARQL SELECT, answered as rows of N-Triples-like terms. */
|
|
79
|
+
export interface SparqlSelect {
|
|
80
|
+
select(query: string): Array<Map<string, Term>>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function now(): number {
|
|
84
|
+
return typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The SPARQL-based constraints of a shapes graph, and those that cannot be run. */
|
|
88
|
+
export function sparqlConstraintsOf(shapes: readonly Quad[]): { constraints: SparqlConstraint[]; skipped: SkippedConstraint[] } {
|
|
89
|
+
const bySubject = new Map<string, Quad[]>();
|
|
90
|
+
for (const q of shapes) {
|
|
91
|
+
const key = idOf(q.subject as Term);
|
|
92
|
+
const list = bySubject.get(key);
|
|
93
|
+
if (list) list.push(q);
|
|
94
|
+
else bySubject.set(key, [q]);
|
|
95
|
+
}
|
|
96
|
+
const objects = (subject: string, predicate: string): Term[] =>
|
|
97
|
+
(bySubject.get(subject) ?? []).filter((q) => q.predicate.value === predicate).map((q) => q.object as Term);
|
|
98
|
+
const value = (subject: string, predicate: string): string | undefined => objects(subject, predicate)[0]?.value;
|
|
99
|
+
const deactivated = (subject: string) => value(subject, `${SH}deactivated`) === 'true';
|
|
100
|
+
|
|
101
|
+
const constraints: SparqlConstraint[] = [];
|
|
102
|
+
const skipped: SkippedConstraint[] = [];
|
|
103
|
+
for (const q of shapes) {
|
|
104
|
+
if (q.predicate.value !== `${SH}sparql`) continue;
|
|
105
|
+
const shape = idOf(q.subject as Term);
|
|
106
|
+
const constraint = idOf(q.object as Term);
|
|
107
|
+
if (deactivated(shape) || deactivated(constraint)) continue;
|
|
108
|
+
const select = value(constraint, `${SH}select`);
|
|
109
|
+
if (!select) {
|
|
110
|
+
skipped.push({ shape, reason: 'only sh:select is supported' });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const pathTerm = objects(shape, `${SH}path`)[0];
|
|
114
|
+
if (pathTerm && pathTerm.termType !== 'NamedNode') {
|
|
115
|
+
skipped.push({ shape, reason: 'a path that is not one property' });
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const path = pathTerm?.value;
|
|
119
|
+
const prefixes = objects(constraint, `${SH}prefixes`).flatMap((ontology) =>
|
|
120
|
+
objects(idOf(ontology), `${SH}declare`).map((declaration) => ({
|
|
121
|
+
prefix: value(idOf(declaration), `${SH}prefix`),
|
|
122
|
+
namespace: value(idOf(declaration), `${SH}namespace`),
|
|
123
|
+
})),
|
|
124
|
+
);
|
|
125
|
+
const declared = new Map<string, string>();
|
|
126
|
+
for (const { prefix, namespace } of prefixes) if (prefix !== undefined && namespace) declared.set(prefix, namespace);
|
|
127
|
+
const header = [...declared].map(([prefix, namespace]) => `PREFIX ${prefix}: <${namespace}>`).join('\n');
|
|
128
|
+
const body = path ? select.replace(/\$PATH\b/g, `<${path}>`) : select;
|
|
129
|
+
const message = (objects(constraint, `${SH}message`)[0] ?? objects(shape, `${SH}message`)[0])?.value ?? '';
|
|
130
|
+
const severity = SEVERITIES[value(shape, `${SH}severity`) ?? ''] ?? 'Violation';
|
|
131
|
+
const targets: ShapeTargets = {
|
|
132
|
+
classes: objects(shape, `${SH}targetClass`).map((t) => t.value),
|
|
133
|
+
nodes: objects(shape, `${SH}targetNode`).filter((t) => t.termType === 'NamedNode').map((t) => t.value),
|
|
134
|
+
subjectsOf: objects(shape, `${SH}targetSubjectsOf`).map((t) => t.value),
|
|
135
|
+
objectsOf: objects(shape, `${SH}targetObjectsOf`).map((t) => t.value),
|
|
136
|
+
};
|
|
137
|
+
if (objects(shape, RDF_TYPE).some((t) => t.value === RDFS_CLASS || t.value === OWL_CLASS) && !shape.startsWith('_:')) {
|
|
138
|
+
targets.classes.push(shape);
|
|
139
|
+
}
|
|
140
|
+
if (targets.classes.length + targets.nodes.length + targets.subjectsOf.length + targets.objectsOf.length === 0) continue;
|
|
141
|
+
constraints.push({ shape, select: header ? `${header}\n${body}` : body, message, severity, targets, ...(path ? { path } : {}) });
|
|
142
|
+
}
|
|
143
|
+
return { constraints, skipped };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The IRIs a constraint's query names — prefixed names expanded — so the
|
|
148
|
+
* metamodels' statements it reads can be given with the model (a viewpoint's
|
|
149
|
+
* `arch:includesConcept`, for instance).
|
|
150
|
+
*/
|
|
151
|
+
export function predicatesNamedBy(constraint: SparqlConstraint): string[] {
|
|
152
|
+
const prefixes = new Map<string, string>();
|
|
153
|
+
for (const m of constraint.select.matchAll(/PREFIX\s+([A-Za-z][\w-]*)?:\s*<([^>]*)>/gi)) prefixes.set(m[1] ?? '', m[2]!);
|
|
154
|
+
const body = withoutComments(constraint.select.replace(/PREFIX\s+[\w-]*:\s*<[^>]*>/gi, ''));
|
|
155
|
+
const out = new Set<string>();
|
|
156
|
+
for (const m of body.matchAll(/<([^>\s]+)>/g)) out.add(m[1]!);
|
|
157
|
+
for (const m of body.matchAll(/(?<![\w?$<"])([A-Za-z][\w-]*)?:([A-Za-z_][\w-]*)/g)) {
|
|
158
|
+
const ns = prefixes.get(m[1] ?? '');
|
|
159
|
+
if (ns) out.add(ns + m[2]);
|
|
160
|
+
}
|
|
161
|
+
return [...out];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Runs constraints against a store. */
|
|
165
|
+
export class SparqlRunner {
|
|
166
|
+
/** Constraints Oxigraph refused, kept so each is reported once. */
|
|
167
|
+
private readonly failed = new Map<string, string>();
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param nameOf A node's name, to put in a message for `{$this}` and the
|
|
171
|
+
* like — meta's messages say "Element {$this} has no relationships", which
|
|
172
|
+
* reads better with a name than an IRI; the IRI where it has none.
|
|
173
|
+
*/
|
|
174
|
+
constructor(
|
|
175
|
+
readonly constraints: readonly SparqlConstraint[],
|
|
176
|
+
private readonly store: SparqlSelect,
|
|
177
|
+
private readonly nameOf: (iri: string) => string | undefined = () => undefined,
|
|
178
|
+
) {}
|
|
179
|
+
|
|
180
|
+
/** Why a constraint could not be run, by shape. */
|
|
181
|
+
get failures(): ReadonlyMap<string, string> {
|
|
182
|
+
return this.failed;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** How long each constraint the last {@link run} ran took, in ms, in the order run (KD-30). */
|
|
186
|
+
get lastTimings(): ReadonlyArray<{ shape: string; ms: number }> {
|
|
187
|
+
return this.timings;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private timings: Array<{ shape: string; ms: number }> = [];
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The results for every target of every constraint — or, given `focus`, for
|
|
194
|
+
* those of them only. `isTarget`, when given, says which of them a
|
|
195
|
+
* constraint's targets hold, as its query would find them: a constraint none
|
|
196
|
+
* of them is a target of is not run (KD-21). Its query may cost as much for
|
|
197
|
+
* a few nodes as for all — a `MINUS` is evaluated over the whole model.
|
|
198
|
+
*/
|
|
199
|
+
run(focus?: readonly Term[], isTarget?: (targets: ShapeTargets, node: Term) => boolean): ValidationResult[] {
|
|
200
|
+
this.timings = [];
|
|
201
|
+
if (focus && focus.length === 0) return [];
|
|
202
|
+
const every = focus ? focus.map(termText).join(' ') : undefined;
|
|
203
|
+
const results: ValidationResult[] = [];
|
|
204
|
+
for (const constraint of this.constraints) {
|
|
205
|
+
const key = `${constraint.shape}\u0000${constraint.select}`;
|
|
206
|
+
if (this.failed.has(key)) continue;
|
|
207
|
+
let values = every;
|
|
208
|
+
if (focus && isTarget) {
|
|
209
|
+
const targets = focus.filter((node) => isTarget(constraint.targets, node));
|
|
210
|
+
if (targets.length === 0) continue;
|
|
211
|
+
values = targets.map(termText).join(' ');
|
|
212
|
+
}
|
|
213
|
+
const query = withTargets(constraint.select, targetPattern(constraint.targets, values));
|
|
214
|
+
let rows: Array<Map<string, Term>>;
|
|
215
|
+
const started = now();
|
|
216
|
+
try {
|
|
217
|
+
rows = this.store.select(query);
|
|
218
|
+
} catch (err: unknown) {
|
|
219
|
+
this.failed.set(key, err instanceof Error ? err.message : String(err));
|
|
220
|
+
continue;
|
|
221
|
+
} finally {
|
|
222
|
+
this.timings.push({ shape: constraint.shape, ms: now() - started });
|
|
223
|
+
}
|
|
224
|
+
for (const row of rows) {
|
|
225
|
+
const focusNode = row.get('this');
|
|
226
|
+
if (!focusNode || (focusNode.termType !== 'NamedNode' && focusNode.termType !== 'BlankNode')) continue;
|
|
227
|
+
const path = row.get('path')?.value ?? constraint.path ?? '';
|
|
228
|
+
// `$value`, when the query selects one: what the finding is about (KD-17).
|
|
229
|
+
const value = row.get('value');
|
|
230
|
+
results.push({
|
|
231
|
+
focusNode: focusNode as ValidationResult['focusNode'],
|
|
232
|
+
resultPath: { termType: 'NamedNode', value: path } as ValidationResult['resultPath'],
|
|
233
|
+
sourceShape: { termType: constraint.shape.startsWith('_:') ? 'BlankNode' : 'NamedNode', value: constraint.shape.replace(/^_:/, '') } as ValidationResult['sourceShape'],
|
|
234
|
+
message: messageOf(constraint.message, row, this.nameOf) || `Violates ${constraint.shape}`,
|
|
235
|
+
severity: constraint.severity,
|
|
236
|
+
sourceConstraintComponent: { termType: 'NamedNode', value: SPARQL_COMPONENT } as ValidationResult['focusNode'],
|
|
237
|
+
...(value && (value.termType === 'NamedNode' || value.termType === 'Literal') ? { value } : {}),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return dedupe(results);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A group pattern binding `$this` to the shape's targets — among `values` (the
|
|
247
|
+
* nodes validated, as SPARQL terms) when given: inside the sub-query, so that
|
|
248
|
+
* it lists those only, not every target.
|
|
249
|
+
*/
|
|
250
|
+
export function targetPattern(targets: ShapeTargets, values?: string): string {
|
|
251
|
+
const parts = [
|
|
252
|
+
...targets.classes.map((c) => `{ $this <${RDF_TYPE}>/<${NAMESPACES.rdfs}subClassOf>* <${c}> . }`),
|
|
253
|
+
...(targets.nodes.length > 0 ? [`{ VALUES $this { ${targets.nodes.map((n) => `<${n}>`).join(' ')} } }`] : []),
|
|
254
|
+
...targets.subjectsOf.map((p) => `{ $this <${p}> [] . }`),
|
|
255
|
+
...targets.objectsOf.map((p) => `{ [] <${p}> $this . }`),
|
|
256
|
+
];
|
|
257
|
+
const among = values === undefined ? '' : `VALUES $this { ${values} } `;
|
|
258
|
+
return `{ SELECT DISTINCT $this WHERE { ${among}${parts.join(' UNION ')} } }\n`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The query with `pattern` first in its outer `WHERE` (or its first group, when it has no `WHERE`). */
|
|
262
|
+
export function withTargets(query: string, pattern: string): string {
|
|
263
|
+
const at = outerGroupStart(query);
|
|
264
|
+
if (at < 0) throw new Error('The query has no group pattern.');
|
|
265
|
+
return `${query.slice(0, at + 1)}\n${pattern}${query.slice(at + 1)}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Where the outer `WHERE`'s `{` is: the first `{` after the `SELECT` — IRIs,
|
|
270
|
+
* strings and comments skipped, since `#` and braces may appear in them.
|
|
271
|
+
*/
|
|
272
|
+
function outerGroupStart(query: string): number {
|
|
273
|
+
let seenSelect = false;
|
|
274
|
+
for (let i = 0; i < query.length; i++) {
|
|
275
|
+
const ch = query[i]!;
|
|
276
|
+
if (ch === '#') {
|
|
277
|
+
const end = query.indexOf('\n', i);
|
|
278
|
+
if (end < 0) return -1;
|
|
279
|
+
i = end;
|
|
280
|
+
} else if (ch === '<' && /^<[^\s<>"{}|^`\\]*>/.test(query.slice(i))) {
|
|
281
|
+
i = query.indexOf('>', i);
|
|
282
|
+
} else if (ch === '"' || ch === "'") {
|
|
283
|
+
const long = query.startsWith(ch.repeat(3), i);
|
|
284
|
+
const close = long ? ch.repeat(3) : ch;
|
|
285
|
+
let j = i + close.length;
|
|
286
|
+
while (j < query.length && !query.startsWith(close, j)) j += query[j] === '\\' ? 2 : 1;
|
|
287
|
+
i = j + close.length - 1;
|
|
288
|
+
} else if (!seenSelect && /^select\b/i.test(query.slice(i, i + 7)) && !/\w/.test(query[i - 1] ?? '')) {
|
|
289
|
+
seenSelect = true;
|
|
290
|
+
i += 5;
|
|
291
|
+
} else if (seenSelect && ch === '{') {
|
|
292
|
+
return i;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return -1;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The query text with its comments taken out (IRIs and strings kept). */
|
|
299
|
+
function withoutComments(query: string): string {
|
|
300
|
+
let out = '';
|
|
301
|
+
for (let i = 0; i < query.length; i++) {
|
|
302
|
+
const ch = query[i]!;
|
|
303
|
+
if (ch === '#') {
|
|
304
|
+
const end = query.indexOf('\n', i);
|
|
305
|
+
i = end < 0 ? query.length : end - 1;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (ch === '<' && /^<[^\s<>"{}|^`\\]*>/.test(query.slice(i))) {
|
|
309
|
+
const end = query.indexOf('>', i);
|
|
310
|
+
out += query.slice(i, end + 1);
|
|
311
|
+
i = end;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
out += ch;
|
|
315
|
+
}
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** A message with `{$var}` and `{?var}` filled in from a solution — a node by its name, when it has one. */
|
|
320
|
+
function messageOf(template: string, row: Map<string, Term>, nameOf: (iri: string) => string | undefined): string {
|
|
321
|
+
return template.replace(/\{[$?]([A-Za-z_]\w*)\}/g, (whole, name: string) => {
|
|
322
|
+
const term = row.get(name);
|
|
323
|
+
if (!term) return whole;
|
|
324
|
+
return (term.termType === 'NamedNode' ? nameOf(term.value) : undefined) ?? term.value;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** A term as SPARQL writes it. */
|
|
329
|
+
function termText(term: Term): string {
|
|
330
|
+
if (term.termType === 'NamedNode') return `<${term.value}>`;
|
|
331
|
+
if (term.termType === 'BlankNode') return `_:${term.value}`;
|
|
332
|
+
return JSON.stringify(term.value);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** A node's key in the shapes graph. */
|
|
336
|
+
function idOf(term: Term): string {
|
|
337
|
+
return term.termType === 'BlankNode' ? `_:${term.value}` : term.value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** One result per focus node, shape, path and message. */
|
|
341
|
+
function dedupe(results: ValidationResult[]): ValidationResult[] {
|
|
342
|
+
const seen = new Set<string>();
|
|
343
|
+
return results.filter((r) => {
|
|
344
|
+
const key = [r.focusNode.value, r.sourceShape.value, r.resultPath.value, r.message].join('\u0000');
|
|
345
|
+
if (seen.has(key)) return false;
|
|
346
|
+
seen.add(key);
|
|
347
|
+
return true;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quads as plain data, to post to a worker and back (Task 22): structured
|
|
3
|
+
* cloning keeps no class, so a term becomes its n3 id — `<iri>` as the IRI,
|
|
4
|
+
* `_:b1`, `"text"@en`, `"1"^^<…#double>` — and an RDF 1.2 triple term (what a
|
|
5
|
+
* relationship `rdf:reifies`) a nested array of three.
|
|
6
|
+
*/
|
|
7
|
+
import { DataFactory, termFromId, termToId } from 'n3';
|
|
8
|
+
import type { Quad, Term } from '@rdfjs/types';
|
|
9
|
+
|
|
10
|
+
/** A term: its n3 id, or a triple term's three. */
|
|
11
|
+
export type EncodedTerm = string | [EncodedTerm, EncodedTerm, EncodedTerm];
|
|
12
|
+
|
|
13
|
+
/** A quad: subject, predicate, object — and a graph when not the default one. */
|
|
14
|
+
export type EncodedQuad = [EncodedTerm, EncodedTerm, EncodedTerm] | [EncodedTerm, EncodedTerm, EncodedTerm, string];
|
|
15
|
+
|
|
16
|
+
function encodeTerm(term: Term): EncodedTerm {
|
|
17
|
+
if (term.termType === 'Quad') {
|
|
18
|
+
const q = term as unknown as Quad;
|
|
19
|
+
return [encodeTerm(q.subject), encodeTerm(q.predicate), encodeTerm(q.object)];
|
|
20
|
+
}
|
|
21
|
+
return termToId(term as never);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function decodeTerm(encoded: EncodedTerm): Term {
|
|
25
|
+
if (Array.isArray(encoded)) {
|
|
26
|
+
return DataFactory.quad(decodeTerm(encoded[0]) as never, decodeTerm(encoded[1]) as never, decodeTerm(encoded[2]) as never) as unknown as Term;
|
|
27
|
+
}
|
|
28
|
+
return termFromId(encoded, DataFactory as never) as unknown as Term;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function encodeQuad(q: Quad): EncodedQuad {
|
|
32
|
+
const triple: [EncodedTerm, EncodedTerm, EncodedTerm] = [encodeTerm(q.subject), encodeTerm(q.predicate), encodeTerm(q.object)];
|
|
33
|
+
return q.graph.termType === 'DefaultGraph' ? triple : [...triple, termToId(q.graph as never)];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function decodeQuad(e: EncodedQuad): Quad {
|
|
37
|
+
return DataFactory.quad(
|
|
38
|
+
decodeTerm(e[0]) as never,
|
|
39
|
+
decodeTerm(e[1]) as never,
|
|
40
|
+
decodeTerm(e[2]) as never,
|
|
41
|
+
e.length === 4 ? (termFromId(e[3], DataFactory as never) as never) : DataFactory.defaultGraph(),
|
|
42
|
+
) as unknown as Quad;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function encodeQuads(quads: readonly Quad[]): EncodedQuad[] {
|
|
46
|
+
return quads.map(encodeQuad);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function decodeQuads(encoded: readonly EncodedQuad[]): Quad[] {
|
|
50
|
+
return encoded.map(decodeQuad);
|
|
51
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TTL Parser implementation using N3.js.
|
|
3
|
+
*
|
|
4
|
+
* Parses Turtle (TTL) strings into RDF quads, returning either
|
|
5
|
+
* the parsed quads or a ParseError with line number and message.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Parser as N3Parser } from 'n3';
|
|
9
|
+
import type { Quad } from '@rdfjs/types';
|
|
10
|
+
import type { TtlParser, ParseResult, PrefixMap } from './interfaces.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Concrete implementation of TtlParser backed by N3.js.
|
|
14
|
+
*
|
|
15
|
+
* After a successful parse, the discovered prefix declarations
|
|
16
|
+
* are available via the `prefixes` property.
|
|
17
|
+
*/
|
|
18
|
+
export class N3TtlParser implements TtlParser {
|
|
19
|
+
/** Prefix declarations discovered during the last successful parse. */
|
|
20
|
+
prefixes: PrefixMap = {};
|
|
21
|
+
|
|
22
|
+
parse(input: string): ParseResult<Quad[]> {
|
|
23
|
+
const parser = new N3Parser();
|
|
24
|
+
try {
|
|
25
|
+
const quads: Quad[] = parser.parse(input, null, (prefix: string, ns: { value: string }) => {
|
|
26
|
+
this.prefixes[prefix] = ns.value;
|
|
27
|
+
}) as unknown as Quad[];
|
|
28
|
+
return { ok: true, quads };
|
|
29
|
+
}
|
|
30
|
+
catch (err: unknown) {
|
|
31
|
+
const { line, message } = extractError(err);
|
|
32
|
+
return { ok: false, error: { line, message } };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Extract a line number and message from an N3.js parse error.
|
|
39
|
+
*
|
|
40
|
+
* N3.js embeds the line number in the error message with the pattern
|
|
41
|
+
* "on line N". We parse that out so callers get structured data.
|
|
42
|
+
*/
|
|
43
|
+
function extractError(err: unknown): { line: number; message: string } {
|
|
44
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
45
|
+
|
|
46
|
+
// N3.js error messages typically contain "on line <number>"
|
|
47
|
+
const lineMatch = message.match(/on line (\d+)/i);
|
|
48
|
+
const line = lineMatch ? parseInt(lineMatch[1], 10) : 1;
|
|
49
|
+
|
|
50
|
+
return { line, message };
|
|
51
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TTL Serializer implementation using N3.js.
|
|
3
|
+
*
|
|
4
|
+
* Serializes RDF quads to pretty-printed Turtle with namespace prefix
|
|
5
|
+
* declarations at the top, triples grouped by subject, and consistent
|
|
6
|
+
* indentation. Uses the shortest matching prefix for each IRI, and for the
|
|
7
|
+
* model the quads are about, the layout's own prefixes (`turtle-writer.ts`).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Quad } from '@rdfjs/types';
|
|
11
|
+
import type { TtlSerializer, PrefixMap } from './interfaces.js';
|
|
12
|
+
import { writeTurtle, type TurtleModel } from './turtle-writer.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Select the shortest matching prefix for each namespace IRI.
|
|
16
|
+
*
|
|
17
|
+
* When multiple prefixes map to IRIs where one is a prefix of another
|
|
18
|
+
* (e.g. `arch: → …/core#` and `arch-vis: → …/core-vis#`), N3.js may
|
|
19
|
+
* pick the wrong one. We deduplicate by keeping only the longest
|
|
20
|
+
* namespace URI per prefix so that the Writer's regex-based matching
|
|
21
|
+
* naturally selects the most specific (and therefore shortest-label)
|
|
22
|
+
* prefix for any given IRI.
|
|
23
|
+
*/
|
|
24
|
+
function buildPrefixMap(prefixes: PrefixMap): PrefixMap {
|
|
25
|
+
// Sort entries so that longer namespace URIs come first.
|
|
26
|
+
// N3.js Writer builds a regex from the prefixes and matches the first
|
|
27
|
+
// one that fits, so ordering matters for "shortest prefix" selection.
|
|
28
|
+
const entries = Object.entries(prefixes).filter(
|
|
29
|
+
([, uri]) => uri.length > 0,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// Deduplicate: if two prefixes point to the exact same URI, keep the
|
|
33
|
+
// shorter prefix label (that's the "shortest matching prefix" rule).
|
|
34
|
+
const uriToPrefix = new Map<string, string>();
|
|
35
|
+
for (const [prefix, uri] of entries) {
|
|
36
|
+
const existing = uriToPrefix.get(uri);
|
|
37
|
+
if (!existing || prefix.length < existing.length) {
|
|
38
|
+
uriToPrefix.set(uri, prefix);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Rebuild the map keeping only the winning prefixes, preserving all
|
|
43
|
+
// unique namespace URIs.
|
|
44
|
+
const result: PrefixMap = {};
|
|
45
|
+
for (const [prefix, uri] of entries) {
|
|
46
|
+
if (uriToPrefix.get(uri) === prefix) {
|
|
47
|
+
result[prefix] = uri;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Concrete implementation of TtlSerializer backed by N3.js Writer.
|
|
55
|
+
*
|
|
56
|
+
* The Writer natively handles:
|
|
57
|
+
* - `@prefix` declarations at the top of the output
|
|
58
|
+
* - Grouping triples by subject (using `;` for predicate lists)
|
|
59
|
+
* - Consistent indentation
|
|
60
|
+
*/
|
|
61
|
+
export class N3TtlSerializer implements TtlSerializer {
|
|
62
|
+
serialize(quads: Quad[], prefixes: PrefixMap, model?: TurtleModel): string {
|
|
63
|
+
const cleanPrefixes = buildPrefixMap(prefixes);
|
|
64
|
+
|
|
65
|
+
// Sort quads by subject IRI so the output is deterministic and
|
|
66
|
+
// logically grouped.
|
|
67
|
+
const sorted = [...quads].sort((a, b) => {
|
|
68
|
+
const sa = a.subject.value;
|
|
69
|
+
const sb = b.subject.value;
|
|
70
|
+
if (sa !== sb) return sa < sb ? -1 : 1;
|
|
71
|
+
const pa = a.predicate.value;
|
|
72
|
+
const pb = b.predicate.value;
|
|
73
|
+
if (pa !== pb) return pa < pb ? -1 : 1;
|
|
74
|
+
return a.object.value < b.object.value ? -1 : a.object.value > b.object.value ? 1 : 0;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return writeTurtle(sorted, { prefixes: cleanPrefixes, ...(model ? { model } : {}) });
|
|
78
|
+
}
|
|
79
|
+
}
|