Changeset - 1b7cde922c3d
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-29 22:17:28
drewp@bigasterisk.com
save some work
1 file changed with 4 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/web/SyncedGraph.ts
Show inline comments
 
import debug from "debug";
 
import * as N3 from "n3";
 
import { Quad, Quad_Object, Quad_Predicate, Quad_Subject } from "n3";
 
import { sortBy, unique } from "underscore";
 
import { AutoDependencies, HandlerFunc } from "./AutoDependencies";
 
import { Patch, patchToDeleteEntireGraph } from "./patch";
 
import { RdfDbClient } from "./rdfdbclient";
 

	
 
const log = debug("graph");
 

	
 
const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 

	
 
export class SyncedGraph {
 
  private autoDeps: AutoDependencies;
 
  private client: RdfDbClient;
 
  private graph: N3.Store;
 
  private cachedFloatValues: Map<string, number> = new Map();
 
  private cachedUriValues: Map<string, N3.NamedNode> = new Map();
 
  private prefixFuncs: (prefix: string) => N3.PrefixedToIri;
 
  private serial: any;
 
  private nextNumber: any;
 
  // Main graph object for a browser to use. Consider using RdfdbSyncedGraph element to create & own
 
  // one of these. Syncs both ways with rdfdb. Meant to hide the choice of RDF lib, so we can change it
 
  // later.
 
  //
 
  // Note that _applyPatch is the only method to write to the graph, so
 
  // it can fire subscriptions.
 

	
 
  constructor(
 
    // The /syncedGraph path of an rdfdb server.
 
    patchSenderUrl: string,
 
    // prefixes can be used in Uri(curie) calls. This mapping may grow during loadTrig calls.
 
    public prefixes: Map<string, string>,
 
    private setStatus: (status: string) => void
 
  ) {
 
    this.prefixFuncs = this.rebuildPrefixFuncs(prefixes);
 
    this.graph = new N3.Store();
 
    this.autoDeps = new AutoDependencies(this);
 
    this.autoDeps.graphError.subscribe((e) => {
 
      log("graph learned of error - reconnecting", e);
 
      this.client.disconnect();
 
    });
 
    this.clearGraph();
 

	
 
    this.client = new RdfDbClient(patchSenderUrl, this._clearGraphOnNewConnection.bind(this), this._applyPatch.bind(this), this.setStatus);
 
  }
 

	
 
  clearGraph() {
 
    // must not try send a patch to the server!
 
    // just deletes the statements; watchers are unaffected.
 
    this.cachedFloatValues = new Map(); // s + '|' + p -> number
 
    this.cachedUriValues = new Map(); // s + '|' + p -> Uri
 

	
 
    this._applyPatch(patchToDeleteEntireGraph(this.graph));
 
    const p = patchToDeleteEntireGraph(this.graph);
 
    if (!p.isEmpty()) {
 
      this._applyPatch(p);
 
    }
 
    // if we had a Store already, this lets N3.Store free all its indices/etc
 
    this.graph = new N3.Store();
 
    this.rebuildPrefixFuncs(this.prefixes);
 
  }
 

	
 
  _clearGraphOnNewConnection() {
 
    // must not try send a patch to the server
 

	
 
    log("clearGraphOnNewConnection");
 
    this.clearGraph();
 
    log("clearGraphOnNewConnection done");
 
  }
 

	
 
  private rebuildPrefixFuncs(prefixes: Map<string, string>) {
 
    const p = Object.create(null);
 
    prefixes.forEach((v: string, k: string) => (p[k] = v));
 

	
 
    this.prefixFuncs = N3.Util.prefixes(p);
 
    return this.prefixFuncs;
 
  }
 

	
 
  U() {
 
    // just a shorthand
 
    return this.Uri.bind(this);
 
  }
 

	
 
  Uri(curie: string) {
 
    if (curie == null) {
 
      throw new Error("no uri");
 
    }
 
    if (curie.match(/^http/)) {
 
      return N3.DataFactory.namedNode(curie);
 
    }
 
    const part = curie.split(":");
 
    return this.prefixFuncs(part[0])(part[1]);
 
  }
 

	
 
  // Uri(shorten(u)).value==u
 
  shorten(uri: N3.NamedNode): string {
 
    for (let row of [
 
      { sh: "dev", lo: "http://light9.bigasterisk.com/theater/vet/device/" },
 
      { sh: "effect", lo: "http://light9.bigasterisk.com/effect/" },
 
      { sh: "", lo: "http://light9.bigasterisk.com/" },
 
      { sh: "rdfs", lo: "http://www.w3.org/2000/01/rdf-schema#" },
 
      { sh: "xsd", lo: "http://www.w3.org/2001/XMLSchema#" },
 
    ]) {
 
      if (uri.value.startsWith(row.lo)) {
 
        return row.sh + ":" + uri.value.substring(row.lo.length);
 
      }
 
    }
 
    return uri.value;
 
  }
 

	
 
  Literal(jsValue: string | number) {
 
    return N3.DataFactory.literal(jsValue);
 
  }
 

	
 
  LiteralRoundedFloat(f: number) {
 
    return N3.DataFactory.literal(f.toPrecision(3), this.Uri("http://www.w3.org/2001/XMLSchema#decimal"));
 
  }
 

	
 
  Quad(s: any, p: any, o: any, g: any) {
 
    return N3.DataFactory.quad(s, p, o, g);
 
  }
 

	
 
  toJs(literal: { value: any }) {
 
    // incomplete
 
    return parseFloat(literal.value);
 
  }
 

	
 
  loadTrig(trig: any, cb: () => any) {
 
    // for debugging
 
    const adds: Quad[] = [];
 
    const parser = new N3.Parser();
 
    parser.parse(trig, (error: any, quad: any, prefixes: any) => {
 
      if (error) {
 
        throw new Error(error);
 
      }
 
      if (quad) {
 
        adds.push(quad);
 
      } else {
 
        this._applyPatch(new Patch([], adds));
 
        // todo: here, add those prefixes to our known set
 
        if (cb) {
 
          cb();
 
        }
 
      }
 
    });
 
  }
 

	
 
  quads(): any {
 
    // for debugging
 
    return Array.from(this.graph.getQuads(null, null, null, null)).map((q: Quad) => [q.subject, q.predicate, q.object, q.graph]);
 
  }
 

	
 
  applyAndSendPatch(patch: Patch) {
0 comments (0 inline, 0 general)