Changeset - dfdc00c4c8fb
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-29 18:48:28
drewp@bigasterisk.com
syncegraph.shorten more cases
1 file changed with 10 insertions and 3 deletions:
0 comments (0 inline, 0 general)
light9/web/SyncedGraph.ts
Show inline comments
 
@@ -47,99 +47,106 @@ export class SyncedGraph {
 
    // 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));
 
    // 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");
 
    if (this.clearCb != null) {
 
      return this.clearCb();
 
    }
 
  }
 

	
 
  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 {
 
    const prefix = "http://light9.bigasterisk.com/";
 
    if (uri.value.startsWith(prefix)) {
 
      return ":" + uri.value.substring(prefix.length);
 
    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#double"));
 
  }
 

	
 
  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) {
 
    console.time("applyAndSendPatch");
0 comments (0 inline, 0 general)