From 190ee966740eba9d06176ba44129c334187dc117 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 29 Nov 2022 19:07:24 +0100 Subject: [PATCH 1/6] fix: problem with assumption that "quoted triples" are quads --- data-model.d.ts | 13 ++++++++----- rdf-js-tests.ts | 14 +++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/data-model.d.ts b/data-model.d.ts index 6fff406..6e46fa0 100644 --- a/data-model.d.ts +++ b/data-model.d.ts @@ -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 | Triple; /** * Contains an IRI. @@ -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. @@ -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. @@ -233,6 +233,9 @@ export interface Quad extends BaseQuad { equals(other: Term | null | undefined): boolean; } +export type BaseTriple = Omit +export type Triple = Omit + /** * A factory for instantiating RDF terms and quads. */ diff --git a/rdf-js-tests.ts b/rdf-js-tests.ts index af12b23..43e173d 100644 --- a/rdf-js-tests.ts +++ b/rdf-js-tests.ts @@ -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() { @@ -114,10 +114,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 = {}; + // @ts-expect-error + dataset.add(quadBobAge2); } } @@ -138,10 +142,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 = {}; + // @ts-expect-error + dataset.add(quadBobAge2); } } From e0854d6c5021443b40168e1037b79ab85f0f5961 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 29 Nov 2022 19:32:21 +0100 Subject: [PATCH 2/6] test: add explanation --- rdf-js-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rdf-js-tests.ts b/rdf-js-tests.ts index 43e173d..a5b06e3 100644 --- a/rdf-js-tests.ts +++ b/rdf-js-tests.ts @@ -120,7 +120,7 @@ function test_datafactory_star() { const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); const dataset: DatasetCore = {}; - // @ts-expect-error + // @ts-expect-error: triple is not assignable to quad. It has no graph property dataset.add(quadBobAge2); } } @@ -148,7 +148,7 @@ function test_datafactory_star_basequad() { const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); const dataset: DatasetCore = {}; - // @ts-expect-error + // @ts-expect-error: triple is not assignable to quad. It has no graph property dataset.add(quadBobAge2); } } From 2f435a483adc4413571c718b898cc24e83782af9 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 29 Nov 2022 19:36:58 +0100 Subject: [PATCH 3/6] refactor: use BaseTriple to align with BaseQuad --- data-model.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-model.d.ts b/data-model.d.ts index 6e46fa0..12f0279 100644 --- a/data-model.d.ts +++ b/data-model.d.ts @@ -10,7 +10,7 @@ * @see DefaultGraph * @see BaseQuad */ -export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph | BaseQuad | Triple; +export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph | BaseQuad | BaseTriple; /** * Contains an IRI. From 03a83f61c411c29e8cd96b0462b829e5cf6596ad Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 29 Nov 2022 20:00:44 +0100 Subject: [PATCH 4/6] test: switch on `termType` --- rdf-js-tests.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rdf-js-tests.ts b/rdf-js-tests.ts index a5b06e3..bbf7fd9 100644 --- a/rdf-js-tests.ts +++ b/rdf-js-tests.ts @@ -54,6 +54,28 @@ function test_terms() { let defaultGraphEqual: boolean = defaultGraph.equals(someTerm); defaultGraphEqual = defaultGraph.equals(null); defaultGraphEqual = defaultGraph.equals(undefined); + + const term: Term = {}; + switch (term.termType) { + case 'Quad': + const itIsQuad: BaseQuad = term; + 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() { From 0c9df08ab2c9cb2fe6b9dcd093478828b85a69c4 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Tue, 29 Nov 2022 20:11:58 +0100 Subject: [PATCH 5/6] attempted fix: new termType --- data-model.d.ts | 10 +++++++--- rdf-js-tests.ts | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/data-model.d.ts b/data-model.d.ts index 12f0279..c2ac4a3 100644 --- a/data-model.d.ts +++ b/data-model.d.ts @@ -233,8 +233,12 @@ export interface Quad extends BaseQuad { equals(other: Term | null | undefined): boolean; } -export type BaseTriple = Omit -export type Triple = Omit +export interface BaseTriple extends Omit { + termType: 'QuotedTriple'; +} +export interface Triple extends Omit { + termType: 'QuotedTriple'; +} /** * A factory for instantiating RDF terms and quads. @@ -289,5 +293,5 @@ export interface DataFactory Date: Tue, 29 Nov 2022 20:16:47 +0100 Subject: [PATCH 6/6] style: not declarations in switch --- rdf-js-tests.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rdf-js-tests.ts b/rdf-js-tests.ts index 057230e..6a5c0e0 100644 --- a/rdf-js-tests.ts +++ b/rdf-js-tests.ts @@ -56,27 +56,28 @@ function test_terms() { defaultGraphEqual = defaultGraph.equals(undefined); const term: Term = {}; + let term2: Term | undefined switch (term.termType) { case 'Quad': - const itIsQuad: BaseQuad = term; + term2 = term; break; case 'QuotedTriple': - const itIsTriple: BaseTriple = term; + term2 = term; break; case 'NamedNode': - const itIsNamedNode: NamedNode = term; + term2 = term; break; case 'Literal': - const itIsLiteral: Literal = term; + term2 = term; break; case 'BlankNode': - const itIsBlankNode: BlankNode = term; + term2 = term; break; case 'Variable': - const itIsVariable: Variable = term; + term2 = term; break; case 'DefaultGraph': - const itIsDefaultGraph: DefaultGraph = term; + term2 = term; break; } }