Merge branch 'story30_XMLs' into splitIntoTwoModules

This commit is contained in:
Joseph Gardner
2017-04-25 14:01:09 +12:00
46 changed files with 3082 additions and 233 deletions
@@ -133,7 +133,7 @@ public class RaceController extends Controller {
//Initialize save annotation array, fps listener, and annotation listeners
//timezone
RaceClock raceClock = new RaceClock(raceData.getMark());
RaceClock raceClock = new RaceClock(raceData.getZonedDateTime());
timeZone.setText(raceClock.getTimeZone());
initializeFPS();
initializeAnnotations();
@@ -103,8 +103,8 @@ public class StartController extends Controller {
boatNameColumn.setCellValueFactory(cellData -> cellData.getValue().getName());
boatCodeColumn.setCellValueFactory(new PropertyValueFactory<>("abbrev"));
//timezone
raceClock = new RaceClock(raceData.getMark());
timeZoneTime.textProperty().bind(raceClock.timeProperty());
raceClock = new RaceClock(raceData.getZonedDateTime());
timeZoneTime.textProperty().bind(raceClock.timeStringProperty());
}
@@ -0,0 +1,156 @@
package seng302.Mock;
import javafx.scene.paint.Color;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.text.ParseException;
import java.util.*;
/**
* Created by Joseph on 24/04/2017.
*/
public class BoatXMLReader extends XMLReader {
Map<Integer, StreamedBoat> streamedBoatMap = new HashMap<>();
Map<Integer, StreamedBoat> participants = new HashMap<>();
private List<Color> colours;
private static int currentColourIndex = 0;
/**
* Constructor for Boat XML Reader
* @param filePath path of the file
* @throws IOException error
* @throws SAXException error
* @throws ParserConfigurationException error
*/
public BoatXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException, ParseException {
this(filePath, false);
makeColours();
}
/**
* Constructor for Boat XML Reader
* @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 BoatXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException {
super(filePath);
makeColours();
if (read) {
read();
}
}
public void read() {
readSettings();
readShapes();
readBoats();
}
/**
* Reads boats settings.
* INFORMATION FROM HERE IS IGNORED FOR NOW
*/
private void readSettings() {
}
/**
* Reads different kinds of boat.
* INFORMATION FROM HERE IS IGNORED FOR NOW
*/
private void readShapes() {
}
/**
* Reads the boats in the race
*/
private void readBoats() {
Element nBoats = (Element) doc.getElementsByTagName("Boats").item(0);
for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) {
Node boat = nBoats.getChildNodes().item(i);
if (boat.getNodeName().equals("Boat")) {
readSingleBoat(boat);
currentColourIndex = (currentColourIndex + 1) % colours.size();
}
}
}
/**
* Reads the information about one boat
* Ignored values: ShapeID, StoweName, HullNum, Skipper
*/
private void readSingleBoat(Node boat) {
StreamedBoat streamedBoat;
String type = "None";
int sourceID = -1;
String boatName = null;
String shortName = null;
String country = null;
if (exists(boat, "Type")) type = boat.getAttributes().getNamedItem("Type").getTextContent();
if (exists(boat, "SourceID")) sourceID = Integer.parseInt(boat.getAttributes().getNamedItem("SourceID").getTextContent());
if (exists(boat, "BoatName")) boatName = boat.getAttributes().getNamedItem("BoatName").getTextContent();
if (exists(boat, "ShortName")) shortName = boat.getAttributes().getNamedItem("ShortName").getTextContent();
if (exists(boat, "Country")) country = boat.getAttributes().getNamedItem("Country").getTextContent();
// Ignore all non participating boats
if (participants.containsKey(sourceID)) {
if (!streamedBoatMap.containsKey(sourceID)) {
if (country != null) {
streamedBoat = new StreamedBoat(sourceID, boatName, colours.get(currentColourIndex), country);
} else {
streamedBoat = new StreamedBoat(sourceID, boatName, colours.get(currentColourIndex), shortName);
}
streamedBoatMap.put(sourceID, streamedBoat);
// Override boat with new boat
participants.put(sourceID, streamedBoat);
}
for (int i = 0; i < boat.getChildNodes().getLength(); i++) {
Node GPSPosition = boat.getChildNodes().item(i);
if (GPSPosition.getNodeName().equals("GPSposition")) readBoatPositionInformation(sourceID, GPSPosition);
}
}
}
/**
* Reads the positional information about a boat
* Ignored values: FlagPosition, MastTop, Z value of GPSposition
*/
private void readBoatPositionInformation(int sourceID, Node GPSPosition) {
// TODO Get relative point before implementing. (GPSposition is based off a relative point).
}
private void makeColours() {
colours = new ArrayList<Color>(Arrays.asList(
Color.BLUEVIOLET,
Color.BLACK,
Color.RED,
Color.ORANGE,
Color.DARKOLIVEGREEN,
Color.LIMEGREEN,
Color.PURPLE,
Color.DARKGRAY,
Color.YELLOW
));
}
public void setParticipants(Map<Integer, StreamedBoat> participants) {
this.participants = participants;
}
public Map<Integer, StreamedBoat> getStreamedBoatMap() {
return streamedBoatMap;
}
}
@@ -3,6 +3,7 @@ package seng302.Mock;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.RaceDataSource;
import seng302.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
@@ -0,0 +1,44 @@
package seng302.Mock;
import javafx.scene.paint.Color;
import seng302.GPSCoordinate;
import seng302.Model.Boat;
import seng302.Model.BoatInRace;
/**
* Created by Joseph on 24/04/2017.
*/
public class StreamedBoat extends BoatInRace {
private int sourceID;
private boolean complete;
public StreamedBoat(int sourceID, String name, Color colour, String abbrev) {
super(name, colour, abbrev);
this.sourceID = sourceID;
this.complete = true;
}
public StreamedBoat(int sourceID) {
super("None", Color.BLACK, "None");
this.sourceID = sourceID;
this.complete = false;
}
/**
* Overridden to ignore this function
* @deprecated
* @return 0
*/
public double getScaledVelocity() {
return 0;
}
/**
* Overridden to ignore this function
* @deprecated
* @param velocity of boat
*/
public void setScaledVelocity(double velocity) {
}
}
@@ -0,0 +1,68 @@
package seng302.Mock;
import seng302.GPSCoordinate;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.RaceDataSource;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by jjg64 on 21/04/17.
*/
public class StreamedCourse implements RaceDataSource {
StreamedCourseXMLReader streamedCourseXMLReader = null;
BoatXMLReader boatXMLReader = null;
List<GPSCoordinate> boundary = new ArrayList<>();
public StreamedCourse(StreamedCourseXMLReader streamedCourseXMLReader) {
this.streamedCourseXMLReader = streamedCourseXMLReader;
}
public StreamedCourse(BoatXMLReader boatXMLReader) {
this.boatXMLReader = boatXMLReader;
}
public void setBoatXMLReader(BoatXMLReader boatXMLReader) {
this.boatXMLReader = boatXMLReader;
if (streamedCourseXMLReader != null) {
boatXMLReader.setParticipants(streamedCourseXMLReader.getParticipants());
boatXMLReader.read();
}
}
public void setStreamedCourseXMLReader(StreamedCourseXMLReader streamedCourseXMLReader) {
this.streamedCourseXMLReader = streamedCourseXMLReader;
if (streamedCourseXMLReader != null) {
boatXMLReader.setParticipants(streamedCourseXMLReader.getParticipants());
boatXMLReader.read();
}
}
public List<BoatInRace> getBoats() {
return null;
}
public List<Leg> getLegs() {
return null;
}
public List<GPSCoordinate> getBoundary() {
return null;
}
public ZonedDateTime getZonedDateTime() {
return null;
}
public GPSCoordinate getMapTopLeft() {
return null;
}
public GPSCoordinate getMapBottomRight() {
return null;
}
}
@@ -0,0 +1,163 @@
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<>();
private Map<Integer, StreamedBoat> participants = new HashMap<>();
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() {
Element nParticipants = (Element) doc.getElementsByTagName("Participants").item(0);
for (int i = 0; i < nParticipants.getChildNodes().getLength(); i++) {
int sourceID;
Node yacht = nParticipants.getChildNodes().item(i);
if (yacht.getNodeName().equals("Yacht")) {
if (exists(yacht, "SourceID")) {
sourceID = Integer.parseInt(yacht.getAttributes().getNamedItem("SourceID").getTextContent());
participants.put(sourceID, new StreamedBoat(sourceID));
}
}
}
}
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;
}
public Map<Integer, StreamedBoat> getParticipants() {
return participants;
}
}
@@ -0,0 +1,56 @@
package seng302.Mock;
import javafx.collections.FXCollections;
import seng302.Controllers.RaceController;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Model.Race;
import seng302.RaceDataSource;
import java.util.*;
/**
* Created by jjg64 on 21/04/17.
*/
public class StreamedRace extends Race {
private RaceDataSource raceData;
public StreamedRace(RaceDataSource raceData, RaceController controller, int scaleFactor) {
super(raceData.getBoats(), raceData.getLegs(), controller, scaleFactor);
this.raceData = raceData;
}
public void initialiseBoats() {
}
/**
* Checks if the boat cannot finish the race
* @return True if boat cannot finish the race
*/
protected boolean doNotFinish() {
return false;
}
/**
* Checks the position of the boat, this updates the boats current position.
*
* @param boat Boat that the postion is to be updated for.
* @param timeElapsed Time that has elapse since the start of the the race.
* @see BoatInRace
*/
protected void checkPosition(BoatInRace boat, long timeElapsed) {
}
/**
* Updates the boat's gps coordinates
*
* @param boat to be updated
* @param millisecondsElapsed time since last update
*/
protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
}
}
@@ -27,6 +27,17 @@ public class Boat {
this.name = new SimpleStringProperty(name);
}
/**
* Boat initialiser which keeps all of the information of the boat.
*
* @param name Name of the Boat.
* @param abbrev nam abbreviation
*/
public Boat(String name, String abbrev) {
this.abbrev = abbrev;
this.name = new SimpleStringProperty(name);
}
/**
* @return Name of the boat
*/
@@ -17,25 +17,25 @@ import java.util.concurrent.ConcurrentLinkedQueue;
*/
public class BoatInRace extends Boat {
private Leg currentLeg;
protected Leg currentLeg;
private double scaledVelocity;
private double distanceTravelledInLeg;
private GPSCoordinate currentPosition;
private long timeFinished;
private Color colour;
private boolean finished = false;
private StringProperty currentLegName;
private boolean started = false;
private StringProperty position;
private double heading;
protected double distanceTravelledInLeg;
protected GPSCoordinate currentPosition;
protected long timeFinished;
protected Color colour;
protected boolean finished = false;
protected StringProperty currentLegName;
protected boolean started = false;
protected StringProperty position;
protected double heading;
private Queue<TrackPoint> track = new ConcurrentLinkedQueue<>();
private long nextValidTime = 0;
protected Queue<TrackPoint> track = new ConcurrentLinkedQueue<>();
protected long nextValidTime = 0;
private static final float BASE_TRACK_POINT_TIME_INTERVAL = 5000;
private static float trackPointTimeInterval = 5000; // every 1 seconds
private final int TRACK_POINT_LIMIT = 10;
private boolean trackVisible = true;
protected static final float BASE_TRACK_POINT_TIME_INTERVAL = 5000;
protected static float trackPointTimeInterval = 5000; // every 1 seconds
protected final int TRACK_POINT_LIMIT = 10;
protected boolean trackVisible = true;
/**
* Constructor method.
@@ -52,6 +52,20 @@ public class BoatInRace extends Boat {
position = new SimpleStringProperty("-");
}
/**
* Constructor method.
*
* @param name Name of the boat.
* @param colour Colour the boat will be displayed as on the map
* @param abbrev of boat
*/
public BoatInRace(String name, Color colour, String abbrev) {
super(name, abbrev);
setColour(colour);
currentLegName = new SimpleStringProperty("");
position = new SimpleStringProperty("-");
}
/**
* Calculates the azimuth of the travel via map coordinates of the raceMarkers
*
@@ -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;
@@ -65,8 +65,8 @@ public abstract class Race implements Runnable {
public abstract void initialiseBoats();
/**
* Randomly generate number to see if boat fails
* @return True if number lower than dnfChance else false
* Checks if the boat cannot finish the race
* @return True if boat cannot finish the race
*/
protected abstract boolean doNotFinish();
@@ -80,7 +80,7 @@ public abstract class Race implements Runnable {
protected abstract void checkPosition(BoatInRace boat, long timeElapsed);
/**
* Updates the boat's gps coordinates depending on time elapsed
* Updates the boat's gps coordinates
*
* @param boat to be updated
* @param millisecondsElapsed time since last update
@@ -6,48 +6,62 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import seng302.GPSCoordinate;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
/**
* Created by Gondr on 19/04/2017.
*/
public class RaceClock {
private StringProperty time;
private DateTimeFormatter dateTimeFormatter;
private String timeZone;
private long lastTime;
private ZoneId zoneId;
private ZonedDateTime time;
private StringProperty timeString;
public RaceClock(GPSCoordinate gpsCoordinate){
public RaceClock(ZonedDateTime zonedDateTime) {
this.zoneId = zonedDateTime.getZone();
this.timeString = new SimpleStringProperty();
this.time = zonedDateTime;
}
public static ZonedDateTime getCurrentZonedDateTime(GPSCoordinate gpsCoordinate) {
TimeZoneLookup timeZoneLookup = new TimeZoneLookup();
TimeZoneResult timeZoneResult = timeZoneLookup.getTimeZone(gpsCoordinate.getLatitude(), gpsCoordinate.getLongitude());
zoneId = ZoneId.of(timeZoneResult.getResult());
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM HH:mm:ss z");
// System.out.println(dateTimeFormatter.format(zonedDateTime));
time = new SimpleStringProperty(dateTimeFormatter.format(zonedDateTime));
DateTimeFormatter timeZoneFormatter = DateTimeFormatter.ofPattern("z");
timeZone = timeZoneFormatter.format(zonedDateTime);
ZoneId zone = ZoneId.of(timeZoneResult.getResult());
return LocalDateTime.now(zone).atZone(zone);
}
public void updateTime(){
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
time.setValue(dateTimeFormatter.format(zonedDateTime));
/**
* Sets time to arbitrary zoned time.
* @param time arbitrary time with timezone.
*/
public void setTime(ZonedDateTime time) {
this.time = time;
this.timeString.set(DateTimeFormatter.ofPattern("dd-MM HH:mm:ss z").format(time));
this.lastTime = System.currentTimeMillis();
}
public String getTime() {
return time.get();
}
public StringProperty timeProperty() {
return time;
/**
* Updates time by duration elapsed since last update.
*/
public void updateTime() {
this.time.plus(Duration.of(System.currentTimeMillis() - this.lastTime, ChronoUnit.MILLIS));
this.lastTime = System.currentTimeMillis();
}
public String getTimeZone() {
return timeZone;
return zoneId.toString();
}
public String getTimeString() {
return timeString.get();
}
public StringProperty timeStringProperty() {
return timeString;
}
}
@@ -3,6 +3,7 @@ package seng302;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import java.time.ZonedDateTime;
import java.util.List;
/**
@@ -13,7 +14,7 @@ public interface RaceDataSource {
List<Leg> getLegs();
List<GPSCoordinate> getBoundary();
GPSCoordinate getMark();
ZonedDateTime getZonedDateTime();
GPSCoordinate getMapTopLeft();
GPSCoordinate getMapBottomRight();
}
@@ -4,12 +4,11 @@ import javafx.scene.paint.Color;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Model.Marker;
import seng302.Model.*;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
@@ -272,8 +271,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
return legs;
}
public GPSCoordinate getMark() {
return mark;
public ZonedDateTime getZonedDateTime() {
return RaceClock.getCurrentZonedDateTime(mark);
}
public GPSCoordinate getStartPt1() {
@@ -2,6 +2,7 @@ package seng302;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
@@ -33,6 +34,10 @@ public abstract class XMLReader {
return n.getElementsByTagName(tagName).item(0).getTextContent();
}
public boolean exists(Node node, String attribute) {
return node.getAttributes().getNamedItem(attribute) != null;
}
public String getAttribute(Element n, String attr) {
return n.getAttribute(attr);
}