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