annotate light9/web/SyncedGraph.ts @ 2277:f61531e21a77

logging
author drewp@bigasterisk.com
date Mon, 29 May 2023 15:18:04 -0700
parents 1b7cde922c3d
children 702c1fd95dfd
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);
b51c74da9d35 more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents: 2268
diff changeset
41 this.client.disconnect();
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 }
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
159 if (patch.isEmpty()) throw "should not get to this point with empty patch";
1517
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
160
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
161 this._applyPatch(patch);
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
162 if (this.client) {
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
163 log("sending patch:\n", patch.dump());
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
164 this.client.sendPatch(patch);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
165 }
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
166 console.timeEnd("applyAndSendPatch");
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
167 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
168
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
169 _applyPatch(patch: Patch) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
170 // In most cases you want applyAndSendPatch.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
171 //
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
172 // This is the only method that writes to this.graph!
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
173 log("_applyPatch [1] \n", patch.dump());
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
174 this.cachedFloatValues.clear();
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
175 this.cachedUriValues.clear();
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
176 patch.applyToGraph(this.graph);
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
177 if (false) {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
178 log("applied patch locally", patch.summary());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
179 } else {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
180 log("applied patch locally:\n" + patch.dump());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
181 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
182 this.autoDeps.graphChanged(patch);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
183 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
184
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
185 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
186 // 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
187 // 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
188 const existing = this.graph.getQuads(s, p, null, g);
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
189 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
190 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
191
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
192 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
193 this.applyAndSendPatch(this.getObjectPatch(s, p, newObject, g));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
194 }
1595
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
195
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
196 clearObjects(s: N3.NamedNode, p: N3.NamedNode, g: N3.NamedNode) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
197 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
198 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
199
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2087
diff changeset
200 public runHandler(func: HandlerFunc, label: string) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
201 // 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
202 // 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
203 // 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
204
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
205 // 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
206 // 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
207 // appending a serial number is backwards.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
208 if (!this.serial) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
209 this.serial = 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
210 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
211 this.serial += 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
212 //label = label + @serial
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
213
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
214 this.autoDeps.runHandler(func, label);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
215 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
216
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
217 _singleValue(s: Quad_Subject, p: Quad_Predicate) {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
218 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
219 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
220 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
221
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
222 switch (objs.size) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
223 case 0:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
224 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
225 case 1:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
226 var obj = objs.values().next().value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
227 return obj;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
228 default:
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
229 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
230 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
231 }
1648
ffa3b81c6d95 some missing askedFor auditing
Drew Perttula <drewp@bigasterisk.com>
parents: 1647
diff changeset
232
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
233 floatValue(s: Quad_Subject, p: Quad_Predicate) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
234 const key = s.value + "|" + p.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
235 const hit = this.cachedFloatValues.get(key);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
236 if (hit !== undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
237 return hit;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
238 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
239 //log('float miss', s, p)
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
240
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
241 const v = this._singleValue(s, p).value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
242 const ret = parseFloat(v);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
243 if (isNaN(ret)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
244 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
245 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
246 this.cachedFloatValues.set(key, ret);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
247 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
248 }
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 stringValue(s: any, p: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
251 return this._singleValue(s, p).value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
252 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
253
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
254 uriValue(s: Quad_Subject, p: Quad_Predicate) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
255 const key = s.value + "|" + p.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
256 const hit = this.cachedUriValues.get(key);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
257 if (hit !== undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
258 return hit;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
259 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
260
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
261 const ret = this._singleValue(s, p);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
262 this.cachedUriValues.set(key, ret);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
263 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
264 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
265
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
266 labelOrTail(uri: { value: { split: (arg0: string) => any } }) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
267 let ret: any;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
268 try {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
269 ret = this.stringValue(uri, this.Uri("rdfs:label"));
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
270 } catch (error) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
271 const words = uri.value.split("/");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
272 ret = words[words.length - 1];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
273 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
274 if (!ret) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
275 ret = uri.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
276 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
277 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
278 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
279
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
280 objects(s: any, p: any): Quad_Object[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
281 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
282 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
283 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
284 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
285
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
286 subjects(p: any, o: any): Quad_Subject[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
287 this.autoDeps.askedFor(null, p, o, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
288 const quads = this.graph.getQuads(null, p, o, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
289 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
290 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
291
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
292 subjectStatements(s: Quad_Subject): Quad[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
293 this.autoDeps.askedFor(s, null, null, null);
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
294 const quads = this.graph.getQuads(s, null, null, null);
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
295 return quads;
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
296 }
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
297
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
298 items(list: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
299 const out = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
300 let current = list;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
301 while (true) {
2130
90f99b3f2b18 items() was broken; needs more typechecking or tests
drewp@bigasterisk.com
parents: 2108
diff changeset
302 if (current.value === RDF + "nil") {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
303 break;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
304 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
305
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
306 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
307
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
308 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
309 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
310 if (firsts.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
311 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
312 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
313 out.push(firsts[0].object);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
314
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
315 if (rests.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
316 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
317 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
318 current = rests[0].object;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
319 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
320
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
321 return out;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
322 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
323
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
324 contains(s: any, p: any, o: any): boolean {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
325 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
326 // 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
327 // 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
328 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
329 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
330
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
331 nextNumberedResources(base: { id: any }, howMany: number) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
332 // base is NamedNode or string
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
333 // 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
334 // always return 'name0'.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
335 if (base.id) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
336 base = base.id;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
337 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
338 const results = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
339
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
340 // @contains is really slow.
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
341 if (this.nextNumber == null) {
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
342 this.nextNumber = new Map();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
343 }
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
344 let start = this.nextNumber.get(base);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
345 if (start === undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
346 start = 0;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
347 }
1387
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
348
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
349 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
350 const uri = this.Uri(`${base}${serial}`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
351 if (!this.contains(uri, null, null)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
352 results.push(uri);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
353 log("nextNumberedResources", `picked ${uri}`);
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
354 this.nextNumber.set(base, serial + 1);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
355 if (results.length >= howMany) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
356 return results;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
357 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
358 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
359 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
360 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
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 nextNumberedResource(base: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
364 return this.nextNumberedResources(base, 1)[0];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
365 }
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
366
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
367 contextsWithPattern(s: any, p: any, o: any) {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
368 this.autoDeps.askedFor(s, p, o, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
369 const ctxs = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
370 for (let q of Array.from(this.graph.getQuads(s, p, o, null))) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
371 ctxs.push(q.graph);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
372 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
373 return unique(ctxs);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
374 }
1792
a07bfbe1e9de make graph sortKey public
Drew Perttula <drewp@bigasterisk.com>
parents: 1789
diff changeset
375
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
376 sortKey(uri: N3.NamedNode) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
377 const parts = uri.value.split(/([0-9]+)/);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
378 const expanded = parts.map(function (p: string) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
379 const f = parseInt(p);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
380 if (isNaN(f)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
381 return p;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
382 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
383 return p.padStart(8, "0");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
384 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
385 return expanded.join("");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
386 }
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
387
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
388 sortedUris(uris: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
389 return sortBy(uris, this.sortKey);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
390 }
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
391
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
392 prettyLiteral(x: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
393 if (typeof x === "number") {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
394 return this.LiteralRoundedFloat(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
395 } else {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
396 return this.Literal(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
397 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
398 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
399 }