-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Traffic lights #10
base: master
Are you sure you want to change the base?
Traffic lights #10
Changes from 22 commits
e7c7269
fd020e4
54fb211
0805fd3
95e1c6e
6a1153f
6375de0
7f3a0aa
93daf66
bba5003
b1d7f4c
644cf4a
3ec97b0
cce2aa7
fd17ed1
5dd5cde
e2f0f1e
c1f85a1
7900785
1e95251
c2e0907
180e791
ca9a981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.descriptors; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Stand-alone Traffic Light rule description | ||
*/ | ||
public class TrafficLightRule { | ||
|
||
/** | ||
* Changing single lights states | ||
* | ||
* @param trafficLightList - list with all single traffic lights | ||
*/ | ||
public static void changeStateofSingleTrafficLights(List<TrafficLight> trafficLightList) { | ||
|
||
trafficLightList.forEach(TrafficLight -> { | ||
TrafficLight.currentState = (TrafficLight.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.descriptors; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Traffic Light in synchronous set rule | ||
*/ | ||
public class TrafficLightsInSetRule { | ||
|
||
/** | ||
* Changes colours at trafficLights in one set | ||
* | ||
* @param trafficLightList List of the traffic lights in one set | ||
*/ | ||
public static void changeStateofSet(List<TrafficLightInSet> trafficLightList){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static? this means that this function only exists once no matter how many
|
||
for(TrafficLightInSet tf : trafficLightList ) { | ||
tf.currentState = (tf.currentState == TrafficLightState.RED) ? TrafficLightState.GREEN : TrafficLightState.RED; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.links; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; | ||
import java.time.LocalDateTime; | ||
|
||
//import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightRule; | ||
//import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightSet; | ||
//import kcl.teamIndexZero.traffic.simulator.data.links.TrafficLightInSet; | ||
|
||
|
||
|
||
/** | ||
* Created by Es on 02/03/2016. | ||
*/ | ||
/* public class TrafficLight { | ||
|
||
private ID id; | ||
|
||
public TrafficLight() { | ||
} | ||
|
||
public enum State { | ||
RED, GREEN | ||
} | ||
|
||
public State currentState; | ||
|
||
|
||
|
||
public TrafficLight(ID id) { | ||
|
||
this.id=id; | ||
this.currentState = TrafficLight.State.RED; | ||
|
||
} | ||
|
||
|
||
public State getState(ID id){ | ||
return this.currentState; | ||
} | ||
|
||
|
||
|
||
public ID getTrafficLightID(TrafficLight trafficLight) { return trafficLight.id;} | ||
|
||
public void tick(SimulationTick tick) { | ||
|
||
} | ||
} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.links; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.ISimulationAware; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightSet; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
|
||
|
||
/** | ||
* Created by Es on 02/03/2016. | ||
*/ | ||
/*public class TrafficLightInSet extends TrafficLight implements ISimulationAware { | ||
public ID id; | ||
public String temp; | ||
public long CurrentTime; | ||
public LocalDateTime Temporary; | ||
public long lastChange = 0; | ||
public long timer; | ||
private TrafficLightmodel; | ||
private TrafficLightSet modelSet; | ||
SimulationTick simulationTick; | ||
|
||
|
||
public TrafficLightInSet(ID id) { | ||
this.id = id; | ||
} | ||
|
||
|
||
public long formatTimeToLong(LocalDateTime date ){ | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | ||
this. temp = date.format(formatter); | ||
return Long.parseLong(temp, 10); | ||
} | ||
|
||
|
||
@Override | ||
public void tick(SimulationTick tick) { | ||
Temporary =simulationTick.getSimulatedTime(); | ||
CurrentTime = formatTimeToLong(Temporary); | ||
|
||
if ((CurrentTime - lastChange) > timer) { | ||
if (modelSet != null) { | ||
// modelSet.changeColour(model, model.currentState); | ||
lastChange = CurrentTime; | ||
} | ||
} | ||
} | ||
} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.trafficLight; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; | ||
|
||
/** | ||
* Stand-alone independent Traffic Light | ||
* Created by Es on 02/03/2016. | ||
*/ | ||
public class TrafficLight { | ||
private ID id; | ||
private TrafficLightState currentState; | ||
|
||
public long TrafficLightDelay; | ||
public TrafficLightState currentState; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param id Link ID tag | ||
* @param id TrafficLight ID tag | ||
*/ | ||
public TrafficLight(ID id) { | ||
this.id = id; | ||
|
||
this.id=id; | ||
this.currentState = TrafficLightState.GREEN; | ||
|
||
} | ||
|
||
/** | ||
* Gets the current Traffic Light state | ||
* Returns the current state of the TrafficLight object | ||
*/ | ||
public TrafficLightState getState() { | ||
public TrafficLightState getState(){ | ||
return this.currentState; | ||
} | ||
|
||
/** | ||
* Returns the ID of the current TrafficLight object | ||
*/ | ||
public ID getTrafficLightID() { return this.id;} | ||
|
||
//@Override | ||
public void tick(SimulationTick tick) { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,118 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.trafficLight; | ||
|
||
import kcl.teamIndexZero.traffic.log.Logger; | ||
import kcl.teamIndexZero.traffic.log.Logger_Interface; | ||
import kcl.teamIndexZero.traffic.simulator.ISimulationAware; | ||
//import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; | ||
import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightRule; | ||
import kcl.teamIndexZero.traffic.simulator.data.descriptors.TrafficLightsInSetRule; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightInSet; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLight; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* Traffic Light controller | ||
* 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 static LocalDateTime start = LocalDateTime.of(1984, 12, 16, 7, 45, 55); | ||
private TrafficLight model; //list this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's not used, get rid of it. |
||
private TrafficLightInSet modelSet; //list this | ||
private TrafficLightSet set; | ||
public List<TrafficLight> TrafficLightSinglesList; | ||
public List<TrafficLightInSet> TrafficLightSetList; | ||
SimulationTick simulationTick; | ||
private static Logger_Interface LOG = Logger.getLoggerInstance(TrafficLightController.class.getSimpleName()); | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public TrafficLightController(){ | ||
TrafficLightSinglesList = new ArrayList<TrafficLight>(); | ||
TrafficLightSetList = new ArrayList<TrafficLightInSet>(); | ||
} | ||
|
||
public long formatTimeToLong(LocalDateTime date) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs docs |
||
long milliseconds = ChronoUnit.MILLIS.between(start, date); | ||
return milliseconds; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
} | ||
|
||
/** | ||
* Adds single traffic light to the List of all single traffic lights | ||
* | ||
* @param trafficLight object to be added to the list | ||
*/ | ||
public void addTrafficlight(TrafficLight trafficLight){ | ||
|
||
if (!(trafficLight == null)) { | ||
TrafficLightSinglesList.add(trafficLight); | ||
LOG.log("Added the following traffic lights to the set of all single Traffic Lights: ", trafficLight.getTrafficLightID() ); | ||
} | ||
else { | ||
LOG.log_Error("Error while adding to TrafficLightLnSet to the set"); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Returns a list List of all single traffic lights | ||
* | ||
*/ | ||
public List<TrafficLight> getSingleSet() {return TrafficLightSinglesList;} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void tick(SimulationTick tick) { | ||
CurrentTime = formatTimeToLong(simulationTick.getSimulatedTime()); | ||
|
||
/*when single traffic lights do not work in a synchronous way with the ticks*/ | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reference to |
||
if ((modelSet.TrafficLightInSetDelay >= timer) && (modelSet.currentState == TrafficLightState.GREEN)){ | ||
modelSet.currentState = TrafficLightState.RED; | ||
} | ||
|
||
if ((CurrentTime - lastChange) > timer) { | ||
if (modelSet != null) { | ||
TrafficLightsInSetRule.changeStateofSet(TrafficLightSetList); | ||
TrafficLightRule.changeStateofSingleTrafficLights(TrafficLightSinglesList); | ||
lastChange = CurrentTime; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void tick(SimulationTick tick) { | ||
/** | ||
* Adds rule to the one traffic light | ||
*/ | ||
public void addRule(TrafficLightRule rule){ | ||
rule.changeStateofSingleTrafficLights(TrafficLightSinglesList); | ||
} | ||
/** | ||
* Adds rule to the one traffic light set | ||
*/ | ||
public void addRule(TrafficLightsInSetRule rule){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So how are the TrafficLightInSet linked together so that they work synchronously? |
||
rule.changeStateofSet(TrafficLightSetList); | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,44 @@ | ||
package kcl.teamIndexZero.traffic.simulator.data.trafficLight; | ||
|
||
import kcl.teamIndexZero.traffic.simulator.data.ID; | ||
|
||
import java.util.Date; | ||
import kcl.teamIndexZero.traffic.simulator.data.SimulationTick; | ||
import kcl.teamIndexZero.traffic.simulator.data.trafficLight.TrafficLightState; | ||
|
||
/** | ||
* Traffic Light part of a synchronous set | ||
*/ | ||
public class TrafficLightInSet { | ||
long CurrentDate = (new Date().getTime()) / 1000; | ||
long lastChange = CurrentDate; | ||
long timer; | ||
private TrafficLight model; | ||
private TrafficLightSet modelSet; | ||
|
||
public TrafficLightInSet(ID linkId) { | ||
//TODO Fix construction? | ||
|
||
private ID id; | ||
public long TrafficLightInSetDelay; | ||
public TrafficLightState currentState; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param id TrafficLightInSet ID tag | ||
*/ | ||
public TrafficLightInSet(ID id) { | ||
|
||
this.id=id; | ||
this.currentState = TrafficLightState.GREEN; | ||
|
||
} | ||
|
||
/** | ||
* Returns the current state of the TrafficLightInSet | ||
*/ | ||
public TrafficLightState getState(){ | ||
return this.currentState; | ||
} | ||
|
||
/** | ||
* Returns the current state of the TrafficLightInSet | ||
*/ | ||
public ID getID() { return this.id;} | ||
|
||
//@Override | ||
public void tick(SimulationTick tick) { | ||
|
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the traffic light state change logic is in a class that is supposed to be used as descriptor. How can we create synchronous (working together) sets of lights if this is here?