comparison src/graph_view.ts @ 28:c751380b70c5

uniqueness problem with NamedNodes in set
author drewp@bigasterisk.com
date Sun, 15 Dec 2019 12:38:11 -0800
parents e90d9021c6a0
children f551a089a98f
comparison
equal deleted inserted replaced
27:e0f5da648199 28:c751380b70c5
9 const rdf = { type: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" }; 9 const rdf = { type: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" };
10 10
11 type TypeToSubjs = Map<NamedNode, Set<NamedNode>>; 11 type TypeToSubjs = Map<NamedNode, Set<NamedNode>>;
12 function groupByRdfType(graph: N3Store): { byType: TypeToSubjs, untyped: Set<NamedNode> } { 12 function groupByRdfType(graph: N3Store): { byType: TypeToSubjs, untyped: Set<NamedNode> } {
13 const rdfType = namedNode(rdf.type); 13 const rdfType = namedNode(rdf.type);
14 const byType: TypeToSubjs = new Map(); // type : subjs 14 const byType: TypeToSubjs = new Map();
15 const untyped: Set<NamedNode> = new Set(); // subjs 15 const untyped: Set<NamedNode> = new Set(); // subjs
16 const internSubjs = new Map<string, NamedNode>();
16 graph.forEach((q) => { 17 graph.forEach((q) => {
18 if (!Util.isNamedNode(q.subject)) {
19 throw new Error("unsupported " + q.subject.value);
20 }
21 const subj = q.subject as NamedNode;
22
17 let subjType: NamedNode | null = null; 23 let subjType: NamedNode | null = null;
18 24
19 graph.forObjects((o: Quad) => { 25 graph.forObjects((o: Quad) => {
20 if (Util.isNamedNode(o.object)) { 26 if (Util.isNamedNode(o.object)) {
21 subjType = o.object as NamedNode; 27 subjType = o.object as NamedNode;
22 } 28 }
23 }, q.subject, rdfType, null); 29 }, subj, rdfType, null);
24 30
25 if (subjType !== null) { 31 if (subjType !== null) {
32 // (subj, rdf:type, subjType) in graph
26 if (!byType.has(subjType)) { 33 if (!byType.has(subjType)) {
27 byType.set(subjType, new Set()); 34 byType.set(subjType, new Set());
28 } 35 }
29 (byType.get(subjType) as Set<NamedNode>).add(q.subject as NamedNode); 36 (byType.get(subjType) as Set<NamedNode>).add(subj);
30 } else { 37 } else {
31 untyped.add(q.subject as NamedNode); 38 // no rdf:type stmt in graph
39 if (!internSubjs.has(subj.value)) {
40 internSubjs.set(subj.value, subj);
41 }
42 const intSubj: NamedNode = internSubjs.get(subj.value as string) as NamedNode;
43 untyped.add(intSubj);
32 } 44 }
33 }, null, null, null, null); 45 }, null, null, null, null);
34 return { byType: byType, untyped: untyped }; 46 return { byType: byType, untyped: untyped };
35 } 47 }
36 48
50 </span>`; 62 </span>`;
51 } 63 }
52 return html`<span class="literal">${n.value}${dtPart}</span>`; 64 return html`<span class="literal">${n.value}${dtPart}</span>`;
53 } 65 }
54 66
55 if (n.termType == "NamedNode") { 67 if (n.termType == "NamedNode") {
56 let dn: string | undefined = this.labels.getLabelForNode(n.value); 68 let shortened = false;
57 if (dn === undefined) { 69 let uriValue: string = n.value;
58 throw new Error(`dn=${dn}`); 70 for (let [long, short] of [
59 } 71 ["http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:"],
60 if (dn!.match(/XMLSchema#.*/)) { 72 ["http://www.w3.org/2000/01/rdf-schema#", "rdfs:"],
61 dn = dn!.replace('XMLSchema#', 'xsd:'); 73 ["http://purl.org/dc/elements/1.1/", "dc:"],
62 } 74 ["http://www.w3.org/2001/XMLSchema#", "xsd:"]]) {
63 if (dn!.match(/rdf-schema#.*/)) { 75 if (uriValue?.startsWith(long)) {
64 dn = dn!.replace('rdf-schema#', 'rdfs:'); 76 uriValue = short + uriValue.substr(long.length);
65 } 77 shortened = true;
66 return html`<a class="graphUri" href="${n.value}">${dn}</a>`; 78 break;
79 }
80 }
81 if (!shortened) {
82
83 let dn: string | undefined = this.labels.getLabelForNode(uriValue);
84 if (dn === undefined) {
85 throw new Error(`dn=${dn}`);
86 }
87 uriValue = dn;
88 }
89
90
91 return html`<a class="graphUri" href="${n.value}">${uriValue}</a>`;
67 } 92 }
68 93
69 return html`[${n.termType} ${n.value}]`; 94 return html`[${n.termType} ${n.value}]`;
70 } 95 }
71 } 96 }