Mercurial > code > home > repos > light9
annotate web/live/Effect.ts @ 2427:cc69faa87c27
tear up and rewrite ascoltami to emit player state into the graph. web ui works but displays nothing but songs
author | drewp@bigasterisk.com |
---|---|
date | Sat, 25 May 2024 15:44:11 -0700 |
parents | 4556eebe5d73 |
children |
rev | line source |
---|---|
2087 | 1 import debug from "debug"; |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
2 import { Literal, NamedNode, Quad, Quad_Object, Quad_Predicate, Quad_Subject, Term } from "n3"; |
2087 | 3 import { some } from "underscore"; |
2372
06bf6dae8e64
reorg tools into light9/web/ and a single vite instance
drewp@bigasterisk.com
parents:
2302
diff
changeset
|
4 import { Patch } from "../patch"; |
06bf6dae8e64
reorg tools into light9/web/ and a single vite instance
drewp@bigasterisk.com
parents:
2302
diff
changeset
|
5 import { SyncedGraph } from "../SyncedGraph"; |
06bf6dae8e64
reorg tools into light9/web/ and a single vite instance
drewp@bigasterisk.com
parents:
2302
diff
changeset
|
6 import { shortShow } from "../show_specific"; |
2246
5c269c03863d
WIP device settings page can now load and save ok. Omitted GraphToControls for now
drewp@bigasterisk.com
parents:
2238
diff
changeset
|
7 import { SubEvent } from "sub-events"; |
2087 | 8 |
2238 | 9 // todo: Align these names with newtypes.py, which uses HexColor and VTUnion. |
2087 | 10 type Color = string; |
11 export type ControlValue = number | Color | NamedNode; | |
12 | |
13 const log = debug("effect"); | |
14 | |
15 function isUri(x: Term | number | string): x is NamedNode { | |
16 return typeof x == "object" && x.termType == "NamedNode"; | |
17 } | |
18 | |
2246
5c269c03863d
WIP device settings page can now load and save ok. Omitted GraphToControls for now
drewp@bigasterisk.com
parents:
2238
diff
changeset
|
19 // todo: eliminate this. address the scaling when we actually scale |
5c269c03863d
WIP device settings page can now load and save ok. Omitted GraphToControls for now
drewp@bigasterisk.com
parents:
2238
diff
changeset
|
20 // stuff, instead of making a mess of every setting |
2087 | 21 function valuePred(graph: SyncedGraph, attr: NamedNode): NamedNode { |
22 const U = graph.U(); | |
23 const scaledAttributeTypes = [U(":color"), U(":brightness"), U(":uv")]; | |
24 if (some(scaledAttributeTypes, (x: NamedNode) => attr.equals(x))) { | |
2246
5c269c03863d
WIP device settings page can now load and save ok. Omitted GraphToControls for now
drewp@bigasterisk.com
parents:
2238
diff
changeset
|
25 return U(":value"); |
2087 | 26 } else { |
27 return U(":value"); | |
28 } | |
29 } | |
30 | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
31 // also see resourcedisplay's version of this |
2290 | 32 function effContext(graph: SyncedGraph, uri: NamedNode): NamedNode { |
33 return graph.Uri(uri.value.replace("light9.bigasterisk.com/effect", `light9.bigasterisk.com/show/${shortShow}/effect`)); | |
34 } | |
35 | |
36 export function newEffect(graph: SyncedGraph): NamedNode { | |
37 // wrong- this should be our editor's scratch effect, promoted to a | |
38 // real one when you name it. | |
39 const uri = graph.nextNumberedResource(graph.Uri("http://light9.bigasterisk.com/effect/effect")); | |
40 | |
41 const effect = new Effect(graph, uri); | |
42 const U = graph.U(); | |
43 const ctx = effContext(graph, uri); | |
44 const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => graph.Quad(s, p, o, ctx); | |
45 | |
46 const addQuads = [ | |
47 quad(uri, U("rdf:type"), U(":Effect")), | |
48 quad(uri, U("rdfs:label"), graph.Literal(uri.value.replace(/.*\//, ""))), | |
49 quad(uri, U(":publishAttr"), U(":strength")), | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
50 quad(uri, U(":effectFunction"), U(":effectFunction/scale")), |
2290 | 51 ]; |
52 const patch = new Patch([], addQuads); | |
53 log("init new effect", patch); | |
54 graph.applyAndSendPatch(patch); | |
55 | |
56 return effect.uri; | |
57 } | |
58 | |
2087 | 59 // effect settings data; r/w sync with the graph |
60 export class Effect { | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
61 // :effect1 a Effect; :setting ?eset . ?eset :effectAttr :deviceSettings; :value ?dset . ?dset :device .. |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
62 private eset?: NamedNode; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
63 private dsettings: Array<{ dset: NamedNode; device: NamedNode; deviceAttr: NamedNode; value: ControlValue }> = []; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
64 |
2248 | 65 private ctxForEffect: NamedNode; |
66 settingsChanged: SubEvent<void> = new SubEvent(); | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
67 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
68 constructor(public graph: SyncedGraph, public uri: NamedNode) { |
2290 | 69 this.ctxForEffect = effContext(this.graph, this.uri); |
2087 | 70 graph.runHandler(this.rebuildSettingsFromGraph.bind(this), `effect sync ${uri.value}`); |
71 } | |
72 | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
73 private getExistingEset(): NamedNode | null { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
74 const U = this.graph.U(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
75 for (let eset of this.graph.objects(this.uri, U(":setting"))) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
76 if (this.graph.uriValue(eset as Quad_Subject, U(":effectAttr")).equals(U(":deviceSettings"))) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
77 return eset as NamedNode; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
78 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
79 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
80 return null; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
81 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
82 private getExistingEsetValueNode(): NamedNode | null { |
2087 | 83 const U = this.graph.U(); |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
84 const eset = this.getExistingEset(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
85 if (eset === null) return null; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
86 try { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
87 return this.graph.uriValue(eset, U(":value")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
88 } catch (e) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
89 return null; |
2089
bbd6816d9e9e
big optimization on effect editing. 50% time still in rebuildSettingsFromGraph though, and the rest is in setLabel
drewp@bigasterisk.com
parents:
2087
diff
changeset
|
90 } |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
91 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
92 private patchForANewEset(): { p: Patch; eset: NamedNode } { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
93 const U = this.graph.U(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
94 const eset = this.graph.nextNumberedResource(U(":e_set")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
95 return { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
96 eset: eset, |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
97 p: new Patch( |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
98 [], |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
99 [ |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
100 // |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
101 new Quad(this.uri, U(":setting"), eset, this.ctxForEffect), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
102 new Quad(eset, U(":effectAttr"), U(":deviceSettings"), this.ctxForEffect), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
103 ] |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
104 ), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
105 }; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
106 } |
2089
bbd6816d9e9e
big optimization on effect editing. 50% time still in rebuildSettingsFromGraph though, and the rest is in setLabel
drewp@bigasterisk.com
parents:
2087
diff
changeset
|
107 |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
108 private rebuildSettingsFromGraph(patch?: Patch) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
109 const U = this.graph.U(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
110 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
111 log("syncFromGraph", this.uri); |
2087 | 112 |
2091
9324fc8285ad
Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents:
2089
diff
changeset
|
113 // this repeats work- it gathers all settings when really some values changed (and we might even know about them). maybe push the value-fetching into a secnod phase of the run, and have the 1st phase drop out early |
2087 | 114 const newSettings = []; |
115 | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
116 const deviceSettingsNode = this.getExistingEsetValueNode(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
117 if (deviceSettingsNode !== null) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
118 for (let dset of Array.from(this.graph.objects(deviceSettingsNode, U(":setting"))) as NamedNode[]) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
119 // // log(` setting ${setting.value}`); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
120 // if (!isUri(dset)) throw new Error(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
121 let value: ControlValue; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
122 const device = this.graph.uriValue(dset, U(":device")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
123 const deviceAttr = this.graph.uriValue(dset, U(":deviceAttr")); |
2087 | 124 |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
125 const pred = valuePred(this.graph, deviceAttr); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
126 try { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
127 value = this.graph.uriValue(dset, pred); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
128 if (!(value as NamedNode).id.match(/^http/)) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
129 throw new Error("not uri"); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
130 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
131 } catch (error) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
132 try { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
133 value = this.graph.floatValue(dset, pred); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
134 } catch (error1) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
135 value = this.graph.stringValue(dset, pred); // this may find multi values and throw |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
136 } |
2087 | 137 } |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
138 // log(`change: graph contains ${deviceAttr.value} ${value}`); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
139 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
140 newSettings.push({ dset, device, deviceAttr, value }); |
2087 | 141 } |
142 } | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
143 this.dsettings = newSettings; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
144 log(`settings is rebuilt to length ${this.dsettings.length}`); |
2248 | 145 this.settingsChanged.emit(); // maybe one emitter per dev+attr? |
2246
5c269c03863d
WIP device settings page can now load and save ok. Omitted GraphToControls for now
drewp@bigasterisk.com
parents:
2238
diff
changeset
|
146 // this.onValuesChanged(); |
2087 | 147 } |
2089
bbd6816d9e9e
big optimization on effect editing. 50% time still in rebuildSettingsFromGraph though, and the rest is in setLabel
drewp@bigasterisk.com
parents:
2087
diff
changeset
|
148 |
2087 | 149 currentValue(device: NamedNode, deviceAttr: NamedNode): ControlValue | null { |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
150 for (let s of this.dsettings) { |
2087 | 151 if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) { |
152 return s.value; | |
153 } | |
154 } | |
155 return null; | |
156 } | |
2089
bbd6816d9e9e
big optimization on effect editing. 50% time still in rebuildSettingsFromGraph though, and the rest is in setLabel
drewp@bigasterisk.com
parents:
2087
diff
changeset
|
157 |
2087 | 158 // change this object now, but return the patch to be applied to the graph so it can be coalesced. |
159 edit(device: NamedNode, deviceAttr: NamedNode, newValue: ControlValue | null): Patch { | |
160 log(`edit: value=${newValue}`); | |
161 let existingSetting: NamedNode | null = null; | |
2258 | 162 let result = new Patch([], []); |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
163 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
164 for (let s of this.dsettings) { |
2087 | 165 if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) { |
166 if (existingSetting !== null) { | |
2091
9324fc8285ad
Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents:
2089
diff
changeset
|
167 // this is corrupt. There was only supposed to be one setting per (dev,attr) pair. But we can fix it because we're going to update existingSetting to the user's requested value. |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
168 log(`${this.uri.value} had two settings for ${device.value} - ${deviceAttr.value} - deleting ${s.dset}`); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
169 result = result.update(this.removeEffectSetting(s.dset)); |
2087 | 170 } |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
171 existingSetting = s.dset; |
2087 | 172 } |
173 } | |
174 | |
175 if (newValue !== null && this.shouldBeStored(deviceAttr, newValue)) { | |
176 if (existingSetting === null) { | |
2258 | 177 result = result.update(this.addEffectSetting(device, deviceAttr, newValue)); |
2087 | 178 } else { |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
179 result = result.update(this.patchExistingDevSetting(existingSetting, deviceAttr, newValue)); |
2087 | 180 } |
181 } else { | |
182 if (existingSetting !== null) { | |
2258 | 183 result = result.update(this.removeEffectSetting(existingSetting)); |
2087 | 184 } |
185 } | |
2091
9324fc8285ad
Effect repairs duplicate :settings edges when it finds them
drewp@bigasterisk.com
parents:
2089
diff
changeset
|
186 return result; |
2087 | 187 } |
188 | |
189 shouldBeStored(deviceAttr: NamedNode, value: ControlValue | null): boolean { | |
190 // this is a bug for zoom=0, since collector will default it to | |
191 // stick at the last setting if we don't explicitly send the | |
192 // 0. rx/ry similar though not the exact same deal because of | |
193 // their remap. | |
194 return value != null && value !== 0 && value !== "#000000"; | |
195 } | |
196 | |
2259 | 197 private addEffectSetting(device: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch { |
2087 | 198 log(" _addEffectSetting", deviceAttr.value, value); |
199 const U = (x: string) => this.graph.Uri(x); | |
2102
9c2e1b5c16e9
speed: don't redo uri string replace all the time
drewp@bigasterisk.com
parents:
2091
diff
changeset
|
200 const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect); |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
201 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
202 let patch = new Patch([], []); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
203 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
204 let eset = this.getExistingEset(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
205 if (eset === null) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
206 const ret = this.patchForANewEset(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
207 patch = patch.update(ret.p); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
208 eset = ret.eset; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
209 } |
2087 | 210 |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
211 let dsValue; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
212 try { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
213 dsValue = this.graph.uriValue(eset, U(":value")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
214 } catch (e) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
215 dsValue = this.graph.nextNumberedResource(U(":ds_val")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
216 patch = patch.update(new Patch([], [quad(eset, U(":value"), dsValue)])); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
217 } |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
218 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
219 const dset = this.graph.nextNumberedResource(this.uri.value + "_set"); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
220 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
221 patch = patch.update( |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
222 new Patch( |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
223 [], |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
224 [ |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
225 quad(dsValue, U(":setting"), dset), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
226 quad(dset, U(":device"), device), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
227 quad(dset, U(":deviceAttr"), deviceAttr), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
228 quad(dset, valuePred(this.graph, deviceAttr), this.nodeForValue(value)), |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
229 ] |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
230 ) |
2258 | 231 ); |
2087 | 232 log(" save", patch); |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
233 this.dsettings.push({ dset, device, deviceAttr, value }); |
2087 | 234 return patch; |
235 } | |
236 | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
237 private patchExistingDevSetting(devSetting: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
238 log(" patch existing", devSetting.value); |
2087 | 239 return this.graph.getObjectPatch( |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
240 devSetting, // |
2087 | 241 valuePred(this.graph, deviceAttr), |
2259 | 242 this.nodeForValue(value), |
2102
9c2e1b5c16e9
speed: don't redo uri string replace all the time
drewp@bigasterisk.com
parents:
2091
diff
changeset
|
243 this.ctxForEffect |
2087 | 244 ); |
245 } | |
246 | |
2259 | 247 private removeEffectSetting(effectSetting: NamedNode): Patch { |
2087 | 248 const U = (x: string) => this.graph.Uri(x); |
249 log(" _removeEffectSetting", effectSetting.value); | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
250 |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
251 const eset = this.getExistingEset(); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
252 if (eset === null) throw "unexpected"; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
253 const dsValue = this.graph.uriValue(eset, U(":value")); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
254 if (dsValue === null) throw "unexpected"; |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
255 const toDel = [this.graph.Quad(dsValue, U(":setting"), effectSetting, this.ctxForEffect)]; |
2087 | 256 for (let q of this.graph.subjectStatements(effectSetting)) { |
257 toDel.push(q); | |
258 } | |
2258 | 259 return new Patch(toDel, []); |
2087 | 260 } |
261 | |
2302
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
262 clearAllSettings() { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
263 for (let s of this.dsettings) { |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
264 this.graph.applyAndSendPatch(this.removeEffectSetting(s.dset)); |
a6b611f32cce
rewrite Effect.ts to operate on new effect graphs
drewp@bigasterisk.com
parents:
2290
diff
changeset
|
265 } |
2290 | 266 } |
267 | |
2259 | 268 private nodeForValue(value: ControlValue): NamedNode | Literal { |
2087 | 269 if (value === null) { |
270 throw new Error("no value"); | |
271 } | |
272 if (isUri(value)) { | |
273 return value; | |
274 } | |
275 return this.graph.prettyLiteral(value); | |
276 } | |
277 } |