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