Commit 7497cf49 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Preparing the rdflib typings

parent 6d72dcbc4899
......@@ -19,19 +19,17 @@
******************************************************************************/
import { RawContent, TabData, fetchSource } from "./data";
import { RdfStore, RDF } from "rdflib";
import * as $rdf from "rdflib";
require("rdflib");
let _rdf = require("rdflib");
export const $rdf: RDF = _rdf;
export function loadRdfStore(tabData: TabData): Promise<RdfStore> {
export function loadRdfStore(tabData: TabData): Promise<$rdf.Formula> {
if (!tabData.command.isActive) {
return new Promise((resolve, reject) => {
reject("no data");
});
}
return new Promise(
(resolve: (store: RdfStore) => void, reject: (reason: any) => void) => {
(resolve: (store: $rdf.Formula) => void, reject: (reason: any) => void) => {
let p1 = fetchSource(tabData.mainSource);
p1.then((value: RawContent) => {
let store = $rdf.graph();
......@@ -40,7 +38,8 @@ export function loadRdfStore(tabData: TabData): Promise<RdfStore> {
value.content,
store,
tabData.mainSource.url,
tabData.mainSource.contentType
tabData.mainSource.contentType,
undefined
);
resolve(store);
} catch (err) {
......
......@@ -18,7 +18,7 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import { RdfStore, Triple, Term, NamedNode, BlankNode, Literal } from "rdflib";
import * as $rdf from "rdflib";
import { definition, application, implementation } from "@logilab/libview";
import { DEFAULT_DESCRIPTOR_TRIPLES } from "./view-defaults";
......@@ -31,13 +31,13 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
this.render = this.render.bind(this);
}
priorityFor(store: RdfStore, target: string): number {
priorityFor(store: $rdf.Formula, target: string): number {
return Number.MAX_SAFE_INTEGER;
}
render(
renderer: application.ViewRenderer,
store: RdfStore,
store: $rdf.Formula,
root: string,
target: string
): HTMLElement {
......@@ -76,7 +76,7 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
let tbody = document.createElement("tbody");
store.statements
.sort(
(a: Triple, b: Triple): number => {
(a: $rdf.Statement, b: $rdf.Statement): number => {
var r = a.subject.value.localeCompare(b.subject.value);
if (r != 0) return r;
r = a.predicate.value.localeCompare(b.predicate.value);
......@@ -84,7 +84,7 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
return a.object.value.localeCompare(b.object.value);
}
)
.map((triple: Triple) => {
.map((triple: $rdf.Statement) => {
tbody.appendChild(renderRdfTriple(renderer, triple));
});
table.appendChild(tbody);
......@@ -96,7 +96,7 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
function renderRdfTriple(
renderer: application.ViewRenderer,
triple: Triple
triple: $rdf.Statement
): HTMLElement {
let tr = document.createElement("tr");
tr.appendChild(renderRdfNode(renderer, triple.subject));
......@@ -107,7 +107,7 @@ function renderRdfTriple(
function renderRdfNode(
renderer: application.ViewRenderer,
node: Term
node: $rdf.Node
): HTMLElement {
let result = document.createElement("td");
if (node == undefined || node == null) {
......@@ -115,7 +115,7 @@ function renderRdfNode(
sub.appendChild(document.createTextNode("?"));
result.appendChild(sub);
} else if (node.termType == "NamedNode") {
let nodeIRI = node as NamedNode;
let nodeIRI = node as $rdf.NamedNode;
let sub = document.createElement("a");
sub.href = nodeIRI.uri;
sub.onclick = (event: Event) => {
......@@ -126,12 +126,12 @@ function renderRdfNode(
sub.appendChild(document.createTextNode(nodeIRI.uri));
result.appendChild(sub);
} else if (node.termType == "BlankNode") {
let nodeBlank = node as BlankNode;
let nodeBlank = node as $rdf.BlankNode;
let sub = document.createElement("span");
sub.appendChild(document.createTextNode("_:" + nodeBlank.id));
result.appendChild(sub);
} else if (node.termType == "Literal") {
let nodeLiteral = node as Literal;
let nodeLiteral = node as $rdf.Literal;
let sub = document.createElement("span");
sub.appendChild(document.createTextNode("_:" + nodeLiteral.value));
result.appendChild(sub);
......
......@@ -30,7 +30,7 @@ import {
import { loadRdfStore } from "../common/rdf";
import "chrome";
import { application, definition, implementation } from "@logilab/libview";
import { RdfStore } from "rdflib";
import * as $rdf from "rdflib";
require("../common/view-defaults-impl");
/**
......@@ -102,7 +102,7 @@ enum StateType {
class State {
stateType: StateType;
message: string;
content: RdfStore;
content: $rdf.Formula;
root: string;
}
......@@ -142,7 +142,7 @@ class MainViewer implements application.Application {
*/
public onContent(
renderer: application.ViewRenderer,
content: RdfStore,
content: $rdf.Formula,
root: string
): void {
this.renderer = renderer;
......
......@@ -18,9 +18,8 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import { RdfStore, RDF, Term, NamedNode } from "rdflib";
let _rdf = require("rdflib");
const $rdf: RDF = _rdf;
import * as $rdf from "rdflib";
require("rdflib");
/**
* Gets the value for the propert of a resource
......@@ -29,10 +28,10 @@ const $rdf: RDF = _rdf;
* @param property The property
*/
export function getValueOf(
store: RdfStore,
store: $rdf.Formula,
resource: string,
property: string
): Term {
): $rdf.Node {
return store.any(
$rdf.sym(resource),
$rdf.sym(property),
......@@ -48,10 +47,10 @@ export function getValueOf(
* @param property The property
*/
export function getValuesOf(
store: RdfStore,
store: $rdf.Formula,
resource: string,
property: string
): Term[] {
): $rdf.Node[] {
return store.each(
$rdf.sym(resource),
$rdf.sym(property),
......@@ -71,7 +70,7 @@ export class RdfEntity {
/**
* The first alias for this entity
*/
private aliases: NamedNode[];
private aliases: $rdf.NamedNode[];
/**
* All the uris for this node
*/
......@@ -86,7 +85,7 @@ export class RdfEntity {
* @param manager The parent manager
* @param first The first alias for this entity
*/
constructor(manager: RdfEntityStore, first: NamedNode) {
constructor(manager: RdfEntityStore, first: $rdf.NamedNode) {
this.manager = manager;
this.aliases = [first];
this.uris = [first.uri];
......@@ -97,7 +96,7 @@ export class RdfEntity {
* Adds an alias to this node
* @param node The new alias
*/
public addAlias(node: NamedNode): void {
public addAlias(node: $rdf.NamedNode): void {
this.aliases.push(node);
this.uris.push(node.uri);
}
......@@ -112,61 +111,61 @@ export class RdfEntity {
return r.length == 0 ? null : r[0];
}
public getEntityForN(property: NamedNode): RdfEntity {
public getEntityForN(property: $rdf.NamedNode): RdfEntity {
let r = this.getEntitiesForN(property);
return r.length == 0 ? null : r[0];
}
public getEntitiesForE(property: RdfEntity): RdfEntity[] {
return this.getValuesForE(property)
.filter((node: Term) => node.termType == "NamedNode")
.map((node: NamedNode) => this.manager.getFromNode(node));
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.getFromNode(node));
}
public getEntitiesForS(property: string): RdfEntity[] {
return this.getValuesForS(property)
.filter((node: Term) => node.termType == "NamedNode")
.map((node: NamedNode) => this.manager.getFromNode(node));
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.getFromNode(node));
}
public getEntitiesForN(property: NamedNode): RdfEntity[] {
public getEntitiesForN(property: $rdf.NamedNode): RdfEntity[] {
return this.getValuesForN(property)
.filter((node: Term) => node.termType == "NamedNode")
.map((node: NamedNode) => this.manager.getFromNode(node));
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.getFromNode(node));
}
public getValueForE(property: RdfEntity): Term {
public getValueForE(property: RdfEntity): $rdf.Node {
let r = this.getValuesForE(property);
return r.length == 0 ? null : r[0];
}
public getValueForS(property: string): Term {
public getValueForS(property: string): $rdf.Node {
let r = this.getValuesForS(property);
return r.length == 0 ? null : r[0];
}
public getValueForN(property: NamedNode): Term {
public getValueForN(property: $rdf.NamedNode): $rdf.Node {
let r = this.getValuesForN(property);
return r.length == 0 ? null : r[0];
}
public getValuesForE(property: RdfEntity): Term[] {
let results: Term[] = [];
property.aliases.map((alias: NamedNode) => {
public getValuesForE(property: RdfEntity): $rdf.Node[] {
let results: $rdf.Node[] = [];
property.aliases.map((alias: $rdf.NamedNode) => {
let values = this.getValuesForN(alias);
values.map((value: Term) => {
values.map((value: $rdf.Node) => {
if (results.indexOf(value) < 0) results.push(value);
});
});
return results;
}
public getValuesForS(property: string): Term[] {
public getValuesForS(property: string): $rdf.Node[] {
return this.getValuesForN($rdf.sym(property));
}
public getValuesForN(property: NamedNode): Term[] {
let results: Term[] = [];
public getValuesForN(property: $rdf.NamedNode): $rdf.Node[] {
let results: $rdf.Node[] = [];
for (var i = 0; i != this.aliases.length; i++) {
let values = this.manager.store.each(
this.aliases[i],
......@@ -174,7 +173,7 @@ export class RdfEntity {
undefined,
undefined
);
values.map((value: Term) => {
values.map((value: $rdf.Node) => {
if (results.indexOf(value) < 0) results.push(value);
});
}
......@@ -189,7 +188,7 @@ export class RdfEntityStore {
/**
* The underlying RDF store
*/
public readonly store: RdfStore;
public readonly store: $rdf.Formula;
/**
* The known RDF entities
*/
......@@ -199,7 +198,7 @@ export class RdfEntityStore {
* Initializes this store
* @param store The underlying store
*/
constructor(store: RdfStore) {
constructor(store: $rdf.Formula) {
this.store = store;
this.entities = {};
}
......@@ -220,7 +219,7 @@ export class RdfEntityStore {
* Gets an entity
* @param node The named node
*/
public getFromNode(node: NamedNode): RdfEntity {
public getFromNode(node: $rdf.NamedNode): RdfEntity {
if (this.entities.hasOwnProperty(node.uri)) return this.entities[node.uri];
let result = new RdfEntity(this, node);
this.entities[node.uri] = result;
......@@ -235,14 +234,14 @@ export class RdfEntityStore {
private resolveAliases(entity: RdfEntity) {
let sameas = $rdf.sym("http://www.w3.org/2002/07/owl#sameAs");
let aliases: string[] = [];
let nodes: NamedNode[] = [];
let nodes: $rdf.NamedNode[] = [];
aliases.push(entity.uri);
nodes.push($rdf.sym(aliases[0]));
for (var i = 0; i != aliases.length; i++) {
let others = this.store.each(nodes[i], sameas, undefined, undefined);
others.map((other: Term) => {
others.map((other: $rdf.Node) => {
if (other.termType == "NamedNode") {
let named = other as NamedNode;
let named = other as $rdf.NamedNode;
if (aliases.indexOf(named.uri) < 0) {
aliases.push(named.uri);
nodes.push(named);
......
......@@ -22,8 +22,7 @@
"license": "LGPL-3.0",
"dependencies": {
"@types/node": "^10.5.3",
"@types/rdflib": "file:../rdflib-types",
"rdflib": "^0.17.0"
"@types/rdflib": "file:../rdflib-types"
},
"devDependencies": {
"copy-webpack-plugin": "^4.5.2",
......
......@@ -18,7 +18,7 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import { RdfStore } from "rdflib";
import * as $rdf from "rdflib";
/**
* Represents the rendering of a resource
......@@ -50,7 +50,7 @@ export interface ViewRenderer {
* @param root The root resource
* @param target The target resource for this view
*/
render(store: RdfStore, root: string, target: string): ViewRendering;
render(store: $rdf.Formula, root: string, target: string): ViewRendering;
}
/**
......@@ -68,7 +68,7 @@ export interface Application {
* @param content The content
* @param root The URI of the root resource
*/
onContent(renderer: ViewRenderer, content: RdfStore, root: string): void;
onContent(renderer: ViewRenderer, content: $rdf.Formula, root: string): void;
/**
* Sets the application on error
......
......@@ -18,7 +18,7 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import { RdfStore } from "rdflib";
import * as $rdf from "rdflib";
import { ViewRenderer, Application, ViewRendering } from "./application";
import {
ViewDescriptor,
......@@ -40,7 +40,7 @@ export interface ViewImplementation {
* @param store The RDF store holding the data to be rendered
* @param target The target resource to be rendered
*/
priorityFor(store: RdfStore, target: string): number;
priorityFor(store: $rdf.Formula, target: string): number;
/**
* Renders a resource belonging to an RDF store
* @param renderer The parent renderer
......@@ -50,7 +50,7 @@ export interface ViewImplementation {
*/
render(
renderer: ViewRenderer,
store: RdfStore,
store: $rdf.Formula,
root: string,
target: string
): HTMLElement;
......@@ -193,7 +193,7 @@ interface PrioritizedViewImplementation {
*/
function resolveViewFor(
implementations: ViewImplementations,
store: RdfStore,
store: $rdf.Formula,
target: string
): ViewImplementation {
let sorted: PrioritizedViewImplementation[] = Object.keys(implementations)
......@@ -267,7 +267,7 @@ class ViewRendererImpl implements ViewRenderer {
* @param root The root resource
* @param target The target resource for this view
*/
render(store: RdfStore, root: string, target: string = root): ViewRendering {
render(store: $rdf.Formula, root: string, target: string = root): ViewRendering {
var view = null;
if (this.forceView != null && this.forceView != "") {
// try to use the forced view
......
......@@ -1133,6 +1133,24 @@ declare namespace rdflib {
predicate: Node,
object: Node
): Statement;
/**
* Parse a string and put the result into the graph kb.
* Normal method is sync.
* Unfortunately jsdonld is currently written to need to be called async.
* Hence the mess below with executeCallback.
* @param str The input string to parse
* @param kb The store to use
* @param base The base URI to use
* @param contentType The content type for the input
* @param callback The callback to call when the data has been loaded
*/
export function parse(
str: string,
kb: Formula,
base: string,
contentType: string,
callback: (error: any, kb: Formula) => void
): void;
/**
* Get the next available unique identifier
*/
......@@ -1150,7 +1168,6 @@ declare namespace rdflib {
Fetcher: require('./fetcher'),
jsonParser: require('./jsonparser'),
N3Parser: require('./n3parser'),
parse: require('./parse'),
Query: require('./query').Query,
queryToSPARQL: require('./query-to-sparql'),
RDFaProcessor: require('./rdfaparser'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment