changeset 679:6723f7ae8f34

refactor plain graph functions from lit-element Ignore-this: db67b7d211f72ba9d14b0d490f7dc9da
author drewp@bigasterisk.com
date Mon, 06 Jan 2020 22:39:46 -0800
parents edbce8aa7107
children 8a49457a9737
files service/wifi/src/graph_access.ts service/wifi/src/index.ts
diffstat 2 files changed, 85 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/wifi/src/graph_access.ts	Mon Jan 06 22:39:46 2020 -0800
@@ -0,0 +1,82 @@
+import { Literal, N3Store, Util, NamedNode, DataFactory } from "n3";
+const { literal, namedNode } = DataFactory;
+
+// workaround for uris that don't have good labels in the graph
+export function labelFromUri(
+  uri: NamedNode,
+  prefix: string,
+  tailsToLabels: { [key: string]: string },
+  defaultLabel: string
+) {
+  let label = defaultLabel === undefined ? uri.value : defaultLabel;
+  Object.entries(tailsToLabels).forEach(([tail, useLabel]) => {
+    if (uri.equals(namedNode(prefix + tail))) {
+      label = useLabel as string;
+    }
+  });
+  return label;
+}
+
+export function graphLiteral(
+  store: N3Store,
+  subj: NamedNode,
+  pred: string,
+  notFoundResult?: string
+): Literal {
+  const keep: Array<Literal> = [];
+  store.forEach(
+    q => {
+      if (!Util.isLiteral(q.object)) {
+        throw new Error("non literal found");
+      }
+      let seen = false;
+      for (let other of keep) {
+        if (other.equals(q.object)) {
+          seen = true;
+        }
+      }
+      if (!seen) {
+        keep.push(q.object as Literal);
+      }
+    },
+    subj,
+    namedNode(pred),
+    null,
+    null
+  );
+  if (keep.length == 0) {
+    return literal(notFoundResult || "(missing)");
+  }
+  if (keep.length == 1) {
+    return keep[0];
+  }
+  console.log(`${subj.value} ${pred} had ${keep.length} objects:`, keep);
+  return keep[0];
+}
+
+export function graphUriValue(
+  store: N3Store,
+  subj: NamedNode,
+  pred: string
+): NamedNode | undefined {
+  const keep: Array<NamedNode> = [];
+  store.forEach(
+    q => {
+      if (!Util.isNamedNode(q.object)) {
+        throw new Error("non uri found");
+      }
+      keep.push(q.object as NamedNode);
+    },
+    subj,
+    namedNode(pred),
+    null,
+    null
+  );
+  if (keep.length == 0) {
+    return undefined;
+  }
+  if (keep.length == 1) {
+    return keep[0];
+  }
+  throw new Error("found multiple matches for pred");
+}
--- a/service/wifi/src/index.ts	Mon Jan 06 22:35:16 2020 -0800
+++ b/service/wifi/src/index.ts	Mon Jan 06 22:39:46 2020 -0800
@@ -4,12 +4,13 @@
 
 import { LitElement, property, html, customElement } from "lit-element";
 
-import { Literal, Term, N3Store, Util } from "n3";
+import { Literal, N3Store } from "n3";
 import { NamedNode, DataFactory } from "n3";
-const { literal, namedNode } = DataFactory;
+const { namedNode } = DataFactory;
 
 import { VersionedGraph } from "streamed-graph";
 import { style } from "./style";
+import { labelFromUri, graphLiteral, graphUriValue } from "./graph_access";
 
 interface DevGroup {
   connectedToAp: NamedNode;
@@ -28,22 +29,6 @@
 }
 const room = "http://projects.bigasterisk.com/room/";
 
-////////////////// funcs that could move out //////////////////////////////
-// workaround for uris that don't have good labels in the graph
-function labelFromUri(
-  uri: NamedNode,
-  prefix: string,
-  tailsToLabels: {[key: string]: string},
-  defaultLabel: string
-) {
-  let label = defaultLabel === undefined ? uri.value : defaultLabel;
-  Object.entries(tailsToLabels).forEach(([tail, useLabel]) => {
-    if (uri.equals(namedNode(prefix + tail))) {
-      label = useLabel as string;
-    }
-  });
-  return label;
-}
 function asString(x: Literal | undefined): string {
   if (x && x.value) {
     return x.value;
@@ -51,71 +36,6 @@
   return "(unknown)";
 }
 
-function graphLiteral(
-  store: N3Store,
-  subj: NamedNode,
-  pred: string,
-  notFoundResult?: string
-): Literal {
-  const keep: Array<Literal> = [];
-  store.forEach(
-    q => {
-      if (!Util.isLiteral(q.object)) {
-        throw new Error("non literal found");
-      }
-      let seen = false;
-      for (let other of keep) {
-        if (other.equals(q.object)) {
-          seen = true;
-        }
-      }
-      if (!seen) {
-        keep.push(q.object as Literal);
-      }
-    },
-    subj,
-    namedNode(pred),
-    null,
-    null
-  );
-  if (keep.length == 0) {
-    return literal(notFoundResult || "(missing)");
-  }
-  if (keep.length == 1) {
-    return keep[0];
-  }
-  console.log(`${subj.value} ${pred} had ${keep.length} objects:`, keep);
-  return keep[0];
-}
-
-function graphUriValue(
-  store: N3Store,
-  subj: NamedNode,
-  pred: string
-): NamedNode | undefined {
-  const keep: Array<NamedNode> = [];
-  store.forEach(
-    q => {
-      if (!Util.isNamedNode(q.object)) {
-        throw new Error("non uri found");
-      }
-      keep.push(q.object as NamedNode);
-    },
-    subj,
-    namedNode(pred),
-    null,
-    null
-  );
-  if (keep.length == 0) {
-    return undefined;
-  }
-  if (keep.length == 1) {
-    return keep[0];
-  }
-  throw new Error("found multiple matches for pred");
-}
-//////////////////////////////////////////////////////////////////////////////////
-
 @customElement("wifi-display")
 class WifiDisplay extends LitElement {
   static get styles() {