source: src/main/java/agents/anac/y2015/group2/G2CSVLogger.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.0 KB
Line 
1package agents.anac.y2015.group2;
2import java.io.File;
3import java.io.FileWriter;
4import java.io.IOException;
5import java.io.PrintWriter;
6import java.util.ArrayList;
7
8public class G2CSVLogger {
9
10 FileWriter fileWriter = null;
11 PrintWriter printWriter;
12 String eol = System.getProperty("line.separator");
13
14 ArrayList<ArrayList<Double>> Buffer = new ArrayList<ArrayList<Double>>();
15
16 G2CSVLogger () {
17
18 //Empty previous logs
19 File dir = new File("./logs/model/");
20 if(dir != null){
21 for(File file: dir.listFiles()) file.delete();
22 }
23 dir = new File("./logs/real/");
24 if(dir != null){
25 for(File file: dir.listFiles()) file.delete();
26 }
27
28 }
29
30 void init (int partyNumber, String name) {
31 try {
32 fileWriter = new FileWriter("./logs/"+name+"/party"+partyNumber+".csv");
33 //printWriter = new PrintWriter(fileWriter);
34
35 for(ArrayList<Double> list: Buffer) {
36 for(double s:list){
37 fileWriter.append(String.valueOf(s));
38 fileWriter.append(',');
39 }
40 fileWriter.append(eol);
41 }
42 Buffer.clear();
43 fileWriter.flush();
44 } catch (IOException e) {
45 System.out.println("Could not create log for party " + partyNumber);
46 }
47
48
49 }
50
51 void log(ArrayList<Double> list) {
52 if(fileWriter == null) {
53 Buffer.add(list);
54 } else {
55 try {
56 for(double s:list){
57 fileWriter.append(String.valueOf(s));
58 fileWriter.append(',');
59 }
60 fileWriter.append(eol);
61 fileWriter.flush();
62 }catch (IOException e) {
63 System.out.println("Could not add to log");
64 }
65 }
66 }
67 void log(Double d) {
68 if(fileWriter == null) {
69 ArrayList<Double> list = new ArrayList<Double>();
70 list.add(d);
71 Buffer.add(list);
72 } else {
73 try {
74
75 fileWriter.append(String.valueOf(d));
76 fileWriter.append(eol);
77 fileWriter.flush();
78 }catch (IOException e) {
79 System.out.println("Could not add to log");
80 }
81 }
82 }
83
84 boolean isInitialized() {
85 return fileWriter != null;
86 }
87 void close(){
88 try {
89 fileWriter.close();
90 }catch (IOException e) {
91 System.out.println("Could not close file");
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.