🤖 Merge PR #46847 Add RDF* support to rdf-js by @rubensworks

* Add RDF* support to rdf-js

* Apply rdf-js breaking change for typed NamedNodes in factory

Originally part of 16d29e86cd6fe34e6ac6f53bba6ba1a1988d7401
by @Vinnl

* Update types/rdf-js/index.d.ts

Co-authored-by: Tomasz Pluskiewicz <tpluscode@users.noreply.github.com>

* Update types/rdf-js/index.d.ts

Co-authored-by: Tomasz Pluskiewicz <tpluscode@users.noreply.github.com>

* Update types/rdf-js/index.d.ts

Co-authored-by: Tomasz Pluskiewicz <tpluscode@users.noreply.github.com>

* Fix breaking typings to to rdf-js 4.0

* Fix incorrect n3 constructor type

Co-authored-by: Tomasz Pluskiewicz <tpluscode@users.noreply.github.com>
This commit is contained in:
Ruben Taelman 2020-08-27 08:14:30 +02:00 committed by GitHub
parent 03f3cf9673
commit 9c7b900779
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 17 deletions

10
types/n3/index.d.ts vendored
View File

@ -21,10 +21,10 @@ export interface Prefixes<I = RDF.NamedNode> {
export type Term = NamedNode | BlankNode | Literal | Variable | DefaultGraph;
export type PrefixedToIri = (suffix: string) => NamedNode;
export class NamedNode implements RDF.NamedNode {
export class NamedNode<Iri extends string = string> implements RDF.NamedNode<Iri> {
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<Iri extends string = string>(value: Iri): NamedNode<Iri>;
function blankNode(value?: string): BlankNode;
function literal(value: string | number, languageOrDatatype?: string | RDF.NamedNode): Literal;
function variable(value: string): Variable;

View File

@ -27,7 +27,7 @@ declare class DataFactoryExt {
PrefixMap: typeof PrefixMap;
};
static factory: typeof DataFactoryExt;
static namedNode(value: string): NamedNodeExt;
static namedNode<Iri extends string = string>(value: Iri): NamedNodeExt<Iri>;
static blankNode(value?: string): BlankNodeExt;
static literal(value: string, languageOrDatatype?: string | NamedNode): LiteralExt;
static variable(value: string): VariableExt;

View File

@ -1,7 +1,7 @@
import { NamedNode, Term } from "rdf-js";
import { PropType } from './_PropType';
interface NamedNodeExt extends NamedNode {
interface NamedNodeExt<Iri extends string = string> extends NamedNode<Iri> {
toCanonical(): string;
toJSON(): {
value: PropType<NamedNode, 'value'>;

View File

@ -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;

View File

@ -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 <https://github.com/rubensworks>
// Laurens Rietveld <https://github.com/LaurensRietveld>
@ -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<OutQuad extends BaseQuad = Quad, InQuad extends Bas
* @return A new instance of NamedNode.
* @see NamedNode
*/
// TODO: This could be changed into a Generic method that returns a NamedNode constained to the
// given `value` - but note that that would be a breaking change. See commit
// 16d29e86cd6fe34e6ac6f53bba6ba1a1988d7401.
namedNode(value: string): NamedNode;
namedNode<Iri extends string = string>(value: Iri): NamedNode<Iri>;
/**
* @param value The optional blank node identifier.

View File

@ -69,6 +69,11 @@ function test_datafactory() {
const dataFactory: DataFactory = <any> {};
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 = <any> {};
// Compose the triple "<<ex:bob ex:age 23>> 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<BaseQuad> = <any> {};
// Compose the triple "<<ex:bob ex:age 23>> 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 = <any> {};
const quad: Quad | null = stream.read();