annotate web/SyncedGraph.ts @ 2440:d1f86109e3cc

more *value getter variants
author drewp@bigasterisk.com
date Thu, 30 May 2024 01:08:45 -0700
parents ac55319a2eac
children
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) {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
154 if (!this.client) {
2421
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
155 throw new Error("no client yet");
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
156 }
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
157 if (patch.isEmpty()) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
158 return;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
159 }
2421
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
160 this._applyPatch(patch);
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
161 // // chaos delay
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
162 // setTimeout(()=>{
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
163 log("sending patch:\n", patch.dump());
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
164 this.client.sendPatch(patch);
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
165
ac55319a2eac don't drop patches that arrive before we get WS connected
drewp@bigasterisk.com
parents: 2376
diff changeset
166 // },300*Math.random())
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!
2278
702c1fd95dfd another empty-patch guard, to cause me to fix problems upstream of this call
drewp@bigasterisk.com
parents: 2277
diff changeset
173 if (patch.isEmpty()) throw "dont send empty patches here";
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
174 log("_applyPatch [1] \n", patch.dump());
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
175 this.cachedFloatValues.clear();
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
176 this.cachedUriValues.clear();
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
177 patch.applyToGraph(this.graph);
2277
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
178 if (false) {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
179 log("applied patch locally", patch.summary());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
180 } else {
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
181 log("applied patch locally:\n" + patch.dump());
f61531e21a77 logging
drewp@bigasterisk.com
parents: 2276
diff changeset
182 }
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
183 this.autoDeps.graphChanged(patch);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
184 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
185
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
186 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
187 // 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
188 // 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
189 const existing = this.graph.getQuads(s, p, null, g);
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
190 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
191 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
192
2230
eeb2b3928715 fix 'unlink' button. (event was not well-typed)
drewp@bigasterisk.com
parents: 2130
diff changeset
193 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
194 this.applyAndSendPatch(this.getObjectPatch(s, p, newObject, g));
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
195 }
1595
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
196
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
197 clearObjects(s: N3.NamedNode, p: N3.NamedNode, g: N3.NamedNode) {
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2230
diff changeset
198 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
199 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
200
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2087
diff changeset
201 public runHandler(func: HandlerFunc, label: string) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
202 // 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
203 // 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
204 // 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
205
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
206 // 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
207 // 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
208 // appending a serial number is backwards.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
209 if (!this.serial) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
210 this.serial = 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
211 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
212 this.serial += 1;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
213 //label = label + @serial
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
214
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
215 this.autoDeps.runHandler(func, label);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
216 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
217
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
218 _singleValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError: boolean) {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
219 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
220 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
221 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
222
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
223 switch (objs.size) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
224 case 0:
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
225 if (_nullNotError) {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
226 return null;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
227 }
2071
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
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
237 floatValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) {
2071
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
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
245 const gv = this._singleValue(s, p, _nullNotError);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
246 if (_nullNotError && gv == null) return null;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
247 const v = gv.value;
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
248 const ret = parseFloat(v);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
249 if (isNaN(ret)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
250 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
251 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
252 this.cachedFloatValues.set(key, ret);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
253 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
254 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
255
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
256 _booleanValue(s: any, p: any, _nullNotError = false) {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
257 const gv = this._singleValue(s, p, _nullNotError);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
258 if (_nullNotError && gv == null) return null;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
259 return gv.value == "true";
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
260 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
261 booleanValue(s: any, p: any): boolean {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
262 return this._booleanValue(s, p, false) as boolean;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
263 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
264 stringValue(s: any, p: any, _nullNotError = false) {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
265 const gv = this._singleValue(s, p, _nullNotError);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
266 if (_nullNotError && gv == null) return null;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
267 return gv.value;
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
268 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
269
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
270 uriValue(s: Quad_Subject, p: Quad_Predicate, _nullNotError = false) {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
271 const key = s.value + "|" + p.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
272 const hit = this.cachedUriValues.get(key);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
273 if (hit !== undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
274 return hit;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
275 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
276
2440
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
277 const gv = this._singleValue(s, p, _nullNotError);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
278 if (_nullNotError && gv == null) return null;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
279 this.cachedUriValues.set(key, gv);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
280 return gv;
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
281 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
282
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
283 optionalFloatValue(s: Quad_Subject, p: Quad_Predicate): number | null {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
284 return this.floatValue(s, p, true);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
285 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
286 optionalBooleanValue(s: Quad_Subject, p: Quad_Predicate): boolean | null {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
287 return this._booleanValue(s, p, true);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
288 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
289 optionalStringValue(s: Quad_Subject, p: Quad_Predicate): string | null {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
290 return this.stringValue(s, p, true);
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
291 }
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
292 optionalUriValue(s: Quad_Subject, p: Quad_Predicate): N3.NamedNode | null {
d1f86109e3cc more *value getter variants
drewp@bigasterisk.com
parents: 2421
diff changeset
293 return this.uriValue(s, p, true);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
294 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
295
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
296 labelOrTail(uri: { value: { split: (arg0: string) => any } }) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
297 let ret: any;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
298 try {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
299 ret = this.stringValue(uri, this.Uri("rdfs:label"));
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
300 } catch (error) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
301 const words = uri.value.split("/");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
302 ret = words[words.length - 1];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
303 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
304 if (!ret) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
305 ret = uri.value;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
306 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
307 return ret;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
308 }
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
309
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
310 objects(s: any, p: any): Quad_Object[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
311 this.autoDeps.askedFor(s, p, null, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
312 const quads = this.graph.getQuads(s, p, null, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
313 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
314 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
315
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
316 subjects(p: any, o: any): Quad_Subject[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
317 this.autoDeps.askedFor(null, p, o, null);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
318 const quads = this.graph.getQuads(null, p, o, null);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
319 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
320 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
321
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
322 subjectStatements(s: Quad_Subject): Quad[] {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
323 this.autoDeps.askedFor(s, null, null, null);
2087
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
324 const quads = this.graph.getQuads(s, null, null, null);
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
325 return quads;
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
326 }
1b6e7016e3de rewrite state mgmt in live/
drewp@bigasterisk.com
parents: 2076
diff changeset
327
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
328 items(list: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
329 const out = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
330 let current = list;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
331 while (true) {
2130
90f99b3f2b18 items() was broken; needs more typechecking or tests
drewp@bigasterisk.com
parents: 2108
diff changeset
332 if (current.value === RDF + "nil") {
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
333 break;
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
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
336 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
337
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
338 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
339 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
340 if (firsts.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
341 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
342 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
343 out.push(firsts[0].object);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
344
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
345 if (rests.length !== 1) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
346 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
347 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
348 current = rests[0].object;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
349 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
350
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
351 return out;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
352 }
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
353
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents: 2071
diff changeset
354 contains(s: any, p: any, o: any): boolean {
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
355 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
356 // 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
357 // 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
358 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
359 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
360
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
361 nextNumberedResources(base: { id: any }, howMany: number) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
362 // base is NamedNode or string
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
363 // 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
364 // always return 'name0'.
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
365 if (base.id) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
366 base = base.id;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
367 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
368 const results = [];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
369
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
370 // @contains is really slow.
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
371 if (this.nextNumber == null) {
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
372 this.nextNumber = new Map();
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
373 }
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
374 let start = this.nextNumber.get(base);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
375 if (start === undefined) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
376 start = 0;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
377 }
1387
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
378
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
379 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
380 const uri = this.Uri(`${base}${serial}`);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
381 if (!this.contains(uri, null, null)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
382 results.push(uri);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
383 log("nextNumberedResources", `picked ${uri}`);
2259
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
384 this.nextNumber.set(base, serial + 1);
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
385 if (results.length >= howMany) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
386 return results;
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 }
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 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
391 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
392
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
393 nextNumberedResource(base: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
394 return this.nextNumberedResources(base, 1)[0];
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
395 }
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
396
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
397 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
398 this.autoDeps.askedFor(s, p, o, null);
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
399 const ctxs: N3.NamedNode[] = [];
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
400 for (let q of Array.from(this.graph.getQuads(s, p, o, null))) {
2296
6529768a96a2 stronger types
drewp@bigasterisk.com
parents: 2293
diff changeset
401 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
402 ctxs.push(q.graph);
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 return unique(ctxs);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
405 }
1792
a07bfbe1e9de make graph sortKey public
Drew Perttula <drewp@bigasterisk.com>
parents: 1789
diff changeset
406
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
407 sortKey(uri: N3.NamedNode) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
408 const parts = uri.value.split(/([0-9]+)/);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
409 const expanded = parts.map(function (p: string) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
410 const f = parseInt(p);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
411 if (isNaN(f)) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
412 return p;
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
413 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
414 return p.padStart(8, "0");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
415 });
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
416 return expanded.join("");
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
417 }
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
418
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
419 sortedUris(uris: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
420 return sortBy(uris, this.sortKey);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
421 }
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
422
2071
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
423 prettyLiteral(x: any) {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
424 if (typeof x === "number") {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
425 return this.LiteralRoundedFloat(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
426 } else {
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
427 return this.Literal(x);
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
428 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
429 }
56a9eaf5e882 rough ports from coffee to ts. untested
drewp@bigasterisk.com
parents: 1905
diff changeset
430 }