From e7c72692b9bd7a97e947973d10d1c4bc82fa69be Mon Sep 17 00:00:00 2001 From: "E. A. Davison" Date: Wed, 9 Mar 2016 18:32:41 +0000 Subject: [PATCH 01/21] TrafficLight cleanup based on discussed model --- .../data/descriptors/TrafficLightRule.java | 2 +- .../descriptors/TrafficLightsInSetRule.java | 7 +++ .../simulator/data/links/TrafficLight.java | 52 ------------------- .../data/links/TrafficLightInSet.java | 38 -------------- .../simulator/data/links/TrafficLightSet.java | 44 ---------------- .../data/trafficLight/TrafficLight.java | 30 +++++++++++ .../trafficLight/TrafficLightController.java | 17 ++++++ .../data/trafficLight/TrafficLightInSet.java | 20 +++++++ .../data/trafficLight/TrafficLightSet.java | 19 +++++++ .../data/trafficLight/TrafficLightState.java | 9 ++++ 10 files changed, 103 insertions(+), 135 deletions(-) create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java delete mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLight.java delete mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightInSet.java delete mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightSet.java create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java create mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightState.java 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 efa40bd..a9292c1 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,7 +1,7 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; /** - * Created by Es on 02/03/2016. + * Stand-alone Traffic Light rule description */ public class TrafficLightRule { 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 new file mode 100644 index 0000000..25e1bec --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRule.java @@ -0,0 +1,7 @@ +package kcl.teamIndexZero.traffic.simulator.data.descriptors; + +/** + * Traffic Light in synchronous set rule + */ +public class TrafficLightsInSetRule { +} 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 deleted file mode 100644 index 2daab11..0000000 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLight.java +++ /dev/null @@ -1,52 +0,0 @@ -package kcl.teamIndexZero.traffic.simulator.data.links; - -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.links.TrafficLightSet; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; - - - -/** - * Created by Es on 02/03/2016. - */ -public class TrafficLight extends Link { - - - public enum State { - RED, GREEN - } - - public State currentState; - - - /** - * Constructor - * - * @param id Link ID tag - */ - public TrafficLight(ID id) { - - super(id); - this.currentState = TrafficLight.State.RED; - - } - - /** - * showState - method returns the current state of the Lights - * - * @param id Link ID tag - */ - public State showState(ID id){ - return this.currentState; - } - - /** - * {@inheritDoc} - */ - @Override - public void tick(SimulationTick tick) { - - } -} 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 deleted file mode 100644 index 4bbaf2b..0000000 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightInSet.java +++ /dev/null @@ -1,38 +0,0 @@ -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 java.util.Date; - -/** - * Created by Es on 02/03/2016. - */ -public /*abstract*/ class TrafficLightInSet extends TrafficLight implements ISimulationAware { - long CurrentDate = (new Date().getTime()) / 1000; - long lastChange = CurrentDate; - long timer; - private TrafficLight model; - private TrafficLightSet modelSet; - - public TrafficLightInSet(ID linkId) { - super(linkId); - //TODO Fix construction? - } - - /** - * {@inheritDoc} - */ - @Override - public void tick(SimulationTick tick) { - CurrentDate = (new Date().getTime()) / 1000; - - if (CurrentDate - lastChange > timer) { - if (modelSet != null) { - modelSet.changeColour(model, model.currentState); - lastChange = CurrentDate; - } - } - } -} \ No newline at end of file diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightSet.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightSet.java deleted file mode 100644 index 59f3f4f..0000000 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightSet.java +++ /dev/null @@ -1,44 +0,0 @@ -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 java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * Created by Es on 02/03/2016. - */ - -public class TrafficLightSet implements ISimulationAware { - - List lights = new ArrayList<>(); - private ID id; - private TrafficLight trafficLight; - private Map map; - - - public Map createLightsmap(ID id, TrafficLight trafficLight) { - map.put(id, trafficLight); - return map; - } - - - public TrafficLight.State changeColour(TrafficLight trafficLight, TrafficLight.State currentState) { - TrafficLight.State temp; - temp = (currentState == TrafficLight.State.RED) ? TrafficLight.State.GREEN : TrafficLight.State.RED; - return temp; - } - - public void addLights(TrafficLight light) { - lights.add(light); - } - - - @Override - public void tick(SimulationTick tick) { - - } -} 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 new file mode 100644 index 0000000..726779e --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java @@ -0,0 +1,30 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.data.ID; + + +/** + * Stand-alone independent Traffic Light + */ +public class TrafficLight { + private ID id; + private TrafficLightState currentState; + + + /** + * Constructor + * + * @param id Link ID tag + */ + public TrafficLight(ID id) { + this.id = id; + this.currentState = TrafficLightState.GREEN; + } + + /** + * Gets the current Traffic Light state + */ + public TrafficLightState getState() { + return this.currentState; + } +} 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 new file mode 100644 index 0000000..8ff9c4f --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java @@ -0,0 +1,17 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.ISimulationAware; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; + +/** + * Traffic Light controller + * Controls the traffic lights in the map based on their rules + */ +public class TrafficLightController implements ISimulationAware { + + + @Override + public void tick(SimulationTick tick) { + + } +} 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 new file mode 100644 index 0000000..047b7e9 --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSet.java @@ -0,0 +1,20 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.data.ID; + +import java.util.Date; + +/** + * 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? + } +} \ 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 new file mode 100644 index 0000000..3d9cae2 --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSet.java @@ -0,0 +1,19 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import kcl.teamIndexZero.traffic.simulator.ISimulationAware; +import kcl.teamIndexZero.traffic.simulator.data.ID; +import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; + +/** + * Traffic Light in a synchronous set + */ + +public class TrafficLightSet implements ISimulationAware { + private ID id; + + + @Override + public void tick(SimulationTick tick) { + + } +} diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightState.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightState.java new file mode 100644 index 0000000..7d278ff --- /dev/null +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightState.java @@ -0,0 +1,9 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +/** + * Describes the different possible traffic light state + */ +public enum TrafficLightState { + RED, + GREEN +} From fd020e4cdbf4d79161725a76ffa1fb2e2d8c9294 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Thu, 10 Mar 2016 04:43:51 +0000 Subject: [PATCH 02/21] Traffic Light adding getter method. --- .../simulator/data/links/TrafficLight.java | 19 ++++++++++++++----- .../data/links/TrafficLightInSet.java | 11 ++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) 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 index 2daab11..e3e7384 100644 --- 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 @@ -11,8 +11,9 @@ /** * Created by Es on 02/03/2016. */ -public class TrafficLight extends Link { +public class TrafficLight { + private ID id; public enum State { RED, GREEN @@ -28,24 +29,32 @@ public enum State { */ public TrafficLight(ID id) { - super(id); + this.id=id; this.currentState = TrafficLight.State.RED; } /** - * showState - method returns the current state of the Lights + * getState - method returns the current state of the TrafficLights * * @param id Link ID tag */ - public State showState(ID id){ + public State getState(ID id){ return this.currentState; } + + /** + * getTrafficLightID - method returns the ID of the current object + * + * @param trafficLight an object of a TrafficLight class + */ + public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.id;} + /** * {@inheritDoc} */ - @Override + //@Override public void tick(SimulationTick tick) { } 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 index 4bbaf2b..ab0697f 100644 --- 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 @@ -1,6 +1,7 @@ package kcl.teamIndexZero.traffic.simulator.data.links; import kcl.teamIndexZero.traffic.simulator.ISimulationAware; +import kcl.teamIndexZero.traffic.simulator.Simulator; import kcl.teamIndexZero.traffic.simulator.data.ID; import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; @@ -10,8 +11,8 @@ * Created by Es on 02/03/2016. */ public /*abstract*/ class TrafficLightInSet extends TrafficLight implements ISimulationAware { - long CurrentDate = (new Date().getTime()) / 1000; - long lastChange = CurrentDate; + long CurrentTime; + long lastChange; long timer; private TrafficLight model; private TrafficLightSet modelSet; @@ -26,12 +27,12 @@ public TrafficLightInSet(ID linkId) { */ @Override public void tick(SimulationTick tick) { - CurrentDate = (new Date().getTime()) / 1000; + // CurrentTime = - if (CurrentDate - lastChange > timer) { + if (CurrentTime - lastChange > timer) { if (modelSet != null) { modelSet.changeColour(model, model.currentState); - lastChange = CurrentDate; + lastChange = CurrentTime; } } } From 0805fd37319f35fbd84a14b07af341368c88d6aa Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 14 Mar 2016 01:49:55 +0000 Subject: [PATCH 03/21] Single traffic light: class, rule and controller. --- .../data/descriptors/TrafficLightRule.java | 7 +++ .../simulator/data/links/TrafficLight.java | 36 +++++--------- .../data/links/TrafficLightInSet.java | 49 ++++++++++++------- .../data/trafficLight/TrafficLight.java | 42 +++++++++++++--- .../trafficLight/TrafficLightController.java | 38 ++++++++++++-- .../data/trafficLight/TrafficLightInSet.java | 2 +- .../data/links/TrafficLightTest.java | 36 -------------- 7 files changed, 123 insertions(+), 87 deletions(-) delete mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightTest.java 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..798f1d8 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,15 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; +import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; /** * Stand-alone Traffic Light rule description */ public class TrafficLightRule { + public static TrafficLight.State changeColour(TrafficLight trafficLight, TrafficLight.State currentState) { + TrafficLight.State temp; + temp = (currentState == TrafficLight.State.RED) ? TrafficLight.State.GREEN : TrafficLight.State.RED; + return temp; + } + } \ No newline at end of file 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 index e3e7384..7b8e4f6 100644 --- 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 @@ -2,19 +2,24 @@ 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.links.TrafficLightSet; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; +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 { +/* public class TrafficLight { private ID id; + public TrafficLight() { + } + public enum State { RED, GREEN } @@ -22,11 +27,7 @@ public enum State { public State currentState; - /** - * Constructor - * - * @param id Link ID tag - */ + public TrafficLight(ID id) { this.id=id; @@ -34,28 +35,17 @@ public TrafficLight(ID id) { } - /** - * getState - method returns the current state of the TrafficLights - * - * @param id Link ID tag - */ + public State getState(ID id){ return this.currentState; } - /** - * getTrafficLightID - method returns the ID of the current object - * - * @param trafficLight an object of a TrafficLight class - */ + public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.id;} - /** - * {@inheritDoc} - */ - //@Override 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 index ab0697f..0ab33d4 100644 --- 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 @@ -1,39 +1,54 @@ package kcl.teamIndexZero.traffic.simulator.data.links; import kcl.teamIndexZero.traffic.simulator.ISimulationAware; -import kcl.teamIndexZero.traffic.simulator.Simulator; + 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; + -import java.util.Date; /** * Created by Es on 02/03/2016. */ -public /*abstract*/ class TrafficLightInSet extends TrafficLight implements ISimulationAware { - long CurrentTime; - long lastChange; - long timer; - private TrafficLight model; +/*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 linkId) { - super(linkId); - //TODO Fix construction? + public TrafficLightInSet(ID id) { + this.id = id; } - /** - * {@inheritDoc} - */ + + 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) { - // CurrentTime = + Temporary =simulationTick.getSimulatedTime(); + CurrentTime = formatTimeToLong(Temporary); - if (CurrentTime - lastChange > timer) { + if ((CurrentTime - lastChange) > timer) { if (modelSet != null) { - modelSet.changeColour(model, model.currentState); + // modelSet.changeColour(model, model.currentState); lastChange = CurrentTime; } } } -} \ No newline at end of file +} +*/ \ 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..f80385e 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,14 +1,24 @@ -package kcl.teamIndexZero.traffic.simulator.data.trafficLight; +package kcl.teamIndexZero.traffic.simulator.data.links; 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 TrafficLight() { + } + + public enum State { + RED, GREEN + } + + public State currentState; /** @@ -17,14 +27,32 @@ public class TrafficLight { * @param id Link ID tag */ public TrafficLight(ID id) { - this.id = id; - this.currentState = TrafficLightState.GREEN; + + this.id=id; + this.currentState = TrafficLight.State.GREEN; + } /** - * Gets the current Traffic Light state + * getState - method returns the current state of the TrafficLights + * + * @param id Link ID tag */ - public TrafficLightState getState() { + public State getState(ID id){ return this.currentState; } + + + /** + * getTrafficLightID - method returns the ID of the current object + * + * @param trafficLight an object of a TrafficLight class + */ + public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.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..0815ce8 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,17 +1,49 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; 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.descriptors.TrafficLightRule; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * Traffic Light controller * Controls the traffic lights in the map based on their rules */ public class TrafficLightController implements ISimulationAware { + public String temp; + public long CurrentTime; + public LocalDateTime Temporary; + public long lastChange = 0; + public long timer; + private kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight model; + private TrafficLightSet modelSet; + SimulationTick simulationTick; + + + 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); + } + /** + * {@inheritDoc} + */ + @Override + public void tick(SimulationTick tick) { + Temporary = simulationTick.getSimulatedTime(); + CurrentTime = formatTimeToLong(Temporary); - @Override - public void tick(SimulationTick tick) { + if ((CurrentTime - lastChange) > timer) { + if (modelSet != null) { + TrafficLightRule.changeColour(model, model.currentState); + lastChange = CurrentTime; + } + } + } - } } 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..24da487 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 @@ -11,7 +11,7 @@ public class TrafficLightInSet { long CurrentDate = (new Date().getTime()) / 1000; long lastChange = CurrentDate; long timer; - private TrafficLight model; + private TrafficLightSet modelSet; public TrafficLightInSet(ID linkId) { diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightTest.java deleted file mode 100644 index 211ebc6..0000000 --- a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/links/TrafficLightTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package kcl.teamIndexZero.traffic.simulator.data.links; - - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** - * Created by bobo on 09-03-16. - */ -public class TrafficLightTest { - - @Before - public void setUp() throws Exception { - - } - - @After - public void tearDown() throws Exception { - - } - - @Test - public void testShowState() throws Exception { - assertTrue( true ); - } - - - - @Test - public void testTick() throws Exception { - - } -} \ No newline at end of file From 95e1c6e9c7f51a1bcac7e2683de5a3c4cfdfdf3d Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 14 Mar 2016 03:04:14 +0000 Subject: [PATCH 04/21] Traffic lights further improvements. --- .../traffic/simulator/data/GraphTools.java | 2 +- .../descriptors/TrafficLightsInSetRule.java | 18 ++++++++++++++++++ .../data/trafficLight/TrafficLight.java | 6 ++---- .../data/trafficLight/TrafficLightSet.java | 1 + 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java index 082b9c2..c953e2e 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java @@ -7,7 +7,7 @@ import kcl.teamIndexZero.traffic.simulator.data.links.Link; import kcl.teamIndexZero.traffic.simulator.data.links.LinkType; import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; import kcl.teamIndexZero.traffic.simulator.exceptions.MapIntegrityException; import kcl.teamIndexZero.traffic.simulator.exceptions.MissingImplementationException; 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..1bf7b50 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,25 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; +import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; + +import java.util.List; + /** * Traffic Light in synchronous set rule */ public class TrafficLightsInSetRule { + + /** + * Changes colours at trafficLights in one set + * + * @param trafficLight1List List of the traffic light in one set + */ + public static void changeColourSet(List trafficLight1List){ + + trafficLight1List.forEach(TrafficLight -> { + TrafficLight.currentState = (TrafficLight.currentState == kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.RED) ? + kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.GREEN : kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.RED; + }); + + } } 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 f80385e..b69670b 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 @@ -36,9 +36,8 @@ public TrafficLight(ID id) { /** * getState - method returns the current state of the TrafficLights * - * @param id Link ID tag */ - public State getState(ID id){ + public State getState(){ return this.currentState; } @@ -46,9 +45,8 @@ public State getState(ID id){ /** * getTrafficLightID - method returns the ID of the current object * - * @param trafficLight an object of a TrafficLight class */ - public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.id;} + public ID getTrafficLightID() { return this.id;} //@Override 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..b4d3d8a 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,5 +1,6 @@ package kcl.teamIndexZero.traffic.simulator.data.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; From 6a1153f5ca0148fe1cb06e903454847716a19dda Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 14 Mar 2016 03:17:17 +0000 Subject: [PATCH 05/21] Traffic lights core of the test, errors in graphtool.class --- .../TrafficLightControllerTest.java | 22 +++++++++++ .../data/trafficLight/TrafficLightTest.java | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightControllerTest.java create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightTest.java 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..5ada88b --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightControllerTest.java @@ -0,0 +1,22 @@ +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; + +import org.junit.After; +import org.junit.Before; + +import static org.junit.Assert.*; + +/** + * Created by JK on 14-03-16. + */ +public class TrafficLightControllerTest { + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } +} \ 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..b208086 --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightTest.java @@ -0,0 +1,37 @@ +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.links.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State; +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 getState()throws Exception { + assertTrue(modelTrafficLight.getState() == State.GREEN); + } +} \ No newline at end of file From 6375de039cfaefbe1183f4d7e2142f0df4e4f272 Mon Sep 17 00:00:00 2001 From: "E. A. Davison" Date: Tue, 15 Mar 2016 09:13:29 +0000 Subject: [PATCH 06/21] Repaired problem with GraphTool/GraphToolTest classes. for this branch --- .../teamIndexZero/traffic/simulator/data/GraphTools.java | 8 ++------ .../traffic/simulator/data/GraphToolsTest.java | 6 ++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java index c953e2e..6b38cbe 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/GraphTools.java @@ -6,8 +6,6 @@ import kcl.teamIndexZero.traffic.simulator.data.features.Lane; import kcl.teamIndexZero.traffic.simulator.data.links.Link; import kcl.teamIndexZero.traffic.simulator.data.links.LinkType; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; -import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; import kcl.teamIndexZero.traffic.simulator.exceptions.MapIntegrityException; import kcl.teamIndexZero.traffic.simulator.exceptions.MissingImplementationException; @@ -54,11 +52,9 @@ public Link createLink(LinkType type, ID linkID) throws MissingImplementationExc case GENERIC: return new Link(linkID); case AUTONOMOUS_TL: - //TODO maybe add the TrafficLight to the tfcontroller? - return new TrafficLight(linkID); + return new Link(linkID); case SYNC_TL: - //TODO definitely add the TrafficLight to the TFcontroller! - return new TrafficLightInSet(linkID); + return new Link(linkID); default: LOG.log_Error("LinkType not implemented in .createLink(..)!"); throw new MissingImplementationException("LinkType not implemented!"); diff --git a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/GraphToolsTest.java b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/GraphToolsTest.java index 80e25eb..725aad9 100644 --- a/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/GraphToolsTest.java +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/GraphToolsTest.java @@ -5,8 +5,6 @@ import kcl.teamIndexZero.traffic.simulator.data.geo.GeoPolyline; import kcl.teamIndexZero.traffic.simulator.data.links.Link; import kcl.teamIndexZero.traffic.simulator.data.links.LinkType; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; import kcl.teamIndexZero.traffic.simulator.exceptions.MapIntegrityException; import org.junit.After; import org.junit.Before; @@ -82,7 +80,7 @@ public void testCreateLink() throws Exception { Link l2 = gt.createLink(LinkType.AUTONOMOUS_TL, new ID("ATFlink")); Link l3 = gt.createLink(LinkType.SYNC_TL, new ID("STFlink")); assertTrue(l1 instanceof Link); - assertTrue(l2 instanceof TrafficLight); - assertTrue(l3 instanceof TrafficLightInSet); + assertTrue(l2 instanceof Link); + assertTrue(l3 instanceof Link); } } \ No newline at end of file From 7f3a0aa60529bb821cef62865ea69249879fd839 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 12:50:27 +0000 Subject: [PATCH 07/21] Traffic lights improvements; --- .../data/TrafficLightController.java | 4 +- .../data/descriptors/TrafficLightRule.java | 2 +- .../data/trafficLight/TrafficLight.java | 8 +--- .../trafficLight/TrafficLightController.java | 18 +++++++- .../data/trafficLight/TrafficLightInSet.java | 44 +++++++++++++++---- 5 files changed, 57 insertions(+), 19 deletions(-) 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 index d8d3ba7..02591f3 100644 --- a/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java +++ b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java @@ -3,6 +3,6 @@ /** * Created by Es on 02/03/2016. */ -public class TrafficLightController { +//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 798f1d8..f24d301 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,5 +1,5 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; /** * Stand-alone Traffic Light rule description 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 b69670b..be06b81 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,4 +1,4 @@ -package kcl.teamIndexZero.traffic.simulator.data.links; +package kcl.teamIndexZero.traffic.simulator.data.trafficLight; import kcl.teamIndexZero.traffic.simulator.data.ID; import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; @@ -11,20 +11,16 @@ public class TrafficLight { private ID id; - public TrafficLight() { - } - public enum State { RED, GREEN } public State currentState; - /** * Constructor * - * @param id Link ID tag + * @param id TrafficLight ID tag */ public TrafficLight(ID id) { 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 0815ce8..bd8ca3e 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 @@ -4,6 +4,8 @@ //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.TrafficLight; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -19,7 +21,7 @@ public class TrafficLightController implements ISimulationAware { public LocalDateTime Temporary; public long lastChange = 0; public long timer; - private kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight model; + private TrafficLight model; private TrafficLightSet modelSet; SimulationTick simulationTick; @@ -45,5 +47,19 @@ public void tick(SimulationTick tick) { } } } + /** + * Adding rule to the one traffic light + * + */ + public void addRule(TrafficLightRule rule){ + + } + /** + * Adding rule to the one traffic light set + * + */ + public void addRule(TrafficLightsInSetRule rule){ + + } } 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 24da487..3bd1958 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,46 @@ 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 TrafficLightSet modelSet; + private ID id; + + public TrafficLightState currentState; + + /** + * Constructor + * + * @param id TrafficLightInSet ID tag + */ + public TrafficLightInSet(ID id) { + + this.id=id; + this.currentState = currentState.GREEN; + + } + + /** + * getState - method returns the current state of the TrafficLightInSet + * + */ + public TrafficLightState getState(){ + return this.currentState; + } + + /** + * getID - method returns the current state of the TrafficLightInSet + * + */ + public ID getID() { return this.id;} + + //@Override + public void tick(SimulationTick tick) { - public TrafficLightInSet(ID linkId) { - //TODO Fix construction? } -} \ No newline at end of file +} From 93daf662fdda15bc151c0842e8253624fa636b0a Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 13:23:22 +0000 Subject: [PATCH 08/21] TrafficLightSet -- adding traffic lights to one set, if they are on the same junction. --- .../data/trafficLight/TrafficLightSet.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) 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 b4d3d8a..b90ab2a 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,9 +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 @@ -11,7 +18,43 @@ public class TrafficLightSet implements ISimulationAware { private ID id; + public List TrafficLightSetList; + private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightSet.class.getSimpleName()); + + /** + * Constructor + * + * @param id TrafficLightSet ID + */ + public TrafficLightSet(ID id) { + this.id=id; + } + + /** + * Adding traffic lights to the List of traffic lights within one junction + * + * @param trafficLightLnSet who will be added, id of the object adding to the list + */ + public List addTrafficlight(TrafficLightInSet trafficLightLnSet){ + + if (trafficLightLnSet != null) { + TrafficLightSetList.add(trafficLightLnSet); + LOG.log("Added the following traffic lights to the set: ", trafficLightLnSet.getID()); + } + + else { + LOG.log_Fatal("Error while adding to TrafficLightLnSet to the set"); + } + + return TrafficLightSetList; + } + + /** + * getID - method returns the ID of the TrafficLightSet + * + */ + public ID getID() { return this.id;} @Override public void tick(SimulationTick tick) { From bba50035dec29012da04b1da57bd9db5434d5d5e Mon Sep 17 00:00:00 2001 From: "E. A. Davison" Date: Wed, 16 Mar 2016 13:48:41 +0000 Subject: [PATCH 09/21] small changes --- .../simulator/data/TrafficLightController.java | 8 -------- .../simulator/data/descriptors/TrafficLightRule.java | 9 ++++----- .../simulator/data/trafficLight/TrafficLight.java | 12 +++--------- 3 files changed, 7 insertions(+), 22 deletions(-) delete mode 100644 simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/TrafficLightController.java 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 02591f3..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 f24d301..d1ac905 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,15 +1,14 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; + import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; /** * Stand-alone Traffic Light rule description */ public class TrafficLightRule { - - public static TrafficLight.State changeColour(TrafficLight trafficLight, TrafficLight.State currentState) { - TrafficLight.State temp; - temp = (currentState == TrafficLight.State.RED) ? TrafficLight.State.GREEN : TrafficLight.State.RED; - return temp; + public TrafficLightState changeColour(TrafficLight trafficLight, TrafficLightState currentState) { + return (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/trafficLight/TrafficLight.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLight.java index be06b81..f3ddee6 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 @@ -8,14 +8,8 @@ * Created by Es on 02/03/2016. */ public class TrafficLight { - private ID id; - - public enum State { - RED, GREEN - } - - public State currentState; + public TrafficLightState currentState; /** * Constructor @@ -25,7 +19,7 @@ public enum State { public TrafficLight(ID id) { this.id=id; - this.currentState = TrafficLight.State.GREEN; + this.currentState = TrafficLightState.GREEN; } @@ -33,7 +27,7 @@ public TrafficLight(ID id) { * getState - method returns the current state of the TrafficLights * */ - public State getState(){ + public TrafficLightState getState(){ return this.currentState; } From b1d7f4c887cbda45bc7f7543f84c46ee33bafc73 Mon Sep 17 00:00:00 2001 From: "E. A. Davison" Date: Wed, 16 Mar 2016 13:48:55 +0000 Subject: [PATCH 10/21] small changes --- .../data/trafficLight/TrafficLightController.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 bd8ca3e..4bc430e 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 @@ -21,8 +21,8 @@ public class TrafficLightController implements ISimulationAware { public LocalDateTime Temporary; public long lastChange = 0; public long timer; - private TrafficLight model; - private TrafficLightSet modelSet; + private TrafficLight model; //list this + private TrafficLightSet modelSet; //list this SimulationTick simulationTick; @@ -37,12 +37,10 @@ public long formatTimeToLong(LocalDateTime date) { */ @Override public void tick(SimulationTick tick) { - Temporary = simulationTick.getSimulatedTime(); - CurrentTime = formatTimeToLong(Temporary); - + CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); if ((CurrentTime - lastChange) > timer) { if (modelSet != null) { - TrafficLightRule.changeColour(model, model.currentState); + TrafficLightRule.changeColour(model, model.currentState); //change state got all tfs in list lastChange = CurrentTime; } } From 644cf4a0dde86e6788631caf33a5789c01ce9de2 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 14:36:10 +0000 Subject: [PATCH 11/21] TrafficLights rules improvements --- .../data/descriptors/TrafficLightRule.java | 16 +++++++++++++--- .../data/descriptors/TrafficLightsInSetRule.java | 13 ++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) 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 d1ac905..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 @@ -3,12 +3,22 @@ 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 { - public TrafficLightState changeColour(TrafficLight trafficLight, TrafficLightState currentState) { - return (currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; - } + /** + * 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 1bf7b50..20ab7c4 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,6 +1,7 @@ package kcl.teamIndexZero.traffic.simulator.data.descriptors; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; +import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; import java.util.List; @@ -12,14 +13,12 @@ public class TrafficLightsInSetRule { /** * Changes colours at trafficLights in one set * - * @param trafficLight1List List of the traffic light in one set + * @param trafficLightList List of the traffic light in one set */ - public static void changeColourSet(List trafficLight1List){ + public static void changeStateofSet(List trafficLightList){ - trafficLight1List.forEach(TrafficLight -> { - TrafficLight.currentState = (TrafficLight.currentState == kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.RED) ? - kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.GREEN : kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State.RED; + trafficLightList.forEach(TrafficLight -> { + TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; }); - } } From 3ec97b0a08c790fed6bd4bdbd7d2b55ea01c40df Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 14:41:54 +0000 Subject: [PATCH 12/21] TrafficLightController & TrafficLightSet improvements. --- .../trafficLight/TrafficLightController.java | 34 ++++++++++++++++++- .../data/trafficLight/TrafficLightSet.java | 16 +++++---- 2 files changed, 43 insertions(+), 7 deletions(-) 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 4bc430e..e61551e 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,10 @@ 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; @@ -9,6 +12,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.List; /** @@ -23,7 +27,10 @@ public class TrafficLightController implements ISimulationAware { public long timer; private TrafficLight model; //list this private TrafficLightSet modelSet; //list this + public List TrafficLightSinglesList; + public List TrafficLightSetList; SimulationTick simulationTick; + private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightController.class.getSimpleName()); public long formatTimeToLong(LocalDateTime date) { @@ -32,6 +39,29 @@ public long formatTimeToLong(LocalDateTime date) { return Long.parseLong(temp, 10); } + /** + * 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: ", 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} */ @@ -40,11 +70,13 @@ public void tick(SimulationTick tick) { CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); if ((CurrentTime - lastChange) > timer) { if (modelSet != null) { - TrafficLightRule.changeColour(model, model.currentState); //change state got all tfs in list + TrafficLightsInSetRule.changeStateofSet(TrafficLightSetList); + TrafficLightRule.changeStateofSingleTrafficLights(TrafficLightSinglesList); lastChange = CurrentTime; } } } + /** * Adding rule to the one traffic light * 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 b90ab2a..8c9f9ed 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 @@ -31,11 +31,11 @@ public TrafficLightSet(ID id) { } /** - * Adding traffic lights to the List of traffic lights within one junction + * Adds traffic lights to the List of traffic lights within one junction * - * @param trafficLightLnSet who will be added, id of the object adding to the list + * @param trafficLightLnSet object to be added to the list */ - public List addTrafficlight(TrafficLightInSet trafficLightLnSet){ + public void addTrafficlight(TrafficLightInSet trafficLightLnSet){ if (trafficLightLnSet != null) { TrafficLightSetList.add(trafficLightLnSet); @@ -43,12 +43,16 @@ public List addTrafficlight(TrafficLightInSet trafficLightLnS } else { - LOG.log_Fatal("Error while adding to TrafficLightLnSet to the set"); + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); } - - return TrafficLightSetList; } + /** + * Returns a list of traffic lights within one junction + * + * @param id of the junction, to which list is assigned + */ + public List getSet(ID id) {return this.TrafficLightSetList;} /** * getID - method returns the ID of the TrafficLightSet From cce2aa727b706db8572f11868837bc14d9867e4a Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 14:57:54 +0000 Subject: [PATCH 13/21] Traffic lights improvements. --- .../traffic/simulator/data/trafficLight/TrafficLight.java | 6 ++---- .../data/trafficLight/TrafficLightController.java | 8 +++----- .../simulator/data/trafficLight/TrafficLightInSet.java | 6 ++---- .../simulator/data/trafficLight/TrafficLightSet.java | 5 ++--- 4 files changed, 9 insertions(+), 16 deletions(-) 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 f3ddee6..4bd8226 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 @@ -24,8 +24,7 @@ public TrafficLight(ID id) { } /** - * getState - method returns the current state of the TrafficLights - * + * Returns the current state of the TrafficLight object */ public TrafficLightState getState(){ return this.currentState; @@ -33,8 +32,7 @@ public TrafficLightState getState(){ /** - * getTrafficLightID - method returns the ID of the current object - * + * Returns the ID of the current TrafficLight object */ public ID getTrafficLightID() { return this.id;} 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 e61551e..cf3ba26 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 @@ -48,7 +48,7 @@ public void addTrafficlight(TrafficLight trafficLight){ if (trafficLight != null) { TrafficLightSinglesList.add(trafficLight); - LOG.log("Added the following traffic lights to the set: ", trafficLight.getTrafficLightID()); + LOG.log("Added the following traffic lights to the set of all single Traffic Lights: ", trafficLight.getTrafficLightID() ); } else { @@ -78,15 +78,13 @@ public void tick(SimulationTick tick) { } /** - * Adding rule to the one traffic light - * + * Adds rule to the one traffic light */ public void addRule(TrafficLightRule rule){ } /** - * Adding rule to the one traffic light set - * + * Adds rule to the one traffic light set */ public void addRule(TrafficLightsInSetRule rule){ 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 3bd1958..f83a93c 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 @@ -26,16 +26,14 @@ public TrafficLightInSet(ID id) { } /** - * getState - method returns the current state of the TrafficLightInSet - * + * Returns the current state of the TrafficLightInSet */ public TrafficLightState getState(){ return this.currentState; } /** - * getID - method returns the current state of the TrafficLightInSet - * + * Returns the current state of the TrafficLightInSet */ public ID getID() { return this.id;} 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 8c9f9ed..fc34d97 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 @@ -39,7 +39,7 @@ public void addTrafficlight(TrafficLightInSet trafficLightLnSet){ if (trafficLightLnSet != null) { TrafficLightSetList.add(trafficLightLnSet); - LOG.log("Added the following traffic lights to the set: ", trafficLightLnSet.getID()); + LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID()," to the set: ", this.id); } else { @@ -55,8 +55,7 @@ public void addTrafficlight(TrafficLightInSet trafficLightLnSet){ public List getSet(ID id) {return this.TrafficLightSetList;} /** - * getID - method returns the ID of the TrafficLightSet - * + * Returns the ID of the TrafficLightSet */ public ID getID() { return this.id;} From fd17ed1e87b3eb8561f3af242f0782cf99443202 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Wed, 16 Mar 2016 17:39:39 +0000 Subject: [PATCH 14/21] Traffic lights: adding rules for single lights and those in the sets. --- .../descriptors/TrafficLightsInSetRule.java | 9 +++++++- .../data/trafficLight/TrafficLight.java | 4 +--- .../trafficLight/TrafficLightController.java | 21 ++++++++++++++++--- .../data/trafficLight/TrafficLightInSet.java | 4 ++-- 4 files changed, 29 insertions(+), 9 deletions(-) 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 20ab7c4..cd5604f 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 @@ -17,8 +17,15 @@ public class TrafficLightsInSetRule { */ public static void changeStateofSet(List trafficLightList){ + final int[] i = {0}; + trafficLightList.forEach(TrafficLight -> { - TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; + if (i[0] % 2 == 0){ + TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; + i[0]++; + } + else + i[0]++; }); } } 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 4bd8226..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 @@ -3,12 +3,12 @@ import kcl.teamIndexZero.traffic.simulator.data.ID; import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; - /** * Created by Es on 02/03/2016. */ public class TrafficLight { private ID id; + public long TrafficLightDelay; public TrafficLightState currentState; /** @@ -30,13 +30,11 @@ 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 cf3ba26..487536b 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 @@ -8,6 +8,7 @@ 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; @@ -25,8 +26,9 @@ public class TrafficLightController implements ISimulationAware { public LocalDateTime Temporary; public long lastChange = 0; public long timer; + public long delay; private TrafficLight model; //list this - private TrafficLightSet modelSet; //list this + private TrafficLightInSet modelSet; //list this public List TrafficLightSinglesList; public List TrafficLightSetList; SimulationTick simulationTick; @@ -68,6 +70,19 @@ public void addTrafficlight(TrafficLight trafficLight){ @Override public void tick(SimulationTick tick) { CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); + + /*when single traffic lights do not work in a synchronous way with the ticks*/ + model.TrafficLightDelay = CurrentTime - lastChange; + if ((model.TrafficLightDelay >= timer) && (model.currentState == TrafficLightState.GREEN)) { + model.currentState = TrafficLightState.RED; + } + + /*when set's traffic lights do not work in a synchronous way with the ticks*/ + modelSet.TrafficLightInSetDelay = CurrentTime - lastChange; + if ((modelSet.TrafficLightInSetDelay >= timer) && (modelSet.currentState == TrafficLightState.GREEN)){ + modelSet.currentState = TrafficLightState.RED; + } + if ((CurrentTime - lastChange) > timer) { if (modelSet != null) { TrafficLightsInSetRule.changeStateofSet(TrafficLightSetList); @@ -81,13 +96,13 @@ 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 f83a93c..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 @@ -10,7 +10,7 @@ public class TrafficLightInSet { private ID id; - + public long TrafficLightInSetDelay; public TrafficLightState currentState; /** @@ -21,7 +21,7 @@ public class TrafficLightInSet { public TrafficLightInSet(ID id) { this.id=id; - this.currentState = currentState.GREEN; + this.currentState = TrafficLightState.GREEN; } From 5dd5cde2b66c61b85f8b22251d58cf25cb195f36 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Sun, 20 Mar 2016 20:10:49 +0000 Subject: [PATCH 15/21] single traffic lights class: completed test. TrafficLightSet class: added two interior lists for two types of lights at a one junction. --- .../data/trafficLight/TrafficLightSet.java | 48 +++++++++++++++---- .../data/trafficLight/TrafficLightTest.java | 13 +++-- 2 files changed, 49 insertions(+), 12 deletions(-) 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 fc34d97..e1b0cd7 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 @@ -19,6 +19,9 @@ public class TrafficLightSet implements ISimulationAware { private ID id; public List TrafficLightSetList; + public List InteriorListA; + public List InteriorListB; + private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightSet.class.getSimpleName()); /** @@ -35,16 +38,45 @@ public TrafficLightSet(ID id) { * * @param trafficLightLnSet object to be added to the list */ - public void addTrafficlight(TrafficLightInSet trafficLightLnSet){ + public void addTrafficlight(TrafficLightInSet trafficLightLnSet, char type ){ // type: A or B, the type of the InteriorList + + switch (type) { + case('A'): + + if (trafficLightLnSet != null) { + LOG.log("Traffic Lights type is the first type"); + TrafficLightSetList.add(trafficLightLnSet); + InteriorListA.add(trafficLightLnSet); + + LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID(), " to the set: ", this.id); + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + break; + + case('B'): + if (trafficLightLnSet != null) { + LOG.log("Traffic Lights type is the second type"); + TrafficLightSetList.add(trafficLightLnSet); + InteriorListB.add(trafficLightLnSet); + + LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID(), " to the set: ", this.id); + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + break; - if (trafficLightLnSet != null) { - TrafficLightSetList.add(trafficLightLnSet); - LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID()," to the set: ", this.id); - } + default: + if (trafficLightLnSet != null) { + LOG.log("Traffic Lights type is not specified!!"); + TrafficLightSetList.add(trafficLightLnSet); + InteriorListA.add(trafficLightLnSet); - else { - LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); - } + LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID(), " to the set: ", this.id); + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } + } } /** 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 index b208086..6ba1258 100644 --- 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 @@ -3,8 +3,8 @@ 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.links.TrafficLight; -import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLight.State; +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; @@ -31,7 +31,12 @@ public void tearDown() throws Exception { } @Test - public void getState()throws Exception { - assertTrue(modelTrafficLight.getState() == State.GREEN); + 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 From e2f0f1e8afb0addc1ea0cf2d09e9010dfa07c6aa Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Sun, 20 Mar 2016 21:08:03 +0000 Subject: [PATCH 16/21] Frther tests: Traffic LightRule & TraficLightlnSet & small changes in Rule for the set --- .../descriptors/TrafficLightsInSetRule.java | 17 ++++--- .../descriptors/TrafficLightRuleTest.java | 49 +++++++++++++++++++ .../trafficLight/TrafficLightInSetTest.java | 33 +++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRuleTest.java create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightInSetTest.java 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 cd5604f..0a0f87c 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,5 +1,6 @@ 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; @@ -17,15 +18,15 @@ public class TrafficLightsInSetRule { */ public static void changeStateofSet(List trafficLightList){ - final int[] i = {0}; + int i = 0; - trafficLightList.forEach(TrafficLight -> { - if (i[0] % 2 == 0){ - TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; - i[0]++; + for(TrafficLightInSet tf : trafficLightList ) { + if (i % 2 == 0){ + tf.currentState = (tf.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; + i++; } - else - i[0]++; - }); + else + i++; + } } } 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..f84f697 --- /dev/null +++ b/simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightRuleTest.java @@ -0,0 +1,49 @@ +package kcl.teamIndexZero.traffic.simulator.data.descriptors; + +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; + public List trafficLightSinglesList; + boolean flag = false; + + @Before + public void setUp() throws Exception { + model = new TrafficLightRule(); + trafficLightSinglesList = new ArrayList(); + } + + + @After + public void tearDown() throws Exception { + model = null; + } + + @Test + public void testChangeStateofSingleTrafficLights() throws Exception { + + model.changeStateofSingleTrafficLights(trafficLightSinglesList); + + for(TrafficLight tf : trafficLightSinglesList ) { + if(tf.currentState == TrafficLightState.GREEN){ + flag = true; + }; + } + assertEquals(flag, false); + + } +} \ 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 From c1f85a16d31dd397c06bfc774129849e26e81535 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Sun, 20 Mar 2016 21:41:18 +0000 Subject: [PATCH 17/21] Test of the rule of the single traffic lights list & fixes --- .../trafficLight/TrafficLightController.java | 12 ++++++++-- .../descriptors/TrafficLightRuleTest.java | 22 ++++++++++++------- .../TrafficLightControllerTest.java | 20 ++++++++++++++++- 3 files changed, 43 insertions(+), 11 deletions(-) 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 487536b..0e0af70 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 @@ -13,6 +13,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; @@ -34,6 +35,13 @@ public class TrafficLightController implements ISimulationAware { SimulationTick simulationTick; private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightController.class.getSimpleName()); + /** + * Constructor + */ + public TrafficLightController(){ + TrafficLightSinglesList = new ArrayList(); + TrafficLightSetList = new ArrayList(); + } public long formatTimeToLong(LocalDateTime date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); @@ -48,14 +56,14 @@ public long formatTimeToLong(LocalDateTime date) { */ public void addTrafficlight(TrafficLight trafficLight){ - if (trafficLight != null) { + 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"); } + } /** 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 index f84f697..c07cb5e 100644 --- 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 @@ -1,5 +1,6 @@ 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; @@ -18,32 +19,37 @@ public class TrafficLightRuleTest { private TrafficLightRule model; - public List trafficLightSinglesList; - boolean flag = false; + private TrafficLight trafficLight; + private TrafficLightController controller; + private boolean flag = false; + @Before public void setUp() throws Exception { model = new TrafficLightRule(); - trafficLightSinglesList = new ArrayList(); + 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 { - model.changeStateofSingleTrafficLights(trafficLightSinglesList); + controller.addTrafficlight(trafficLight); + model.changeStateofSingleTrafficLights(controller.TrafficLightSinglesList); - for(TrafficLight tf : trafficLightSinglesList ) { - if(tf.currentState == TrafficLightState.GREEN){ + for(TrafficLight tf : controller.TrafficLightSinglesList ) { + if(tf.currentState == TrafficLightState.RED){ flag = true; }; } - assertEquals(flag, false); + assertEquals(flag, 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 index 5ada88b..d827a7b 100644 --- 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 @@ -2,21 +2,39 @@ import org.junit.After; 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 controllerModel; + private Date date; + @Before public void setUp() throws Exception { - + controllerModel = new TrafficLightController(); } @After public void tearDown() throws Exception { + controllerModel = null; + } + + @Test + public void test() { + } } \ No newline at end of file From 7900785b3527dbb1216ab6fe79efb11aa455bd51 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Sun, 20 Mar 2016 23:51:28 +0000 Subject: [PATCH 18/21] Further tests & small fixes --- .../descriptors/TrafficLightsInSetRule.java | 10 +-- .../data/trafficLight/TrafficLightSet.java | 18 ++++- .../trafficLight/TrafficLightSetTest.java | 71 +++++++++++++++++++ 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightSetTest.java 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 0a0f87c..1b61904 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 @@ -14,19 +14,11 @@ public class TrafficLightsInSetRule { /** * Changes colours at trafficLights in one set * - * @param trafficLightList List of the traffic light in one set + * @param trafficLightList List of the traffic lights in one set */ public static void changeStateofSet(List trafficLightList){ - - int i = 0; - for(TrafficLightInSet tf : trafficLightList ) { - if (i % 2 == 0){ tf.currentState = (tf.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; - i++; - } - else - i++; } } } 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 e1b0cd7..5981cc2 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 @@ -21,7 +21,6 @@ public class TrafficLightSet implements ISimulationAware { public List TrafficLightSetList; public List InteriorListA; public List InteriorListB; - private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightSet.class.getSimpleName()); /** @@ -31,6 +30,9 @@ public class TrafficLightSet implements ISimulationAware { */ public TrafficLightSet(ID id) { this.id=id; + this.TrafficLightSetList =new ArrayList(); + this.InteriorListA =new ArrayList(); + this.InteriorListB =new ArrayList(); } /** @@ -43,7 +45,7 @@ public void addTrafficlight(TrafficLightInSet trafficLightLnSet, char type ){ // switch (type) { case('A'): - if (trafficLightLnSet != null) { + if (!(trafficLightLnSet == null)) { LOG.log("Traffic Lights type is the first type"); TrafficLightSetList.add(trafficLightLnSet); InteriorListA.add(trafficLightLnSet); @@ -61,6 +63,18 @@ public void addTrafficlight(TrafficLightInSet trafficLightLnSet, char type ){ // InteriorListB.add(trafficLightLnSet); LOG.log("Added the following traffic lights: ", trafficLightLnSet.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"); } 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..61d083e --- /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, 'A'); + model.addTrafficlight(trafficLightInSetB, 'B'); + + 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.getSet(new ID("TrafficLightSetTest")), model.TrafficLightSetList); + } + + @Test + public void testGetID() throws Exception { + assertEquals(model.getID(), new ID("TrafficLightSetTest")); + } +} \ No newline at end of file From 1e952516e36e48d1d133a7a1abbdefc312628be9 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 21 Mar 2016 00:21:03 +0000 Subject: [PATCH 19/21] Further tests. --- .../TrafficLightsInSetRuleTest.java | 70 +++++++++++++++++++ .../TrafficLightControllerTest.java | 42 +++++++++-- 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 simulator/src/test/java/kcl/teamIndexZero/traffic/simulator/data/descriptors/TrafficLightsInSetRuleTest.java 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..7b38cdb --- /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, 'A'); + trafficLightSet.addTrafficlight(trafficLightInSetB, 'B'); + 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 index d827a7b..cded8aa 100644 --- 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 @@ -1,6 +1,10 @@ package kcl.teamIndexZero.traffic.simulator.data.trafficLight; +import kcl.teamIndexZero.traffic.simulator.data.ID; +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; @@ -18,23 +22,53 @@ * Created by JK on 14-03-16. */ public class TrafficLightControllerTest { - private TrafficLightController controllerModel; + + private TrafficLightController model; + private TrafficLightRule rule; + private TrafficLightsInSetRule setRule; + private TrafficLight trafficLight; + private TrafficLightInSet trafficLightInSet; + private TrafficLightSet trafficLightSet; + private boolean flag = false; private Date date; @Before public void setUp() throws Exception { - controllerModel = new TrafficLightController(); + model = new TrafficLightController(); + rule = new TrafficLightRule(); + setRule = new TrafficLightsInSetRule(); + trafficLightSet = new TrafficLightSet(new ID("TrafficLightSetTest")); + trafficLight = new TrafficLight(new ID("TrafficLightTest")); + trafficLightInSet= new TrafficLightInSet(new ID("TrafficLightInSetTest")); } @After public void tearDown() throws Exception { - controllerModel = null; + model = null; + rule = null; + setRule = null; + trafficLightSet = null; + trafficLight = null; + trafficLightInSet = null; } @Test - public void test() { + public void testGetSingleSet() { + Assert.assertEquals(model.getSingleSet(), model.TrafficLightSinglesList); + } + @Test + public void testAddRuleSingleTF() { + model.addTrafficlight(trafficLight);//At the beginning the State was GREEN + model.addRule(rule); //should be RED now + for (TrafficLight tf : model.TrafficLightSinglesList) { + if (tf.currentState == TrafficLightState.RED) { + flag = true; + } + ; + } + Assert.assertTrue(flag == true); } } \ No newline at end of file From c2e0907c380f69c1aaba1774c71875b146b9a8aa Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 21 Mar 2016 01:51:38 +0000 Subject: [PATCH 20/21] Controller and its test. --- .../trafficLight/TrafficLightController.java | 24 ++++---- .../TrafficLightControllerTest.java | 61 ++++++++++++++++--- 2 files changed, 64 insertions(+), 21 deletions(-) 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 0e0af70..d461065 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 @@ -13,6 +13,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; @@ -26,16 +27,18 @@ public class TrafficLightController implements ISimulationAware { public long CurrentTime; public LocalDateTime Temporary; public long lastChange = 0; - public long timer; + public long timer = 5; public long delay; + public static LocalDateTime start = LocalDateTime.of(1984, 12, 16, 7, 45, 55); private TrafficLight model; //list this private TrafficLightInSet modelSet; //list this + private TrafficLightSet set; public List TrafficLightSinglesList; public List TrafficLightSetList; SimulationTick simulationTick; private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightController.class.getSimpleName()); - /** + /** * Constructor */ public TrafficLightController(){ @@ -44,12 +47,11 @@ public TrafficLightController(){ } 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); + long milliseconds = ChronoUnit.MILLIS.between(start, date); + return milliseconds; } - /** + /** * Adds single traffic light to the List of all single traffic lights * * @param trafficLight object to be added to the list @@ -76,7 +78,7 @@ public void addTrafficlight(TrafficLight trafficLight){ * {@inheritDoc} */ @Override - public void tick(SimulationTick tick) { + public void tick(SimulationTick tick) { CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); /*when single traffic lights do not work in a synchronous way with the ticks*/ @@ -100,16 +102,16 @@ public void tick(SimulationTick tick) { } } - /** + /** * Adds rule to the one traffic light */ - public void addRule(TrafficLightRule rule){ + public void addRule(TrafficLightRule rule){ rule.changeStateofSingleTrafficLights(TrafficLightSinglesList); } - /** + /** * Adds rule to the one traffic light set */ - public void addRule(TrafficLightsInSetRule rule){ + public void addRule(TrafficLightsInSetRule rule){ rule.changeStateofSet(TrafficLightSetList); } 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 index cded8aa..4571429 100644 --- 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 @@ -1,6 +1,7 @@ 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; @@ -27,10 +28,10 @@ public class TrafficLightControllerTest { private TrafficLightRule rule; private TrafficLightsInSetRule setRule; private TrafficLight trafficLight; - private TrafficLightInSet trafficLightInSet; + private TrafficLightInSet trafficLightInSetA; + private TrafficLightInSet trafficLightInSetB; private TrafficLightSet trafficLightSet; - private boolean flag = false; - private Date date; + private boolean flag; @Before @@ -38,9 +39,10 @@ public void setUp() throws Exception { model = new TrafficLightController(); rule = new TrafficLightRule(); setRule = new TrafficLightsInSetRule(); - trafficLightSet = new TrafficLightSet(new ID("TrafficLightSetTest")); + trafficLightSet = new TrafficLightSet(new ID("TrafficLightSetTestA")); trafficLight = new TrafficLight(new ID("TrafficLightTest")); - trafficLightInSet= new TrafficLightInSet(new ID("TrafficLightInSetTest")); + trafficLightInSetA = new TrafficLightInSet(new ID("TrafficLightInSetTestA")); + trafficLightInSetB = new TrafficLightInSet(new ID("TrafficLightInSetTestB")); } @After @@ -50,7 +52,8 @@ public void tearDown() throws Exception { setRule = null; trafficLightSet = null; trafficLight = null; - trafficLightInSet = null; + trafficLightInSetA = null; + trafficLightInSetB = null; } @Test @@ -60,15 +63,53 @@ public void testGetSingleSet() { @Test public void testAddRuleSingleTF() { + flag = false; model.addTrafficlight(trafficLight);//At the beginning the State was GREEN - model.addRule(rule); //should be RED now + model.addRule(rule); //Should be RED by now for (TrafficLight tf : model.TrafficLightSinglesList) { if (tf.currentState == TrafficLightState.RED) { flag = true; - } - ; + }; } - Assert.assertTrue(flag == true); + Assert.assertTrue(flag); + } + + @Test + public void testAddRuleSetA() { + flag = false; + trafficLightSet.addTrafficlight(trafficLightInSetA, 'A'); //initially the State was GREEN + model.TrafficLightSetList = trafficLightSet.TrafficLightSetList; + model.addRule(setRule); // Now should be RED + + for (TrafficLightInSet tf : model.TrafficLightSetList) { + if (tf.currentState == TrafficLightState.RED) { + flag = true; + }; + } + Assert.assertTrue(flag); + } + + @Test + public void testAddRuleSetB() { + flag = false; + trafficLightSet.addTrafficlight(trafficLightInSetB, 'B'); //initially the State was RED + model.TrafficLightSetList = trafficLightSet.TrafficLightSetList; + model.addRule(setRule); // Now should be GREEN + + 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 From ca9a9810f47b04a54ce5e19d8d27c5403fc78a37 Mon Sep 17 00:00:00 2001 From: Inspiron17R Date: Mon, 28 Mar 2016 01:08:47 +0100 Subject: [PATCH 21/21] Traffic lights: changes in a few classes. Removing unused variables, implementing enum type in traffic light set, fixing controller and corresponding tests. --- .../descriptors/TrafficLightsInSetRule.java | 4 +- .../trafficLight/TrafficLightController.java | 41 +++++++----- .../data/trafficLight/TrafficLightSet.java | 67 ++++++++----------- .../TrafficLightsInSetRuleTest.java | 4 +- .../TrafficLightControllerTest.java | 32 +++++---- .../trafficLight/TrafficLightSetTest.java | 6 +- 6 files changed, 79 insertions(+), 75 deletions(-) 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 1b61904..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 @@ -17,8 +17,10 @@ public class TrafficLightsInSetRule { * @param trafficLightList List of the traffic lights in one set */ public static void changeStateofSet(List trafficLightList){ - for(TrafficLightInSet tf : 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/trafficLight/TrafficLightController.java b/simulator/src/main/java/kcl/teamIndexZero/traffic/simulator/data/trafficLight/TrafficLightController.java index d461065..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 @@ -23,16 +23,13 @@ * Controls the traffic lights in the map based on their rules */ public class TrafficLightController implements ISimulationAware { - public String temp; + public long CurrentTime; - public LocalDateTime Temporary; public long lastChange = 0; public long timer = 5; - public long delay; + public long TrafficLightDelay; + public long TrafficLightInSetDelay; public static LocalDateTime start = LocalDateTime.of(1984, 12, 16, 7, 45, 55); - private TrafficLight model; //list this - private TrafficLightInSet modelSet; //list this - private TrafficLightSet set; public List TrafficLightSinglesList; public List TrafficLightSetList; SimulationTick simulationTick; @@ -46,9 +43,13 @@ public TrafficLightController(){ TrafficLightSetList = new ArrayList(); } + /** + * Converts LocalDateTime to long + * + * @param date a LocalDateTime object + */ public long formatTimeToLong(LocalDateTime date) { - long milliseconds = ChronoUnit.MILLIS.between(start, date); - return milliseconds; + return ChronoUnit.MILLIS.between(start, date); } /** @@ -82,19 +83,25 @@ public void tick(SimulationTick tick) { CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); /*when single traffic lights do not work in a synchronous way with the ticks*/ - model.TrafficLightDelay = CurrentTime - lastChange; - if ((model.TrafficLightDelay >= timer) && (model.currentState == TrafficLightState.GREEN)) { - model.currentState = TrafficLightState.RED; - } + 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*/ - modelSet.TrafficLightInSetDelay = CurrentTime - lastChange; - if ((modelSet.TrafficLightInSetDelay >= timer) && (modelSet.currentState == TrafficLightState.GREEN)){ - modelSet.currentState = TrafficLightState.RED; - } + this.TrafficLightInSetDelay = CurrentTime - lastChange; + + TrafficLightSetList.forEach(TrafficLightInSet -> { + if ((this.TrafficLightInSetDelay >= timer) && (TrafficLightInSet.currentState == TrafficLightState.GREEN)){ + TrafficLightInSet.currentState = TrafficLightState.RED; + } + }); if ((CurrentTime - lastChange) > timer) { - if (modelSet != null) { + if ((TrafficLightSetList != null) && (TrafficLightSinglesList != null)) { TrafficLightsInSetRule.changeStateofSet(TrafficLightSetList); TrafficLightRule.changeStateofSingleTrafficLights(TrafficLightSinglesList); lastChange = CurrentTime; 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 5981cc2..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 @@ -18,11 +18,13 @@ public class TrafficLightSet implements ISimulationAware { private ID id; - public List TrafficLightSetList; public List InteriorListA; public List InteriorListB; private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightSet.class.getSimpleName()); - + public enum TrafficLightSetGroup{ + GROUPA, + GROUPB + } /** * Constructor * @@ -30,7 +32,6 @@ public class TrafficLightSet implements ISimulationAware { */ public TrafficLightSet(ID id) { this.id=id; - this.TrafficLightSetList =new ArrayList(); this.InteriorListA =new ArrayList(); this.InteriorListB =new ArrayList(); } @@ -38,67 +39,57 @@ public TrafficLightSet(ID id) { /** * Adds traffic lights to the List of traffic lights within one junction * - * @param trafficLightLnSet object to be added to the list + * @param trafficLightInSet object to be added to the list */ - public void addTrafficlight(TrafficLightInSet trafficLightLnSet, char type ){ // type: A or B, the type of the InteriorList + public void addTrafficlight(TrafficLightInSet trafficLightInSet, TrafficLightSetGroup group ){ - switch (type) { - case('A'): - if (!(trafficLightLnSet == null)) { + if (group == TrafficLightSetGroup.GROUPA){ + + if (!(trafficLightInSet == null)) { LOG.log("Traffic Lights type is the first type"); - TrafficLightSetList.add(trafficLightLnSet); - InteriorListA.add(trafficLightLnSet); + //TrafficLightSetList.add(trafficLightInSet); + InteriorListA.add(trafficLightInSet); - LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID(), " to the set: ", this.id); + 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"); } - break; - case('B'): - if (trafficLightLnSet != null) { - LOG.log("Traffic Lights type is the second type"); - TrafficLightSetList.add(trafficLightLnSet); - InteriorListB.add(trafficLightLnSet); + } + + 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: ", trafficLightLnSet.getID(), " to the set: ", this.id); + 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; - } + 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"); - } - break; - - default: - if (trafficLightLnSet != null) { - LOG.log("Traffic Lights type is not specified!!"); - TrafficLightSetList.add(trafficLightLnSet); - InteriorListA.add(trafficLightLnSet); - - LOG.log("Added the following traffic lights: ", trafficLightLnSet.getID(), " to the set: ", this.id); - } else { - LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); - } + } else { + LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); + } } } + /** - * Returns a list of traffic lights within one junction + * Returns a groupA from a list of traffic lights within one junction * * @param id of the junction, to which list is assigned */ - public List getSet(ID id) {return this.TrafficLightSetList;} + public List getSetGroupA(ID id) {return this.InteriorListA;} /** * Returns the ID of the TrafficLightSet 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 index 7b38cdb..423a377 100644 --- 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 @@ -44,8 +44,8 @@ public void tearDown() throws Exception { @Test public void testChangeStateofSet() throws Exception { - trafficLightSet.addTrafficlight(trafficLightInSetA, 'A'); - trafficLightSet.addTrafficlight(trafficLightInSetB, 'B'); + 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 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 index 4571429..c66e28e 100644 --- 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 @@ -78,31 +78,35 @@ public void testAddRuleSingleTF() { @Test public void testAddRuleSetA() { flag = false; - trafficLightSet.addTrafficlight(trafficLightInSetA, 'A'); //initially the State was GREEN - model.TrafficLightSetList = trafficLightSet.TrafficLightSetList; + trafficLightSet.addTrafficlight(trafficLightInSetA, TrafficLightSet.TrafficLightSetGroup.GROUPA); //initially the State was GREEN + model.TrafficLightSetList = trafficLightSet.InteriorListA; model.addRule(setRule); // Now should be RED - for (TrafficLightInSet tf : model.TrafficLightSetList) { - if (tf.currentState == TrafficLightState.RED) { - flag = true; - }; + if (model.TrafficLightSetList != null) { + for (TrafficLightInSet tf : model.TrafficLightSetList) { + if (tf.currentState == TrafficLightState.RED) { + flag = true; + }; + } + Assert.assertTrue(flag); } - Assert.assertTrue(flag); } @Test public void testAddRuleSetB() { flag = false; - trafficLightSet.addTrafficlight(trafficLightInSetB, 'B'); //initially the State was RED - model.TrafficLightSetList = trafficLightSet.TrafficLightSetList; + trafficLightSet.addTrafficlight(trafficLightInSetB, TrafficLightSet.TrafficLightSetGroup.GROUPB); //initially the State was RED + model.TrafficLightSetList = trafficLightSet.InteriorListB; model.addRule(setRule); // Now should be GREEN - for (TrafficLightInSet tf : model.TrafficLightSetList) { - if (tf.currentState == TrafficLightState.GREEN) { - flag = true; - }; + if (model.TrafficLightSetList != null) { + for (TrafficLightInSet tf : model.TrafficLightSetList) { + if (tf.currentState == TrafficLightState.GREEN) { + flag = true; + }; + } + Assert.assertTrue(flag); } - Assert.assertTrue(flag); } @Test 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 index 61d083e..9431937 100644 --- 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 @@ -33,8 +33,8 @@ public void tearDown() throws Exception { @Test public void testAddTrafficlight() throws Exception { - model.addTrafficlight(trafficLightInSetA, 'A'); - model.addTrafficlight(trafficLightInSetB, 'B'); + 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 @@ -61,7 +61,7 @@ public void testAddTrafficlight() throws Exception { @Test public void testGetSet() throws Exception { - assertEquals(model.getSet(new ID("TrafficLightSetTest")), model.TrafficLightSetList); + assertEquals(model.getSetGroupA(new ID("TrafficLightSetTest")), model.InteriorListA); } @Test