diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java deleted file mode 100644 index d8d3ba7..0000000 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java +++ /dev/null @@ -1,8 +0,0 @@ -package kcl.teamIndexZero.traffic.simulator.data; - -/** - * Created by Es on 02/03/2016. - */ -public class TrafficLightController { - -} diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRule.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRule.java index a9292c1..93cd10e 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRule.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRule.java @@ -1,8 +1,24 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; + +import java.util.List; + /** * Stand-alone Traffic Light rule description */ public class TrafficLightRule { + /** + * Changing single lights states + * + * @param trafficLightList - list with all single traffic lights + */ + public static void changeStateofSingleTrafficLights(List trafficLightList) { + + trafficLightList.forEach(TrafficLight -> { + TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; + }); + } } \ No newline at end of file diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java index 25e1bec..cb9a066 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java @@ -1,7 +1,26 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; + +import java.util.List; + /** * Traffic Light in synchronous set rule */ public class TrafficLightsInSetRule { + + /** + * Changes colours at trafficLights in one set + * + * @param trafficLightList List of the traffic lights in one set + */ + public static void changeStateofSet(List trafficLightList){ + if (trafficLightList != null ) { + for (TrafficLightInSet tf : trafficLightList) { + tf.currentState = (tf.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; + } + } + } } diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLight.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLight.java new file mode 100644 index 0000000..7b8e4f6 --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLight.java @@ -0,0 +1,51 @@ +package kcl.teamIndexZero.traffic.simulator.data.links; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import java.time.LocalDateTime; + +//import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightRule; +//import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightSet; +//import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; + + + +/** + * Created by Es on 02/03/2016. + */ +/* public class TrafficLight { + + private ID id; + + public TrafficLight() { + } + + public enum State { + RED, GREEN + } + + public State currentState; + + + + public TrafficLight(ID id) { + + this.id=id; + this.currentState = TrafficLight.State.RED; + + } + + + public State getState(ID id){ + return this.currentState; + } + + + + public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.id;} + + public void tick(SimulationTick tick) { + + } +} +*/ \ No newline at end of file diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightInSet.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightInSet.java new file mode 100644 index 0000000..0ab33d4 --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightInSet.java @@ -0,0 +1,54 @@ +package kcl.teamIndexZero.traffic.simulator.data.links; + +import kcl.teamIndexZero.traffic.simulator.ISimulationAware; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightSet; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + + + +/** + * Created by Es on 02/03/2016. + */ +/*public class TrafficLightInSet extends TrafficLight implements ISimulationAware { + public ID id; + public String temp; + public long CurrentTime; + public LocalDateTime Temporary; + public long lastChange = 0; + public long timer; + private TrafficLightmodel; + private TrafficLightSet modelSet; + SimulationTick simulationTick; + + + public TrafficLightInSet(ID id) { + this.id = id; + } + + + public long formatTimeToLong(LocalDateTime date ){ + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); + this. temp = date.format(formatter); + return Long.parseLong(temp, 10); + } + + + @Override + public void tick(SimulationTick tick) { + Temporary =simulationTick.getSimulatedTime(); + CurrentTime = formatTimeToLong(Temporary); + + if ((CurrentTime - lastChange) > timer) { + if (modelSet != null) { + // modelSet.changeColour(model, model.currentState); + lastChange = CurrentTime; + } + } + } +} +*/ \ No newline at end of file diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java index 726779e..8c988c6 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java @@ -1,30 +1,42 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; import kcl.teamIndexZero.traffic.simulator.data.ID; - +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; /** - * Stand-alone independent Traffic Light + * Created by Es on 02/03/2016. */ public class TrafficLight { private ID id; - private TrafficLightState currentState; - + public long TrafficLightDelay; + public TrafficLightState currentState; /** * Constructor * - * @param id Link ID tag + * @param id TrafficLight ID tag */ public TrafficLight(ID id) { - this.id = id; + + this.id=id; this.currentState = TrafficLightState.GREEN; + } /** - * Gets the current Traffic Light state + * Returns the current state of the TrafficLight object */ - public TrafficLightState getState() { + public TrafficLightState getState(){ return this.currentState; } + + /** + * Returns the ID of the current TrafficLight object + */ + public ID getTrafficLightID() { return this.id;} + + //@Override + public void tick(SimulationTick tick) { + + } } diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java index 8ff9c4f..942cdef 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java @@ -1,7 +1,22 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; +import kcl.teamIndexZero.traffic.log.Logger; +import kcl.teamIndexZero.traffic.log.Logger_Interface; import kcl.teamIndexZero.traffic.simulator.ISimulationAware; +//import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.ID; import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightRule; +import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightsInSetRule; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; + /** * Traffic Light controller @@ -9,9 +24,102 @@ */ public class TrafficLightController implements ISimulationAware { + public long CurrentTime; + public long lastChange = 0; + public long timer = 5; + public long TrafficLightDelay; + public long TrafficLightInSetDelay; + public static LocalDateTime start = LocalDateTime.of(1984, 12, 16, 7, 45, 55); + public List TrafficLightSinglesList; + public List TrafficLightSetList; + SimulationTick simulationTick; + private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightController.class.getSimpleName()); + + /** + * Constructor + */ + public TrafficLightController(){ + TrafficLightSinglesList = new ArrayList(); + TrafficLightSetList = new ArrayList(); + } + + /** + * Converts LocalDateTime to long + * + * @param date a LocalDateTime object + */ + public long formatTimeToLong(LocalDateTime date) { + return ChronoUnit.MILLIS.between(start, date); + } + + /** + * Adds single traffic light to the List of all single traffic lights + * + * @param trafficLight object to be added to the list + */ + public void addTrafficlight(TrafficLight trafficLight){ + + if (!(trafficLight == null)) { + TrafficLightSinglesList.add(trafficLight); + LOG.log("Added the following traffic lights to the set of all single Traffic Lights: ", trafficLight.getTrafficLightID() ); + } + else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + + } + + /** + * Returns a list List of all single traffic lights + * + */ + public List getSingleSet() {return TrafficLightSinglesList;} + + /** + * {@inheritDoc} + */ + @Override + public void tick(SimulationTick tick) { + CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); + + /*when single traffic lights do not work in a synchronous way with the ticks*/ + this.TrafficLightDelay = CurrentTime - lastChange; + + TrafficLightSinglesList.forEach(TrafficLight -> { + if ((this.TrafficLightDelay >= timer) && (TrafficLight.currentState == TrafficLightState.GREEN)) { + TrafficLight.currentState = TrafficLightState.RED; + } + }); + + /*when set's traffic lights do not work in a synchronous way with the ticks*/ + this.TrafficLightInSetDelay = CurrentTime - lastChange; + + TrafficLightSetList.forEach(TrafficLightInSet -> { + if ((this.TrafficLightInSetDelay >= timer) && (TrafficLightInSet.currentState == TrafficLightState.GREEN)){ + TrafficLightInSet.currentState = TrafficLightState.RED; + } + }); + + if ((CurrentTime - lastChange) > timer) { + if ((TrafficLightSetList != null) && (TrafficLightSinglesList != null)) { + TrafficLightsInSetRule.changeStateofSet(TrafficLightSetList); + TrafficLightRule.changeStateofSingleTrafficLights(TrafficLightSinglesList); + lastChange = CurrentTime; + } + } + } - @Override - public void tick(SimulationTick tick) { + /** + * Adds rule to the one traffic light + */ + public void addRule(TrafficLightRule rule){ + rule.changeStateofSingleTrafficLights(TrafficLightSinglesList); + } + /** + * Adds rule to the one traffic light set + */ + public void addRule(TrafficLightsInSetRule rule){ + rule.changeStateofSet(TrafficLightSetList); + } - } } diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java index 047b7e9..867a559 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java @@ -1,20 +1,44 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; import kcl.teamIndexZero.traffic.simulator.data.ID; - -import java.util.Date; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; /** * Traffic Light part of a synchronous set */ public class TrafficLightInSet { - long CurrentDate = (new Date().getTime()) / 1000; - long lastChange = CurrentDate; - long timer; - private TrafficLight model; - private TrafficLightSet modelSet; - - public TrafficLightInSet(ID linkId) { - //TODO Fix construction? + + private ID id; + public long TrafficLightInSetDelay; + public TrafficLightState currentState; + + /** + * Constructor + * + * @param id TrafficLightInSet ID tag + */ + public TrafficLightInSet(ID id) { + + this.id=id; + this.currentState = TrafficLightState.GREEN; + + } + + /** + * Returns the current state of the TrafficLightInSet + */ + public TrafficLightState getState(){ + return this.currentState; + } + + /** + * Returns the current state of the TrafficLightInSet + */ + public ID getID() { return this.id;} + + //@Override + public void tick(SimulationTick tick) { + } -} \ No newline at end of file +} diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java index 3d9cae2..c1b0230 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java @@ -1,8 +1,16 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; +import kcl.teamIndexZero.traffic.log.Logger; +import kcl.teamIndexZero.traffic.log.Logger_Interface; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; import kcl.teamIndexZero.traffic.simulator.ISimulationAware; import kcl.teamIndexZero.traffic.simulator.data.ID; import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; + +import java.util.List; +import java.util.*; /** * Traffic Light in a synchronous set @@ -10,7 +18,83 @@ public class TrafficLightSet implements ISimulationAware { private ID id; + public List InteriorListA; + public List InteriorListB; + private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightSet.class.getSimpleName()); + public enum TrafficLightSetGroup{ + GROUPA, + GROUPB + } + /** + * Constructor + * + * @param id TrafficLightSet ID + */ + public TrafficLightSet(ID id) { + this.id=id; + this.InteriorListA =new ArrayList(); + this.InteriorListB =new ArrayList(); + } + + /** + * Adds traffic lights to the List of traffic lights within one junction + * + * @param trafficLightInSet object to be added to the list + */ + public void addTrafficlight(TrafficLightInSet trafficLightInSet, TrafficLightSetGroup group ){ + + + if (group == TrafficLightSetGroup.GROUPA){ + + if (!(trafficLightInSet == null)) { + LOG.log("Traffic Lights type is the first type"); + //TrafficLightSetList.add(trafficLightInSet); + InteriorListA.add(trafficLightInSet); + + LOG.log("Added the following traffic lights: ", trafficLightInSet.getID(), " to the set: ", this.id); + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + + } + + if (group == TrafficLightSetGroup.GROUPB) { + if (trafficLightInSet != null) { + LOG.log("Traffic Lights type is the second type"); + //TrafficLightSetList.add(trafficLightInSet); + InteriorListB.add(trafficLightInSet); + + LOG.log("Added the following traffic lights: ", trafficLightInSet.getID(), " to the set: ", this.id); + + /* ********* + * changing initial states in the second group from GREEN to RED + ********** */ + for (TrafficLightInSet tf : InteriorListB) { + tf.currentState = TrafficLightState.RED; + } + + /* ********* + * end of changing initial states in the group B + ********** */ + + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + } + } + + + /** + * Returns a groupA from a list of traffic lights within one junction + * + * @param id of the junction, to which list is assigned + */ + public List getSetGroupA(ID id) {return this.InteriorListA;} + /** + * Returns the ID of the TrafficLightSet + */ + public ID getID() { return this.id;} @Override public void tick(SimulationTick tick) { diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRuleTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRuleTest.java new file mode 100644 index 0000000..c07cb5e --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRuleTest.java @@ -0,0 +1,55 @@ +package kcl.teamIndexZero.traffic.simulator.data.descriptors; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightController; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Created by JK on 20-03-16. + */ +public class TrafficLightRuleTest { + + private TrafficLightRule model; + private TrafficLight trafficLight; + private TrafficLightController controller; + private boolean flag = false; + + + @Before + public void setUp() throws Exception { + model = new TrafficLightRule(); + controller= new TrafficLightController(); + trafficLight = new TrafficLight(new ID("TrafficLightTest")); + } + + @After + public void tearDown() throws Exception { + model = null; + controller = null; + trafficLight = null; + } + + @Test + public void testChangeStateofSingleTrafficLights() throws Exception { + + controller.addTrafficlight(trafficLight); + model.changeStateofSingleTrafficLights(controller.TrafficLightSinglesList); + + for(TrafficLight tf : controller.TrafficLightSinglesList ) { + if(tf.currentState == TrafficLightState.RED){ + flag = true; + }; + } + assertEquals(flag, true); + + } +} \ No newline at end of file diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRuleTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRuleTest.java new file mode 100644 index 0000000..423a377 --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRuleTest.java @@ -0,0 +1,70 @@ +package kcl.teamIndexZero.traffic.simulator.data.descriptors; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightController; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by JK on 20-03-16. + */ +public class TrafficLightsInSetRuleTest { + + private TrafficLightsInSetRule model; + private TrafficLightInSet trafficLightInSetA; + private TrafficLightInSet trafficLightInSetB; + private TrafficLightSet trafficLightSet; + private TrafficLightController controller; + private boolean flagA = false; + private boolean flagB = false; + + @Before + public void setUp() throws Exception { + model = new TrafficLightsInSetRule(); + controller= new TrafficLightController(); + trafficLightInSetA = new TrafficLightInSet(new ID("TrafficLightInSetTest")); + trafficLightInSetB = new TrafficLightInSet(new ID("TrafficLightInSetTest")); + trafficLightSet = new TrafficLightSet(new ID("TrafficLightSetTest")); + } + + @After + public void tearDown() throws Exception { + model = null; + controller = null; + trafficLightInSetA = null; + trafficLightInSetB = null; + } + + @Test + public void testChangeStateofSet() throws Exception { + + trafficLightSet.addTrafficlight(trafficLightInSetA, TrafficLightSet.TrafficLightSetGroup.GROUPA); + trafficLightSet.addTrafficlight(trafficLightInSetB, TrafficLightSet.TrafficLightSetGroup.GROUPB); + model.changeStateofSet(trafficLightSet.InteriorListA); //Set A starts from GREEN so after change has RED + model.changeStateofSet(trafficLightSet.InteriorListB); // The opposite from above + + + for (TrafficLightInSet tf : trafficLightSet.InteriorListA) { + if (tf.currentState == TrafficLightState.RED) { + flagA = true; + }; + } + + for (TrafficLightInSet tf : trafficLightSet.InteriorListB) { + if (tf.currentState == TrafficLightState.GREEN) { + flagB = true; + }; + } + if (flagB == flagA) { + assertEquals(flagA, true); + } else { + assertEquals(false, true); + } + } +} \ No newline at end of file diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightControllerTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightControllerTest.java new file mode 100644 index 0000000..c66e28e --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightControllerTest.java @@ -0,0 +1,119 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; +import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightRule; +import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightsInSetRule; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.time.LocalDateTime; +import java.util.Date; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.*; + +/** + * Created by JK on 14-03-16. + */ +public class TrafficLightControllerTest { + + private TrafficLightController model; + private TrafficLightRule rule; + private TrafficLightsInSetRule setRule; + private TrafficLight trafficLight; + private TrafficLightInSet trafficLightInSetA; + private TrafficLightInSet trafficLightInSetB; + private TrafficLightSet trafficLightSet; + private boolean flag; + + + @Before + public void setUp() throws Exception { + model = new TrafficLightController(); + rule = new TrafficLightRule(); + setRule = new TrafficLightsInSetRule(); + trafficLightSet = new TrafficLightSet(new ID("TrafficLightSetTestA")); + trafficLight = new TrafficLight(new ID("TrafficLightTest")); + trafficLightInSetA = new TrafficLightInSet(new ID("TrafficLightInSetTestA")); + trafficLightInSetB = new TrafficLightInSet(new ID("TrafficLightInSetTestB")); + } + + @After + public void tearDown() throws Exception { + model = null; + rule = null; + setRule = null; + trafficLightSet = null; + trafficLight = null; + trafficLightInSetA = null; + trafficLightInSetB = null; + } + + @Test + public void testGetSingleSet() { + Assert.assertEquals(model.getSingleSet(), model.TrafficLightSinglesList); + } + + @Test + public void testAddRuleSingleTF() { + flag = false; + model.addTrafficlight(trafficLight);//At the beginning the State was GREEN + model.addRule(rule); //Should be RED by now + + for (TrafficLight tf : model.TrafficLightSinglesList) { + if (tf.currentState == TrafficLightState.RED) { + flag = true; + }; + } + Assert.assertTrue(flag); + } + + @Test + public void testAddRuleSetA() { + flag = false; + trafficLightSet.addTrafficlight(trafficLightInSetA, TrafficLightSet.TrafficLightSetGroup.GROUPA); //initially the State was GREEN + model.TrafficLightSetList = trafficLightSet.InteriorListA; + model.addRule(setRule); // Now should be RED + + if (model.TrafficLightSetList != null) { + for (TrafficLightInSet tf : model.TrafficLightSetList) { + if (tf.currentState == TrafficLightState.RED) { + flag = true; + }; + } + Assert.assertTrue(flag); + } + } + + @Test + public void testAddRuleSetB() { + flag = false; + trafficLightSet.addTrafficlight(trafficLightInSetB, TrafficLightSet.TrafficLightSetGroup.GROUPB); //initially the State was RED + model.TrafficLightSetList = trafficLightSet.InteriorListB; + model.addRule(setRule); // Now should be GREEN + + if (model.TrafficLightSetList != null) { + for (TrafficLightInSet tf : model.TrafficLightSetList) { + if (tf.currentState == TrafficLightState.GREEN) { + flag = true; + }; + } + Assert.assertTrue(flag); + } + } + + @Test + public void testFormatTimeToLong() { + LocalDateTime date = LocalDateTime.of(1984, 12, 16, 7, 45, 56); // 1 second = 1000 millisec. The "starting" date has 55 seconds in the end + long diff; + diff = model.formatTimeToLong(date); + Assert.assertTrue(diff == 1000L); + } +} \ No newline at end of file diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSetTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSetTest.java new file mode 100644 index 0000000..e6ed73c --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSetTest.java @@ -0,0 +1,33 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TrafficLightInSetTest { + + private TrafficLightInSet model; + + @Before + public void setUp() throws Exception { + model = new TrafficLightInSet(new ID("TrafficLightInSeTest")); + } + + @After + public void tearDown() throws Exception { + model = null; + } + + @Test + public void testGetState() throws Exception { + assertTrue(model.getState() == TrafficLightState.GREEN); + } + + @Test + public void testGetID() throws Exception { + assertEquals(model.getID(), new ID("TrafficLightInSeTest")); + } +} \ No newline at end of file diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSetTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSetTest.java new file mode 100644 index 0000000..9431937 --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSetTest.java @@ -0,0 +1,71 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.data.ID; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by JK on 20-03-16. + */ +public class TrafficLightSetTest { + + private TrafficLightSet model; + private TrafficLightInSet trafficLightInSetA; + private TrafficLightInSet trafficLightInSetB; + private boolean flagA = false; + private boolean flagB = false; + @Before + public void setUp() throws Exception { + model = new TrafficLightSet(new ID("TrafficLightSetTest")); + trafficLightInSetA = new TrafficLightInSet(new ID("TrafficLightInSetTest")); + trafficLightInSetB = new TrafficLightInSet(new ID("TrafficLightInSetTest")); + } + + @After + public void tearDown() throws Exception { + model = null; + trafficLightInSetA = null; + trafficLightInSetB = null; + } + + @Test + public void testAddTrafficlight() throws Exception { + model.addTrafficlight(trafficLightInSetA, TrafficLightSet.TrafficLightSetGroup.GROUPA); + model.addTrafficlight(trafficLightInSetB, TrafficLightSet.TrafficLightSetGroup.GROUPB); + + for (TrafficLightInSet tf : model.InteriorListA) { + if (tf.currentState == TrafficLightState.GREEN) { //should fulfill this statement as initial state is GREEN in this group + flagA = true; + }; + } + + for (TrafficLightInSet tf : model.InteriorListB) { + if (tf.currentState == TrafficLightState.RED) { //should fulfill this statement as initial state is GREEN in this group + flagB = true; + }; + } + + if (!flagA){ //flagA or flagB should not remain false! + assertEquals(false, true); + }; + if (!flagB){ + assertEquals(false, true); + }; + if (flagB == flagA) { + assertEquals(flagA, true); + } + } + + @Test + public void testGetSet() throws Exception { + assertEquals(model.getSetGroupA(new ID("TrafficLightSetTest")), model.InteriorListA); + } + + @Test + public void testGetID() throws Exception { + assertEquals(model.getID(), new ID("TrafficLightSetTest")); + } +} \ No newline at end of file diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightTest.java new file mode 100644 index 0000000..6ba1258 --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightTest.java @@ -0,0 +1,42 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import javafx.scene.chart.StackedAreaChart; +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.IDTest; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Created by JK on 14-03-16. + */ +public class TrafficLightTest { + private TrafficLight modelTrafficLight; + + + @Before + public void setUp() throws Exception { + modelTrafficLight = new TrafficLight(new ID("TrafficLightTest")); + } + + @After + public void tearDown() throws Exception { + modelTrafficLight = null; + } + + @Test + public void testgetState()throws Exception { + assertTrue(modelTrafficLight.getState() == TrafficLightState.GREEN); + } + + @Test + public void testgetTrafficLightID()throws Exception { + assertEquals(modelTrafficLight.getTrafficLightID(), new ID("TrafficLightTest")); + } +} \ No newline at end of file