-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloadGraphFromSparql.ts
240 lines (225 loc) · 9.03 KB
/
loadGraphFromSparql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**Loads the graph from the SNIK SPARQL endpoint. No layouting. May use caching.*/
import * as sparql from "./sparql";
import { timer } from "./timer";
import { config } from "./config/config";
import log from "loglevel";
import type { ElementDefinition, Core } from "cytoscape";
interface ClassBinding {
src: { value: string };
c: { value: string };
l: { value: string };
st: { value: string };
inst: { value: boolean };
}
/**
* Query for classes from the endpoint
* @param from - SPARQL from clause
* @returns SPARQL JSON result
*/
async function selectClasses(from: string): Promise<Array<ClassBinding>> {
const sparqlClassesTimer = timer("sparql-classes");
const nodeQuerySimple = `
PREFIX ov: <http://open.vocab.org/terms/>
SELECT ?c
GROUP_CONCAT(distinct(CONCAT(?l,"@",lang(?l)));separator="|") as ?l
?src
${from}
{
?c a ?type.
OPTIONAL {?c rdfs:label ?l.}
OPTIONAL {?src ov:defines ?c.}
}
`;
// eslint-disable-next-line ban-ts-comment Needed to easily swap different config files, ts-expect-error not suitable when defined
// @ts-ignore Needed to easily swap different config files, we handle it not existing here
const nodeQuery = config.ontology.sparql?.queries?.nodes ? config.ontology.sparql.queries.nodes(from) : nodeQuerySimple;
const bindings = (await sparql.select(nodeQuery)) as Array<ClassBinding>;
sparqlClassesTimer.stop(bindings.length + " classes using " + (config.ontology.name ? `${config.ontology.name} query` : "default query"));
return bindings;
}
/** Parse "|"-separated labels with language tag into the SNIK graph label structure.
@param s - a string containing "|"-separated parts
@returns an object containing different language labels keyed by language tag */
function parseLabels(s: string): object {
const labels = s.split("|");
const l = {};
for (const label of labels) {
const [lex, tag] = label.split("@");
if (!lex.trim()) {
continue;
}
{
if (!l[tag]) {
l[tag] = [];
}
}
l[tag].push(lex);
}
return l;
}
/**
* Creates cytoscape nodes for the classes
* @param from - a SPARQL FROM clause defining where to load the classes from
* @returns nodes - representing the classes
*/
async function createClassNodes(from: string): Promise<Array<ElementDefinition>> {
const bindings = await selectClasses(from);
const nodes: Array<ElementDefinition> = [];
const sources = new Set<string>();
for (let i = 0; i < bindings.length; i++) {
// The source value if it exists does not come from the SPARQL graph the node comes from but instead from a class that ov:defines it.
let source;
if (bindings[i].src) {
source = bindings[i].src.value;
if (source.includes("http://www.snik.eu/ontology/")) {
source = source.replace("http://www.snik.eu/ontology/", "");
} // abbreviate snik
sources.add(source);
}
nodes.push({
group: "nodes",
data: {
id: bindings[i].c.value,
l: parseLabels(bindings[i].l.value),
...(bindings[i].st && { st: bindings[i].st.value.replace("http://www.snik.eu/ontology/meta/", "").substring(0, 1) }),
...(source && { source: source }),
...(bindings[i].inst && { inst: true }), // has at least one instance
},
});
}
const colors = ["rgb(30,152,255)", "rgb(255,173,30)", "rgb(80,255,250)", "rgb(150,255,120)", "rgb(204, 0, 204)", "rgb(255, 255, 0)"];
let count = 0;
for (const source of sources) {
if (!config.ontology.style.colorMap.has(source) && !config.ontology.style.color(source)) {
config.ontology.style.colorMap.set(source, colors[count]);
count = (count + 1) % colors.length;
}
}
log.debug(bindings.length + " Nodes loaded from SPARQL");
return nodes;
}
/** Query for instances from the endpoint
* @param from - a SPARQL FROM clause defining where to load the instances from
@returns SPARQL JSON result */
async function selectInstances(from: string): Promise<Array<object>> {
const sparqlInstancesTimer = timer("sparql-classes");
const instanceQuery = `SELECT
DISTINCT(?i)
GROUP_CONCAT(DISTINCT(CONCAT(?l,"@",lang(?l)));separator="|") AS ?l
${from}
{
?i a [a owl:Class].
OPTIONAL {?i rdfs:label ?l.}
}
`;
const json = await sparql.select(instanceQuery);
sparqlInstancesTimer.stop(json.length + " instances");
return json;
}
/** Create cytoscape nodes for the instances.
* @param from - a SPARQL FROM clause defining where to load the instances from
@returns cytoscape nodes for the instances */
async function createInstanceNodes(from: string): Promise<Array<object>> {
const json = await selectInstances(from);
const nodes: Array<ElementDefinition> = [];
for (let i = 0; i < json.length; i++) {
nodes.push({
group: "nodes",
data: {
id: json[i]["i"].value,
l: parseLabels(json[i]["l"].value),
instance: true,
},
});
}
return nodes;
}
/**
* Query for triples between resources in the SPARQL endpoint
* @param from - SPARQL from clause
* @param fromNamed - SPARQL from named clause
* @param instances - whether to load instances as well
* @param virtual - whether to select virtual triples from domain and range statements
* @returns SPARQL query result object
*/
async function selectTriples(from: string, fromNamed: string, instances: boolean, virtual: boolean): Promise<Array<object>> {
const sparqlPropertiesTimer = timer("sparql-properties");
const tripleQuerySimple = `
select ?c ?p ?d
${from}
{
{?c ?p ?d.} ${virtual ? " UNION {?p rdfs:domain ?c; rdfs:range ?d.}" : ""}
{?c a owl:Class.} ${instances ? " UNION {?c a [a owl:Class]}" : ""}
{?d a owl:Class.} ${instances ? " UNION {?d a [a owl:Class}" : ""}
}`;
// the optional part should be a union
// eslint-disable-next-line ban-ts-comment Needed to easily swap different config files, ts-expect-error not suitable when defined
// @ts-ignore Needed to easily swap different config files, we handle it not existing here
const tripleQuery = config.ontology.sparql?.queries?.triples
? config.ontology.sparql.queries.triples(from, fromNamed, virtual, instances)
: tripleQuerySimple;
const json = await sparql.select(tripleQuery);
sparqlPropertiesTimer.stop(json.length + " properties using " + (config.ontology.name ? `${config.ontology.name} query` : "default query"));
return json;
}
/** Creates cytoscape edges for the resources in the SPARQL endpoint
* @param from - SPARQL from clause
* @param fromNamed - SPARQL from named clause
* @param instances - whether to load instances as well
* @param virtual - whether to select virtual triples from domain and range statements
* @returns SPARQL query result object
*/
async function createEdges(from: string, fromNamed: string, instances: boolean, virtual: boolean): Promise<Array<ElementDefinition>> {
const json = await selectTriples(from, fromNamed, instances, virtual);
const edges: Array<ElementDefinition> = [];
for (let i = 0; i < json.length; i++) {
edges.push({
group: "edges",
data: {
source: json[i]["c"].value,
target: json[i]["d"].value,
id: String(i),
p: json[i]["p"].value,
pl: json[i]["p"].value.replace(/.*[#/]/, ""),
...(json[i]["g"] && { g: json[i]["g"].value }), // don't add null/undefined values, see https://stackoverflow.com/a/40560953/398963
...(json[i]["ax"] && { ax: json[i]["ax"].value }), // in case of virtual triples: the URI of the axiom
},
//position: { x: 200, y: 200 }
});
}
log.debug(json.length + " Edges loaded from SPARQL");
return edges;
}
/**
* Create cytoscape nodes for classes and optionally also instances.
* @param from - a SPARQL from clause
* @param instances - whether to load instances in addition to the classes
* @returns an array of nodes
*/
async function createNodes(from: string, instances: boolean): Promise<Array<ElementDefinition>> {
if (!instances) {
return createClassNodes(from);
}
const [classNodes, instanceNodes] = await Promise.all([createClassNodes(from), createInstanceNodes(from)]);
// @ts-expect-error concat types
return classNodes.concat(instanceNodes);
}
/** Clears the given graph and loads a set of subontologies. Data from RDF helper graphs is loaded as well, such as virtual triples.
* @param cy - the cytoscape graph to clear and to load the data into
* @param graphs - subontologies to load.
* @param instances - whether to load instances as well
* @param virtual - whether to select virtual triples from domain and range statements
* @returns nothing
@example
loadGraphFromSparql(cy,new Set(["meta","bb"]))
*/
export async function loadGraphFromSparql(cy: Core, graphs: Array<string>, instances: boolean = false, virtual: boolean = false): Promise<void> {
log.debug(`Loading graph from endpoint ${config.ontology.sparql.endpoint} with graphs ${graphs.join(",")}.`);
const from = graphs.map((g) => `FROM <${g}>`).reduce((a, b) => a + "\n" + b, "");
const fromNamed = from.replace(/FROM/g, "FROM NAMED");
const [nodes, edges] = await Promise.all([createNodes(from, instances), createEdges(from, fromNamed, instances, virtual)]);
cy.elements().remove();
cy.add(nodes);
cy.add(edges); // will throw an error if any edge refers to a node not contained in the nodes loaded before
cy.elements().addClass("unfiltered");
}