Worked on XML reader to read a given race.xml in the format for the AC35. Added the ability to read the course limit and race settings #Pair[jjg64, cbt24] #Story[782]

This commit is contained in:
Joseph Gardner
2017-04-21 16:43:35 +12:00
parent 9e3ca68df8
commit 0d0b9e143f
8 changed files with 802 additions and 233 deletions
@@ -4,22 +4,20 @@ import seng302.GPSCoordinate;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.RaceDataSource;
import seng302.XMLReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* Created by jjg64 on 21/04/17.
*/
public class StreamedCourse implements RaceDataSource {
XMLReader xmlReader;
StreamedCourseXMLReader streamedCourseXMLReader;
List<GPSCoordinate> boundary = new ArrayList<>();
public StreamedCourse(XMLReader xmlReader) {
this.xmlReader = xmlReader;
public StreamedCourse(StreamedCourseXMLReader streamedCourseXMLReader) {
this.streamedCourseXMLReader = streamedCourseXMLReader;
}
public List<BoatInRace> getBoats() {
@@ -47,4 +45,5 @@ public class StreamedCourse implements RaceDataSource {
}
}
@@ -0,0 +1,149 @@
package seng302.Mock;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import seng302.GPSCoordinate;
import seng302.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by jjg64 on 21/04/17.
*/
public class StreamedCourseXMLReader extends XMLReader {
private static double COORDINATEPADDING = 0.0005;
private GPSCoordinate mapTopLeft, mapBottomRight;
private List<GPSCoordinate> boundary = new ArrayList<>();
Date creationTimeDate;
Date raceStartTime;
int raceID;
String raceType;
boolean postpone;
/**
* Constructor for Streamed Race XML
* @param filePath path of the file
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public StreamedCourseXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException, ParseException {
this(filePath, true);
}
/**
* Constructor for Streamed 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 StreamedCourseXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException {
super(filePath);
if (read) {
read();
}
}
private void read() throws ParseException {
readRace();
readParticipants();
readCourse();
}
private void readRace() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Element settings = (Element) doc.getElementsByTagName("Race").item(0);
raceID = Integer.parseInt(getTextValueOfNode(settings, "RaceID"));
raceType = getTextValueOfNode(settings, "RaceType");
creationTimeDate = dateFormat.parse(getTextValueOfNode(settings, "CreationTimeDate"));
NamedNodeMap raceTimeTag = doc.getElementsByTagName("RaceStartTime").item(0).getAttributes();
raceStartTime = dateFormat.parse(raceTimeTag.getNamedItem("Time").getTextContent());
postpone = Boolean.parseBoolean(raceTimeTag.getNamedItem("Postpone").getTextContent());
}
private void readParticipants() {
//TODO Modify BoatInRace to take in a sourceID
//TODO Produce list of BoatInRace
}
private void readCourse() {
readCompoundMarks();
readCompoundMarkSequence();
readCourseLimit();
}
private void readCompoundMarks() {
// TODO Produce map of sequence id to compound mark nodes (in xml)
// TODO Populate list of markers
}
private void readCompoundMarkSequence() {
// TODO Produce list of legs according to mark sequence ids
}
private void readCourseLimit() {
Element nCourseLimit = (Element) doc.getElementsByTagName("CourseLimit").item(0);
for(int i = 0; i < nCourseLimit.getChildNodes().getLength(); i++) {
Node limit = nCourseLimit.getChildNodes().item(i);
if (limit.getNodeName().equals("Limit")) {
double lat = Double.parseDouble(limit.getAttributes().getNamedItem("Lat").getTextContent());
double lon = Double.parseDouble(limit.getAttributes().getNamedItem("Lon").getTextContent());
boundary.add(new GPSCoordinate(lat, lon));
}
}
double maxLatitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
double maxLongitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
double minLatitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
double minLongitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
}
public List<GPSCoordinate> getBoundary() {
return boundary;
}
public GPSCoordinate getMapTopLeft() {
return mapTopLeft;
}
public GPSCoordinate getMapBottomRight() {
return mapBottomRight;
}
public Double getPadding() {
return COORDINATEPADDING;
}
public Date getCreationTimeDate() {
return creationTimeDate;
}
public Date getRaceStartTime() {
return raceStartTime;
}
public int getRaceID() {
return raceID;
}
public String getRaceType() {
return raceType;
}
public boolean isPostpone() {
return postpone;
}
}
@@ -9,7 +9,6 @@ import java.awt.geom.Point2D;
* Created by esa46 on 29/03/17.
*/
public class Marker {
private GPSCoordinate averageGPSCoordinate;
private GPSCoordinate mark1;
private GPSCoordinate mark2;
@@ -20,7 +20,7 @@ public class RaceClock {
private String timeZone;
private ZoneId zoneId;
public RaceClock(GPSCoordinate gpsCoordinate){
public RaceClock(GPSCoordinate gpsCoordinate) {
TimeZoneLookup timeZoneLookup = new TimeZoneLookup();
TimeZoneResult timeZoneResult = timeZoneLookup.getTimeZone(gpsCoordinate.getLatitude(), gpsCoordinate.getLongitude());
zoneId = ZoneId.of(timeZoneResult.getResult());
@@ -33,7 +33,7 @@ public class RaceClock {
timeZone = timeZoneFormatter.format(zonedDateTime);
}
public void updateTime(){
public void updateTime() {
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
time.setValue(dateTimeFormatter.format(zonedDateTime));
@@ -16,7 +16,7 @@ public class RegattaXMLTest {
@Before
public void findFile(){
try {
regattaXMLReader = new RegattaXMLReader("mockXML/regattaXML/regattaTest.xml", false);
regattaXMLReader = new RegattaXMLReader("mockXML/regattaXML/regattaTest.xml");
} catch (Exception e) {
fail("Cannot find mockXML/regattaXML/regattaTest.xml in the resources folder");
}
@@ -24,29 +24,19 @@ public class RegattaXMLTest {
@Test
public void makeRegattaTest() {
try {
regattaXMLReader = new RegattaXMLReader("mockXML/regattaXML/regattaTest.xml");
assertNotEquals(regattaXMLReader.getRegatta(), null);
} catch (Exception e) {
fail("Did not make a Regatta object");
}
assertNotEquals(regattaXMLReader.getRegatta(), null);
}
@Test
public void correctValuesTest() {
try {
regattaXMLReader = new RegattaXMLReader("mockXML/regattaXML/regattaTest.xml");
Regatta regatta = regattaXMLReader.getRegatta();
assertEquals(regatta.getRegattaID(), 3);
assertEquals(regatta.getRegattaName(), "New Zealand Test");
assertEquals(regatta.getCourseName(), "North Head");
assertEquals(regatta.getCentralLatitude(), -36.82791529, 0.00000001);
assertEquals(regatta.getCentralLongitude(), 174.81218919, 0.00000001);
assertEquals(regatta.getCentralAltitude(), 0.00, 0.00000001);
assertEquals(regatta.getUtcOffset(), 12.0, 0.001);
assertEquals(regatta.getMagneticVariation(), 14.1, 0.001);
} catch (Exception e) {
fail("Did not have the correct values");
}
Regatta regatta = regattaXMLReader.getRegatta();
assertEquals(regatta.getRegattaID(), 3);
assertEquals(regatta.getRegattaName(), "New Zealand Test");
assertEquals(regatta.getCourseName(), "North Head");
assertEquals(regatta.getCentralLatitude(), -36.82791529, 0.00000001);
assertEquals(regatta.getCentralLongitude(), 174.81218919, 0.00000001);
assertEquals(regatta.getCentralAltitude(), 0.00, 0.00000001);
assertEquals(regatta.getUtcOffset(), 12.0, 0.001);
assertEquals(regatta.getMagneticVariation(), 14.1, 0.001);
}
}
@@ -1,8 +1,63 @@
package seng302.Mock;
import org.junit.Before;
import org.junit.Test;
import seng302.GPSCoordinate;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Created by jjg64 on 21/04/17.
* Tests only work on the current version of mockXML/raceXML/raceTest.xml
*/
public class StreamedRaceTest {
StreamedCourseXMLReader streamedCourseXMLReader;
List<GPSCoordinate> boundary;
@Before
public void setup() {
try {
streamedCourseXMLReader = new StreamedCourseXMLReader("mockXML/raceXML/raceTest.xml", true);
boundary = streamedCourseXMLReader.getBoundary();
} catch (Exception e) {
e.printStackTrace();
//fail("Cannot find mockXML/raceXML/raceTest.xml in the resources folder");
}
}
@Test
public void testAllBoundaryPointsRead() {
assertEquals(boundary.size(), 10);
}
@Test
public void testBoundaryPointData() {
// First point
assertEquals(boundary.get(0).getLatitude(), -36.8325, 1e-6);
assertEquals(boundary.get(0).getLongitude(), 174.8325, 1e-6);
// Last point
assertEquals(boundary.get(boundary.size() - 1).getLatitude(), -36.83417, 1e-6);
assertEquals(boundary.get(boundary.size() - 1).getLongitude(), 174.84767, 1e-6);
}
@Test
public void testMapEdges() {
double maxLatitude = streamedCourseXMLReader.getMapBottomRight().getLatitude() - streamedCourseXMLReader.getPadding();
double maxLongitude = streamedCourseXMLReader.getMapBottomRight().getLongitude() - streamedCourseXMLReader.getPadding();
double minLatitude = streamedCourseXMLReader.getMapTopLeft().getLatitude() - streamedCourseXMLReader.getPadding();
double minLongitude = streamedCourseXMLReader.getMapTopLeft().getLongitude() - streamedCourseXMLReader.getPadding();
assertEquals(maxLatitude, -36.81033, 1e-6);
assertEquals(maxLongitude, 174.88217, 1e-6);
assertEquals(minLatitude, -36.83417, 1e-6);
assertEquals(minLongitude, 174.81983, 1e-6);
}
@Test
public void testRaceSettings() {
}
}