1 package org.catacomb.numeric.data;
2
3 import org.catacomb.datalish.RunDataBlock;
4 import org.catacomb.datalish.RunDataStore;
5 import org.catacomb.datalish.SpriteData;
6 import org.catacomb.datalish.SpriteStore;
7 import org.catacomb.interlish.structure.*;
8 import org.catacomb.report.E;
9
10
11 import java.util.HashMap;
12 import java.util.ArrayList;
13
14 public class NumDataStore implements RunDataStore, Named, TreeRoot,
15 TreeChangeReporter, Parent {
16
17 String name;
18
19
20 ArrayList<Object> children;
21
22 HashMap<String, BlockStack> stackHM;
23
24 HashMap<String, DVector> vecHM;
25 HashMap<String, DVector[]> vecArrayHM;
26
27 ArrayList<DVector> dVectors;
28 ArrayList<BlockStack> blockStacks;
29
30 ArrayList<EventSequence> evtSequences;
31 HashMap <String, EventSequence> evtSeqHM;
32
33
34 TreeChangeReporter tcReporter;
35
36 ArrayList<DataWatcher> dataWatchers;
37
38 NDSAccessor ndsAccessor;
39
40
41 SpriteStore spriteStore;
42
43
44 public double time;
45
46
47 int nrep = 0;
48
49 public NumDataStore(String s) {
50 name = s;
51 stackHM = new HashMap<String, BlockStack>();
52 vecHM = new HashMap<String, DVector>();
53 vecArrayHM = new HashMap<String, DVector[]>();
54
55 dVectors = new ArrayList<DVector>();
56 blockStacks = new ArrayList<BlockStack>();
57
58 evtSequences = new ArrayList<EventSequence>();
59 evtSeqHM = new HashMap<String, EventSequence>();
60
61 children = new ArrayList<Object>();
62 dataWatchers = new ArrayList<DataWatcher>();
63
64 spriteStore = new SpriteStore();
65
66 ndsAccessor = new NDSAccessor(this);
67 }
68
69 public String getName() {
70 return name;
71 }
72
73 public String toString() {
74 return name;
75 }
76
77 public void setTime(double t) {
78 time = t;
79 }
80
81 public double getTime() {
82 return time;
83 }
84
85 public SpriteStore getSpriteStore() {
86 return spriteStore;
87 }
88
89 public ArrayList<DVector> getDVectors() {
90 return dVectors;
91 }
92
93 public ArrayList<BlockStack> getBlockStacks() {
94 return blockStacks;
95 }
96
97 public ArrayList<EventSequence> getEventSequences() {
98 return evtSequences;
99 }
100
101
102
103 public ArrayList<NumVector> getPossibleAbscissae() {
104 ArrayList<NumVector> andi = new ArrayList<NumVector>();
105 for (Object ch : children) {
106 if (ch instanceof BlockStack) {
107 NumVector numv = ((BlockStack)ch).getFirstArraySlice();
108 if (numv != null) {
109 andi.add(numv);
110 }
111 }
112 }
113 if (dVectors != null && dVectors.size() > 0) {
114 andi.add(dVectors.get(0));
115 }
116 return andi;
117 }
118
119
120
121 public void add(NumDataItem ndi) {
122 if (ndi instanceof EventSequence) {
123 evtSequences.add((EventSequence)ndi);
124 children.add(ndi);
125 evtSeqHM.put(ndi.getName(), (EventSequence)ndi);
126 reportDataStructureChange();
127 reportNewChild();
128 } else {
129 E.error("cant add " + ndi);
130 }
131
132 }
133
134 public void addSprite(SpriteData sd) {
135 spriteStore.add(sd);
136 }
137
138
139
140 public ArrayList<AnimSlice> getMovies() {
141 ArrayList<AnimSlice> msls = new ArrayList<AnimSlice>();
142 for (Object ch : children) {
143 if (ch instanceof BlockStack) {
144 msls.addAll(((BlockStack)ch).getMovies());
145 }
146 }
147 return msls;
148 }
149
150
151
152 public void addDataWatcher(DataWatcher dw) {
153
154 ArrayList<DataWatcher> newdw = new ArrayList<DataWatcher>();
155 newdw.addAll(dataWatchers);
156 newdw.add(dw);
157 dataWatchers = newdw;
158 }
159
160
161
162 public void removeDataWatcher(DataWatcher dw) {
163 ArrayList<DataWatcher> newdw = new ArrayList<DataWatcher>();
164 for (DataWatcher odw : dataWatchers) {
165 if (odw == dw) {
166
167 } else {
168 newdw.add(odw);
169 }
170 }
171 dataWatchers = newdw;
172 }
173
174
175
176 public void stack(RunDataBlock se) {
177 String s = se.getClass().getName();
178 s = s.substring(s.lastIndexOf(".")+1, s.length());
179 stack(s, se);
180 }
181
182
183
184 public void addEvent(String snm, double t, int ich) {
185 if (evtSeqHM.containsKey(snm)) {
186 evtSeqHM.get(snm).addEvent(t, ich);
187 } else {
188 EventSequence es = new EventSequence(snm);
189 evtSeqHM.put(snm, es);
190 evtSequences.add(es);
191 es.addEvent(t, ich);
192 reportDataStructureChange();
193 }
194
195 }
196
197
198
199
200 public void stack(String snm, double val) {
201 if (vecHM.containsKey(snm)) {
202 vecHM.get(snm).add(val);
203
204 } else {
205 DVector dvec = new DVector(this, snm);
206 dVectors.add(dvec);
207 dvec.add(val);
208 vecHM.put(snm, dvec);
209 children.add(dvec);
210 reportNewChild();
211 reportDataStructureChange();
212 }
213 reportDataValueChange();
214 }
215
216
217 public void stack(String snm, double[] val) {
218 stack(snm, null, val);
219 }
220
221
222 public void stack(String snm, String[] labsin, double[] val) {
223 String[] labs = labsin;
224 DVector[] dva = null;
225 if (vecArrayHM.containsKey(snm)) {
226 dva = vecArrayHM.get(snm);
227 } else {
228 if (labs == null) {
229 labs = new String[val.length];
230 for (int i = 0; i < val.length; i++) {
231 labs[i] = "v_" + i;
232 }
233 }
234 dva = new DVector[labs.length];
235 vecArrayHM.put(snm, dva);
236 for (int i = 0; i < labs.length; i++) {
237 dva[i] = new DVector(this, labs[i]);
238 children.add(dva[i]);
239 vecHM.put(labs[i], dva[i]);
240 dVectors.add(dva[i]);
241 }
242
243 E.info("stacked array " + labs.length);
244 E.dump(labs);
245 reportNewChild();
246 reportDataStructureChange();
247 }
248
249 for (int i = 0; i < dva.length; i++) {
250 dva[i].add(val[i]);
251 }
252 reportDataValueChange();
253 }
254
255
256
257
258 public void stack(String snm, RunDataBlock str) {
259 if (str instanceof Timestampable) {
260 ((Timestampable)str).stampTime(time);
261 }
262
263 if (stackHM.containsKey(snm)) {
264 stackHM.get(snm).addToStack(str);
265
266 } else {
267 BlockStack ss = new BlockStack(this, snm);
268 stackHM.put(snm, ss);
269 blockStacks.add(ss);
270
271 children.add(ss);
272
273 ss.setTreeChangeReporter(this);
274
275 ss.addToStack(str);
276
277 reportNewChild();
278 reportDataStructureChange();
279 }
280
281 reportDataValueChange();
282
283 }
284
285
286 private void reportNewChild() {
287 if (tcReporter != null) {
288 tcReporter.nodeAddedUnder(this, null);
289 } else {
290
291 }
292
293 }
294
295
296 public NumDataItem getNumDataItem(String s) {
297 return ndsAccessor.getNumDataItem(s);
298 }
299
300 public ArrayList<NumDataItem> getOrdinates(NumVector numV) {
301 return ndsAccessor.getOrdinates(numV);
302 }
303
304 public NumDataItem getSibling(NumVector numV, String snm) {
305 return ndsAccessor.getSibling(numV, snm);
306 }
307
308 public void reportDataValueChange() {
309 for (DataWatcher dw : dataWatchers) {
310 dw.dataValueChanged(this, null);
311 }
312 }
313
314 public void reportDataStructureChange() {
315 for (DataWatcher dw : dataWatchers) {
316 dw.dataStructureChanged(this, null);
317 }
318 }
319
320 public void reportCompletion() {
321 for (DataWatcher dw : dataWatchers) {
322 dw.dataComplete(this);
323 }
324 }
325
326
327
328
329 public void nodeAddedUnder(TreeNode parent, TreeNode child) {
330 if (tcReporter != null) {
331 tcReporter.nodeAddedUnder(parent, child);
332 } else {
333 E.error("need tcReporter");
334 }
335 }
336
337
338 public void nodeRemoved(TreeNode parent, TreeNode child) {
339 if (tcReporter != null) {
340 tcReporter.nodeRemoved(parent, child);
341 }
342 }
343
344
345
346
347 public Object getParent() {
348 return null;
349 }
350
351 public TreeNode getRoot() {
352 return this;
353 }
354
355 public int getRootPolicy() {
356 return Tree.AUTO_ROOT;
357 }
358
359 public void setTreeChangeReporter(TreeChangeReporter tcr) {
360 tcReporter = tcr;
361 }
362
363 public int getChildCount() {
364 return children.size();
365 }
366
367 public Object getChild(int index) {
368 return children.get(index);
369 }
370
371 public int getIndexOfChild(Object child) {
372 return children.indexOf(child);
373 }
374
375 public boolean isLeaf() {
376 return false;
377 }
378
379
380
381
382 public boolean hasChild(String s) {
383 return (stackHM.containsKey(s) ||
384 vecHM.containsKey(s) ||
385 evtSeqHM.containsKey(s));
386 }
387
388 public Object getChild(String s) {
389 Object ret = null;
390 if (stackHM.containsKey(s)) {
391 ret = stackHM.get(s);
392 } else if (vecHM.containsKey(s)) {
393 ret = vecHM.get(s);
394
395 } else if (evtSeqHM.containsKey(s)) {
396 ret = evtSeqHM.get(s);
397 }
398 return ret;
399 }
400
401 public Object[] getObjectPath(String s, boolean b) {
402 return (ndsAccessor.getObjectPath(s)).toArray(new Object[0]);
403 }
404
405
406
407
408
409 public void dumpChildren() {
410 for (Object obj : children) {
411 E.info("store child: " + obj);
412 }
413 }
414
415
416
417 public String getPath(Object obj) {
418 String ret = null;
419 if (obj instanceof Named) {
420 ret = ((Named)obj).getName();
421 }
422
423 if (obj instanceof TreeNode) {
424 Object par = ((TreeNode)obj).getParent();
425 if (par == null) {
426
427 } else {
428 String pp = getPath(par);
429 if (pp != null) {
430 ret = pp + "/" + ret;
431 }
432 }
433 } else {
434
435 }
436 return ret;
437 }
438
439 public void spriteAt(String snm, double t, double[] pxy, double[] hxy,
440 double[] wx, double[] wy) {
441 stack(snm, new SimpleSpriteBlock(t, pxy, hxy, wx, wy));
442 }
443
444
445 }