annotate web/SyncedGraph.ts @ 2404:9cbc93f80b05

cleanup
author drewp@bigasterisk.com
date Fri, 17 May 2024 17:41:22 -0700
parents 4556eebe5d73
children ac55319a2eac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
1 import debug from "debug";
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
2 import * as N3 from "n3";
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
3 import { Quad, Quad_Object, Quad_Predicate, Quad_Subject } from "n3";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
4 import { sortBy, unique } from "underscore";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
5 import { AutoDependencies, HandlerFunc } from "./AutoDependencies";
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
6 import { Patch, patchToDeleteEntireGraph } from "./patch";
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
7 import { RdfDbClient } from "./rdfdbclient";
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
8
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
9 const log = debug("graph");
1362
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
10
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
11 const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
1350
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
12
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
13 export class SyncedGraph {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
14 private autoDeps: AutoDependencies;
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
15 private client: RdfDbClient;
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
16 private graph: N3.Store;
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
17 private cachedFloatValues: Map<string, number> = new Map();
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
18 private cachedUriValues: Map<string, N3.NamedNode> = new Map();
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
19 private prefixFuncs: (prefix: string) => N3.PrefixedToIri;
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
20 private serial: any;
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
21 private nextNumber: any;
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
22 // Main graph object for a browser to use. Consider using RdfdbSyncedGraph element to create & own
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
23 // one of these. Syncs both ways with rdfdb. Meant to hide the choice of RDF lib, so we can change it
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
24 // later.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
25 //
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
26 // Note that _applyPatch is the only method to write to the graph, so
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
27 // it can fire subscriptions.
1651
e00492e1c0b1 include some of the handler tree optimization code, turned off
Drew Perttula <drewp@bigasterisk.com>
parents: 1648
diff changeset
28
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
29 constructor(
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
30 // The /syncedGraph path of an rdfdb server.
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
31 patchSenderUrl: string,
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
32 // prefixes can be used in Uri(curie) calls. This mapping may grow during loadTrig calls.
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
33 public prefixes: Map<string, string>,
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
34 private setStatus: (status: string) => void
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
35 ) {
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
36 this.prefixFuncs = this.rebuildPrefixFuncs(prefixes);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
37 this.graph = new N3.Store();
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
38 this.autoDeps = new AutoDependencies(this);
2270
b51c74da9d35 more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents: 2268
diff changeset
39 this.autoDeps.graphError.subscribe((e) => {
b51c74da9d35 more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents: 2268
diff changeset
40 log("graph learned of error - reconnecting", e);
2322
cdfd2901918a logging
drewp@bigasterisk.com
parents: 2296
diff changeset
41 this.client.disconnect("graph error");
2270
b51c74da9d35 more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents: 2268
diff changeset
42 });
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
43 this.clearGraph();
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
45 this.client = new RdfDbClient(patchSenderUrl, this._clearGraphOnNewConnection.bind(this), this._applyPatch.bind(this), this.setStatus);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
46 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
47
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
48 clearGraph() {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
49 // must not try send a patch to the server!
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
50 // just deletes the statements; watchers are unaffected.
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
51 this.cachedFloatValues = new Map(); // s + '|' + p -> number
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
52 this.cachedUriValues = new Map(); // s + '|' + p -> Uri
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
53
2276
1b7cde922c3d save some work
drewp@bigasterisk.com
parents: 2275
diff changeset
54 const p = patchToDeleteEntireGraph(this.graph);
1b7cde922c3d save some work
drewp@bigasterisk.com
parents: 2275
diff changeset
55 if (!p.isEmpty()) {
1b7cde922c3d save some work
drewp@bigasterisk.com
parents: 2275
diff changeset
56 this._applyPatch(p);
1b7cde922c3d save some work
drewp@bigasterisk.com
parents: 2275
diff changeset
57 }
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
58 // if we had a Store already, this lets N3.Store free all its indices/etc
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
59 this.graph = new N3.Store();
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
60 this.rebuildPrefixFuncs(this.prefixes);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
61 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
62
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
63 _clearGraphOnNewConnection() {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
64 // must not try send a patch to the server
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
65
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
66 log("clearGraphOnNewConnection");
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
67 this.clearGraph();
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
68 log("clearGraphOnNewConnection done");
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
69 }
1786
11affc6d6045 more graph methods. some cleanup of the code that runs only required handlers, but it's not turned on yet
Drew Perttula <drewp@bigasterisk.com>
parents: 1783
diff changeset
70
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
71 private rebuildPrefixFuncs(prefixes: Map<string, string>) {
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
72 const p = Object.create(null);
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
73 prefixes.forEach((v: string, k: string) => (p[k] = v));
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
74
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
75 this.prefixFuncs = N3.Util.prefixes(p);
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
76 return this.prefixFuncs;
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
77 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
78
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
79 U() {
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
80 // just a shorthand
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
81 return this.Uri.bind(this);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
82 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
83
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
84 Uri(curie: string) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
85 if (curie == null) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
86 throw new Error("no uri");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
87 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
88 if (curie.match(/^http/)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
89 return N3.DataFactory.namedNode(curie);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
90 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
91 const part = curie.split(":");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
92 return this.prefixFuncs(part[0])(part[1]);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
93 }
1624
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
94
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
95 // Uri(shorten(u)).value==u
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
96 shorten(uri: N3.NamedNode): string {
2266
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
97 for (let row of [
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
98 { sh: "dev", lo: "http://light9.bigasterisk.com/theater/vet/device/" },
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
99 { sh: "effect", lo: "http://light9.bigasterisk.com/effect/" },
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
100 { sh: "", lo: "http://light9.bigasterisk.com/" },
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
101 { sh: "rdfs", lo: "http://www.w3.org/2000/01/rdf-schema#" },
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
102 { sh: "xsd", lo: "http://www.w3.org/2001/XMLSchema#" },
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
103 ]) {
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
104 if (uri.value.startsWith(row.lo)) {
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
105 return row.sh + ":" + uri.value.substring(row.lo.length);
dfdc00c4c8fb syncegraph.shorten more cases
drewp@bigasterisk.com
parents: 2259
diff changeset
106 }
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
107 }
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
108 return uri.value;
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
109 }
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
110
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
111 Literal(jsValue: string | number) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
112 return N3.DataFactory.literal(jsValue);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
113 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
114
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
115 LiteralRoundedFloat(f: number) {
2275
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2273
diff changeset
116 return N3.DataFactory.literal(f.toPrecision(3), this.Uri("http://www.w3.org/2001/XMLSchema#decimal"));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
117 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
118
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
119 Quad(s: any, p: any, o: any, g: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
120 return N3.DataFactory.quad(s, p, o, g);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
121 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
122
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
123 toJs(literal: { value: any }) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
124 // incomplete
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
125 return parseFloat(literal.value);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
126 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
127
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
128 loadTrig(trig: any, cb: () => any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
129 // for debugging
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
130 const adds: Quad[] = [];
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
131 const parser = new N3.Parser();
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
132 parser.parse(trig, (error: any, quad: any, prefixes: any) => {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
133 if (error) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
134 throw new Error(error);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
135 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
136 if (quad) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
137 adds.push(quad);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
138 } else {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
139 this._applyPatch(new Patch([], adds));
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
140 // todo: here, add those prefixes to our known set
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
141 if (cb) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
142 cb();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
143 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
144 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
145 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
146 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
147
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
148 quads(): any {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
149 // for debugging
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
150 return Array.from(this.graph.getQuads(null, null, null, null)).map((q: Quad) => [q.subject, q.predicate, q.object, q.graph]);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
151 }
1356
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
152
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
153 applyAndSendPatch(patch: Patch) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
154 console.time("applyAndSendPatch");
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
155 if (!this.client) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
156 log("not connected-- dropping patch");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
157 return;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
158 }
2293
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
159 if (!patch.isEmpty()) {
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
160 this._applyPatch(patch);
2323
855f1abf5c66 client side chaos (disabled)
drewp@bigasterisk.com
parents: 2322
diff changeset
161 // // chaos delay
855f1abf5c66 client side chaos (disabled)
drewp@bigasterisk.com
parents: 2322
diff changeset
162 // setTimeout(()=>{
2293
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
163 if (this.client) {
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
164 log("sending patch:\n", patch.dump());
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
165 this.client.sendPatch(patch);
c070f46c0761 you can ask to apply empty patch and it will no-op
drewp@bigasterisk.com
parents: 2278
diff changeset
166 }
2323
855f1abf5c66 client side chaos (disabled)
drewp@bigasterisk.com
parents: 2322
diff changeset
167 // },300*Math.random())
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
168 }
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
169 console.timeEnd("applyAndSendPatch");
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
170 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
171
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
172 _applyPatch(patch: Patch) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
173 // In most cases you want applyAndSendPatch.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
174 //
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
175 // This is the only method that writes to this.graph!
2278
702c1fd95dfd another empty-patch guard, to cause me to fix problems upstream of this call
drewp@bigasterisk.com
parents: 2277
diff changeset
176 if (patch.isEmpty()) throw "dont send empty patches here";
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
177 log("_applyPatch [1] \n", patch.dump());
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
178 this.cachedFloatValues.clear();
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
179 this.cachedUriValues.clear();
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
180 patch.applyToGraph(this.graph);
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
181 if (false) {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
182 log("applied patch locally", patch.summary());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
183 } else {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
184 log("applied patch locally:\n" + patch.dump());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
185 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
186 this.autoDeps.graphChanged(patch);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
187 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
188
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
189 getObjectPatch(s: N3.NamedNode, p: N3.NamedNode, newObject: N3.Quad_Object | null, g: N3.NamedNode): Patch {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
190 // make a patch which removes existing values for (s,p,*,c) and
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
191 // adds (s,p,newObject,c). Values in other graphs are not affected.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
192 const existing = this.graph.getQuads(s, p, null, g);
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
193 return new Patch(existing, newObject !== null ? [this.Quad(s, p, newObject, g)] : []);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
194 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
195
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
196 patchObject(s: N3.NamedNode, p: N3.NamedNode, newObject: N3.Quad_Object | null, g: N3.NamedNode) {
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
197 this.applyAndSendPatch(this.getObjectPatch(s, p, newObject, g));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
198 }
1595
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
199
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
200 clearObjects(s: N3.NamedNode, p: N3.NamedNode, g: N3.NamedNode) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
201 this.applyAndSendPatch(new Patch(this.graph.getQuads(s, p, null, g), []));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
202 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
203
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2087
diff changeset
204 public runHandler(func: HandlerFunc, label: string) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
205 // runs your func once, tracking graph calls. if a future patch
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
206 // matches what you queried, we runHandler your func again (and
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
207 // forget your queries from the first time).
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
208
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
209 // helps with memleak? not sure yet. The point was if two matching
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
210 // labels get puushed on, we should run only one. So maybe
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
211 // appending a serial number is backwards.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
212 if (!this.serial) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
213 this.serial = 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
214 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
215 this.serial += 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
216 //label = label + @serial
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
217
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
218 this.autoDeps.runHandler(func, label);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
219 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
220
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
221 _singleValue(s: Quad_Subject, p: Quad_Predicate) {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
222 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
223 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
224 const objs = new Set(Array.from(quads).map((q: Quad) => q.object));
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
225
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
226 switch (objs.size) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
227 case 0:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
228 throw new Error("no value for " + s.value + " " + p.value);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
229 case 1:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
230 var obj = objs.values().next().value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
231 return obj;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
232 default:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
233 throw new Error("too many different values: " + JSON.stringify(quads));
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
234 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
235 }
1648
ffa3b81c6d95 some missing askedFor auditing
Drew Perttula <drewp@bigasterisk.com>
parents: 1647
diff changeset
236
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
237 floatValue(s: Quad_Subject, p: Quad_Predicate) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
238 const key = s.value + "|" + p.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
239 const hit = this.cachedFloatValues.get(key);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
240 if (hit !== undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
241 return hit;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
242 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
243 //log('float miss', s, p)
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
244
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
245 const v = this._singleValue(s, p).value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
246 const ret = parseFloat(v);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
247 if (isNaN(ret)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
248 throw new Error(`${s.value} ${p.value} -> ${v} not a float`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
249 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
250 this.cachedFloatValues.set(key, ret);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
251 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
252 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
253
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
254 stringValue(s: any, p: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
255 return this._singleValue(s, p).value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
256 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
257
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
258 uriValue(s: Quad_Subject, p: Quad_Predicate) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
259 const key = s.value + "|" + p.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
260 const hit = this.cachedUriValues.get(key);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
261 if (hit !== undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
262 return hit;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
263 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
264
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
265 const ret = this._singleValue(s, p);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
266 this.cachedUriValues.set(key, ret);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
267 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
268 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
269
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
270 labelOrTail(uri: { value: { split: (arg0: string) => any } }) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
271 let ret: any;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
272 try {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
273 ret = this.stringValue(uri, this.Uri("rdfs:label"));
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
274 } catch (error) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
275 const words = uri.value.split("/");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
276 ret = words[words.length - 1];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
277 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
278 if (!ret) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
279 ret = uri.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
280 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
281 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
282 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
283
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
284 objects(s: any, p: any): Quad_Object[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
285 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
286 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
287 return Array.from(quads).map((q: { object: any }) => q.object);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
288 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
289
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
290 subjects(p: any, o: any): Quad_Subject[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
291 this.autoDeps.askedFor(null, p, o, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
292 const quads = this.graph.getQuads(null, p, o, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
293 return Array.from(quads).map((q: { subject: any }) => q.subject);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
294 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
295
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
296 subjectStatements(s: Quad_Subject): Quad[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
297 this.autoDeps.askedFor(s, null, null, null);
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
298 const quads = this.graph.getQuads(s, null, null, null);
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
299 return quads;
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
300 }
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
301
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
302 items(list: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
303 const out = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
304 let current = list;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
305 while (true) {
2130
90f99b3f2b18 items() was broken; needs more typechecking or tests
drewp@bigasterisk.com
parents: 2108
diff changeset
306 if (current.value === RDF + "nil") {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
307 break;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
308 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
309
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
310 this.autoDeps.askedFor(current, null, null, null); // a little loose
1786
11affc6d6045 more graph methods. some cleanup of the code that runs only required handlers, but it's not turned on yet
Drew Perttula <drewp@bigasterisk.com>
parents: 1783
diff changeset
311
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
312 const firsts = this.graph.getQuads(current, RDF + "first", null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
313 const rests = this.graph.getQuads(current, RDF + "rest", null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
314 if (firsts.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
315 throw new Error(`list node ${current} has ${firsts.length} rdf:first edges`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
316 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
317 out.push(firsts[0].object);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
318
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
319 if (rests.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
320 throw new Error(`list node ${current} has ${rests.length} rdf:rest edges`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
321 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
322 current = rests[0].object;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
323 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
324
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
325 return out;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
326 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
327
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
328 contains(s: any, p: any, o: any): boolean {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
329 this.autoDeps.askedFor(s, p, o, null);
2108
e92db17f3e7e effectSequencer can now also process some note-like values coming from the fade/ ui
drewp@bigasterisk.com
parents: 2088
diff changeset
330 // Sure this is a nice warning to remind me to rewrite, but the graph.size call itself was taking 80% of the time in here
e92db17f3e7e effectSequencer can now also process some note-like values coming from the fade/ ui
drewp@bigasterisk.com
parents: 2088
diff changeset
331 // log("contains calling getQuads when graph has ", this.graph.size);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
332 return this.graph.getQuads(s, p, o, null).length > 0;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
333 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
334
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
335 nextNumberedResources(base: { id: any }, howMany: number) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
336 // base is NamedNode or string
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
337 // Note this is unsafe before we're synced with the graph. It'll
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
338 // always return 'name0'.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
339 if (base.id) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
340 base = base.id;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
341 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
342 const results = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
343
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
344 // @contains is really slow.
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
345 if (this.nextNumber == null) {
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
346 this.nextNumber = new Map();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
347 }
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
348 let start = this.nextNumber.get(base);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
349 if (start === undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
350 start = 0;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
351 }
1387
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
352
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
353 for (let serial = start, asc = start <= 1000; asc ? serial <= 1000 : serial >= 1000; asc ? serial++ : serial--) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
354 const uri = this.Uri(`${base}${serial}`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
355 if (!this.contains(uri, null, null)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
356 results.push(uri);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
357 log("nextNumberedResources", `picked ${uri}`);
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
358 this.nextNumber.set(base, serial + 1);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
359 if (results.length >= howMany) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
360 return results;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
361 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
362 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
363 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
364 throw new Error(`can't make sequential uri with base ${base}`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
365 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
366
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
367 nextNumberedResource(base: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
368 return this.nextNumberedResources(base, 1)[0];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
369 }
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
370
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
371 contextsWithPattern(s: Quad_Subject | null, p: Quad_Predicate | null, o: Quad_Object | null): N3.NamedNode[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
372 this.autoDeps.askedFor(s, p, o, null);
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
373 const ctxs: N3.NamedNode[] = [];
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
374 for (let q of Array.from(this.graph.getQuads(s, p, o, null))) {
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
375 if (q.graph.termType != "NamedNode") throw `context was ${q.graph.id}`;
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
376 ctxs.push(q.graph);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
377 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
378 return unique(ctxs);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
379 }
1792
a07bfbe1e9de make graph sortKey public
Drew Perttula <drewp@bigasterisk.com>
parents: 1789
diff changeset
380
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
381 sortKey(uri: N3.NamedNode) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
382 const parts = uri.value.split(/([0-9]+)/);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
383 const expanded = parts.map(function (p: string) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
384 const f = parseInt(p);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
385 if (isNaN(f)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
386 return p;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
387 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
388 return p.padStart(8, "0");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
389 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
390 return expanded.join("");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
391 }
1786
11affc6d6045 more graph methods. some cleanup of the code that runs only required handlers, but it's not turned on yet
Drew Perttula <drewp@bigasterisk.com>
parents: 1783
diff changeset
392
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
393 sortedUris(uris: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
394 return sortBy(uris, this.sortKey);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
395 }
1786
11affc6d6045 more graph methods. some cleanup of the code that runs only required handlers, but it's not turned on yet
Drew Perttula <drewp@bigasterisk.com>
parents: 1783
diff changeset
396
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
397 prettyLiteral(x: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
398 if (typeof x === "number") {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
399 return this.LiteralRoundedFloat(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
400 } else {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
401 return this.Literal(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
402 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
403 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
404 }