annotate service/wifi/src/graph_access.ts @ 684:b39d24617a85

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