Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: problem with assumption that "quoted triples" are quads #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions data-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @see DefaultGraph
* @see BaseQuad
*/
export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph | BaseQuad;
export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph | BaseQuad | BaseTriple;

/**
* Contains an IRI.
Expand Down Expand Up @@ -128,12 +128,12 @@ export interface DefaultGraph {
}

/**
* The subject, which is a NamedNode, BlankNode or Variable.
* The subject, which is a NamedNode, BlankNode, Variable, or a quoted triple.
* @see NamedNode
* @see BlankNode
* @see Variable
*/
export type Quad_Subject = NamedNode | BlankNode | Quad | Variable;
export type Quad_Subject = NamedNode | BlankNode | Triple | Variable;

/**
* The predicate, which is a NamedNode or Variable.
Expand All @@ -143,13 +143,13 @@ export type Quad_Subject = NamedNode | BlankNode | Quad | Variable;
export type Quad_Predicate = NamedNode | Variable;

/**
* The object, which is a NamedNode, Literal, BlankNode or Variable.
* The object, which is a NamedNode, Literal, BlankNode, Variable, or a quoted triple.
* @see NamedNode
* @see Literal
* @see BlankNode
* @see Variable
*/
export type Quad_Object = NamedNode | Literal | BlankNode | Quad | Variable;
export type Quad_Object = NamedNode | Literal | BlankNode | Triple | Variable;

/**
* The named graph, which is a DefaultGraph, NamedNode, BlankNode or Variable.
Expand Down Expand Up @@ -233,6 +233,9 @@ export interface Quad extends BaseQuad {
equals(other: Term | null | undefined): boolean;
}

export type BaseTriple = Omit<BaseQuad, 'graph'>
export type Triple = Omit<Quad, 'graph'>

/**
* A factory for instantiating RDF terms and quads.
*/
Expand Down
36 changes: 33 additions & 3 deletions rdf-js-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlankNode, DataFactory, Dataset, DatasetCore, DatasetCoreFactory, DatasetFactory, DefaultGraph, Literal,
NamedNode, Quad, BaseQuad, Sink, Source, Store, Stream, Term, Variable, Quad_Graph } from ".";
NamedNode, Quad, BaseQuad, Sink, Source, Store, Stream, Term, Variable, Quad_Graph, Triple, BaseTriple } from ".";
import { EventEmitter } from "events";

function test_terms() {
Expand Down Expand Up @@ -54,6 +54,28 @@ function test_terms() {
let defaultGraphEqual: boolean = defaultGraph.equals(someTerm);
defaultGraphEqual = defaultGraph.equals(null);
defaultGraphEqual = defaultGraph.equals(undefined);

const term: Term = <any> {};
switch (term.termType) {
case 'Quad':
const itIsQuad: BaseQuad = term;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, I realized that this is in fact a breaking change because we now have two term types "Quad"

I have not found a good way to address this so far

break;
case 'NamedNode':
const itIsNamedNode: NamedNode = term;
break;
case 'Literal':
const itIsLiteral: Literal = term;
break;
case 'BlankNode':
const itIsBlankNode: BlankNode = term;
break;
case 'Variable':
const itIsVariable: Variable = term;
break;
case 'DefaultGraph':
const itIsDefaultGraph: DefaultGraph = term;
break;
}
}

function test_quads() {
Expand Down Expand Up @@ -114,10 +136,14 @@ function test_datafactory_star() {

// Decompose the triple
if (quadBobAgeCertainty.subject.termType === 'Quad') {
const quadBobAge2: Quad = quadBobAgeCertainty.subject;
const quadBobAge2: Triple = quadBobAgeCertainty.subject;

const equalToSelf: boolean = quadBobAge2.equals(quadBobAge);
const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else'));

const dataset: DatasetCore = <any> {};
// @ts-expect-error: triple is not assignable to quad. It has no graph property
dataset.add(quadBobAge2);
}
}

Expand All @@ -138,10 +164,14 @@ function test_datafactory_star_basequad() {

// Decompose the triple
if (quadBobAgeCertainty.subject.termType === 'Quad') {
const quadBobAge2: BaseQuad = quadBobAgeCertainty.subject;
const quadBobAge2: BaseTriple = quadBobAgeCertainty.subject;

const equalToSelf: boolean = quadBobAge2.equals(quadBobAge);
const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else'));

const dataset: DatasetCore<BaseQuad> = <any> {};
// @ts-expect-error: triple is not assignable to quad. It has no graph property
dataset.add(quadBobAge2);
}
}

Expand Down