diff src/graph_queries.ts @ 79:0c188ed3bcd8

starting lit upgrade. total mess right now
author drewp@bigasterisk.com
date Wed, 17 Nov 2021 13:01:08 -0800
parents 601a604c097a
children
line wrap: on
line diff
--- a/src/graph_queries.ts	Tue Feb 11 22:55:24 2020 -0800
+++ b/src/graph_queries.ts	Wed Nov 17 13:01:08 2021 -0800
@@ -1,18 +1,101 @@
-import { N3Store, NamedNode } from "n3";
+// import { DataFactory, Literal, N3Store, NamedNode, Util } from "n3";
+// const { literal, namedNode } = DataFactory;
+
+// // i think this one is a worse subset of graphLiteral, below
+// export function getStringValue(
+//   store: N3Store | undefined,
+//   subj: NamedNode,
+//   pred: NamedNode,
+//   defaultValue: string = ""
+// ): string {
+//   if (store === undefined) {
+//     // this is so you can use the function before you have a graph
+//     return "...";
+//   }
+//   const objs = store.getObjects(subj, pred, null);
+//   if (objs.length == 0) {
+//     return defaultValue;
+//   }
+//   return objs[0].value;
+// }
+
+// // 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 getStringValue(
-  store: N3Store | undefined,
-  subj: NamedNode,
-  pred: NamedNode,
-  defaultValue: string = ""
-): string {
-  if (store === undefined) {
-    // this is so you can use the function before you have a graph
-    return "...";
-  }
-  const objs = store.getObjects(subj, pred, null);
-  if (objs.length == 0) {
-    return defaultValue;
-  }
-  return objs[0].value;
-}
+// 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) {
+//         // why are we getting multiple matches for the same literal? seems like a bug
+//         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");
+// }