1
1
/*******************************************************************************************************
2
2
*
3
- * FileOutput.java, in msi.gama.core, is part of the source code of the
4
- * GAMA modeling and simulation platform (v.1.9.2).
3
+ * FileOutput.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.9.2).
5
4
*
6
5
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
7
6
*
8
7
* Visit https://github.com/gama-platform/gama for license information and contacts.
9
- *
8
+ *
10
9
********************************************************************************************************/
11
10
package msi .gama .outputs ;
12
11
19
18
import java .util .Arrays ;
20
19
import java .util .Calendar ;
21
20
import java .util .List ;
22
- import java .util .stream .Collectors ;
23
21
24
22
import msi .gama .common .interfaces .IKeyword ;
25
23
import msi .gama .kernel .experiment .IExperimentPlan ;
34
32
import msi .gama .runtime .IScope ;
35
33
import msi .gama .runtime .exceptions .GamaRuntimeException ;
36
34
import msi .gama .util .IMap ;
35
+ import msi .gama .util .file .csv .AbstractCSVManipulator ;
37
36
import msi .gaml .compilation .GAML ;
38
37
import msi .gaml .descriptions .IDescription ;
39
38
import msi .gaml .expressions .IExpression ;
46
45
* </p>
47
46
* A particular output file especially design for the batch experiment output
48
47
* </p>
48
+ *
49
49
* @author drogoul
50
50
*/
51
51
@ symbol (
@@ -125,55 +125,55 @@ public FileOutput(/* final ISymbol context, */final IDescription desc) {
125
125
126
126
/** The file. */
127
127
File file = null ;
128
-
128
+
129
129
/** The file name. */
130
130
String fileName = "" ;
131
-
131
+
132
132
/** The rewrite. */
133
133
boolean rewrite = false ;
134
-
134
+
135
135
/** The header. */
136
136
String header = "" ;
137
-
137
+
138
138
/** The footer. */
139
139
String footer = "" ;
140
-
140
+
141
141
/** The last value. */
142
142
Object lastValue = null ;
143
-
143
+
144
144
/** The last values. */
145
145
List <Object > lastValues = null ;
146
-
146
+
147
147
/** The logged batch param. */
148
148
List <String > loggedBatchParam = null ;
149
-
149
+
150
150
/** The solution. */
151
151
ParametersSet solution = null ;
152
-
152
+
153
153
/** The expression text. */
154
154
private String expressionText = null ;
155
-
155
+
156
156
/** The data. */
157
157
private IExpression data ;
158
-
158
+
159
159
/** The Constant LOG_FOLDER. */
160
160
private static final String LOG_FOLDER = "log" ;
161
-
161
+
162
162
/** The Constant XMLHeader. */
163
163
private static final String XMLHeader = "<?xml version=\" 1.0\" encoding=\" ISO-8859-1\" standalone=\" yes\" ?>" ;
164
-
164
+
165
165
/** The Constant XML. */
166
166
private static final int XML = 1 ;
167
-
167
+
168
168
/** The Constant CSV. */
169
169
private static final int CSV = 2 ;
170
-
170
+
171
171
/** The Constant TEXT. */
172
172
private static final int TEXT = 0 ;
173
-
173
+
174
174
/** The Constant extensions. */
175
175
private static final List <String > extensions = Arrays .asList ("txt" , "xml" , "csv" );
176
-
176
+
177
177
/** The type. */
178
178
private int type ;
179
179
@@ -198,7 +198,8 @@ private void createExpression() {
198
198
/**
199
199
* Creates the header.
200
200
*
201
- * @throws GamaRuntimeException the gama runtime exception
201
+ * @throws GamaRuntimeException
202
+ * the gama runtime exception
202
203
*/
203
204
private void createHeader () throws GamaRuntimeException {
204
205
final IExpression exp = getFacet (IKeyword .HEADER );
@@ -212,7 +213,8 @@ private void createHeader() throws GamaRuntimeException {
212
213
/**
213
214
* Creates the footer.
214
215
*
215
- * @throws GamaRuntimeException the gama runtime exception
216
+ * @throws GamaRuntimeException
217
+ * the gama runtime exception
216
218
*/
217
219
private void createFooter () throws GamaRuntimeException {
218
220
final IExpression exp = getFacet (IKeyword .FOOTER );
@@ -226,7 +228,8 @@ private void createFooter() throws GamaRuntimeException {
226
228
/**
227
229
* Creates the rewrite.
228
230
*
229
- * @throws GamaRuntimeException the gama runtime exception
231
+ * @throws GamaRuntimeException
232
+ * the gama runtime exception
230
233
*/
231
234
private void createRewrite () throws GamaRuntimeException {
232
235
final IExpression exp = getFacet (IKeyword .REWRITE );
@@ -307,11 +310,16 @@ public boolean init(final IScope scope) throws GamaRuntimeException {
307
310
/**
308
311
* Instantiates a new file output.
309
312
*
310
- * @param name the name
311
- * @param expr the expr
312
- * @param columns the columns
313
- * @param exp the exp
314
- * @throws GamaRuntimeException the gama runtime exception
313
+ * @param name
314
+ * the name
315
+ * @param expr
316
+ * the expr
317
+ * @param columns
318
+ * the columns
319
+ * @param exp
320
+ * the exp
321
+ * @throws GamaRuntimeException
322
+ * the gama runtime exception
315
323
*/
316
324
public FileOutput (final String name , final String expr , final List <String > columns , final IExperimentPlan exp )
317
325
throws GamaRuntimeException {
@@ -380,7 +388,8 @@ private void createFileName(final IScope scope) throws GamaRuntimeException {
380
388
/**
381
389
* Refresh expression.
382
390
*
383
- * @throws GamaRuntimeException the gama runtime exception
391
+ * @throws GamaRuntimeException
392
+ * the gama runtime exception
384
393
*/
385
394
public void refreshExpression () throws GamaRuntimeException {
386
395
// in case the file writer persists over different simulations (like in
@@ -407,11 +416,12 @@ public boolean step(final IScope scope) {
407
416
public void update () throws GamaRuntimeException {
408
417
writeToFile (getScope ().getClock ().getCycle ());
409
418
}
410
-
419
+
411
420
/**
412
421
* Do write report and close.
413
422
*
414
- * @param report the report
423
+ * @param report
424
+ * the report
415
425
*/
416
426
public void doWriteReportAndClose (final String report ) {
417
427
switch (type ) {
@@ -429,19 +439,21 @@ public void doWriteReportAndClose(final String report) {
429
439
break ;
430
440
}
431
441
}
432
-
442
+
433
443
/**
434
444
* Main method to write down a set of given values for a single a point in the parameter space
445
+ *
435
446
* @param sol
436
447
* @param outputs
437
448
* @throws GamaRuntimeException
438
449
*/
439
- public void doRefreshWriteAndClose (final ParametersSet sol , final IMap <String ,Object > outputs ) throws GamaRuntimeException {
450
+ public void doRefreshWriteAndClose (final ParametersSet sol , final IMap <String , Object > outputs )
451
+ throws GamaRuntimeException {
440
452
setSolution (sol );
441
453
if (outputs == null || outputs .isEmpty ()) {
442
- if (!getScope ().step (this ).passed ()) { return ; }
454
+ if (!getScope ().step (this ).passed ()) return ;
443
455
} else {
444
- this .lastValues = outputs .values ().stream ().toList (); //setLastValue(fitness);
456
+ this .lastValues = outputs .values ().stream ().toList (); // setLastValue(fitness);
445
457
}
446
458
// compute(getOwnScope(), 0l);
447
459
switch (type ) {
@@ -462,11 +474,15 @@ public void doRefreshWriteAndClose(final ParametersSet sol, final IMap<String,Ob
462
474
case CSV :
463
475
if (solution == null ) return ;
464
476
final StringBuilder s = new StringBuilder (loggedBatchParam .size () * 8 );
465
- for (final String var : loggedBatchParam ) { s .append (solution .get (var )).append (',' ); }
477
+ for (final String var : loggedBatchParam ) {
478
+ s .append (solution .get (var )).append (AbstractCSVManipulator .getDefaultDelimiter ());
479
+ }
466
480
if (lastValue != null ) {
467
481
s .append (lastValue );
468
482
} else if (lastValues != null && !lastValues .isEmpty ()) {
469
- for (Object val : lastValues ) {s .append (',' ).append (val ); }
483
+ for (Object val : lastValues ) {
484
+ s .append (AbstractCSVManipulator .getDefaultDelimiter ()).append (val );
485
+ }
470
486
} else {
471
487
s .setLength (s .length () - 1 );
472
488
}
@@ -485,7 +501,8 @@ public void doRefreshWriteAndClose(final ParametersSet sol, final IMap<String,Ob
485
501
/**
486
502
* Write to file.
487
503
*
488
- * @param cycle the cycle
504
+ * @param cycle
505
+ * the cycle
489
506
*/
490
507
void writeToFile (final long cycle ) {
491
508
switch (type ) {
@@ -512,7 +529,8 @@ void writeToFile(final long cycle) {
512
529
/**
513
530
* Sets the rewrite.
514
531
*
515
- * @param rewrite the new rewrite
532
+ * @param rewrite
533
+ * the new rewrite
516
534
*/
517
535
private void setRewrite (final boolean rewrite ) { this .rewrite = rewrite ; }
518
536
@@ -532,7 +550,8 @@ private String getHeader() {
532
550
/**
533
551
* Sets the header.
534
552
*
535
- * @param header the new header
553
+ * @param header
554
+ * the new header
536
555
*/
537
556
private void setHeader (final String header ) { this .header = header ; }
538
557
@@ -552,7 +571,8 @@ private String getFooter() {
552
571
/**
553
572
* Sets the footer.
554
573
*
555
- * @param footer the new footer
574
+ * @param footer
575
+ * the new footer
556
576
*/
557
577
private void setFooter (final String footer ) { this .footer = footer ; }
558
578
@@ -566,14 +586,16 @@ private String getFooter() {
566
586
/**
567
587
* Sets the writer.
568
588
*
569
- * @param writer the new writer
589
+ * @param writer
590
+ * the new writer
570
591
*/
571
592
private void setWriter (final PrintWriter writer ) { this .writer = writer ; }
572
593
573
594
/**
574
595
* Sets the last value.
575
596
*
576
- * @param lastValue the new last value
597
+ * @param lastValue
598
+ * the new last value
577
599
*/
578
600
public void setLastValue (final Object lastValue ) { this .lastValue = lastValue ; }
579
601
@@ -587,9 +609,12 @@ private String getFooter() {
587
609
/**
588
610
* Sets the logged batch param.
589
611
*
590
- * @param loggedBatchParam the new logged batch param
612
+ * @param loggedBatchParam
613
+ * the new logged batch param
591
614
*/
592
- public void setLoggedBatchParam (final List <String > loggedBatchParam ) { this .loggedBatchParam = loggedBatchParam ; }
615
+ public void setLoggedBatchParam (final List <String > loggedBatchParam ) {
616
+ this .loggedBatchParam = loggedBatchParam ;
617
+ }
593
618
594
619
/**
595
620
* Gets the solution.
@@ -601,7 +626,8 @@ private String getFooter() {
601
626
/**
602
627
* Sets the solution.
603
628
*
604
- * @param solution the new solution
629
+ * @param solution
630
+ * the new solution
605
631
*/
606
632
public void setSolution (final ParametersSet solution ) { this .solution = solution ; }
607
633
@@ -622,7 +648,9 @@ public void writeHeaderAndClose() {
622
648
break ;
623
649
case CSV :
624
650
final StringBuilder s = new StringBuilder (loggedBatchParam .size () * 8 );
625
- for (final String var : loggedBatchParam ) { s .append (var ).append (',' ); }
651
+ for (final String var : loggedBatchParam ) {
652
+ s .append (var ).append (AbstractCSVManipulator .getDefaultDelimiter ());
653
+ }
626
654
if (getFacet (IKeyword .DATA ) != null ) {
627
655
s .append (getLiteral (IKeyword .DATA ));
628
656
} else {
0 commit comments