View Javadoc
1   package org.opentrafficsim.road.network;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   
6   import org.djutils.exceptions.Throw;
7   import org.djutils.immutablecollections.Immutable;
8   import org.djutils.immutablecollections.ImmutableHashMap;
9   import org.djutils.immutablecollections.ImmutableMap;
10  import org.opentrafficsim.core.compatibility.GTUCompatibility;
11  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
12  import org.opentrafficsim.core.gtu.GTUType;
13  import org.opentrafficsim.core.network.LongitudinalDirectionality;
14  import org.opentrafficsim.core.network.OTSNetwork;
15  import org.opentrafficsim.road.gtu.lane.tactical.routesystem.RouteSystem;
16  import org.opentrafficsim.road.network.lane.LaneType;
17  
18  /**
19   * OTSRoadNetwork adds a number of methods to the Network class that are specific for roads, such as the LaneTypes. <br>
20   * <br>
21   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
23   * source code and binary code of this software is proprietary information of Delft University of Technology.
24   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
25   */
26  public class OTSRoadNetwork extends OTSNetwork implements RoadNetwork
27  {
28      /** */
29      private static final long serialVersionUID = 1L;
30  
31      /** LaneTypes registered for this network. */
32      private Map<String, LaneType> laneTypeMap = new LinkedHashMap<>();
33      
34      /** Route system. */
35      private RouteSystem routeSystem;
36  
37      /**
38       * Construction of an empty network.
39       * @param id String; the network id.
40       * @param addDefaultTypes add the default GTUTypes, LinkTypesand LaneTypes, or not
41       * @param simulator OTSSimulatorInterface; the DSOL simulator engine
42       */
43      public OTSRoadNetwork(final String id, final boolean addDefaultTypes, final OTSSimulatorInterface simulator)
44      {
45          super(id, addDefaultTypes, simulator);
46          if (addDefaultTypes)
47          {
48              addDefaultLaneTypes();
49          }
50          // TODO: not null once the route system works
51          this.routeSystem = null; //new DefaultRouteSystem();
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public void addDefaultLaneTypes()
57      {
58          GTUCompatibility<LaneType> noTrafficCompatibility = new GTUCompatibility<>((LaneType) null);
59          new LaneType("NONE", noTrafficCompatibility, this);
60          GTUCompatibility<LaneType> roadCompatibility = new GTUCompatibility<>((LaneType) null);
61          roadCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.ROAD_USER), LongitudinalDirectionality.DIR_BOTH);
62          LaneTypene/LaneType.html#LaneType">LaneType twoWayLane = new LaneType("TWO_WAY_LANE", roadCompatibility, this);
63          new LaneType("RURAL_ROAD", twoWayLane, new GTUCompatibility<>(roadCompatibility), this);
64          new LaneType("URBAN_ROAD", twoWayLane, new GTUCompatibility<>(roadCompatibility), this);
65          new LaneType("RESIDENTIAL_ROAD", twoWayLane, new GTUCompatibility<>(roadCompatibility), this);
66          GTUCompatibility<LaneType> oneWayLaneCompatibility = new GTUCompatibility<>(roadCompatibility);
67          oneWayLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.ROAD_USER), LongitudinalDirectionality.DIR_PLUS);
68          oneWayLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.PEDESTRIAN), LongitudinalDirectionality.DIR_BOTH);
69          new LaneType("ONE_WAY_LANE", oneWayLaneCompatibility, this);
70          GTUCompatibility<LaneType> highwayLaneCompatibility = new GTUCompatibility<>(oneWayLaneCompatibility)
71                  .addAllowedGTUType(getGtuType(GTUType.DEFAULTS.PEDESTRIAN), LongitudinalDirectionality.DIR_NONE);
72          new LaneType("FREEWAY", highwayLaneCompatibility, this);
73          new LaneType("HIGHWAY", highwayLaneCompatibility, this);
74          GTUCompatibility<LaneType> busLaneCompatibility = new GTUCompatibility<>(roadCompatibility);
75          busLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.BUS), LongitudinalDirectionality.DIR_BOTH);
76          busLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.ROAD_USER), LongitudinalDirectionality.DIR_NONE);
77          new LaneType("BUS_LANE", busLaneCompatibility, this);
78          GTUCompatibility<LaneType> mopedAndBicycleLaneCompatibility = new GTUCompatibility<>(roadCompatibility);
79          mopedAndBicycleLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.BICYCLE),
80                  LongitudinalDirectionality.DIR_BOTH);
81          mopedAndBicycleLaneCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.ROAD_USER),
82                  LongitudinalDirectionality.DIR_NONE);
83          new LaneType("MOPED_PATH", mopedAndBicycleLaneCompatibility, this);
84          GTUCompatibility<LaneType> bicycleOnlyCompatibility = new GTUCompatibility<>(mopedAndBicycleLaneCompatibility);
85          bicycleOnlyCompatibility.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.MOPED), LongitudinalDirectionality.DIR_NONE);
86          new LaneType("BICYCLE_PATH", bicycleOnlyCompatibility, this);
87          GTUCompatibility<LaneType> pedestriansOnly = new GTUCompatibility<>(roadCompatibility);
88          pedestriansOnly.addAllowedGTUType(getGtuType(GTUType.DEFAULTS.ROAD_USER), LongitudinalDirectionality.DIR_NONE);
89          new LaneType("FOOTPATH", pedestriansOnly, this);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void addLaneType(final LaneType laneType)
95      {
96          this.laneTypeMap.put(laneType.getId(), laneType);
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public LaneType getLaneType(final String laneTypeId)
102     {
103         return this.laneTypeMap.get(laneTypeId);
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public LaneType getLaneType(final LaneType.DEFAULTS laneTypeEnum)
109     {
110         return this.laneTypeMap.get(laneTypeEnum.getId());
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public ImmutableMap<String, LaneType> getLaneTypes()
116     {
117         return new ImmutableHashMap<>(this.laneTypeMap, Immutable.WRAP);
118     }
119     
120     /**
121      * Sets the route system.
122      * @param routeSystem RouteSystem; route system
123      */
124     public void setRouteSystem(final RouteSystem routeSystem)
125     {
126         Throw.whenNull(routeSystem, "Route system may not be null.");
127         this.routeSystem = routeSystem;
128     }
129     
130     /**
131      * Returns the route system.
132      * @return RouteSystem; route system
133      */
134     public RouteSystem getRouteSystem()
135     {
136         return this.routeSystem;
137     }
138 
139 }