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