Search in sources :

Example 21 with Road

use of il.ac.technion.cs.yp.btw.classes.Road in project BTW by TechnionYearlyProject.

the class SmartTrafficLightManager method insertCrossroads.

/*
    * @Author: Sharon Hadar
    * @Date: 2/06/2018
    * here the crossroads are the real crossroads in the map
    * */
@Override
public SmartTrafficLightManager insertCrossroads(Set<CityCrossroad> crossroads) {
    // for every cross road attach all its roads and initialize their time to 0.0
    // get all roads in map
    Set<CityRoad> roadsSet = new HashSet<>();
    crossroads.stream().map(crossroad -> (crossroad.getRealTrafficLights().stream().map(trafficlight -> roadsSet.add(trafficlight.getSourceRoad())).collect(Collectors.toSet()))).collect(Collectors.toSet());
    // for every road attach all its trafficlights
    Map<CityRoad, Set<CityTrafficLight>> roadToTrafficlightsMap = new HashMap<>();
    roadsSet.stream().map(road -> roadToTrafficlightsMap.put(road, road.getDestinationCrossroad().getRealTrafficLightsFromRoad(road))).collect(Collectors.toSet());
    // for every crossroad attach all roads that it ends
    Map<CityCrossroad, List<CityRoad>> crossRoadsToRoadsMap = new HashMap<>();
    crossroads.stream().map(crossroad -> crossRoadsToRoadsMap.put(crossroad, crossroad.getRealTrafficLights().stream().map(trafficLight -> trafficLight.getSourceRoad()).collect(Collectors.toSet()).stream().collect(Collectors.toList()))).collect(Collectors.toSet());
    // create a set of CrossroadManagers
    this.crossroadsManagers = crossRoadsToRoadsMap.entrySet().stream().map(entry -> {
        CityCrossroad crossroad = entry.getKey();
        List<CityRoad> roadsList = entry.getValue();
        AtomicInteger roadSerialNumber = new AtomicInteger(0);
        List<RoadManager> roadManagers = roadsList.stream().map(road -> new RoadManager(road, roadToTrafficlightsMap.get(road), roadSerialNumber.getAndIncrement())).collect(Collectors.toList());
        return new CrossroadManager(crossroad, roadManagers);
    }).collect(Collectors.toSet());
    this.minimumOpenTime = crossroads.stream().flatMap(crossroad -> crossroad.getRealTrafficLights().stream()).mapToInt(CityTrafficLight::getMinimumOpenTime).max().getAsInt() + // may throw - need to catch
    1;
    return this;
}
Also used : Logger(org.apache.log4j.Logger) java.util(java.util) CityRoad(il.ac.technion.cs.yp.btw.citysimulation.CityRoad) Road(il.ac.technion.cs.yp.btw.classes.Road) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CityCrossroad(il.ac.technion.cs.yp.btw.citysimulation.CityCrossroad) CityTrafficLight(il.ac.technion.cs.yp.btw.citysimulation.CityTrafficLight) Crossroad(il.ac.technion.cs.yp.btw.classes.Crossroad) Collectors(java.util.stream.Collectors) TrafficLight(il.ac.technion.cs.yp.btw.classes.TrafficLight) CityTrafficLight(il.ac.technion.cs.yp.btw.citysimulation.CityTrafficLight) CityRoad(il.ac.technion.cs.yp.btw.citysimulation.CityRoad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CityCrossroad(il.ac.technion.cs.yp.btw.citysimulation.CityCrossroad)

Aggregations

Road (il.ac.technion.cs.yp.btw.classes.Road)21 DataRoad (il.ac.technion.cs.yp.btw.db.DataObjects.DataRoad)10 Test (org.junit.Test)7 Point (il.ac.technion.cs.yp.btw.classes.Point)4 PointImpl (il.ac.technion.cs.yp.btw.classes.PointImpl)4 TrafficLight (il.ac.technion.cs.yp.btw.classes.TrafficLight)4 HashSet (java.util.HashSet)4 SQLException (java.sql.SQLException)3 CityRoad (il.ac.technion.cs.yp.btw.citysimulation.CityRoad)2 Crossroad (il.ac.technion.cs.yp.btw.classes.Crossroad)2 DataCrossRoad (il.ac.technion.cs.yp.btw.db.DataObjects.DataCrossRoad)2 DataTrafficLight (il.ac.technion.cs.yp.btw.db.DataObjects.DataTrafficLight)2 LinkedList (java.util.LinkedList)2 PriorityQueue (java.util.PriorityQueue)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 CityCrossroad (il.ac.technion.cs.yp.btw.citysimulation.CityCrossroad)1 CityTrafficLight (il.ac.technion.cs.yp.btw.citysimulation.CityTrafficLight)1