Skip to content
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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e7c7269
TrafficLight cleanup based on discussed model
An7ar35 Mar 9, 2016
fd020e4
Traffic Light adding getter method.
Inspiron17R Mar 10, 2016
54fb211
Merge remote-tracking branch 'origin/TrafficLights' into TrafficLights
Inspiron17R Mar 10, 2016
0805fd3
Single traffic light: class, rule and controller.
Inspiron17R Mar 14, 2016
95e1c6e
Traffic lights further improvements.
Inspiron17R Mar 14, 2016
6a1153f
Traffic lights core of the test, errors in graphtool.class
Inspiron17R Mar 14, 2016
6375de0
Repaired problem with GraphTool/GraphToolTest classes. for this branch
An7ar35 Mar 15, 2016
7f3a0aa
Traffic lights improvements;
Inspiron17R Mar 16, 2016
93daf66
TrafficLightSet -- adding traffic lights to one set, if they are on t…
Inspiron17R Mar 16, 2016
bba5003
small changes
An7ar35 Mar 16, 2016
b1d7f4c
small changes
An7ar35 Mar 16, 2016
644cf4a
TrafficLights rules improvements
Inspiron17R Mar 16, 2016
3ec97b0
TrafficLightController & TrafficLightSet improvements.
Inspiron17R Mar 16, 2016
cce2aa7
Traffic lights improvements.
Inspiron17R Mar 16, 2016
fd17ed1
Traffic lights: adding rules for single lights and those in the sets.
Inspiron17R Mar 16, 2016
5dd5cde
single traffic lights class: completed test.
Inspiron17R Mar 20, 2016
e2f0f1e
Frther tests: Traffic LightRule & TraficLightlnSet & small changes in…
Inspiron17R Mar 20, 2016
c1f85a1
Test of the rule of the single traffic lights list & fixes
Inspiron17R Mar 20, 2016
7900785
Further tests & small fixes
Inspiron17R Mar 20, 2016
1e95251
Further tests.
Inspiron17R Mar 21, 2016
c2e0907
Controller and its test.
Inspiron17R Mar 21, 2016
180e791
Merge remote-tracking branch 'remotes/origin/master' into TrafficLights
An7ar35 Mar 22, 2016
ca9a981
Traffic lights: changes in a few classes. Removing unused variables, …
Inspiron17R Mar 28, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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 {
Copy link
Member

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?


/**
* Changes colours at trafficLights in one set
*
* @param trafficLightList List of the traffic lights in one set
*/
public static void changeStateofSet(List<TrafficLightInSet> trafficLightList){
Copy link
Member

Choose a reason for hiding this comment

The 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 TrafficLightsInSetRule you have. Light change mechanics/logic should be in TrafficController. Rules just need to describe the what the logic must apply, not the logic itself.
e.g.: - Rule state that there are 2 lights that work synchronously in opposite state (when one is green the other is red). Rule also contains the ID of those two lights and the delay between changes.

  • Controller takes this rule and creates 2 lights that start with opposite colour state.
  • Controller's tick looks at the set and the delay inside of it then if the states needs changing then it goes through each of the lights and changes their state appropriately.

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
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs docs

long milliseconds = ChronoUnit.MILLIS.between(start, date);
return milliseconds;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about return ChronoUnit.MILLIS.between(start, date); ? 1 line instead of 2 :)

}

/**
* 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference to modelSet and model but they are never instantiated. In anycase they are singular variable which gives us 1 TrafficLight and 1 TrafficLightInSet for every TrafficLightController. But we only want 1 controller. This is not going to work. Have a rethink.

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){
Copy link
Member

Choose a reason for hiding this comment

The 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) {

}
}
}
Loading