mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Loading course mark order from RaceXML
- Re-engineered code to work with the new marks - Fixed bug where race order wasn't correct (added RacePosition class to fix) - Rewrote tests to work with new RacePosition class Tags: #story[1124] (Task 1)
This commit is contained in:
@@ -1,14 +1,5 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -19,11 +10,21 @@ import seng302.model.stream.xml.parser.RaceXMLData;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.utilities.XMLParser;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class to hold the order of the marks in the race.
|
||||
*/
|
||||
public class MarkOrder {
|
||||
private List<CompoundMark> raceMarkOrder;
|
||||
private List<Mark> raceMarkOrder;
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
|
||||
public MarkOrder(){
|
||||
@@ -34,7 +35,7 @@ public class MarkOrder {
|
||||
* @return An ordered list of marks in the race
|
||||
* OR null if the mark order could not be loaded
|
||||
*/
|
||||
public List<CompoundMark> getMarkOrder(){
|
||||
public List<Mark> getMarkOrder(){
|
||||
if (raceMarkOrder == null){
|
||||
logger.warn("Race order accessed but not instantiated");
|
||||
return null;
|
||||
@@ -45,23 +46,25 @@ public class MarkOrder {
|
||||
|
||||
/**
|
||||
* Returns the mark in the race after the previous mark
|
||||
* @param previous The previous mark
|
||||
* @return the next mark
|
||||
* OR null if there is no next mark
|
||||
* @param position The current race position
|
||||
* @return the next race position
|
||||
* OR null if there is no position
|
||||
*/
|
||||
public CompoundMark getNextMark(CompoundMark previous){
|
||||
for (int i = 0; i < raceMarkOrder.size(); i++){
|
||||
CompoundMark mark = raceMarkOrder.get(i);
|
||||
public RacePosition getNextPosition(RacePosition position){
|
||||
Mark previousMark = position.getNextMark();
|
||||
Mark nextMark;
|
||||
|
||||
if (i + 1 >= raceMarkOrder.size()){
|
||||
return null;
|
||||
if (position.getPositionIndex() + 1 >= raceMarkOrder.size() - 1){
|
||||
RacePosition nextRacePosition = new RacePosition(raceMarkOrder.size() - 1, null, previousMark);
|
||||
nextRacePosition.setFinishingLeg();
|
||||
|
||||
return nextRacePosition;
|
||||
}
|
||||
|
||||
if (mark.equals(previous)){
|
||||
return raceMarkOrder.get(i+1);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
Integer nextPositionIndex = position.getPositionIndex() + 1;
|
||||
RacePosition nextRacePosition = new RacePosition(nextPositionIndex, raceMarkOrder.get(nextPositionIndex), previousMark);
|
||||
|
||||
return nextRacePosition;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +72,7 @@ public class MarkOrder {
|
||||
* @param xml An AC35 RaceXML
|
||||
* @return An ordered list of marks in the race
|
||||
*/
|
||||
private List<CompoundMark> loadRaceOrderFromXML(String xml){
|
||||
private List<Mark> loadRaceOrderFromXML(String xml){
|
||||
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db;
|
||||
@@ -87,14 +90,32 @@ public class MarkOrder {
|
||||
|
||||
if (data != null){
|
||||
logger.debug("Loaded RaceXML for mark order");
|
||||
List<CompoundMark> course = new ArrayList<>(data.getCompoundMarks().values());
|
||||
course.sort(Comparator.comparingInt(CompoundMark::getId));
|
||||
List<Corner> corners = data.getMarkSequence();
|
||||
Map<Integer, CompoundMark> marks = data.getCompoundMarks();
|
||||
List<Mark> course = new ArrayList<>();
|
||||
|
||||
for (Corner corner : corners){
|
||||
CompoundMark compoundMark = marks.get(corner.getCompoundMarkID());
|
||||
course.add(compoundMark.getMarks().get(0));
|
||||
}
|
||||
|
||||
return course;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The first position in the race
|
||||
*/
|
||||
public RacePosition getFirstPosition(){
|
||||
if (raceMarkOrder.size() > 0){
|
||||
return new RacePosition(-1, raceMarkOrder.get(0), null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the raceXML and mark order
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
/**
|
||||
* Represents a boats position between two marks
|
||||
*/
|
||||
public class RacePosition {
|
||||
private Integer positionIndex;
|
||||
private Mark nextMark;
|
||||
private Mark previousMark;
|
||||
private Boolean isFinishingLeg;
|
||||
|
||||
public RacePosition(Integer positionIndex, Mark nextMark, Mark previousMark){
|
||||
this.positionIndex = positionIndex;
|
||||
this.nextMark = nextMark;
|
||||
this.previousMark = previousMark;
|
||||
isFinishingLeg = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The position of the boat (0...number of marks in race - 1)
|
||||
*/
|
||||
public Integer getPositionIndex(){
|
||||
return positionIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading to
|
||||
* will return NULL if this is the finishing legg
|
||||
*/
|
||||
public Mark getNextMark(){
|
||||
return nextMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading away from,
|
||||
* Will return NULL if this is the starting leg
|
||||
*/
|
||||
public Mark getPreviousMark(){
|
||||
return previousMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flag that this is the last leg in the race
|
||||
*/
|
||||
public void setFinishingLeg(){
|
||||
isFinishingLeg = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this is the last leg in the race
|
||||
*/
|
||||
public boolean getIsFinishingLeg() {
|
||||
return isFinishingLeg;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
package seng302.models;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import seng302.model.mark.RacePosition;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class MarkOrderTest {
|
||||
private static MarkOrder markOrder;
|
||||
@@ -25,28 +26,6 @@ public class MarkOrderTest {
|
||||
assertTrue(markOrder != null);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Test if .equals() method on returns true on two marks that are equal
|
||||
// */
|
||||
// @Test
|
||||
// public void testMarkEqualsTrue(){
|
||||
// M mark1 = new SingleMark("asd", 1.1, 2.2, 1, 2);
|
||||
// Mark mark2 = new SingleMark("asd", 1.1, 2.2, 1, 2);
|
||||
//
|
||||
// assertEquals(mark1, mark2);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Test if .equals() method on returns false on two marks that are NOT equal
|
||||
// */
|
||||
// @Test
|
||||
// public void testMarkNotEquals(){
|
||||
// Mark mark1 = new SingleMark("asf", 1.1, 2.2, 2, 2);
|
||||
// Mark mark2 = new SingleMark("asd", 1.1, 2.2, 1, 2);
|
||||
//
|
||||
// assertNotEquals(mark1, mark2);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Test if .getNextMark() returns null if it is called with the final mark in the race
|
||||
*/
|
||||
@@ -57,19 +36,12 @@ public class MarkOrderTest {
|
||||
return;
|
||||
}
|
||||
|
||||
CompoundMark lastMark = markOrder.getMarkOrder().get(markOrder.getMarkOrder().size() - 1);
|
||||
Mark lastMark = markOrder.getMarkOrder().get(markOrder.getMarkOrder().size() - 1);
|
||||
Integer lastIndex = markOrder.getMarkOrder().size() - 1;
|
||||
|
||||
assertEquals(null, markOrder.getNextMark(lastMark));
|
||||
}
|
||||
RacePosition lastRacePosition = new RacePosition(lastIndex, lastMark, null);
|
||||
|
||||
/**
|
||||
* Test if .getNextMark() method on returns null if the mark does not exist in the race
|
||||
*/
|
||||
@Test
|
||||
public void testNextMarkNotExists(){
|
||||
CompoundMark someMark = new CompoundMark(1, "something");
|
||||
|
||||
assertEquals(null, markOrder.getNextMark(someMark));
|
||||
assertEquals(null, markOrder.getNextPosition(lastRacePosition).getNextMark());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,9 +54,26 @@ public class MarkOrderTest {
|
||||
return;
|
||||
}
|
||||
|
||||
CompoundMark firstMark = markOrder.getMarkOrder().get(0);
|
||||
RacePosition firstRacePos = new RacePosition(0, markOrder.getMarkOrder().get(0), null);
|
||||
|
||||
assertEquals(markOrder.getMarkOrder().get(1), markOrder.getNextMark(firstMark));
|
||||
assertEquals(markOrder.getMarkOrder().get(1).getName(), markOrder.getNextPosition(firstRacePos).getNextMark().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a whole race can be completed
|
||||
*/
|
||||
@Test
|
||||
public void testMarkSequence(){
|
||||
RacePosition current = markOrder.getFirstPosition();
|
||||
|
||||
while (!current.getIsFinishingLeg()){
|
||||
|
||||
current = markOrder.getNextPosition(current);
|
||||
|
||||
if (current.getIsFinishingLeg()){
|
||||
assertEquals(null, current.getNextMark());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
||||
Reference in New Issue
Block a user