Commit e222ad25 authored by Xiaoying Pu's avatar Xiaoying Pu

Persistence class modified with functional features... need to be tested

parent c0e1469e
......@@ -7,6 +7,7 @@ package util;
*/
public class Header {
public static String[] CSV_HEADER = new String[]{"digit", "digit_length", "decision_id", "response"};
// It's just too much trouble to know what the digit was tho....
public static String[] DIGIT_HEADER = new String[]{"digit_length", "decision_id", "response"};
}
......@@ -3,6 +3,8 @@ package util;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* @see flyloop CSVFileOutput
......@@ -12,9 +14,11 @@ public class Persistence {
private PrintWriter writer;
private String[] colNames;
private List<String> tempRow;
public Persistence(String fileName, String[] colNames) {
this.colNames = colNames;
this.tempRow = new ArrayList<>();
createPrintWriter(fileName);
}
......@@ -24,34 +28,47 @@ public class Persistence {
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace(System.out);
}
// header row, comma separated
/*header row, comma separated
Write the header to file
*/
String row = "time,";
for (String category : this.colNames) {
row = row.concat(category + ",");
}
// get rid of the last comma at the end of the line
row = row.substring(0, row.length() - 1);
this.writer.println(row);
}
/**
* Assuming the array of data is in the order of the header
* Jot down the
*/
public void initEntry() {
this.tempRow = new ArrayList<>();
String currentTime = String.valueOf(System.currentTimeMillis());
}
/**
* Assuming this function is called in the order in which the header defines
*
* @param data
* @param data data point in a row. Don't put timestamp as parameter because
* it's already there.
*/
public void put(Object[] data) {
assert data.length == this.colNames.length;
String currentTime = System.currentTimeMillis() + ",";
String dataRow = "";
for (Object o : data) {
dataRow = dataRow.concat(o.toString() + ",");
}
dataRow = dataRow.substring(0, dataRow.length() - 1);
dataRow = currentTime + dataRow;
public void put(Object data) {
String s = String.valueOf(data);
this.tempRow.add(s);
}
/**
* Writes the row into the file
*/
public void flush() {
assert this.tempRow.size() == this.colNames.length;
String dataRow;
// TODO for loop
dataRow = this.tempRow
.stream()
.reduce("", (s1, s2) -> s1 + "," + s2);
this.writer.println(dataRow);
this.writer.flush();
}
......
......@@ -8,6 +8,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import main.Dm_task;
import tracker.MouseTrackerFacade;
import static util.Header.DIGIT_HEADER;
/**
* A Trial involves: 1 display, hide the digit, 2 Give MouseTracker stimulus and
......@@ -34,6 +35,9 @@ public class Trial extends Thread {
/* for digit recall counters? */
private Task aTask;
/* csv dump for digit recall */
private Persistence writer;
private String templine = "";
/**
* The constructor
......@@ -48,6 +52,7 @@ public class Trial extends Thread {
this.iterations = new AtomicInteger(0);
this.maxIteration = Configurator.getnTrials();
this.aTask = new Task();
this.writer = new Persistence("digittask.csv", DIGIT_HEADER);
}
public void startTrial() {
......@@ -97,6 +102,9 @@ public class Trial extends Thread {
case DIGITINPUT_FINISHED:
// TODO who calls the listener in this case?
// TODO: the counter may be happier if it lived here
// TODO: csv writing here
templine = "";
actionQueue.add(new ShowDigitAction(ActionType.START_HWL_DIGIT));
break;
......@@ -183,6 +191,7 @@ public class Trial extends Thread {
System.out.println("====================AT Trial " + iterations.get() + "======================");
// how ugly is this
if (type == ActionType.START_HWL_DIGIT
|| type == ActionType.START_LWL_DIGIT) {
WLState curState;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment