OTSNetworkUtils.java

  1. package org.opentrafficsim.core.network;

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

  4. import org.opentrafficsim.core.compatibility.GTUCompatibility;
  5. import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
  6. import org.opentrafficsim.core.gtu.GTU;
  7. import org.opentrafficsim.core.gtu.GTUType;
  8. import org.opentrafficsim.core.network.route.Route;
  9. import org.opentrafficsim.core.object.InvisibleObjectInterface;

  10. /**
  11.  * OTSNetworkCloner makes a deep clone of a network. <br>
  12.  * <br>
  13.  * Copyright (c) 2003-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  14.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  15.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  16.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  17.  */
  18. public final class OTSNetworkUtils
  19. {
  20.     /** */
  21.     private OTSNetworkUtils()
  22.     {
  23.         // utility class
  24.     }

  25.     /**
  26.      * Clone the OTSNetwork.
  27.      * @param network OTSNetwork; the network to clone
  28.      * @param newId String; the new id of the network
  29.      * @param newSimulator OTSSimulatorInterface; the new simulator for this network
  30.      * @return a clone of this network
  31.      * @throws NetworkException in case the cloning fails
  32.      */
  33.     @SuppressWarnings("checkstyle:designforextension")
  34.     public static OTSNetwork clone(final OTSNetwork network, final String newId, final OTSSimulatorInterface newSimulator)
  35.             throws NetworkException
  36.     {
  37.         OTSNetwork newNetwork = new OTSNetwork(newId, false, newSimulator);

  38.         // clone the nodes
  39.         for (Node node : network.getNodeMap().values())
  40.         {
  41.             ((OTSNode) node).clone1(newNetwork);
  42.         }

  43.         // clone the links
  44.         for (Link oldLink : network.getLinkMap().values())
  45.         {
  46.             ((OTSLink) oldLink).clone(newNetwork);
  47.         }

  48.         // make the link-connections for the cloned nodes
  49.         for (Node oldNode : network.getNodeMap().values())
  50.         {
  51.             ((OTSNode) oldNode).clone2(newNetwork);
  52.         }

  53.         // clone the graphs that had been created for the old network
  54.         for (GTUType gtuType : network.getLinkGraphs().keySet())
  55.         {
  56.             newNetwork.buildGraph(gtuType);
  57.         }

  58.         // clone the routes
  59.         Map<GTUType, Map<String, Route>> newRouteMap = new LinkedHashMap<>();
  60.         for (GTUType gtuType : network.getRouteMap().keySet())
  61.         {
  62.             Map<String, Route> newRoutes = new LinkedHashMap<>();
  63.             for (Route route : network.getRouteMap().get(gtuType).values())
  64.             {
  65.                 newRoutes.put(route.getId(), route.clone(newNetwork));
  66.             }
  67.             newRouteMap.put(gtuType, newRoutes);
  68.         }
  69.         newNetwork.setRawRouteMap(newRouteMap);

  70.         // clone the traffic lights
  71.         for (InvisibleObjectInterface io : network.getInvisibleObjectMap().values())
  72.         {
  73.             InvisibleObjectInterface clonedIO = io.clone(newSimulator, newNetwork);
  74.             newNetwork.addInvisibleObject(clonedIO);
  75.         }

  76.         // TODO clone the visible objects

  77.         // clone the GTUTypes
  78.         for (GTUType gtuType : network.getGtuTypes().values())
  79.         {
  80.             if (gtuType.getParent() == null)
  81.             {
  82.                 new GTUType(gtuType.getId(), newNetwork);
  83.             }
  84.         }
  85.         for (GTUType gtuType : network.getGtuTypes().values())
  86.         {
  87.             if (gtuType.getParent() != null)
  88.             {
  89.                 new GTUType(gtuType.getId(), gtuType.getParent());
  90.             }
  91.         }

  92.         // clone the LinkTypes TODO LinkType should clone itself!
  93.         for (LinkType linkType : network.getLinkTypes().values())
  94.         {
  95.             if (linkType.getParent() != null)
  96.             {
  97.                 new LinkType(linkType.getId(), linkType.getParent(), new GTUCompatibility<>(linkType.getCompatibility()),
  98.                         newNetwork);
  99.             }
  100.         }

  101.         return newNetwork;
  102.     }

  103.     /**
  104.      * Remove all objects and animation in the network.
  105.      * @param network OTSNetwork; the network to destroy
  106.      */
  107.     public static void destroy(final OTSNetwork network)
  108.     {
  109.         for (GTU gtu : network.getGTUs())
  110.         {
  111.             gtu.destroy();
  112.         }

  113.         network.getRawNodeMap().clear();
  114.         network.getRawLinkMap().clear();
  115.         network.getRawLinkGraphs().clear();
  116.         network.getRawRouteMap().clear();
  117.     }

  118. }