@shexjs/shape-path-query
v1.0.0-alpha.33
Published
ShapePath query interface for ShEx.js
Downloads
480
Readme
@shexjs/shape-path-query
Query RDF data by pointing at schema elements: a ShapePath expression selects constraints in a ShEx schema (shape-path-core), and shapePathQuery answers with the terms those constraints matched when the data validated — XPath for the schema, results from the graph.
Install
npm install @shexjs/shape-path-queryQuick start
const Fs = require("fs");
const Path = require("path");
const {Store, Parser} = require("n3");
const Sp = require("shape-path-core");
const {ctor: RdfJsDb} = require("@shexjs/neighborhood-rdfjs");
const {shapePathQuery} = require("@shexjs/shape-path-query");
// the Issue example that ships with shape-path-core
const schema = JSON.parse(Fs.readFileSync(Path.join(Sp.examples, "issue/Issue.json"), "utf8"));
const turtle = Fs.readFileSync(Path.join(Sp.examples, "issue/Issue2.ttl"), "utf8");
// 1. a ShapePath picks schema elements: the <p:href> constraint in <#DiscItem>
const yy = {base: new URL("http://project.example/schema"), prefixes: {}};
const pathExpr = new Sp.Parser.ShapePathParser(yy).parse(
"@<http://project.example/schema#DiscItem>~<http://project.example/ns#href>"
);
const nodeSet = pathExpr.evalPathExpr([schema], new Sp.Ast.EvalContext(schema));
// 2. validate, and answer with what those elements matched in the data
const graph = new Store(new Parser({baseIRI: "http://project.example/"}).parse(turtle));
shapePathQuery(schema, nodeSet, RdfJsDb(graph),
[{node: "http://instance.example/project1/Issue2",
shape: "http://project.example/schema#Issue"}]
).then(terms => console.log(terms));[ 'http://instance.example/project1/img1.jpg' ]shapePathQuery(schema, nodeSet, db, shapeMap) → Promise<terms[]>
- schema — a ShExJ schema (
@types/shexjshaped); - nodeSet — schema elements selected by
evalPathExpr(they are annotated in place with ShExMap variable bindings — that's the mechanism: the ShapePath aims the query, ShExMap captures the matches); - db — a
@shexjs/neighborhood-apiimplementation, e.g.@shexjs/neighborhood-rdfjsover your graph; - shapeMap — which node/shape pairs to validate (a parsed shape map).
Returns the data terms the selected schema elements matched, one entry per binding; throws if the shape map doesn't validate.
From the command line
spquery.js takes a ShapePath (,-separated for a union), a schema, -d data and -m shape map:
./bin/spquery.js \
'@<http://project.example/schema#DiscItem>~<http://project.example/ns#href>,@<http://project.example/schema#Issue>~<http://project.example/ns#spec>/valueExpr/shapeExprs~<http://project.example/ns#href>' \
node_modules/shape-path-core/examples/issue/Issue.json \
-d node_modules/shape-path-core/examples/issue/Issue2.ttl \
-m '<http://instance.example/project1/Issue2>@<http://project.example/schema#Issue>'[
"http://instance.example/project1/img1.jpg",
"http://instance.example/project1/spec3"
](or npm run toy from this directory).
@shexjs/shape-path-query is one of the shex.js packages; installing shex pulls in the whole suite, and its README maps them.
