1 package org.catacomb.numeric.data;
2
3 import org.catacomb.report.E;
4
5 import java.util.HashMap;
6
7
8 public class DataSlice {
9
10
11 String item = null;
12
13 String func = null;
14 String slice = null;
15
16
17 DataSlice subslice;
18 DataSlice[] subsliceArray;
19
20 double scalarValue;
21 double[] vectorValue;
22 NumDataSet objectValue;
23 DataSetArray arrayValue;
24
25 int resultType;
26 final static int SCALAR = 1;
27 final static int VECTOR = 2;
28 final static int OBJECT = 3;
29 final static int ARRAY = 4;
30
31
32
33 public DataSlice(String pe) {
34 if (pe != null) {
35 extractFrom(pe);
36 }
37 }
38
39
40
41 private DataSlice copy() {
42 DataSlice ret = new DataSlice(null);
43 ret.item = item;
44 ret.func = func;
45 ret.slice = slice;
46 if (subslice != null) {
47 ret.subslice = subslice.copy();
48 }
49 return ret;
50 }
51
52
53
54 private void extractFrom(String pein) {
55 String pe = pein;
56 int iosb = pe.indexOf("[");
57 int icsb = pe.lastIndexOf("]");
58 if (iosb > 0 && icsb > iosb) {
59 slice = pe.substring(iosb + 1, icsb);
60 String npe = pe.substring(0, iosb);
61 if (pe.length() > icsb) {
62 npe += pe.substring(icsb + 1, pe.length());
63 }
64 pe = npe;
65 }
66
67
68 int iorb = pe.indexOf("(");
69 int icrb = pe.lastIndexOf(")");
70 if (iorb > 0 && icrb > iorb) {
71 func = pe.substring(iorb + 1, icrb);
72 pe = pe.substring(0, iorb);
73 }
74 item = pe;
75
76
77
78
79
80 }
81
82
83 public void setSubslice(DataSlice dsl) {
84 subslice = dsl;
85 }
86
87
88
89 public void resolve(HashMap<String, NumDataSet> hmap) {
90 NumDataSet target = hmap.get(item);
91 if (target == null) {
92 E.error("data set (type " + resultType + ") hash map has no element " + item);
93 for (String s : hmap.keySet()) {
94 E.info("hmap val " + hmap.get(s));
95 }
96 } else {
97 applyResolve(target);
98 }
99 }
100
101
102
103 public void mark(HashMap<String, NumDataSet> hm) {
104 NumDataSet dset = hm.get(item);
105 applyMark(dset);
106
107 }
108
109
110 public void mark(NumDataSet dset) {
111 dset.mark();
112 DataItem target = dset.get(item);
113 if (target == null) {
114 E.error("data slice result is null on slice=" + slice + " func=" + func + " item=" + item
115 + " dataSet=" + dset);
116 } else {
117 applyMark(target);
118 }
119 }
120
121
122 private void applyMark(DataItem ditin) {
123 DataItem dit = ditin;
124 dit.mark();
125
126 if (dit instanceof DataSetArray) {
127 if (slice != null && !(slice.equals("-1"))) {
128 dit = ((DataSetArray)dit).slice(slice);
129 }
130 if (subslice != null) {
131
132 NumDataSet[] dsa = ((DataSetArray)dit).getDataSets();
133
134 for (int i = 0; i < dsa.length; i++) {
135 dsa[i].mark();
136 subslice.mark(dsa[i]);
137 }
138 }
139 } else if (dit instanceof NumDataSet && subslice != null) {
140 subslice.mark((NumDataSet)dit);
141 }
142 }
143
144
145
146 public void resolve(NumDataSet dset) {
147 Object target = dset.get(item);
148
149 if (target == null) {
150 E.error("data slice result is null on slice=" + slice + " func=" + func + " item=" + item
151 + " dataSet=" + dset);
152 } else {
153 applyResolve(target);
154 }
155 }
156
157
158 private void applyResolve(Object target) {
159
160 if (target instanceof FloatScalar) {
161 scalarValue = ((FloatScalar)target).getValue();
162 if (func != null) {
163 scalarValue = applyFuncToScalar(scalarValue, func);
164 }
165 resultType = SCALAR;
166
167
168 } else if (target instanceof FloatVector) {
169 vectorValue = ((FloatVector)target).getValue();
170 if (slice != null) {
171 vectorValue = applySliceToVector(vectorValue, slice);
172 }
173 if (func != null) {
174 vectorValue = applyFuncToVector(vectorValue, func);
175 }
176 resultType = VECTOR;
177
178
179 } else if (target instanceof NumDataSet) {
180 objectValue = (NumDataSet)target;
181 resultType = OBJECT;
182
183
184 } else if (target instanceof DataSetArray) {
185
186 arrayValue = ((DataSetArray)target);
187 resultType = ARRAY;
188
189 if (slice != null) {
190 arrayValue = arrayValue.slice(slice);
191 if (arrayValue.length() == 1) {
192 resultType = OBJECT;
193 objectValue = arrayValue.firstElement();
194 }
195 }
196
197 } else {
198 E.error("Data Slice - unknown target type " + target);
199 }
200
201 if (subslice != null) {
202 if (resultType == OBJECT) {
203 subslice.resolve(objectValue);
204
205 } else if (resultType == ARRAY) {
206 NumDataSet[] dsa = arrayValue.getDataSets();
207
208 subsliceArray = new DataSlice[dsa.length];
209 for (int i = 0; i < dsa.length; i++) {
210
211 subsliceArray[i] = subslice.copy();
212 subsliceArray[i].resolve(dsa[i]);
213 }
214 subslice = null;
215 }
216 }
217
218 }
219
220
221
222 public double getScalar() {
223 double ret = 0.;
224 if (subslice != null) {
225 ret = subslice.getScalar();
226
227 } else {
228 if (resultType == SCALAR) {
229 ret = scalarValue;
230
231 } else if (resultType == VECTOR && vectorValue.length == 1) {
232 ret = vectorValue[0];
233
234 } else {
235 E.error("requested scalar from non-scalar data slice " + resultType);
236 }
237 }
238 return ret;
239 }
240
241
242 public double[] getVector() {
243 double[] ret = null;
244
245 if (subslice != null) {
246 ret = subslice.getVector();
247
248 } else if (subsliceArray != null) {
249 int ns = subsliceArray.length;
250 ret = new double[ns];
251 for (int i = 0; i < ns; i++) {
252 ret[i] = subsliceArray[i].getScalar();
253 }
254
255 } else if (resultType == VECTOR) {
256 ret = vectorValue;
257
258 } else if (resultType == OBJECT && subslice != null) {
259 ret = subslice.getVector();
260
261 } else {
262 E.debugError("requested vector from non-scalar data slice " + resultType + " " + item);
263 }
264 return ret;
265 }
266
267
268
269 public int[] getIntVector() {
270 int[] ret = null;
271
272 if (subslice != null) {
273 ret = subslice.getIntVector();
274
275 } else if (subsliceArray != null) {
276 int ns = subsliceArray.length;
277
278 ret = new int[ns];
279 for (int i = 0; i < ns; i++) {
280 ret[i] = i;
281 }
282
283 } else if (resultType == ARRAY) {
284 int ns = arrayValue.length();
285 ret = new int[ns];
286 for (int i = 0; i < ns; i++) {
287 ret[i] = i;
288 }
289
290 } else {
291 E.error("cant get int vector from " + this + " restype=" + resultType);
292 }
293 return ret;
294 }
295
296
297
298 public void markVector() {
299
300 }
301
302
303 public void markIntVector() {
304
305 }
306
307
308 public void markScalar() {
309
310 }
311
312
313
314
315 private double[] applyFuncToVector(double[] da, String lfunc) {
316 double[] ret = new double[da.length];
317 if (lfunc.equals("log10")) {
318 double mlten = Math.log(10.);
319 for (int i = 0; i < da.length; i++) {
320 ret[i] = Math.log(da[i]) / mlten;
321 }
322 } else {
323 E.error("Data slice unknown function " + lfunc);
324 }
325 return ret;
326 }
327
328
329
330 private double applyFuncToScalar(double d, String lfunc) {
331 double ret = 0.;
332 if (lfunc.equals("log10")) {
333 double mlten = Math.log(10.);
334 ret = Math.log(d) / mlten;
335 } else {
336 E.error("Data slice unknown function " + lfunc);
337 }
338 return ret;
339 }
340
341
342
343
344 private double[] applySliceToVector(double[] da, String lslice) {
345 double[] ret = new double[1];
346 int ival = Integer.parseInt(lslice);
347 ret[0] = da[ival];
348 return ret;
349 }
350
351
352
353 }