Skip to content

Commit

Permalink
feat: check for undefined, not falsy
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr committed Jan 23, 2025
1 parent 1ba430b commit 0a5891a
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rdf-lens",
"version": "1.3.3",
"version": "1.3.4",
"main": "./dist/index.js",
"type": "module",
"exports": {
Expand Down
4 changes: 2 additions & 2 deletions src/shacl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function toLens(
return field.path
.thenFlat(thenListExtract.or(noListExtract).asMulti())
.thenAll(field.extract)
.map((x) => x.filter((x) => !!x))
.map((x) => x.filter((x) => x !== undefined))
.map((xs) => {
if (xs.length < minCount) {
throw `${shape.ty}:${field.name} required at least ${minCount} elements, found ${xs.length}`;
Expand All @@ -78,7 +78,7 @@ export function toLens(
return xs;
})
.map((x) => {
const out = x.filter((x) => !!x);
const out = x.filter((x) => x !== undefined);
if (maxCount < 2) {
return out[0];
} else {
Expand Down
25 changes: 25 additions & 0 deletions test/regressions/maregraph/data.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
_:df_9_1 <https://w3id.org/sds#payload> <http://marineregions.org/mrgid/3959?t=1104534000> <https://w3id.org/sds#DataDescription> .
_:df_9_1 <https://w3id.org/sds#stream> <http://example.org/ns#BenchmarkStream> <https://w3id.org/sds#DataDescription> .
_:df_9_1 <https://w3id.org/sds#bucket> <> <https://w3id.org/sds#DataDescription> .
<http://marineregions.org/mrgid/3959?t=1104534000> <http://purl.org/dc/terms/isVersionOf> <http://marineregions.org/mrgid/3959> .
<http://marineregions.org/mrgid/3959?t=1104534000> <http://purl.org/dc/terms/modified> "2004-03-01T23:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://marineregions.org/ns/ontology#MRGeoObject> .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://marineregions.org/ns/placetypes#Canyons> .
<http://marineregions.org/mrgid/3959> <http://marineregions.org/ns/ontology#isPartOf> <http://marineregions.org/mrgid/4279> .
<http://marineregions.org/mrgid/3959> <http://purl.org/dc/terms/modified> "2004-03-01T23:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/2004/02/skos/core#exactMatch> <urn:acuf:ufi:45863> .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/2004/02/skos/core#note> ": Named after Skikda city"@en .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/2004/02/skos/core#note> ": Removed from GEBCO gazetteer 2010"@en .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/2004/02/skos/core#note> "Coordinates in ACUF: Latitude: 37.133333; Longitude: 6.783333"@en .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/2004/02/skos/core#prefLabel> "Skikda Canyons"@en .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/ns/dcat#centroid> "<http://www.opengis.net/def/crs/OGC/1.3/CRS84> POINT (6.78333333 37.13333333)"^^<http://www.opengis.net/ont/geosparql#wktLiteral> .
<http://marineregions.org/mrgid/3959> <http://www.w3.org/ns/prov#hadPrimarySource> <http://www.ngdc.noaa.gov/gazetteer/> .
<urn:acuf:ufi:45863> <https://schema.org/identifier> _:b5_n3-69958 .
<urn:acuf:ufi:45863> <https://schema.org/url> <https://geonames.nga.mil/arcgis/rest/services/Research/GeoNames/MapServer/0/query?where=UNIQUE_FEATURE_IDENTIFIER_UFI%3D45863> .
_:b5_n3-69958 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://schema.org/PropertyValue> .
_:b5_n3-69958 <https://schema.org/propertyID> <http://www.wikidata.org/entity/P2326> .
_:b5_n3-69958 <https://schema.org/value> "45863" .
<> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://w3id.org/sds#Bucket> <https://w3id.org/sds#DataDescription> .
<> <https://w3id.org/sds#immutable> "false" <https://w3id.org/sds#DataDescription> .
<> <https://w3id.org/sds#isRoot> "true" <https://w3id.org/sds#DataDescription> .
<> <https://w3id.org/sds#stream> <http://example.org/ns#BenchmarkStream> <https://w3id.org/sds#DataDescription> .
92 changes: 92 additions & 0 deletions test/regressions/maregraph/maregraph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { describe, expect, test } from "vitest";
import { readFile } from "fs/promises";
import type * as RDF from "@rdfjs/types";
import { RDF as RDFT, RelationType, SDS } from "@treecg/types";
import { Parser } from "n3";

import { extractShapes, match, subject } from "../../../src/index";

export type Record = {
stream: string;
payload: string;
buckets: string[];
dataless?: boolean;
};

export type Bucket = {
id: string;
streamId: string;
immutable?: boolean;
root?: boolean;
empty?: boolean;
};

export type RdfThing = {
id: RDF.Term;
quads: RDF.Quad[];
};

export type Relation = {
type: RelationType;
stream: string;
origin: string;
bucket: string;
value?: RdfThing;
path?: RdfThing;
};

async function setupLenses() {
const shape_str = await readFile(__dirname + "/shape.ttl", {
encoding: "utf8",
});
const Shapes = extractShapes(new Parser().parse(shape_str));
console.log(Shapes);
const RecordLens = match(undefined, SDS.terms.payload, undefined)
.thenAll(subject)
.thenSome(Shapes.lenses["Record"]);

const BucketLens = match(
undefined,
RDFT.terms.type,
SDS.terms.custom("Bucket"),
)
.thenAll(subject)
.thenSome(Shapes.lenses["Bucket"]);

const RelationLens = match(
undefined,
RDFT.terms.type,
SDS.terms.custom("Relation"),
)
.thenAll(subject)
.thenSome(Shapes.lenses["Relation"]);

return { RecordLens, BucketLens, RelationLens };
}

async function getData() {
const data_str = await readFile(__dirname + "/data.ttl", {
encoding: "utf8",
});
const quads = new Parser().parse(data_str);

return quads.filter((q) =>
q.graph.equals(SDS.terms.custom("DataDescription")),
);
}

describe("test empty ids get extracted", async () => {
const lenses = await setupLenses();
const data = await getData();

test("Records get extracted", () => {
const records = <Record[]>lenses.RecordLens.execute(data);

expect(records.length).toBe(1);
expect(records[0].buckets.length).toBe(1);
});
test("Records get extracted", () => {
const buckets = <Bucket[]>lenses.BucketLens.execute(data);
expect(buckets.length).toBe(1);
});
});
119 changes: 119 additions & 0 deletions test/regressions/maregraph/shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
@prefix cidoc: <http://www.cidoc-crm.org/cidoc-crm/>.
@prefix rdfl: <https://w3id.org/rdf-lens/ontology#>.
@prefix sosa: <http://www.w3.org/ns/sosa/>.
@prefix dcterms: <http://purl.org/dc/terms/>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix sh: <http://www.w3.org/ns/shacl#>.
@prefix sds: <https://w3id.org/sds#>.

[ ] a sh:NodeShape;
sh:targetClass <Record>;
sh:property [
sh:name "stream";
sh:path sds:stream;
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "payload";
sh:path sds:payload;
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "buckets";
sh:path sds:bucket;
sh:datatype xsd:string;
], [
sh:name "dataless";
sh:path sds:dataless;
sh:datatype xsd:boolean;
sh:minCount 0;
sh:maxCount 1;
].

[ ] a sh:NodeShape;
sh:targetClass <Bucket>;
sh:property [
sh:name "id";
sh:path ( );
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "streamId";
sh:path sds:stream;
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "immutable";
sh:path sds:immutable;
sh:datatype xsd:boolean;
sh:maxCount 1;
], [
sh:name "root";
sh:path sds:isRoot;
sh:datatype xsd:boolean;
sh:maxCount 1;
], [
sh:name "empty";
sh:path sds:empty;
sh:datatype xsd:boolean;
sh:maxCount 1;
].

[ ] a sh:NodeShape;
sh:targetClass <Relation>;
sh:property [
sh:name "type";
sh:path sds:relationType;
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "stream";
sh:path ( [ sh:inversePath sds:relation ] sds:stream );
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "origin";
sh:path [ sh:inversePath sds:relation ];
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "bucket";
sh:path sds:relationBucket;
sh:datatype xsd:string;
sh:minCount 1;
sh:maxCount 1;
], [
sh:name "path";
sh:path sds:relationPath;
sh:class <RdfThing>;
sh:maxCount 1;
], [
sh:name "value";
sh:path sds:relationValue;
sh:class <RdfThing>;
sh:maxCount 1;
].

[ ] a sh:NodeShape;
sh:targetClass <RdfThing>;
sh:property [
sh:name "id";
sh:path ( );
sh:maxCount 1;
sh:minCount 1;
sh:datatype xsd:any;
], [
sh:name "quads";
sh:path ( );
sh:maxCount 1;
sh:minCount 1;
sh:class rdfl:CBD;
].

82 changes: 82 additions & 0 deletions test/regressions/maregraph/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { readFile } from "fs/promises";
import type * as RDF from "@rdfjs/types";
import { RDF as RDFT, RelationType, SDS } from "@treecg/types";
import { Parser } from "n3";

import { extractShapes, match, subject } from "../../../src/index";

export type Record = {
stream: string;
payload: string;
buckets: string[];
dataless?: boolean;
};

export type Bucket = {
id: string;
streamId: string;
immutable?: boolean;
root?: boolean;
empty?: boolean;
};

export type RdfThing = {
id: RDF.Term;
quads: RDF.Quad[];
};

export type Relation = {
type: RelationType;
stream: string;
origin: string;
bucket: string;
value?: RdfThing;
path?: RdfThing;
};

async function setupLenses() {
const shape_str = await readFile("/shape.ttl", { encoding: "utf8" });
const Shapes = extractShapes(new Parser().parse(shape_str));
const RecordLens = match(undefined, SDS.terms.payload, undefined)
.thenAll(subject)
.thenSome(Shapes.lenses["Record"]);

const BucketLens = match(
undefined,
RDFT.terms.type,
SDS.terms.custom("Bucket"),
)
.thenAll(subject)
.thenSome(Shapes.lenses["Bucket"]);

const RelationLens = match(
undefined,
RDFT.terms.type,
SDS.terms.custom("Relation"),
)
.thenAll(subject)
.thenSome(Shapes.lenses["Relation"]);

return { RecordLens, BucketLens, RelationLens };
}

async function getData() {
const data_str = await readFile("/data.ttl", { encoding: "utf8" });
const quads = new Parser().parse(data_str);

return quads.filter((q) =>
q.graph.equals(SDS.terms.custom("DataDescription")),
);
}

describe("test empty ids get extracted", async () => {
const lenses = await setupLenses();
const data = await getData();

test("Records get extracted", () => {
const records = <Record[]>lenses.RecordLens.execute(data);
console.log(records);
expect(2).toBe(4);
});
const buckets = <Bucket[]>lenses.BucketLens.execute(data);
});

0 comments on commit 0a5891a

Please sign in to comment.