From d2568bfeea43b3b4e89346749cb582ed97c8b0d3 Mon Sep 17 00:00:00 2001 From: Christiaan Goossens Date: Sat, 10 Dec 2016 16:15:34 +0100 Subject: [PATCH] Create a file creation system for argument output --- simulator/.idea/misc.xml | 5 +- simulator/.idea/workspace.xml | 380 ++++++++++++------ .../pos/simulator/SimulatorConfig.java | 2 +- .../simulator/dataWriter/AOPDataWriter.java | 47 +++ .../pos/simulator/dataWriter/DataWriter.java | 58 +-- .../simulator/dataWriter/PosDataWriter.java | 70 ++++ .../pos/simulator/processor/Processor.java | 44 +- 7 files changed, 405 insertions(+), 201 deletions(-) create mode 100644 simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java create mode 100644 simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java diff --git a/simulator/.idea/misc.xml b/simulator/.idea/misc.xml index e1fe88f..2a80d86 100644 --- a/simulator/.idea/misc.xml +++ b/simulator/.idea/misc.xml @@ -1,9 +1,12 @@ + + + - + \ No newline at end of file diff --git a/simulator/.idea/workspace.xml b/simulator/.idea/workspace.xml index 3536c1a..81a29f4 100644 --- a/simulator/.idea/workspace.xml +++ b/simulator/.idea/workspace.xml @@ -7,11 +7,16 @@ + + + + + + - + - @@ -23,6 +28,7 @@ + @@ -32,56 +38,106 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + + - - + + + + + + + + + + + + - - + + + + + + + + + + + + - - + - - + + - - + + - - + + - - + + - - + + - - + + @@ -126,23 +182,26 @@ - @@ -160,7 +219,16 @@ - + + + + + + + + + + @@ -176,9 +244,17 @@ + + + + + + - - @@ -345,7 +419,7 @@ - + @@ -452,6 +508,27 @@ + + + + + + + + @@ -526,6 +603,34 @@ + + + + + @@ -536,7 +641,7 @@ - + - - + + - - + - - + - - + - + - - + @@ -650,25 +760,10 @@ - - - - - - - - - - - - - - - - + @@ -676,7 +771,7 @@ - + @@ -697,21 +792,21 @@ - + - + - + @@ -719,7 +814,7 @@ - + @@ -740,21 +835,21 @@ - + - + - + @@ -762,7 +857,7 @@ - + @@ -783,21 +878,21 @@ - + - + - + @@ -805,7 +900,7 @@ - + @@ -826,21 +921,21 @@ - + - + - + @@ -862,7 +957,7 @@ - + @@ -877,7 +972,7 @@ - + @@ -889,21 +984,22 @@ - + - - + + + - + @@ -911,14 +1007,14 @@ - + - + @@ -926,7 +1022,7 @@ - + @@ -944,71 +1040,87 @@ - + - - + + - - + + + + + + + + + + + + + - - + + - + - - - - - + + + - + - - + + - + - - + + - + - - + + - - + - + + + + + + + + + - - + + diff --git a/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java b/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java index a087b72..d1f26e2 100644 --- a/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java +++ b/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java @@ -13,7 +13,7 @@ public class SimulatorConfig { */ public static String sunName = "Sun"; // The name of the sun to calculate values TO - public static String[] objectNames = { "Sedna" }; // The name of the object(s) your want to calculate the values OF + public static String[] objectNames = { "Jupiter", "Sedna" }; // The name of the object(s) your want to calculate the values OF /** * Output preferences diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java new file mode 100644 index 0000000..9362127 --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java @@ -0,0 +1,47 @@ +package com.verictas.pos.simulator.dataWriter; + +import com.verictas.pos.simulator.Object; +import com.verictas.pos.simulator.Simulator; +import com.verictas.pos.simulator.SimulatorConfig; +import com.verictas.pos.simulator.mathUtils.AU; +import com.verictas.pos.simulator.processor.ObjectProcessor; + +import javax.vecmath.Vector3d; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.math.RoundingMode; +import java.text.*; +import java.util.*; + +public class AOPDataWriter extends DataWriter { + public AOPDataWriter() throws WritingException { + super("arguments"); + try { + + /** + * Write the lines with information about the columns + */ + + this.writer.write("OBJECT" + DELIMITER + "ROUND" + DELIMITER + "ARGUMENT (RAD)" + NEW_LINE); + this.counter++; + } catch (Exception e) { + e.printStackTrace(); + throw new WritingException("An error occurred while writing to the file!"); + } + } + + public void write(String object, TreeMap arguments) throws WritingException { + try { + for (Map.Entry entry : arguments.entrySet()) { + Integer key = entry.getKey(); + Double value = entry.getValue(); + this.writer.append(object + DELIMITER + key + DELIMITER + decimalFormatter(value) + NEW_LINE); + this.counter++; + } + } catch (Exception e) { + e.printStackTrace(); + throw new WritingException("An error occurred while writing to the file!"); + } + } +} \ No newline at end of file diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java index 91ef51a..4173cba 100644 --- a/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java @@ -16,15 +16,15 @@ import java.util.Date; import java.util.Locale; public class DataWriter { - private FileWriter writer = null; + protected FileWriter writer = null; /** * Set global variables, such as the delimiter and the new line character */ - private static final String DELIMITER = "\t"; - private static final String NEW_LINE = "\n"; + protected static final String DELIMITER = "\t"; + protected static final String NEW_LINE = "\n"; - private int counter = 0; + protected int counter = 0; /** * Decimal formatter @@ -36,7 +36,7 @@ public class DataWriter { * Constructor * @throws WritingException */ - public DataWriter() throws WritingException { + public DataWriter(String filenameAppendix) throws WritingException { /** * Prepare the locale @@ -49,7 +49,7 @@ public class DataWriter { String directory = System.getProperty("user.home") + File.separator + "simulatorExports"; File directoryPath = new File(directory); - String path = directory + File.separator + getCurrentTimeStamp() + ".txt"; + String path = directory + File.separator + getCurrentTimeStamp() + "-" + filenameAppendix + ".txt"; System.out.println("WRITING DATA TO: " + path); /** @@ -64,18 +64,6 @@ public class DataWriter { */ this.writer = new FileWriter(path); - /** - * Write the lines with information about the columns - */ - - if (SimulatorConfig.outputUnit.equals("AU")) { - this.writer.write("Object" + DELIMITER + "X (AU)" + DELIMITER + "Y (AU)" + DELIMITER + "Z (AU)" + DELIMITER + "VX (AU/day)" + DELIMITER + "VY (AU/day)" + DELIMITER + "VZ (AU/day)" + NEW_LINE); - } else { - this.writer.write("Object" + DELIMITER + "X (m)" + DELIMITER + "Y (m)" + DELIMITER + "Z (m)" + DELIMITER + "VX (m/s)" + DELIMITER + "VY (m/s)" + DELIMITER + "VZ (m/s)" + NEW_LINE); - } - - this.counter++; - /** * Configure the decimal formatter */ @@ -118,39 +106,7 @@ public class DataWriter { } } - /** - * - * @param object The object you want to write data about - * @param reference The system's star - * @throws WritingException - */ - public void write(Object object, Object reference) throws WritingException { - String id = object.name; - Vector3d position = object.position; - Vector3d speed = object.speed; - Vector3d AUposition = AU.convertFromMeter(position); - Vector3d AUspeed = AU.convertFromMetersPerSecond(speed); - - if (this.writer == null) { - throw new WritingException("The writer isn't defined yet"); - } else { - try { - if (this.counter % SimulatorConfig.skipLines == 0) { - if (SimulatorConfig.outputUnit.equals("AU")) { - this.writer.append(id + DELIMITER + decimalFormatter(AUposition.getX()) + DELIMITER + decimalFormatter(AUposition.getY()) + DELIMITER + decimalFormatter(AUposition.getZ()) + DELIMITER + decimalFormatter(AUspeed.getX()) + DELIMITER + decimalFormatter(AUspeed.getY()) + DELIMITER + decimalFormatter(AUspeed.getZ()) + NEW_LINE); - } else { - this.writer.append(id + DELIMITER + decimalFormatter(position.getX()) + DELIMITER + decimalFormatter(position.getY()) + DELIMITER + decimalFormatter(position.getZ()) + DELIMITER + decimalFormatter(speed.getX()) + DELIMITER + decimalFormatter(speed.getY()) + DELIMITER + decimalFormatter(speed.getZ()) + NEW_LINE); - } - } - this.counter++; - } catch (Exception e) { - e.printStackTrace(); - throw new WritingException("An error occurred while writing to the file!"); - } - } - } - - private String decimalFormatter(double input) { + protected String decimalFormatter(double input) { return this.formatter.format(input); } diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java new file mode 100644 index 0000000..6944f4a --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java @@ -0,0 +1,70 @@ +package com.verictas.pos.simulator.dataWriter; + +import com.verictas.pos.simulator.Object; +import com.verictas.pos.simulator.Simulator; +import com.verictas.pos.simulator.SimulatorConfig; +import com.verictas.pos.simulator.mathUtils.AU; +import com.verictas.pos.simulator.processor.ObjectProcessor; + +import javax.vecmath.Vector3d; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.math.RoundingMode; +import java.text.*; +import java.util.Date; +import java.util.Locale; + +public class PosDataWriter extends DataWriter { + public PosDataWriter() throws WritingException { + super("position"); + try { + + /** + * Write the lines with information about the columns + */ + + if (SimulatorConfig.outputUnit.equals("AU")) { + this.writer.write("Object" + DELIMITER + "X (AU)" + DELIMITER + "Y (AU)" + DELIMITER + "Z (AU)" + DELIMITER + "VX (AU/day)" + DELIMITER + "VY (AU/day)" + DELIMITER + "VZ (AU/day)" + NEW_LINE); + } else { + this.writer.write("Object" + DELIMITER + "X (m)" + DELIMITER + "Y (m)" + DELIMITER + "Z (m)" + DELIMITER + "VX (m/s)" + DELIMITER + "VY (m/s)" + DELIMITER + "VZ (m/s)" + NEW_LINE); + } + + this.counter++; + } catch (Exception e) { + e.printStackTrace(); + throw new WritingException("An error occurred while writing to the file!"); + } + } + + /** + * + * @param object The object you want to write data about + * @throws WritingException + */ + public void write(Object object) throws WritingException { + String id = object.name; + Vector3d position = object.position; + Vector3d speed = object.speed; + Vector3d AUposition = AU.convertFromMeter(position); + Vector3d AUspeed = AU.convertFromMetersPerSecond(speed); + + if (this.writer == null) { + throw new WritingException("The writer isn't defined yet"); + } else { + try { + if (this.counter % SimulatorConfig.skipLines == 0) { + if (SimulatorConfig.outputUnit.equals("AU")) { + this.writer.append(id + DELIMITER + decimalFormatter(AUposition.getX()) + DELIMITER + decimalFormatter(AUposition.getY()) + DELIMITER + decimalFormatter(AUposition.getZ()) + DELIMITER + decimalFormatter(AUspeed.getX()) + DELIMITER + decimalFormatter(AUspeed.getY()) + DELIMITER + decimalFormatter(AUspeed.getZ()) + NEW_LINE); + } else { + this.writer.append(id + DELIMITER + decimalFormatter(position.getX()) + DELIMITER + decimalFormatter(position.getY()) + DELIMITER + decimalFormatter(position.getZ()) + DELIMITER + decimalFormatter(speed.getX()) + DELIMITER + decimalFormatter(speed.getY()) + DELIMITER + decimalFormatter(speed.getZ()) + NEW_LINE); + } + } + this.counter++; + } catch (Exception e) { + e.printStackTrace(); + throw new WritingException("An error occurred while writing to the file!"); + } + } + } +} \ No newline at end of file diff --git a/simulator/src/com/verictas/pos/simulator/processor/Processor.java b/simulator/src/com/verictas/pos/simulator/processor/Processor.java index 9f6ba43..82f313d 100644 --- a/simulator/src/com/verictas/pos/simulator/processor/Processor.java +++ b/simulator/src/com/verictas/pos/simulator/processor/Processor.java @@ -3,25 +3,31 @@ package com.verictas.pos.simulator.processor; import com.verictas.pos.simulator.Object; import com.verictas.pos.simulator.Simulator; import com.verictas.pos.simulator.SimulatorConfig; +import com.verictas.pos.simulator.dataWriter.AOPDataWriter; import com.verictas.pos.simulator.dataWriter.DataWriter; +import com.verictas.pos.simulator.dataWriter.PosDataWriter; import com.verictas.pos.simulator.dataWriter.WritingException; import com.verictas.pos.simulator.mathUtils.AOP; import com.verictas.pos.simulator.mathUtils.AU; +import javax.xml.crypto.Data; import java.util.ArrayList; import java.util.HashMap; +import java.util.TreeMap; public class Processor { - private DataWriter writer; + private PosDataWriter writer; + private AOPDataWriter aopWriter; public HashMap initialObjectValues = new HashMap<>(); public HashMap objects = new HashMap<>(); - public HashMap> arguments = new HashMap<>(); + public HashMap> arguments = new HashMap<>(); public Processor(Object[] objects) throws ProcessingException, WritingException { /** * Initialize DataWriter */ - this.writer = new DataWriter(); + this.writer = new PosDataWriter(); + this.aopWriter = new AOPDataWriter(); /** * Store the initial values of all the objects in memory (and to a file) for later use @@ -53,11 +59,10 @@ public class Processor { if (Simulator.round % SimulatorConfig.moduloArgument == 0) { if (arguments.get(objectName) == null) { // If not defined - ArrayList agmnts = new ArrayList<>(); + TreeMap agmnts = new TreeMap<>(); arguments.put(objectName, agmnts); } - - arguments.get(objectName).add(object.calculateAOP()); + arguments.get(objectName).put(Simulator.round, object.calculateAOP()); } this.objects.put(objectName, object); @@ -69,11 +74,11 @@ public class Processor { private void write(HashMap objects) throws ProcessingException, WritingException { if (SimulatorConfig.skipUnnecessary) { for (String name : SimulatorConfig.objectNames) { - this.writer.write(objects.get(name), objects.get(SimulatorConfig.sunName)); + this.writer.write(objects.get(name)); } } else { for (Object object : objects.values()) { - this.writer.write(object, objects.get(SimulatorConfig.sunName)); + this.writer.write(object); } } } @@ -92,30 +97,41 @@ public class Processor { public void close() throws ProcessingException { try { this.writer.save(); + System.out.println(""); System.out.println("TOTAL RESULTS: " + arguments); + System.out.println(""); for(String objectName : SimulatorConfig.objectNames) { - ArrayList arguments = this.arguments.get(objectName); + TreeMap arguments = this.arguments.get(objectName); + + this.aopWriter.write(objectName, arguments); + double score = 0; + Double[] empty = new Double[arguments.size()]; + Double[] agmnts = arguments.values().toArray(empty); + // Calculate score - for(int i = 1; i < arguments.size() - 1; i++) { - score = score + Math.abs(arguments.get(i-1) - arguments.get(i)); + for(int i = 1; i < agmnts.length - 1; i++) { + score = score + Math.abs(agmnts[i-1] - agmnts[i]); } System.out.println("SCORE (" + objectName + "): " + score); // CALCULATE AVERAGE double sum = 0; - for (int i = 0; i < arguments.size(); i++){ - sum = sum + arguments.get(i); + for (int i = 0; i < agmnts.length; i++){ + sum = sum + agmnts[i]; } // calculate average - double average = sum / arguments.size(); + double average = sum / agmnts.length; System.out.println("AVERAGE (" + objectName + ") (degrees): " + Math.toDegrees(average)); + System.out.println(""); } + + this.aopWriter.save(); } catch(WritingException e) { throw new ProcessingException("An error occurred during creation of the file writer: " + e.toString()); }