View Javadoc
1   package org.opentrafficsim.road.network;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.opentrafficsim.core.compatibility.GTUCompatibility;
7   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
8   import org.opentrafficsim.core.gtu.GTUType;
9   import org.opentrafficsim.core.network.Link;
10  import org.opentrafficsim.core.network.LinkType;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.Node;
13  import org.opentrafficsim.core.network.OTSLink;
14  import org.opentrafficsim.core.network.OTSNode;
15  import org.opentrafficsim.core.network.route.Route;
16  import org.opentrafficsim.core.object.InvisibleObjectInterface;
17  import org.opentrafficsim.road.network.lane.LaneType;
18  
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  
21  /**
22   * OTSRoadNetworkCloner makes a deep clone of a network. <br>
23   * <br>
24   * Copyright (c) 2003-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
25   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
26   * source code and binary code of this software is proprietary information of Delft University of Technology.
27   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
28   */
29  public final class OTSRoadNetworkUtils
30  {
31      /** */
32      private OTSRoadNetworkUtils()
33      {
34          // utility class
35      }
36  
37      /**
38       * Clone the OTSRoadNetwork.
39       * @param network OTSRoadNetwork; the network to clone
40       * @param newId String; the new id of the network
41       * @param oldSimulator SimulatorInterface.TimeDoubleUnit; the old simulator for this network
42       * @param newSimulator OTSSimulatorInterface; the new simulator for this network
43       * @return a clone of this network
44       * @throws NetworkException in case the cloning fails
45       */
46      @SuppressWarnings("checkstyle:designforextension")
47      public static OTSRoadNetwork clone(final OTSRoadNetwork network, final String newId,
48              final SimulatorInterface.TimeDoubleUnit oldSimulator, final OTSSimulatorInterface newSimulator)
49              throws NetworkException
50      {
51          OTSRoadNetwork newNetwork = new OTSRoadNetwork(newId, false);
52  
53          // clone the nodes
54          for (Node node : network.getNodeMap().values())
55          {
56              ((OTSNode) node).clone1(newNetwork, newSimulator);
57          }
58  
59          // clone the links
60          for (Link oldLink : network.getLinkMap().values())
61          {
62              ((OTSLink) oldLink).clone(newNetwork, newSimulator);
63          }
64  
65          // make the link-connections for the cloned nodes
66          for (Node oldNode : network.getNodeMap().values())
67          {
68              ((OTSNode) oldNode).clone2(newNetwork, newSimulator);
69          }
70  
71          // clone the graphs that had been created for the old network
72          for (GTUType gtuType : network.getLinkGraphs().keySet())
73          {
74              newNetwork.buildGraph(gtuType);
75          }
76  
77          // clone the routes
78          Map<GTUType, Map<String, Route>> newRouteMap = new HashMap<>();
79          for (GTUType gtuType : network.getRouteMap().keySet())
80          {
81              Map<String, Route> newRoutes = new HashMap<>();
82              for (Route route : network.getRouteMap().get(gtuType).values())
83              {
84                  newRoutes.put(route.getId(), route.clone(newNetwork, newSimulator));
85              }
86              newRouteMap.put(gtuType, newRoutes);
87          }
88          newNetwork.setRawRouteMap(newRouteMap);
89  
90          // clone the traffic lights
91          for (InvisibleObjectInterface io : network.getInvisibleObjectMap().values())
92          {
93              InvisibleObjectInterface clonedIO = io.clone(newSimulator, newNetwork);
94              newNetwork.addInvisibleObject(clonedIO);
95          }
96  
97          // TODO clone the visible objects
98  
99          // clone the GTUTypes
100         for (GTUType gtuType : network.getGtuTypes().values())
101         {
102             if (gtuType.getParent() == null)
103             {
104                 new GTUType(gtuType.getId(), newNetwork);
105             }
106         }
107         for (GTUType gtuType : network.getGtuTypes().values())
108         {
109             if (gtuType.getParent() != null)
110             {
111                 new GTUType(gtuType.getId(), gtuType.getParent());
112             }
113         }
114 
115         // clone the LinkTypes
116         for (LinkType linkType : network.getLinkTypes().values())
117         {
118             if (linkType.getParent() != null)
119             {
120                 new LinkType(linkType.getId(), linkType.getParent(), new GTUCompatibility<>(linkType.getCompatibility()),
121                         newNetwork);
122             }
123         }
124 
125         // clone the LaneTypes
126         for (LaneType laneType : network.getLaneTypes().values())
127         {
128             if (laneType.getParent() != null)
129             {
130                 new LaneType(laneType.getId(), laneType.getParent(), new GTUCompatibility<>(laneType.getCompatibility()),
131                         newNetwork);
132             }
133         }
134 
135         return newNetwork;
136     }
137 
138     /**
139      * Remove all objects and animation in the road network.
140      * @param network OTSRoadNetwork; the network to destroy
141      * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator of the old network
142      */
143     public static void destroy(final OTSRoadNetwork network, final SimulatorInterface.TimeDoubleUnit simulator)
144     {
145         OTSRoadNetworkUtils.destroy(network, simulator);
146     }
147 
148 }