annotate light9/web/graph.coffee @ 1648:ffa3b81c6d95

some missing askedFor auditing Ignore-this: 581b102bbdaef0dc715e61430d639e01
author Drew Perttula <drewp@bigasterisk.com>
date Sat, 10 Jun 2017 10:23:48 +0000
parents 67347c027b2a
children e00492e1c0b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1346
2809a8b732f6 optimize graph lookup calls during zoom steps
Drew Perttula <drewp@bigasterisk.com>
parents: 1334
diff changeset
1 log = console.log
2809a8b732f6 optimize graph lookup calls during zoom steps
Drew Perttula <drewp@bigasterisk.com>
parents: 1334
diff changeset
2
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 # Patch is {addQuads: <quads>, delQuads: <quads>}
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 # <quads> is [{subject: s, ...}, ...]
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5
1362
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
6 # for mocha
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
7 if require?
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
8 `window = {}`
1375
ca5b10d7ecd1 switch N3 to an updated pull/61 version
Drew Perttula <drewp@bigasterisk.com>
parents: 1363
diff changeset
9 `N3 = require('./lib/N3.js-pull61/N3.js')`
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
10 `d3 = require('./lib/d3/build/d3.min.js')`
1362
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
11 `RdfDbClient = require('./rdfdbclient.js').RdfDbClient`
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
12 module.exports = window
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
13
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
14 RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
15
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
16 patchSizeSummary = (patch) ->
1350
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
17 '-' + patch.delQuads.length + ' +' + patch.addQuads.length
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
18
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
19 # (sloppily shared to rdfdbclient.coffee too)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
20 window.patchSizeSummary = patchSizeSummary
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
21
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
22 class Handler
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
23 # a function and the quad patterns it cared about
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
24 constructor: (@func, @label) ->
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
25 @patterns = [] # s,p,o,g quads that should trigger the next run
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
26 @innerHandlers = [] # Handlers requested while this one was running
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
27
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
28 class AutoDependencies
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
29 constructor: () ->
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
30 @handlers = new Handler(null) # tree of all known Handlers (at least those with non-empty patterns). Top node is not a handler.
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
31 @handlerStack = [@handlers] # currently running
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
32
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
33 runHandler: (func, label) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
34 # what if we have this func already? duplicate is safe?
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
35
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
36 if not label?
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
37 throw new Error("missing label")
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
38
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
39 h = new Handler(func, label)
1645
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
40 tailChildren = @handlerStack[@handlerStack.length - 1].innerHandlers
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
41 matchingLabel = _.filter(tailChildren, ((c) -> c.label == label)).length
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
42 # ohno, something depends on some handlers getting run twice :(
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
43 if matchingLabel < 2
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
44 tailChildren.push(h)
1537
b95b97177de6 console time logs
Drew Perttula <drewp@bigasterisk.com>
parents: 1534
diff changeset
45 console.time("handler #{label}")
1494
ea9e8f581eea WIP optimization for note point dragging
Drew Perttula <drewp@bigasterisk.com>
parents: 1415
diff changeset
46 @_rerunHandler(h, null)
1537
b95b97177de6 console time logs
Drew Perttula <drewp@bigasterisk.com>
parents: 1534
diff changeset
47 console.timeEnd("handler #{label}")
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
48 @_logHandlerTree()
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
49
1494
ea9e8f581eea WIP optimization for note point dragging
Drew Perttula <drewp@bigasterisk.com>
parents: 1415
diff changeset
50 _rerunHandler: (handler, patch) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
51 handler.patterns = []
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
52 @handlerStack.push(handler)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
53 try
1494
ea9e8f581eea WIP optimization for note point dragging
Drew Perttula <drewp@bigasterisk.com>
parents: 1415
diff changeset
54 handler.func(patch)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
55 catch e
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
56 log('error running handler: ', e)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
57 # assuming here it didn't get to do all its queries, we could
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
58 # add a *,*,*,* handler to call for sure the next time?
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
59 finally
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
60 #log('done. got: ', handler.patterns)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
61 @handlerStack.pop()
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
62 # handler might have no watches, in which case we could forget about it
1645
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
63
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
64 _logHandlerTree: ->
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
65 log('handler tree:')
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
66 prn = (h, depth) ->
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
67 indent = ''
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
68 for i in [0...depth]
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
69 indent += ' '
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
70 log("#{indent} \"#{h.label}\" #{h.patterns.length} pats")
1645
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
71 for c in h.innerHandlers
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
72 prn(c, depth + 1)
9c5e62615a63 don't let handlers pile up (workaround)
Drew Perttula <drewp@bigasterisk.com>
parents: 1643
diff changeset
73 prn(@handlers, 0)
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
74
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
75 _allPatchSubjs: (patch) ->
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
76
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
77 allPatchSubjs = []
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
78 for stmt in patch.addQuads
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
79 allPatchSubjs.push(stmt.subject)
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
80 for stmt in patch.delQuads
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
81 allPatchSubjs.push(stmt.subject)
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
82 allPatchSubjs = _.uniq(allPatchSubjs)
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
83 if _.contains(allPatchSubjs, null) or allPatchSubjs.length == 0
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
84 allPatchSubjs = null
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
85 log('allPatchSubjs', allPatchSubjs)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
86
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
87 _handlerIsAffected: (child, allPatchSubjs) ->
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
88 if allPatchSubjs == null
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
89 return true
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
90 if not child.patterns.length
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
91 return false
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
92
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
93 for stmt in child.patterns
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
94 if _.contains(allPatchSubjs, stmt[0])
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
95 return true
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
96
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
97 return false
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
98
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
99 graphChanged: (patch) ->
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
100 # SyncedGraph is telling us this patch just got applied to the graph.
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
101
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
102 allPatchSubjs = @_allPatchSubjs(patch)
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
103
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
104 rerunInners = (cur) =>
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
105 toRun = cur.innerHandlers.slice()
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
106 for child in toRun
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
107 match = @_handlerIsAffected(child, allPatchSubjs)
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
108 log('match', child.label, match)
1415
edc17fdcaaf1 big rewrite of adjuster update code. origin & p3 adjusters kind of work. adj layout slightly works
Drew Perttula <drewp@bigasterisk.com>
parents: 1387
diff changeset
109 #child.innerHandlers = [] # let all children get called again
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
110
1494
ea9e8f581eea WIP optimization for note point dragging
Drew Perttula <drewp@bigasterisk.com>
parents: 1415
diff changeset
111 @_rerunHandler(child, patch)
1415
edc17fdcaaf1 big rewrite of adjuster update code. origin & p3 adjusters kind of work. adj layout slightly works
Drew Perttula <drewp@bigasterisk.com>
parents: 1387
diff changeset
112 rerunInners(child)
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
113 rerunInners(@handlers)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
114
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
115 askedFor: (s, p, o, g) ->
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
116 # SyncedGraph is telling us someone did a query that depended on
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
117 # quads in the given pattern.
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
118 current = @handlerStack[@handlerStack.length - 1]
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
119 if current? and current != @handlers
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
120 current.patterns.push([s, p, o, g])
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
121 #log('push', s,p,o,g)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
122
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
123 class window.SyncedGraph
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
124 # Main graph object for a browser to use. Syncs both ways with
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
125 # rdfdb. Meant to hide the choice of RDF lib, so we can change it
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
126 # later.
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
127 #
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
128 # Note that _applyPatch is the only method to write to the graph, so
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
129 # it can fire subscriptions.
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
130
1355
9476e97b57fb status widget for rdfdb-synced-graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1354
diff changeset
131 constructor: (@patchSenderUrl, @prefixes, @setStatus) ->
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
132 # patchSenderUrl is the /syncedGraph path of an rdfdb server.
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
133 # prefixes can be used in Uri(curie) calls.
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
134 @_autoDeps = new AutoDependencies() # replaces GraphWatchers
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
135 @clearGraph()
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
136
1362
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
137 if @patchSenderUrl
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
138 @_client = new RdfDbClient(@patchSenderUrl, @_clearGraphOnNewConnection.bind(@),
1362
168262618f2d new test_js target for testing SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1361
diff changeset
139 @_applyPatch.bind(@), @setStatus)
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
140
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
141 clearGraph: ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
142 # just deletes the statements; watchers are unaffected.
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
143 if @graph?
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
144 @_applyPatch({addQuads: [], delQuads: @graph.find()})
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
145
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
146 # if we had a Store already, this lets N3.Store free all its indices/etc
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
147 @graph = N3.Store()
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
148 @_addPrefixes(@prefixes)
1517
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
149 @cachedFloatValues = new Map();
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
150
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
151 _clearGraphOnNewConnection: -> # must not send a patch to the server!
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
152 log('graph: clearGraphOnNewConnection')
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
153 @clearGraph()
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
154 log('graph: clearGraphOnNewConnection done')
1347
5c54a1f94050 browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents: 1346
diff changeset
155
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
156 _addPrefixes: (prefixes) ->
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
157 @graph.addPrefixes(prefixes)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
158
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
159 Uri: (curie) ->
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
160 N3.Util.expandPrefixedName(curie, @graph._prefixes)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
161
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
162 Literal: (jsValue) ->
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
163 N3.Util.createLiteral(jsValue)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
164
1350
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
165 LiteralRoundedFloat: (f) ->
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
166 N3.Util.createLiteral(d3.format(".3f")(f),
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
167 "http://www.w3.org/2001/XMLSchema#decimal")
36f58b2aa8ef browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents: 1347
diff changeset
168
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
169 toJs: (literal) ->
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
170 # incomplete
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
171 parseFloat(N3.Util.getLiteralValue(literal))
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
172
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
173 loadTrig: (trig, cb) -> # for debugging
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
174 patch = {delQuads: [], addQuads: []}
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
175 parser = N3.Parser()
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
176 parser.parse trig, (error, quad, prefixes) =>
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
177 if (quad)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
178 patch.addQuads.push(quad)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
179 else
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
180 @_applyPatch(patch)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
181 @_addPrefixes(prefixes)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
182 cb() if cb
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
183
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
184 quads: () -> # for debugging
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
185 [q.subject, q.predicate, q.object, q.graph] for q in @graph.find()
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
186
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
187 applyAndSendPatch: (patch) ->
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
188 console.time('applyAndSendPatch')
1383
73762e14ee98 typecheck on applyPatch
Drew Perttula <drewp@bigasterisk.com>
parents: 1375
diff changeset
189 if !Array.isArray(patch.addQuads) || !Array.isArray(patch.delQuads)
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
190 throw new Error("corrupt patch: #{JSON.stringify(patch)}")
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
191
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
192 for qs in [patch.addQuads, patch.delQuads]
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
193 for q in qs
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
194 if not q.graph?
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
195 throw new Error("corrupt patch: #{JSON.stringify(q)}")
1643
77eaccba654e try to fix timeline sending empty ctx to rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1624
diff changeset
196
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
197 @_applyPatch(patch)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
198 @_client.sendPatch(patch) if @_client
1647
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
199 console.timeEnd('applyAndSendPatch')
67347c027b2a hopefully harmmless steps towards making the handler tree right
Drew Perttula <drewp@bigasterisk.com>
parents: 1646
diff changeset
200
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
201 _applyPatch: (patch) ->
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
202 # In most cases you want applyAndSendPatch.
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
203 #
1354
59eab70254fa refactor the rdfdb networking out of (js) SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1352
diff changeset
204 # This is the only method that writes to @graph!
1517
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
205 @cachedFloatValues.clear()
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
206 for quad in patch.delQuads
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
207 @graph.removeTriple(quad)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
208 for quad in patch.addQuads
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
209 @graph.addTriple(quad)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
210 #log('applied patch locally', patchSizeSummary(patch))
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
211 @_autoDeps.graphChanged(patch)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
212
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
213 getObjectPatch: (s, p, newObject, g) ->
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
214 # make a patch which removes existing values for (s,p,*,c) and
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
215 # adds (s,p,newObject,c). Values in other graphs are not affected.
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
216 existing = @graph.findByIRI(s, p, null, g)
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
217 return {
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
218 delQuads: existing,
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
219 addQuads: [{subject: s, predicate: p, object: newObject, graph: g}]
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
220 }
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
221
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
222 patchObject: (s, p, newObject, g) ->
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
223 @applyAndSendPatch(@getObjectPatch(s, p, newObject, g))
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
224
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
225 runHandler: (func, label) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
226 # runs your func once, tracking graph calls. if a future patch
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
227 # matches what you queried, we runHandler your func again (and
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
228 # forget your queries from the first time).
1624
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
229
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
230 # helps with memleak? not sure yet. The point was if two matching
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
231 # labels get puushed on, we should run only one. So maybe
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
232 # appending a serial number is backwards.
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
233 @serial = 1 if not @serial
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
234 @serial += 1
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
235 #label = label + @serial
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
236
1386
29a1382de5c8 introduce autodep handler tree; only rerun highest-level affected handler and let it rerun its children
Drew Perttula <drewp@bigasterisk.com>
parents: 1383
diff changeset
237 @_autoDeps.runHandler(func, label)
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
238
1356
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
239 _singleValue: (s, p) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
240 @_autoDeps.askedFor(s, p, null, null)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
241 quads = @graph.findByIRI(s, p)
1534
d4de13e83b7f graph.js don't error if a statement is in multiple graphs
Drew Perttula <drewp@bigasterisk.com>
parents: 1517
diff changeset
242 objs = new Set(q.object for q in quads)
d4de13e83b7f graph.js don't error if a statement is in multiple graphs
Drew Perttula <drewp@bigasterisk.com>
parents: 1517
diff changeset
243
d4de13e83b7f graph.js don't error if a statement is in multiple graphs
Drew Perttula <drewp@bigasterisk.com>
parents: 1517
diff changeset
244 switch objs.size
1356
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
245 when 0
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
246 throw new Error("no value for "+s+" "+p)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
247 when 1
1534
d4de13e83b7f graph.js don't error if a statement is in multiple graphs
Drew Perttula <drewp@bigasterisk.com>
parents: 1517
diff changeset
248 obj = objs.values().next().value
1358
6ea2e1aa5070 uriValue support
Drew Perttula <drewp@bigasterisk.com>
parents: 1357
diff changeset
249 return obj
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
250 else
1534
d4de13e83b7f graph.js don't error if a statement is in multiple graphs
Drew Perttula <drewp@bigasterisk.com>
parents: 1517
diff changeset
251 throw new Error("too many different values: " + JSON.stringify(quads))
1356
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
252
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
253 floatValue: (s, p) ->
1517
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
254 key = s + '|' + p
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
255 hit = @cachedFloatValues.get(key)
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
256 return hit if hit != undefined
1624
4a751aaaee52 big timeline rewrites. hopefully it's faster and less leaky
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
257 #log('float miss', s, p)
1517
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
258
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
259 ret = parseFloat(N3.Util.getLiteralValue(@_singleValue(s, p)))
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
260 @cachedFloatValues.set(key, ret)
3bb58b74c9c1 timeline: add cache of floats between graph updates for smoother redraws
Drew Perttula <drewp@bigasterisk.com>
parents: 1494
diff changeset
261 return ret
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
262
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
263 stringValue: (s, p) ->
1358
6ea2e1aa5070 uriValue support
Drew Perttula <drewp@bigasterisk.com>
parents: 1357
diff changeset
264 N3.Util.getLiteralValue(@_singleValue(s, p))
1356
16aa26b7d685 timeline audio loads the current song img
Drew Perttula <drewp@bigasterisk.com>
parents: 1355
diff changeset
265
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
266 uriValue: (s, p) ->
1358
6ea2e1aa5070 uriValue support
Drew Perttula <drewp@bigasterisk.com>
parents: 1357
diff changeset
267 @_singleValue(s, p)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
268
1595
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
269 labelOrTail: (uri) ->
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
270 try
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
271 @graph.stringValue(uri, @graph.Uri('rdfs:label'))
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
272 catch
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
273 words = uri.split('/')
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
274 words[words.length-1]
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1591
diff changeset
275
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
276 objects: (s, p) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
277 @_autoDeps.askedFor(s, p, null, null)
1358
6ea2e1aa5070 uriValue support
Drew Perttula <drewp@bigasterisk.com>
parents: 1357
diff changeset
278 quads = @graph.findByIRI(s, p)
6ea2e1aa5070 uriValue support
Drew Perttula <drewp@bigasterisk.com>
parents: 1357
diff changeset
279 return (q.object for q in quads)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
280
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
281 subjects: (p, o) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
282 @_autoDeps.askedFor(null, p, o, null)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
283 quads = @graph.findByIRI(null, p, o)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
284 return (q.subject for q in quads)
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
285
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
286 items: (list) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
287 out = []
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
288 current = list
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
289 while true
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
290 if current == RDF + 'nil'
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
291 break
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
292
1648
ffa3b81c6d95 some missing askedFor auditing
Drew Perttula <drewp@bigasterisk.com>
parents: 1647
diff changeset
293 @_autoDeps.askedFor(current, null, null, null) # a little loose
ffa3b81c6d95 some missing askedFor auditing
Drew Perttula <drewp@bigasterisk.com>
parents: 1647
diff changeset
294
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
295 firsts = @graph.findByIRI(current, RDF + 'first', null)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
296 rests = @graph.findByIRI(current, RDF + 'rest', null)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
297 if firsts.length != 1
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
298 throw new Error("list node #{current} has #{firsts.length} rdf:first edges")
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
299 out.push(firsts[0].object)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
300
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
301 if rests.length != 1
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
302 throw new Error("list node #{current} has #{rests.length} rdf:rest edges")
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
303 current = rests[0].object
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
304
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
305 return out
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
306
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
307 contains: (s, p, o) ->
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
308 @_autoDeps.askedFor(s, p, o, null)
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
309 return @graph.findByIRI(s, p, o).length > 0
1317
4c6d88aa9e26 enough element and rdf support to drag one adjuster and see its value change
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
310
1387
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
311 nextNumberedResources: (base, howMany) ->
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
312 results = []
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
313 # we could cache [base,lastSerial]
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
314 for serial in [0..1000]
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
315 uri = @Uri("#{base}#{serial}")
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
316 if not @contains(uri, null, null)
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
317 results.push(uri)
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
318 if results.length >= howMany
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
319 return results
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
320 throw new Error("can't make sequential uri with base #{base}")
1363
233b81d9bd9d simple first version of SyncedGraph.runHandler
Drew Perttula <drewp@bigasterisk.com>
parents: 1362
diff changeset
321
1387
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
322 nextNumberedResource: (base) ->
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
323 @nextNumberedResources(base, 1)[0]
26fabcf3a0a8 relocate nextNumberedResource to SyncedGraph
Drew Perttula <drewp@bigasterisk.com>
parents: 1386
diff changeset
324
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
325 contextsWithPattern: (s, p, o) ->
1648
ffa3b81c6d95 some missing askedFor auditing
Drew Perttula <drewp@bigasterisk.com>
parents: 1647
diff changeset
326 @_autoDeps.askedFor(s, p, o, null)
1591
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
327 ctxs = []
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
328 for q in @graph.find(s, p, o)
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
329 ctxs.push(q.graph)
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
330 return _.unique(ctxs)
2713d2f7a0fc resource-display can have a rename button for editing rdfs:label
Drew Perttula <drewp@bigasterisk.com>
parents: 1537
diff changeset
331