Skip to content

Commit

Permalink
accept env things in shacl paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr committed Dec 16, 2024
1 parent 926b121 commit 27ec6bd
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/shacl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
unique,
} from "./lens";

import { DataFactory } from "rdf-data-factory";
import { DataFactory, NamedNode } from "rdf-data-factory";
import { RDFL, RDFS, SHACL } from "./ontology";

const { literal } = new DataFactory();
Expand Down Expand Up @@ -156,9 +156,7 @@ export const ShaclAlternativepath: BasicLens<
export const ShaclPredicatePath: BasicLens<
Cont,
BasicLensM<Cont, Cont>
> = new BasicLens((c) => {
return pred(c.id);
});
> = extractLeaf(XSD.terms.custom("iri")).map(pred);

export const ShaclInversePath: BasicLens<Cont, BasicLensM<Cont, Cont>> = pred(
SHACL.inversePath,
Expand Down Expand Up @@ -289,6 +287,9 @@ function dataTypeToExtract(dataType: Term, t: Term): unknown {
if (dataType.equals(XSD.terms.string)) return t.value;
if (dataType.equals(XSD.terms.dateTime)) return new Date(t.value);
if (dataType.equals(XSD.terms.custom("boolean"))) return t.value === "true";
if (dataType.equals(XSD.terms.custom("iri"))) return new NamedNode(t.value);
if (dataType.equals(XSD.terms.custom("anyURI")))
return new NamedNode(t.value);

return t;
}
Expand Down Expand Up @@ -341,6 +342,12 @@ function envLens(dataType: Term): BasicLens<Cont, unknown> {
});
}

function extractLeaf(datatype: Term): BasicLens<Cont, unknown> {
return envLens(datatype).or(
empty<Cont>().map((item) => dataTypeToExtract(datatype, item.id)),
);
}

function extractProperty(
cache: Cache,
_subClasses: SubClasses,
Expand All @@ -360,9 +367,7 @@ function extractProperty(
pred(SHACL.datatype)
.one()
.map(({ id }) => ({
extract: envLens(id).or(
empty<Cont>().map((item) => dataTypeToExtract(id, item.id)),
),
extract: extractLeaf(id),
}));

const clazzLens: BasicLens<Cont, { extract: ShapeField["extract"] }> =
Expand Down
107 changes: 107 additions & 0 deletions test/shacl2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RDF } from "@treecg/types";
import { Parser } from "n3";
import { extractShapes } from "../src/shacl";
import { RDFL } from "../src/ontology";
import { BasicLensM, Cont } from "../src";

const prefixes = `
@prefix js: <https://w3id.org/conn/js#> .
Expand Down Expand Up @@ -457,4 +458,110 @@ ${prefixes}
expect(obj.custom.value).toBe("VALUE");
});
});

describe("Extract path from env variable", () => {
const shape = `
${prefixes}
[] a sh:NodeShape;
sh:targetClass js:Point;
sh:property [
sh:datatype xsd:string;
sh:path <str>;
sh:name "str";
sh:maxCount 1;
], [
sh:class rdfl:PathLens;
sh:path <path>;
sh:name "path";
sh:maxCount 1;
], [
sh:class rdfl:PathLens;
sh:path <path2>;
sh:name "path2";
sh:maxCount 1;
], [
sh:class rdfl:PathLens;
sh:path <complex>;
sh:name "complex";
sh:maxCount 1;
].
`;
const data = `
${prefixes}
<abc> a js:Point;
js:generatedAtTime 42;
<testpred> 43;
<entry> [
js:generatedAtTime 45;
];
<str> [
a rdfl:EnvVariable;
rdfl:envDefault js:generatedAtTime;
rdfl:envKey "envworks"
];
<path> [
a rdfl:EnvVariable;
rdfl:envDefault js:generatedAtTime;
rdfl:envKey "test"
];
<path2> [
a rdfl:EnvVariable;
rdfl:envDefault js:generatedAtTime;
rdfl:envKey "notset"
];
<complex> ([
a rdfl:EnvVariable;
rdfl:envDefault js:generatedAtTime;
rdfl:envKey "test2"
]
[
a rdfl:EnvVariable;
rdfl:envDefault js:generatedAtTime;
rdfl:envKey "notset"
]
).
`;

const output = extractShapes(parseQuads(shape));
const quads = parseQuads(data);

process.env["envworks"] = "true";
process.env["test"] = "testpred";
process.env["test2"] = "entry";

const quad = quads.find((x) => x.predicate.equals(RDF.terms.type))!;
const start = {
id: quad.subject,
quads,
};

const obj = <
{
str: string;
path: BasicLensM<Cont, Cont>;
path2: BasicLensM<Cont, Cont>;
complex: BasicLensM<Cont, Cont>;
}
>output.lenses[quad.object.value].execute(start, []);

test("From env variable that is set", () => {
const path1 = obj.path.execute(start).map((x) => x.id.value);
expect(path1).toEqual(["43"]);
});

test("From env variable that is not set", () => {
const path2 = obj.path2.execute(start).map((x) => x.id.value);
expect(path2).toEqual(["42"]);
});

test("Works in complex paths", () => {
const path2 = obj.complex.execute(start).map((x) => x.id.value);
expect(path2).toEqual(["45"]);
});

test("Check if process.env.VAR is the way", () => {
expect(obj.str).toBe("true");
});
});
});

0 comments on commit 27ec6bd

Please sign in to comment.