From aeee8ca7487aa9c85f11d706a53568742f038a14 Mon Sep 17 00:00:00 2001 From: fjc40 Date: Wed, 10 May 2017 15:11:51 +1200 Subject: [PATCH] Added polar data file. Added PolarParse class - currently commented out as it depends on polar table class. #story[900] --- .../java/seng302/DataInput/PolarParser.java | 170 ++++++++++++++++++ .../Exceptions/InvalidPolarFileException.java | 28 +++ mock/src/main/resources/polars/acc_polars.csv | 8 + .../seng302/DataInput/PolarParserTest.java | 26 +++ 4 files changed, 232 insertions(+) create mode 100644 mock/src/main/java/seng302/Exceptions/InvalidPolarFileException.java create mode 100644 mock/src/main/resources/polars/acc_polars.csv create mode 100644 mock/src/test/java/seng302/DataInput/PolarParserTest.java diff --git a/mock/src/main/java/seng302/DataInput/PolarParser.java b/mock/src/main/java/seng302/DataInput/PolarParser.java index e33612bb..475125a9 100644 --- a/mock/src/main/java/seng302/DataInput/PolarParser.java +++ b/mock/src/main/java/seng302/DataInput/PolarParser.java @@ -3,5 +3,175 @@ package seng302.DataInput; /** * Created by hba56 on 10/05/17. */ + +import seng302.Exceptions.InvalidPolarFileException; +import seng302.Model.Polar; + +import java.io.*; +import java.util.ArrayList; + + +/** + * Responsible for parsing a polar data file, and creating a polar data object. + */ public class PolarParser { + + + /** + * Given a filename, this function parses it and generates a PolarTable object, which can be queried for polar information. + * @param filename + * @return + */ + /* + ///TEMP PolarTable = ArrayList + public static ArrayList parse(String filename) throws InvalidPolarFileException { + //Temporary table to return later. + ArrayList polarTable = new ArrayList(); + + + //Open the file for reading. + InputStream fileStream = PolarParser.class.getClassLoader().getResourceAsStream(filename); + if (fileStream == null) { + throw new InvalidPolarFileException("Could not open polar data file: " + filename); + } + //Wrap it with buffered input stream to set encoding and buffer. + InputStreamReader in = null; + try { + in = new InputStreamReader(fileStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new InvalidPolarFileException("Unsupported encoding: UTF-8", e); + } + BufferedReader inputStream = new BufferedReader(in); + + + //We expect the polar data file to have the column headings: + // Tws, Twa0, Bsp0, Twa1, Bsp1, UpTwa, UpBsp, Twa2, Bsp2, Twa3, Bsp3, Twa4, Bsp4, Twa5, Bsp5, Twa6, Bsp6, DnTwa, DnBsp, Twa7, Bsp7 + //and to have 7 rows of data. + //Angles are expected to be in degrees, and velocities in knots. + + + //We read the heading and data rows, and split them into arrays of elements. + String[] headings; + ArrayList dataRows = new ArrayList<>(7); + try { + //Heading. + //Read heading row. + String headingRow = inputStream.readLine(); + + //Split it into individual headings. + headings = headingRow.split(","); + + + //Data rows. + while (inputStream.ready()) { + //Read line. + String dataRow = inputStream.readLine(); + + //Split line. + String[] dataElements = dataRow.split(","); + + //Add to collection. + dataRows.add(dataElements); + + } + + } catch (IOException e) { + throw new InvalidPolarFileException("Could not read from polar data file: " + filename, e); + } + + //Finished reading in data, now we need to construct polar rows and table from it. + //For each row... + int rowNumber = 0; + for (String[] row : dataRows) { + //Create Polar row object. + Polar polarRow = new Polar(); + + //For each column... + for (int i = 0; i < row.length; i++) { + + //Convert value to a double. + Double value; + try { + value = Double.parseDouble(row[i]); + } + catch (NumberFormatException e) { + throw new InvalidPolarFileException("Could not convert (Row,Col): (" + rowNumber + "," + i +") = " + row[i] + "to a double.", e); + } + + //Set the values of the row. + //For reference: + //Tws, Twa0, Bsp0, Twa1, Bsp1, UpTwa, UpBsp, Twa2, Bsp2, Twa3, Bsp3, Twa4, Bsp4, Twa5, Bsp5, Twa6, Bsp6, DnTwa, DnBsp, Twa7, Bsp7 + if (headings[i] == "Tws") { + polarRow.setTrueWindSpeed(value); + } + else if (headings[i] == "Twa0") { + polarRow.setTrueWindAngle0(value); + } + else if (headings[i] == "Bsp0") { + polarRow.setBoatSpeed0(value); + } + else if (headings[i] == "Twa1") { + polarRow.setTrueWindAngle1(value); + } + else if (headings[i] == "Bsp1") { + polarRow.setBoatSpeed1(value); + } + else if (headings[i] == "UpTwa") { + polarRow.setUpTrueWindAngel(value); + } + else if (headings[i] == "UpBsp") { + polarRow.setUpBoatSpeed(value); + } + else if (headings[i] == "Twa2") { + polarRow.setTrueWindAngle2(value); + } + else if (headings[i] == "Bsp2") { + polarRow.setBoatSpeed2(value); + } + else if (headings[i] == "Twa3") { + polarRow.setTrueWindAngle3(value); + } + else if (headings[i] == "Bsp3") { + polarRow.setBoatSpeed3(value); + } + else if (headings[i] == "Twa4") { + polarRow.setTrueWindAngle4(value); + } + else if (headings[i] == "Bsp4") { + polarRow.setBoatSpeed4(value); + } + else if (headings[i] == "Twa5") { + polarRow.setTrueWindAngle5(value); + } + else if (headings[i] == "Bsp5") { + polarRow.setBoatSpeed5(value); + } + else if (headings[i] == "Twa6") { + polarRow.setTrueWindAngle6(value); + } + else if (headings[i] == "Bsp6") { + polarRow.setBoatSpeed6(value); + } + else if (headings[i] == "Twa7") { + polarRow.setTrueWindAngle7(value); + } + else if (headings[i] == "Bsp7") { + polarRow.setBoatSpeed7(value); + } + + //Add the polar row to the polar table. + polarTable.add(polarRow); + + } + + //Increment row number. + rowNumber++; + + } + + + return polarTable; + } +*/ + } diff --git a/mock/src/main/java/seng302/Exceptions/InvalidPolarFileException.java b/mock/src/main/java/seng302/Exceptions/InvalidPolarFileException.java new file mode 100644 index 00000000..cc997c67 --- /dev/null +++ b/mock/src/main/java/seng302/Exceptions/InvalidPolarFileException.java @@ -0,0 +1,28 @@ +package seng302.Exceptions; + +/** + * Created by f123 on 10-May-17. + */ + +/** + * An exception thrown when we cannot parse a polar data file. + */ +public class InvalidPolarFileException extends RuntimeException { + + /** + * Constructs the exception with a given message. + * @param message Message to store. + */ + public InvalidPolarFileException(String message) { + super(message); + } + + /** + * Constructs the exception with a given message and cause. + * @param message Message to store. + * @param cause Cause to store. + */ + public InvalidPolarFileException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/mock/src/main/resources/polars/acc_polars.csv b/mock/src/main/resources/polars/acc_polars.csv new file mode 100644 index 00000000..ee7ea80e --- /dev/null +++ b/mock/src/main/resources/polars/acc_polars.csv @@ -0,0 +1,8 @@ +Tws,Twa0,Bsp0,Twa1,Bsp1,UpTwa,UpBsp,Twa2,Bsp2,Twa3,Bsp3,Twa4,Bsp4,Twa5,Bsp5,Twa6,Bsp6,DnTwa,DnBsp,Twa7,Bsp7 +4,0,0,30,4,45,8,60,9,75,10,90,10,115,10,145,10,155,10,175,4 +8,0,0,30,7,43,10,60,11,75,11,90,11,115,12,145,12,153,12,175,10 +12,0,0,30,11,43,14.4,60,16,75,20,90,23,115,24,145,23,153,21.6,175,14 +16,0,0,30,12,42,19.2,60,25,75,27,90,31,115,32,145,30,153,28.8,175,20 +20,0,0,30,13,41,24,60,29,75,37,90,39,115,40,145,38,153,36,175,24 +25,0,0,30,15,40,30,60,38,75,44,90,49,115,50,145,49,151,47,175,30 +30,0,0,30,15,42,30,60,37,75,42,90,48,115,49,145,48,150,46,175,32 diff --git a/mock/src/test/java/seng302/DataInput/PolarParserTest.java b/mock/src/test/java/seng302/DataInput/PolarParserTest.java new file mode 100644 index 00000000..0bb06326 --- /dev/null +++ b/mock/src/test/java/seng302/DataInput/PolarParserTest.java @@ -0,0 +1,26 @@ +package seng302.DataInput; + +import org.testng.annotations.Test; + +import java.io.File; + +import static org.testng.Assert.*; + +/** + * Created by f123 on 10-May-17. + */ +public class PolarParserTest { + + @Test + /** + * Tests if we can parse a polar data file (stored in a string), and create a polar table. + */ + public void testParse() throws Exception { + + + //Polars = PolarParser.parse("polars/acc_polars.csv"); + + + } + +}