clownface-shacl-path
Advanced tools
Comparing version 2.0.2 to 2.1.0
import { NamedNode } from 'rdf-js'; | ||
import type { MultiPointer } from 'clownface'; | ||
import { ShaclPropertyPath } from './path.js'; | ||
/** | ||
@@ -9,3 +10,3 @@ * Finds all nodes connected to the input node by following a [SHACL Property Path](https://www.w3.org/TR/shacl/#dfn-shacl-property-path) | ||
*/ | ||
export declare function findNodes(pointer: MultiPointer, shPath: MultiPointer | NamedNode): MultiPointer; | ||
export declare function findNodes(pointer: MultiPointer, shPath: MultiPointer | NamedNode | ShaclPropertyPath): MultiPointer; | ||
//# sourceMappingURL=findNodes.d.ts.map |
@@ -59,6 +59,15 @@ import TermSet from '@rdfjs/term-set'; | ||
export function findNodes(pointer, shPath) { | ||
const path = 'termType' in shPath ? pointer.node(shPath) : shPath; | ||
const terms = new FindNodesVisitor().visit(Path.fromNode(path), { pointer }); | ||
let path; | ||
if ('termType' in shPath) { | ||
path = Path.fromNode(pointer.node(shPath)); | ||
} | ||
else if ('value' in shPath) { | ||
path = Path.fromNode(shPath); | ||
} | ||
else { | ||
path = shPath; | ||
} | ||
const terms = new FindNodesVisitor().visit(path, { pointer }); | ||
return pointer.node([...new TermSet(terms)]); | ||
} | ||
//# sourceMappingURL=findNodes.js.map |
@@ -51,4 +51,8 @@ import { NamedNode } from 'rdf-js'; | ||
} | ||
export declare function fromNode(path: MultiPointer | NamedNode): ShaclPropertyPath; | ||
interface Options { | ||
allowNamedNodeSequencePaths?: boolean; | ||
} | ||
export declare function fromNode(path: MultiPointer | NamedNode, { allowNamedNodeSequencePaths }?: Options): ShaclPropertyPath; | ||
export declare function assertWellFormedPath(ptr: MultiPointer): asserts ptr is GraphPointer; | ||
export {}; | ||
//# sourceMappingURL=path.d.ts.map |
@@ -93,3 +93,6 @@ import { sh } from '@tpluscode/rdf-ns-builders'; | ||
} | ||
export function fromNode(path) { | ||
export function fromNode(path, { allowNamedNodeSequencePaths = false } = {}) { | ||
return transformNode({ allowNamedNodeSequencePaths }, path); | ||
} | ||
function transformNode(options, path) { | ||
if ('termType' in path) { | ||
@@ -99,3 +102,3 @@ return new PredicatePath(path); | ||
assertWellFormedPath(path); | ||
if (path.term.termType === 'NamedNode') { | ||
if (path.term.termType === 'NamedNode' && !options.allowNamedNodeSequencePaths) { | ||
return new PredicatePath(path.term); | ||
@@ -107,3 +110,3 @@ } | ||
assertWellFormedShaclList(paths); | ||
return new SequencePath(paths.map(fromNode)); | ||
return new SequencePath(paths.map(transformNode.bind(null, options))); | ||
} | ||
@@ -113,3 +116,3 @@ if (path.term.termType === 'BlankNode') { | ||
if (inversePath.term) { | ||
return new InversePath(fromNode(inversePath)); | ||
return new InversePath(transformNode(options, inversePath)); | ||
} | ||
@@ -120,17 +123,20 @@ const alternativePath = path.out(sh.alternativePath); | ||
assertWellFormedShaclList(list); | ||
return new AlternativePath(list.map(fromNode)); | ||
return new AlternativePath(list.map(transformNode.bind(null, options))); | ||
} | ||
const zeroOrMorePath = path.out(sh.zeroOrMorePath); | ||
if (zeroOrMorePath.term) { | ||
return new ZeroOrMorePath(fromNode(zeroOrMorePath)); | ||
return new ZeroOrMorePath(transformNode(options, zeroOrMorePath)); | ||
} | ||
const oneOrMorePath = path.out(sh.oneOrMorePath); | ||
if (oneOrMorePath.term) { | ||
return new OneOrMorePath(fromNode(oneOrMorePath)); | ||
return new OneOrMorePath(transformNode(options, oneOrMorePath)); | ||
} | ||
const zeroOrOnePath = path.out(sh.zeroOrOnePath); | ||
if (zeroOrOnePath.term) { | ||
return new ZeroOrOnePath(fromNode(zeroOrOnePath)); | ||
return new ZeroOrOnePath(transformNode(options, zeroOrOnePath)); | ||
} | ||
} | ||
if (path.term.termType === 'NamedNode' && options.allowNamedNodeSequencePaths) { | ||
return new PredicatePath(path.term); | ||
} | ||
throw new Error(`Unrecognized property path ${path.value}`); | ||
@@ -137,0 +143,0 @@ } |
{ | ||
"name": "clownface-shacl-path", | ||
"description": "Find nodes in graph by following SHACL Paths", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -33,34 +33,18 @@ "type": "module", | ||
"@changesets/cli": "^2.19.0", | ||
"@rdfjs/data-model": "^2.0.1", | ||
"@rdfjs/dataset": "^2.0.1", | ||
"@rdfjs/namespace": "^2.0.0", | ||
"@tpluscode/eslint-config": "^0.4.2", | ||
"@tpluscode/eslint-config": "^0.4.4", | ||
"@types/chai": "^4.2.19", | ||
"@types/clownface": "^2.0.0", | ||
"@types/mocha": "^10", | ||
"@types/node": "^18", | ||
"@types/rdf-js": "^4.0.1", | ||
"@types/rdfjs__data-model": "^2.0.2", | ||
"@types/rdfjs__dataset": "^2.0.1", | ||
"@types/rdfjs__term-set": "^2.0.2", | ||
"@typescript-eslint/eslint-plugin": "^5.55.0", | ||
"@typescript-eslint/parser": "^5.55.0", | ||
"babel-plugin-add-import-extension": "^1.5.1", | ||
"@zazuko/env-node": "^1.0.3", | ||
"c8": "^7.13.0", | ||
"chai": "^4.3.4", | ||
"clownface": "^2.0.0", | ||
"eslint": "^8.34.0", | ||
"eslint-config-standard": "^17.0.0", | ||
"eslint-import-resolver-typescript": "^3.5.3", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-n": "^15.6.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"eslint-plugin-require-extensions": "^0.1.2", | ||
"husky": "^7.0.0", | ||
"lint-staged": "^11.0.0", | ||
"husky": "^8.0.3", | ||
"lint-staged": "^15.2.0", | ||
"mocha": "^10.2.0", | ||
"npm-run-all": "^4.1.5", | ||
"standard": "^17.0.0", | ||
"ts-node": "^10.9.1", | ||
"string-to-stream": "^3.0.1", | ||
"tsm": "^2.3.0", | ||
"typescript": "^5.0.2" | ||
@@ -81,3 +65,3 @@ }, | ||
], | ||
"loader": "ts-node/esm" | ||
"loader": "tsm" | ||
}, | ||
@@ -84,0 +68,0 @@ "lint-staged": { |
@@ -125,5 +125,23 @@ # clownface-shacl-path | ||
## Advanced options | ||
## Advanced usage | ||
### Allow Named Node Sequence Paths | ||
The SHACL specification requires that lists in Sequence Paths are blank nodes. However, some implementations | ||
may use Named Nodes instead. To allow that, you can manually create the SHACL Property Path object from a graph pointer | ||
and pass it to `findNodes` or `toSparql`: | ||
```typescript | ||
import type { GraphPointer } from 'clownface' | ||
import { findNodes, fromNode } from 'clownface-shacl-path' | ||
let pathNode: GraphPointer | ||
let startNode: GraphPointer | ||
const path = fromNode(pathNode, { allowNamedNodeSequencePaths: true }) | ||
const nodes = findNodes(startNode, path) | ||
``` | ||
## Advanced Property Path handling | ||
If it is necessary to implement a custom logic for processing of Property Paths, create a class extending from | ||
@@ -130,0 +148,0 @@ [`PathVisitor`](src/lib/path.ts). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33745
18
367
189