diff --git a/types/n3/index.d.ts b/types/n3/index.d.ts index cfb6149111..1b9fb95db0 100644 --- a/types/n3/index.d.ts +++ b/types/n3/index.d.ts @@ -21,10 +21,10 @@ export interface Prefixes { export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph; export type PrefixedToIri = (suffix: string) => NamedNode; -export class NamedNode implements RDF.NamedNode { +export class NamedNode implements RDF.NamedNode { readonly termType: "NamedNode"; - readonly value: string; - constructor(iri: string); + readonly value: Iri; + constructor(iri: Iri); readonly id: string; toJSON(): {}; equals(other: RDF.Term): boolean; @@ -83,6 +83,8 @@ export type Quad_Graph = DefaultGraph | NamedNode | BlankNode | Variable; export class BaseQuad implements RDF.BaseQuad { constructor(subject: Term, predicate: Term, object: Term, graph?: Term); + termType: 'Quad'; + value: ''; subject: Term; predicate: Term; object: Term; @@ -104,7 +106,7 @@ export class Quad extends BaseQuad implements RDF.Quad { export class Triple extends Quad implements RDF.Quad {} export namespace DataFactory { - function namedNode(value: string): NamedNode; + function namedNode(value: Iri): NamedNode; function blankNode(value?: string): BlankNode; function literal(value: string | number, languageOrDatatype?: string | RDF.NamedNode): Literal; function variable(value: string): Variable; diff --git a/types/rdf-ext/lib/DataFactory.d.ts b/types/rdf-ext/lib/DataFactory.d.ts index e9e28cade9..3a3f54c02f 100644 --- a/types/rdf-ext/lib/DataFactory.d.ts +++ b/types/rdf-ext/lib/DataFactory.d.ts @@ -27,7 +27,7 @@ declare class DataFactoryExt { PrefixMap: typeof PrefixMap; }; static factory: typeof DataFactoryExt; - static namedNode(value: string): NamedNodeExt; + static namedNode(value: Iri): NamedNodeExt; static blankNode(value?: string): BlankNodeExt; static literal(value: string, languageOrDatatype?: string | NamedNode): LiteralExt; static variable(value: string): VariableExt; diff --git a/types/rdf-ext/lib/NamedNode.d.ts b/types/rdf-ext/lib/NamedNode.d.ts index f3bfd8c12c..61aa48516a 100644 --- a/types/rdf-ext/lib/NamedNode.d.ts +++ b/types/rdf-ext/lib/NamedNode.d.ts @@ -1,7 +1,7 @@ import { NamedNode, Term } from "rdf-js"; import { PropType } from './_PropType'; -interface NamedNodeExt extends NamedNode { +interface NamedNodeExt extends NamedNode { toCanonical(): string; toJSON(): { value: PropType; diff --git a/types/rdf-ext/lib/Quad.d.ts b/types/rdf-ext/lib/Quad.d.ts index 5319066a59..37c8de44c5 100644 --- a/types/rdf-ext/lib/Quad.d.ts +++ b/types/rdf-ext/lib/Quad.d.ts @@ -7,6 +7,8 @@ import VariableExt = require('./Variable'); import DefaultGraphExt = require('./DefaultGraph'); interface QuadExt extends Quad { + termType: 'Quad'; + value: ''; subject: NamedNodeExt | BlankNodeExt | VariableExt; predicate: NamedNodeExt | VariableExt; object: NamedNodeExt | LiteralExt | BlankNodeExt | VariableExt; diff --git a/types/rdf-js/index.d.ts b/types/rdf-js/index.d.ts index f5e027f35b..e20b9ae4f4 100644 --- a/types/rdf-js/index.d.ts +++ b/types/rdf-js/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for the RDFJS specification 3.0 +// Type definitions for the RDFJS specification 4.0 // Project: https://github.com/rdfjs/representation-task-force // Definitions by: Ruben Taelman // Laurens Rietveld @@ -15,14 +15,15 @@ import { EventEmitter } from "events"; /* https://rdf.js.org/data-model-spec/ */ /** - * Contains an Iri, RDF blank Node, RDF literal, variable name, or a default graph + * Contains an Iri, RDF blank Node, RDF literal, variable name, default graph, or a quad * @see NamedNode * @see BlankNode * @see Literal * @see Variable * @see DefaultGraph + * @see Quad */ -export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph; +export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph | BaseQuad; /** * Contains an IRI. @@ -145,7 +146,7 @@ export interface DefaultGraph { * @see BlankNode * @see Variable */ -export type Quad_Subject = NamedNode | BlankNode | Variable; +export type Quad_Subject = NamedNode | BlankNode | Quad | Variable; /** * The predicate, which is a NamedNode or Variable. @@ -161,7 +162,7 @@ export type Quad_Predicate = NamedNode | Variable; * @see BlankNode * @see Variable */ -export type Quad_Object = NamedNode | Literal | BlankNode | Variable; +export type Quad_Object = NamedNode | Literal | BlankNode | Quad | Variable; /** * The named graph, which is a DefaultGraph, NamedNode, BlankNode or Variable. @@ -176,6 +177,15 @@ export type Quad_Graph = DefaultGraph | NamedNode | BlankNode | Variable; * An RDF quad, taking any Term in its positions, containing the subject, predicate, object and graph terms. */ export interface BaseQuad { + /** + * Contains the constant "Quad". + */ + termType: "Quad"; + /** + * Contains an empty string as constant value. + */ + value: ""; + /** * The subject. * @see Quad_Subject @@ -201,7 +211,7 @@ export interface BaseQuad { * @param other The term to compare with. * @return True if and only if the argument is a) of the same type b) has all components equal. */ - equals(other: BaseQuad | null | undefined): boolean; + equals(other: Term | null | undefined): boolean; } /** @@ -233,7 +243,7 @@ export interface Quad extends BaseQuad { * @param other The term to compare with. * @return True if and only if the argument is a) of the same type b) has all components equal. */ - equals(other: BaseQuad | null | undefined): boolean; + equals(other: Term | null | undefined): boolean; } /** @@ -245,10 +255,7 @@ export interface DataFactory(value: Iri): NamedNode; /** * @param value The optional blank node identifier. diff --git a/types/rdf-js/rdf-js-tests.ts b/types/rdf-js/rdf-js-tests.ts index eae4829551..738b9555b0 100644 --- a/types/rdf-js/rdf-js-tests.ts +++ b/types/rdf-js/rdf-js-tests.ts @@ -69,6 +69,11 @@ function test_datafactory() { const dataFactory: DataFactory = {}; const namedNode: NamedNode = dataFactory.namedNode('http://example.org'); + const constantValue: 'http://example.org' = dataFactory.namedNode('http://example.org').value; + // $ExpectError + const otherConstantValue: 'http://not-example.org' = dataFactory.namedNode('http://example.org').value; + // $ExpectError + const otherConstantNamedNode: NamedNode<'http://not-example.org'> = dataFactory.namedNode('http://example.org'); const blankNode1: BlankNode = dataFactory.blankNode('b1'); const blankNode2: BlankNode = dataFactory.blankNode(); @@ -92,6 +97,54 @@ function test_datafactory() { const hasBnode = quad.predicate.termType === "BlankNode"; } +function test_datafactory_star() { + const dataFactory: DataFactory = {}; + + // Compose the triple "<> ex:certainty 0.9." + const quadBobAge: Quad = dataFactory.quad( + dataFactory.namedNode('ex:bob'), + dataFactory.namedNode('ex:age'), + dataFactory.literal('23'), + ); + const quadBobAgeCertainty: Quad = dataFactory.quad( + quadBobAge, + dataFactory.namedNode('ex:certainty'), + dataFactory.literal('0.9'), + ); + + // Decompose the triple + if (quadBobAgeCertainty.subject.termType === 'Quad') { + const quadBobAge2: Quad = quadBobAgeCertainty.subject; + + const equalToSelf: boolean = quadBobAge2.equals(quadBobAge); + const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); + } +} + +function test_datafactory_star_basequad() { + const dataFactory: DataFactory = {}; + + // Compose the triple "<> ex:certainty 0.9." + const quadBobAge: BaseQuad = dataFactory.quad( + dataFactory.namedNode('ex:bob'), + dataFactory.namedNode('ex:age'), + dataFactory.literal('23'), + ); + const quadBobAgeCertainty: BaseQuad = dataFactory.quad( + quadBobAge, + dataFactory.namedNode('ex:certainty'), + dataFactory.literal('0.9'), + ); + + // Decompose the triple + if (quadBobAgeCertainty.subject.termType === 'Quad') { + const quadBobAge2: BaseQuad = quadBobAgeCertainty.subject; + + const equalToSelf: boolean = quadBobAge2.equals(quadBobAge); + const notEqualToOtherType: boolean = quadBobAge2.equals(dataFactory.namedNode('ex:something:else')); + } +} + function test_stream() { const stream: Stream = {}; const quad: Quad | null = stream.read();