/
ChartDataSet.java
611 lines (499 loc) · 18 KB
/
ChartDataSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package msi.gama.outputs.layers.charts;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.common.util.FileUtils;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaList;
import msi.gama.util.IList;
import msi.gaml.expressions.IExpression;
import msi.gaml.operators.Cast;
import msi.gaml.operators.Files;
import msi.gaml.operators.Strings;
public class ChartDataSet {
private static String chartFolder = "charts";
ArrayList<ChartDataSource> sources = new ArrayList<ChartDataSource>();
LinkedHashMap<String, ChartDataSeries> series = new LinkedHashMap<String, ChartDataSeries>();
LinkedHashMap<String, ChartDataSeries> deletedseries = new LinkedHashMap<String, ChartDataSeries>();
ArrayList<String> Xcategories = new ArrayList<String>(); // for categories
// datasets
ArrayList<Double> XSeriesValues = new ArrayList<Double>(); // for series
ArrayList<String> Ycategories = new ArrayList<String>(); // for Y categories
// datasets
ArrayList<Double> YSeriesValues = new ArrayList<Double>(); // for 3d series
LinkedHashMap<String, Integer> serieCreationDate = new LinkedHashMap<String, Integer>();
int commonXindex = -1; // current index on X value (usually last of list,
// can be less when going back in time...)
int commonYindex = -1; // current index on X value (usually last of list,
// can be less when going back in time...)
IExpression xsource; // to replace default common X Source
IExpression ysource; // to replace default common X Labels
IExpression xlabels; // to replace default common Y Source
IExpression ylabels; // to replace default common Y Labels
LinkedHashMap<String, Integer> serieRemovalDate = new LinkedHashMap<String, Integer>();
LinkedHashMap<String, Integer> serieToUpdateBefore = new LinkedHashMap<String, Integer>();
ChartOutput mainoutput;
int resetAllBefore = 0;
boolean forceResetAll = false;
String defaultstyle = IKeyword.DEFAULT;
int lastchartcycle = -1;
boolean forceNoXAccumulate = false;
boolean forceNoYAccumulate = false;
boolean useXSource = false;
boolean useXLabels = false;
boolean useYSource = false;
boolean useYLabels = false;
boolean commonXSeries = false; // series
boolean commonYSeries = false; // heatmap & 3d
boolean byCategory = false; // histogram/pie
boolean keepOldSeries = true; // keep old series or move to deleted (to keep
// history)
StringBuilder history;
public int getCommonXIndex() {
return commonXindex;
}
public int getCommonYIndex() {
return commonYindex;
}
public int getResetAllBefore() {
return resetAllBefore;
}
public void setResetAllBefore(final int resetAllBefore) {
this.resetAllBefore = resetAllBefore;
forceResetAll = true;
}
public boolean isKeepOldSeries() {
return keepOldSeries;
}
public void setKeepOldSeries(final boolean keepOldSeries) {
this.keepOldSeries = keepOldSeries;
}
private ArrayList<String> getCategories() {
return Xcategories;
}
public String getCategories(final IScope scope, final int i) {
if (Xcategories.size() > i) {
return Xcategories.get(i);
}
for (int c = Xcategories.size(); c <= i; c++) {
this.Xcategories.add("c" + c);
}
return Xcategories.get(i);
}
public String getLastCategories(final IScope scope) {
if (Xcategories.size() > 0) {
return Xcategories.get(Xcategories.size() - 1);
}
this.Xcategories.add("c" + 0);
return Xcategories.get(Xcategories.size() - 1);
}
public void setCategories(final ArrayList<String> categories) {
this.Xcategories = categories;
}
public ArrayList<Double> getXSeriesValues() {
return XSeriesValues;
}
public ArrayList<Double> getYSeriesValues() {
return YSeriesValues;
}
public void setXSeriesValues(final ArrayList<Double> xSeriesValues) {
XSeriesValues = xSeriesValues;
}
public boolean isByCategory() {
return byCategory;
}
public void setByCategory(final boolean byCategory) {
this.byCategory = byCategory;
}
public boolean isCommonXSeries() {
return commonXSeries;
}
public void setCommonXSeries(final boolean temporalSeries) {
this.commonXSeries = temporalSeries;
}
public boolean isCommonYSeries() {
return commonYSeries;
}
public void setCommonYSeries(final boolean temporalSeries) {
this.commonYSeries = temporalSeries;
}
public LinkedHashMap<String, Integer> getSerieCreationDate() {
return serieCreationDate;
}
public LinkedHashMap<String, Integer> getSerieRemovalDate() {
return serieRemovalDate;
}
public ChartDataSet() {
history = new StringBuilder();
}
public StringBuilder getHistory() {
return history;
}
public ChartOutput getOutput() {
return mainoutput;
}
public void setOutput(final ChartOutput output) {
mainoutput = output;
this.defaultstyle = output.getStyle();
}
public void addNewSerie(final String id, final ChartDataSeries serie, final int date) {
if (series.keySet().contains(id)) {
// Series name already present, should do something.... Don't change
// creation date?
series.put(id, serie);
// serieCreationDate.put(id, date);
serieToUpdateBefore.put(id, date);
serieRemovalDate.put(id, -1);
} else {
series.put(id, serie);
serieCreationDate.put(id, date);
serieToUpdateBefore.put(id, date);
serieRemovalDate.put(id, -1);
}
}
public ArrayList<ChartDataSource> getSources() {
return sources;
}
public void addDataSource(final ChartDataSource source) {
sources.add(source);
final LinkedHashMap<String, ChartDataSeries> newseries = source.getSeries();
for (final Entry<String, ChartDataSeries> entry : newseries.entrySet()) {
// should do something... raise an exception?
addNewSerie(entry.getKey(), entry.getValue(), -1);
}
// series.putAll(source.getSeries());
}
public boolean doResetAll(final IScope scope, final int lastUpdateCycle) {
// TODO Auto-generated method stub
if (resetAllBefore > lastUpdateCycle || forceResetAll) {
forceResetAll = false;
return true;
}
return false;
}
public Set<String> getDataSeriesIds(final IScope scope) {
// TODO Auto-generated method stub
return series.keySet();
}
public ChartDataSeries getDataSeries(final IScope scope, final String serieid) {
// TODO Auto-generated method stub
return series.get(serieid);
}
public boolean didReload(final IScope scope, final int chartCycle) {
boolean didr = false;
final int mychartcycle = chartCycle;
// int mychartcycle=scope.getSimulationScope().getCycle(scope)+1;
// System.out.println("cycle "+mychartcycle+" last: "+lastchartcycle);
if (lastchartcycle >= mychartcycle) {
lastchartcycle = mychartcycle - 1;
didr = true;
} else {
lastchartcycle = mychartcycle;
}
return didr;
}
public void BackwardSim(final IScope scope, final int chartCycle) {
this.setResetAllBefore(chartCycle);
final ArrayList<ChartDataSource> sourcestoremove = new ArrayList<ChartDataSource>();
final ArrayList<ChartDataSource> sourcestoadd = new ArrayList<ChartDataSource>();
for (final ChartDataSource source : sources) {
if (source.isCumulative || source.isCumulativeY) {
final ChartDataSource newsource = source.getClone(scope, chartCycle);
newsource.createInitialSeries(scope);
sourcestoremove.add(source);
sourcestoadd.add(newsource);
}
}
for (final ChartDataSource source : sourcestoremove) {
sources.remove(source);
}
for (final ChartDataSource source : sourcestoadd) {
this.addDataSource(source);
}
if (this.getXSeriesValues().size() > 0) {
final ArrayList<Double> ser = this.getXSeriesValues();
for (int i = 0; i < this.getXSeriesValues().size(); i++) {
if (ser.get(i) == chartCycle - 1) {
this.commonXindex = i;
}
}
}
if (this.getYSeriesValues().size() > 0) {
final ArrayList<Double> sery = this.getYSeriesValues();
for (int i = 0; i < this.getYSeriesValues().size(); i++) {
if (sery.get(i) == chartCycle - 1) {
this.commonYindex = i;
}
}
}
}
public void updatedataset(final IScope scope, final int chartCycle) {
// TODO Auto-generated method stub
commonXindex++;
commonYindex++;
if (didReload(scope, chartCycle)) {
BackwardSim(scope, chartCycle);
}
updateXValues(scope, chartCycle);
updateYValues(scope, chartCycle);
if (commonXindex >= this.getXSeriesValues().size())
commonXindex = this.getXSeriesValues().size() - 1;
if (commonYindex >= this.getYSeriesValues().size())
commonYindex = this.getYSeriesValues().size() - 1;
for (final ChartDataSource source : sources) {
source.updatevalues(scope, chartCycle);
source.savehistory(scope, history);
}
history.append(Strings.LN);
}
public void updateYValues(final IScope scope, final int chartCycle, int targetNb) {
Object xval, xlab;
if (this.useYSource || this.useYLabels) {
if (this.useYSource) {
xval = ysource.resolveAgainst(scope).value(scope);
} else {
xval = ylabels.resolveAgainst(scope).value(scope);
}
if (this.useYLabels) {
xlab = ylabels.resolveAgainst(scope).value(scope);
} else {
xlab = ysource.resolveAgainst(scope).value(scope);
}
if (xval instanceof GamaList) {
final IList<?> xv2 = Cast.asList(scope, xval);
final IList<?> xl2 = Cast.asList(scope, xlab);
if (this.useYSource && xv2.size() > 0 && xv2.get(0) instanceof Number) {
YSeriesValues = new ArrayList<Double>();
Ycategories = new ArrayList<String>();
for (int i = 0; i < xv2.size(); i++) {
YSeriesValues.add(new Double(Cast.asFloat(scope, xv2.get(i))));
Ycategories.add(Cast.asString(scope, xl2.get(i)));
}
} else {
if (xv2.size() > Ycategories.size()) {
Ycategories = new ArrayList<String>();
for (int i = 0; i < xv2.size(); i++) {
if (i >= YSeriesValues.size()) {
YSeriesValues.add(new Double(getYCycleOrPlusOneForBatch(scope, chartCycle)));
}
Ycategories.add(Cast.asString(scope, xl2.get(i)));
}
}
}
if (xv2.size() < targetNb) {
throw GamaRuntimeException.error("The x-serie length (" + xv2.size()
+ ") should NOT be shorter than any series length (" + targetNb + ") !", scope);
}
} else {
if (this.useYSource && xval instanceof Number) {
final double dvalue = Cast.asFloat(scope, xval);
final String lvalue = Cast.asString(scope, xlab);
YSeriesValues.add(new Double(dvalue));
Ycategories.add(lvalue);
}
if (targetNb == -1 && !this.forceNoYAccumulate)
targetNb = YSeriesValues.size() + 1;
while (YSeriesValues.size() < targetNb) {
YSeriesValues.add(new Double(getYCycleOrPlusOneForBatch(scope, chartCycle)));
Ycategories.add(Cast.asString(scope, xlab));
}
}
}
if (!this.useYSource && !this.useYLabels) {
if (targetNb == -1 && !this.forceNoYAccumulate && commonYindex >= YSeriesValues.size())
targetNb = YSeriesValues.size() + 1;
while (YSeriesValues.size() < targetNb) {
addCommonYValue(scope, getYCycleOrPlusOneForBatch(scope, chartCycle));
}
}
}
public void updateYValues(final IScope scope, final int chartCycle) {
updateYValues(scope, chartCycle, -1);
}
public int getYCycleOrPlusOneForBatch(final IScope scope, final int chartcycle) {
if (this.YSeriesValues.contains((double) chartcycle))
return (int) YSeriesValues.get(YSeriesValues.size() - 1).doubleValue() + 1;
return chartcycle;
}
private void addCommonYValue(final IScope scope, final int chartCycle) {
// TODO Auto-generated method stub
YSeriesValues.add(new Double(chartCycle));
Ycategories.add("" + chartCycle);
}
public double getCurrentCommonYValue() {
// TODO Auto-generated method stub
return this.YSeriesValues.get(this.commonYindex);
}
public double getCurrentCommonXValue() {
// TODO Auto-generated method stub
return this.XSeriesValues.get(this.commonXindex);
}
public void updateXValues(final IScope scope, final int chartCycle, int targetNb) {
Object xval, xlab;
if (this.useXSource || this.useXLabels) {
if (this.useXSource) {
xval = xsource.resolveAgainst(scope).value(scope);
} else {
xval = xlabels.resolveAgainst(scope).value(scope);
}
if (this.useXLabels) {
xlab = xlabels.resolveAgainst(scope).value(scope);
} else {
xlab = xsource.resolveAgainst(scope).value(scope);
}
if (xval instanceof GamaList) {
final IList<?> xv2 = Cast.asList(scope, xval);
final IList<?> xl2 = Cast.asList(scope, xlab);
if (this.useXSource && xv2.size() > 0 && xv2.get(0) instanceof Number) {
XSeriesValues = new ArrayList<Double>();
Xcategories = new ArrayList<String>();
for (int i = 0; i < xv2.size(); i++) {
XSeriesValues.add(new Double(Cast.asFloat(scope, xv2.get(i))));
Xcategories.add(Cast.asString(scope, xl2.get(i)));
}
} else {
if (xv2.size() > Xcategories.size()) {
Xcategories = new ArrayList<String>();
for (int i = 0; i < xv2.size(); i++) {
if (i >= XSeriesValues.size()) {
XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
}
Xcategories.add(Cast.asString(scope, xl2.get(i)));
}
}
}
if (xv2.size() < targetNb) {
throw GamaRuntimeException.error("The x-serie length (" + xv2.size()
+ ") should NOT be shorter than any series length (" + targetNb + ") !", scope);
}
} else {
if (this.useXSource && xval instanceof Number) {
final double dvalue = Cast.asFloat(scope, xval);
final String lvalue = Cast.asString(scope, xlab);
XSeriesValues.add(new Double(dvalue));
Xcategories.add(lvalue);
}
if (targetNb == -1 && !this.forceNoXAccumulate)
targetNb = XSeriesValues.size() + 1;
while (XSeriesValues.size() < targetNb) {
XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
Xcategories.add(Cast.asString(scope, xlab));
}
}
}
if (!this.useXSource && !this.useXLabels) {
if (targetNb == -1 && !this.forceNoXAccumulate && commonXindex >= XSeriesValues.size())
targetNb = XSeriesValues.size() + 1;
while (XSeriesValues.size() < targetNb) {
addCommonXValue(scope, getXCycleOrPlusOneForBatch(scope, chartCycle));
}
}
}
public void updateXValues(final IScope scope, final int chartCycle) {
updateXValues(scope, chartCycle, -1);
}
public int getXCycleOrPlusOneForBatch(final IScope scope, final int chartcycle) {
if (this.XSeriesValues.contains((double) chartcycle))
return (int) XSeriesValues.get(XSeriesValues.size() - 1).doubleValue() + 1;
return chartcycle;
}
private void addCommonXValue(final IScope scope, final int chartCycle) {
// TODO Auto-generated method stub
XSeriesValues.add(new Double(chartCycle));
Xcategories.add("" + chartCycle);
}
public int getDate(final IScope scope) {
return scope.getClock().getCycle();
}
public void setXSource(final IScope scope, final IExpression data) {
// TODO Auto-generated method stub
this.useXSource = true;
this.xsource = data;
}
public void setXLabels(final IScope scope, final IExpression data) {
// TODO Auto-generated method stub
this.useXLabels = true;
this.xlabels = data;
}
public void setYSource(final IScope scope, final IExpression data) {
// TODO Auto-generated method stub
this.useYSource = true;
this.ysource = data;
}
public void setYLabels(final IScope scope, final IExpression data) {
// TODO Auto-generated method stub
this.useYLabels = true;
this.ylabels = data;
}
public ChartDataSeries createOrGetSerie(final IScope scope, final String id, final ChartDataSourceList source) {
// TODO Auto-generated method stub
if (series.keySet().contains(id)) {
return series.get(id);
}
if (deletedseries.keySet().contains(id)) {
final ChartDataSeries myserie = deletedseries.get(id);
deletedseries.remove(id);
this.serieRemovalDate.put(id, -1);
myserie.setMysource(source);
myserie.setDataset(this);
myserie.setName(id);
addNewSerie(id, myserie, getDate(scope));
return myserie;
}
final ChartDataSeries myserie = new ChartDataSeries();
myserie.setMysource(source);
myserie.setDataset(this);
myserie.setName(id);
addNewSerie(id, myserie, getDate(scope));
return myserie;
}
public void removeserie(final IScope scope, final String id) {
// TODO Auto-generated method stub
final ChartDataSeries serie = this.getDataSeries(scope, id);
if (serie != null) {
this.deletedseries.put(id, serie);
this.series.remove(id);
this.serieRemovalDate.put(id, this.getDate(scope));
serieToUpdateBefore.put(id, this.getDate(scope));
this.deletedseries.put(id, serie);
this.setResetAllBefore(this.getDate(scope));
}
}
public void setStyle(final IScope scope, final String stval) {
// TODO Auto-generated method stub
defaultstyle = stval;
}
public String getStyle(final IScope scope) {
// TODO Auto-generated method stub
return defaultstyle;
}
public void setForceNoYAccumulate(final boolean b) {
// TODO Auto-generated method stub
this.forceNoYAccumulate = b;
}
public void saveHistory(final IScope scope, final String name) {
if (scope == null) {
return;
}
try {
Files.newFolder(scope, chartFolder);
String file = chartFolder + "/" + "chart_" + name + ".csv";
BufferedWriter bw;
// file = FileUtils.constructAbsoluteFilePath(scope, file, false);
file = FileUtils.constructAbsoluteFilePath(scope, file, false);
bw = new BufferedWriter(new FileWriter(file));
bw.append(history);
bw.close();
} catch (final Exception e) {
e.printStackTrace();
return;
} finally {
// GAMA.releaseScope(scope);
}
}
}