Mercurial > code > home > repos > homeauto
view service/wifi/src/graph_access.ts @ 1480:ae023bba2104
refactor plain graph functions from lit-element
Ignore-this: db67b7d211f72ba9d14b0d490f7dc9da
darcs-hash:88cdb343d3f5e47f06e2737402ef48257b0f80e9
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 06 Jan 2020 22:39:46 -0800 |
parents | |
children | 50d66febeeb0 |
line wrap: on
line source
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"); }