Removed some unneccessary code and comments from PolarParser.parse(...).

Fixed a bug where the polar parser would attempt to insert (wind speed, windspeed, windangle) instead of (windspeed, windangle, boatspeed).
Removed some unused constructors from RaceXMLReader, and fixed a bug where it wouldn't actually use the polar table correctly.
Boat's constructor now expects a Polars table.
Added some comments to the Polars class, and specified the HashMap's template parameters
Added PolarsTest - which needs to be implemented properly when the VMG calculations have been added.

#story[900]
This commit is contained in:
fjc40
2017-05-10 17:21:18 +12:00
parent 4f8d6b14d4
commit 693da8a82a
14 changed files with 142 additions and 65 deletions
+3
View File
@@ -5,6 +5,7 @@ import javafx.application.Application;
import javafx.stage.Stage;
import org.xml.sax.SAXException;
import seng302.DataInput.*;
import seng302.Exceptions.InvalidPolarFileException;
import seng302.Model.Event;
import seng302.Model.Polars;
@@ -37,6 +38,8 @@ public class App extends Application {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (InvalidPolarFileException e) {
e.printStackTrace();
}
}
}
@@ -50,18 +50,13 @@ public class PolarParser {
//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;
//We read data rows, and split them into arrays of elements.
ArrayList<String[]> dataRows = new ArrayList<>(7);
try {
//Heading.
//Read heading row.
//Heading row.
//We skip the heading row by reading it.
String headingRow = inputStream.readLine();
//Split it into individual headings.
headings = headingRow.split(",");
//Data rows.
while (inputStream.ready()) {
//Read line.
@@ -83,19 +78,19 @@ public class PolarParser {
//For each row...
int rowNumber = 0;
for (String[] row : dataRows) {
//Create Polar row object.
//For each column...
for (int i = 0; i < row.length / 2; i += 2) {
//For each pair of columns (the pair is angle, speed).
//We start at column 1 since column 0 is the wind speed column.
// (row.length - 1) / 2 means the number of pairs of angle+speed.
for (int i = 1; i < (row.length - 1) / 2; i += 2) {
//Convert value to a double.
Double value;
//Add angle+speed=velocity estimate to polar table.
try {
//Add the polar value to the polar table
polarTable.addEstimate( Double.parseDouble(row[0]), Double.parseDouble(row[i]), Double.parseDouble(row[i + 1]));
}
catch (NumberFormatException e) {
throw new InvalidPolarFileException("Could not convert (Row,Col): (" + rowNumber + "," + i +") = " + row[i] + "to a double.", e);
throw new InvalidPolarFileException("Could not convert (Row,Col): (" + rowNumber + "," + i +") = " + row[i] + " to a double.", e);
}
}
@@ -104,7 +99,6 @@ public class PolarParser {
}
return polarTable;
}
@@ -25,48 +25,35 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
private GPSCoordinate mapTopLeft, mapBottomRight;
private List<GPSCoordinate> boundary = new ArrayList<>();
private List<Marker> markers = new ArrayList<>();
///Filename to read data from.
private String filePath;
///The polar table object to assign to each boat.
private Polars boatPolars;
/**
* Constractor for Race XML
*
* @param filePath path of the file
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public RaceXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException {
this(filePath, true);
}
/**
* Constractor for Race XML
*
* @param filePath path of the file
* @param filePath The path to the file to read data from.
* @param boatPolars The polar table to assign to each boat.
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public RaceXMLReader(String filePath, Polars boatPolars) throws IOException, SAXException, ParserConfigurationException {
this(filePath, true);
super(filePath);
this.filePath = filePath;
this.boatPolars = boatPolars;
read();
}
/**
* COnstructor for Race XML
*
* @param filePath file path to read
* @param read whether or not to read and store the files straight away.
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public RaceXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException {
super(filePath);
if (read) {
read();
}
}
/**
* Read the files
@@ -100,8 +87,9 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
String abbrev = getTextValueOfNode((Element) nBoats.item(i), "abbr");
double velo = Double.parseDouble(getTextValueOfNode((Element) nBoats.item(i), "speed"));
int sourceID = Integer.parseInt(getTextValueOfNode((Element) nBoats.item(i), "sourceID"));
Boat boat = new Boat(name, velo, abbrev, sourceID);
boat.setPolars(boatPolars);
Boat boat = new Boat(name, velo, abbrev, sourceID, this.boatPolars);
boat.setCurrentPosition(startPt1);
if (legs.size() > 0) {
boat.setCurrentLeg(legs.get(0));
+3 -1
View File
@@ -29,12 +29,14 @@ public class Boat {
* @param velocity Speed in m/s that the boat travels at.
* @param abbrev nam abbreviation
* @param sourceID id of boat
* @param polars The polars table to use for this boat.
*/
public Boat(String name, double velocity, String abbrev, int sourceID) {
public Boat(String name, double velocity, String abbrev, int sourceID, Polars polars) {
this.velocity = velocity;
this.abbrev = abbrev;
this.name = name;
this.sourceID = sourceID;
this.polars = polars;
}
/**
+29 -8
View File
@@ -7,19 +7,40 @@ import java.util.HashMap;
/**
* Created by hba56 on 10/05/17.
*/
public class Polars {
private HashMap polarValues;
/**
* Encapsulates an entire polar table. Has a function to calculate VMG.
*/
public class Polars {
///Internal store of data. Maps pair<windSpeed, windAngle> to boatSpeed.
private HashMap<Pair<Double, Double>, Double> polarValues = new HashMap<>();
/**
* Ctor.
*/
public Polars() {
polarValues = new HashMap();
}
public void addEstimate(double trueWindSpeed, double trueWindAngle, double boatSpeed){
Pair newKey = new Pair(trueWindSpeed, trueWindAngle);
polarValues.put(newKey, boatSpeed);
}
public HashMap getPolarValues() {
/**
* Adds an estimated velocity to the polar table object, for a given (windSpeed, windAngle) pair. That is, stores a mapping from (windSpeed, windAngle) to (boatVelocity).
* @param trueWindSpeed The true wind speed of the estimate.
* @param trueWindAngle The true wind angle of the estimate.
* @param boatSpeed The boat speed of the estimate.
*/
public void addEstimate(double trueWindSpeed, double trueWindAngle, double boatSpeed){
Pair newKey = new Pair(trueWindSpeed, trueWindAngle);
polarValues.put(newKey, boatSpeed);
}
//TODO calculate VMG from (windAngle, destAngle, windSpeed).
/**
* Returns the hashmap used to store polar data.
* @return A hashmap containing estimated boat speeds for a given (windSpeed, windAngle) pair.
*/
public HashMap<Pair<Double, Double>, Double> getPolarValues() {
return polarValues;
}
}