annotate service/mqtt_to_rdf/src/graph_access.ts @ 1706:2085ed9cfcc4

reworking UI to reflect the new inferencing code
author drewp@bigasterisk.com
date Sat, 23 Oct 2021 13:22:40 -0700
parents b459b5c3c33a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1586
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
1 import { Literal, N3Store, Util, NamedNode, DataFactory } from "n3";
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
2 const { literal, namedNode } = DataFactory;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
3
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
4 // workaround for uris that don't have good labels in the graph
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
5 export function labelFromUri(
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
6 uri: NamedNode,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
7 prefix: string,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
8 tailsToLabels: { [key: string]: string },
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
9 defaultLabel: string
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
10 ) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
11 let label = defaultLabel === undefined ? uri.value : defaultLabel;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
12 Object.entries(tailsToLabels).forEach(([tail, useLabel]) => {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
13 if (uri.equals(namedNode(prefix + tail))) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
14 label = useLabel as string;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
15 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
16 });
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
17 return label;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
18 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
19
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
20 export function graphLiteral(
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
21 store: N3Store,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
22 subj: NamedNode,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
23 pred: string,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
24 notFoundResult?: string
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
25 ): Literal {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
26 const keep: Array<Literal> = [];
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
27 store.forEach(
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
28 q => {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
29 if (!Util.isLiteral(q.object)) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
30 throw new Error("non literal found");
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
31 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
32 let seen = false;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
33 for (let other of keep) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
34 // why are we getting multiple matches for the same literal? seems like a bug
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
35 if (other.equals(q.object)) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
36 seen = true;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
37 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
38 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
39 if (!seen) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
40 keep.push(q.object as Literal);
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
41 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
42 },
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
43 subj,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
44 namedNode(pred),
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
45 null,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
46 null
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
47 );
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
48 if (keep.length == 0) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
49 return literal(notFoundResult || "(missing)");
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
50 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
51 if (keep.length == 1) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
52 return keep[0];
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
53 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
54 console.log(`${subj.value} ${pred} had ${keep.length} objects:`, keep);
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
55 return keep[0];
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
56 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
57
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
58 export function graphUriValue(
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
59 store: N3Store,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
60 subj: NamedNode,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
61 pred: string
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
62 ): NamedNode | undefined {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
63 const keep: Array<NamedNode> = [];
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
64 store.forEach(
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
65 q => {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
66 if (!Util.isNamedNode(q.object)) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
67 throw new Error("non uri found");
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
68 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
69 keep.push(q.object as NamedNode);
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
70 },
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
71 subj,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
72 namedNode(pred),
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
73 null,
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
74 null
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
75 );
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
76 if (keep.length == 0) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
77 return undefined;
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
78 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
79 if (keep.length == 1) {
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
80 return keep[0];
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
81 }
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
82 throw new Error("found multiple matches for pred");
b459b5c3c33a some rdf code. maybe copied from light9?
drewp@bigasterisk.com
parents:
diff changeset
83 }