OTSRoadNetworkUtils.java

  1. package org.opentrafficsim.road.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.GTUType;
  7. import org.opentrafficsim.core.network.Link;
  8. import org.opentrafficsim.core.network.LinkType;
  9. import org.opentrafficsim.core.network.NetworkException;
  10. import org.opentrafficsim.core.network.Node;
  11. import org.opentrafficsim.core.network.OTSLink;
  12. import org.opentrafficsim.core.network.OTSNode;
  13. import org.opentrafficsim.core.network.route.Route;
  14. import org.opentrafficsim.core.object.InvisibleObjectInterface;
  15. import org.opentrafficsim.road.network.lane.LaneType;

  16. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;

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

  32.     /**
  33.      * Clone the OTSRoadNetwork.
  34.      * @param network OTSRoadNetwork; the network to clone
  35.      * @param newId String; the new id of the network
  36.      * @param oldSimulator SimulatorInterface.TimeDoubleUnit; the old simulator for this network
  37.      * @param newSimulator OTSSimulatorInterface; the new simulator for this network
  38.      * @return a clone of this network
  39.      * @throws NetworkException in case the cloning fails
  40.      */
  41.     @SuppressWarnings("checkstyle:designforextension")
  42.     public static OTSRoadNetwork clone(final OTSRoadNetwork network, final String newId,
  43.             final SimulatorInterface.TimeDoubleUnit oldSimulator, final OTSSimulatorInterface newSimulator)
  44.             throws NetworkException
  45.     {
  46.         OTSRoadNetwork newNetwork = new OTSRoadNetwork(newId, false);

  47.         // clone the nodes
  48.         for (Node node : network.getNodeMap().values())
  49.         {
  50.             ((OTSNode) node).clone1(newNetwork, newSimulator);
  51.         }

  52.         // clone the links
  53.         for (Link oldLink : network.getLinkMap().values())
  54.         {
  55.             ((OTSLink) oldLink).clone(newNetwork, newSimulator);
  56.         }

  57.         // make the link-connections for the cloned nodes
  58.         for (Node oldNode : network.getNodeMap().values())
  59.         {
  60.             ((OTSNode) oldNode).clone2(newNetwork, newSimulator);
  61.         }

  62.         // clone the graphs that had been created for the old network
  63.         for (GTUType gtuType : network.getLinkGraphs().keySet())
  64.         {
  65.             newNetwork.buildGraph(gtuType);
  66.         }

  67.         // clone the routes
  68.         Map<GTUType, Map<String, Route>> newRouteMap = new LinkedHashMap<>();
  69.         for (GTUType gtuType : network.getRouteMap().keySet())
  70.         {
  71.             Map<String, Route> newRoutes = new LinkedHashMap<>();
  72.             for (Route route : network.getRouteMap().get(gtuType).values())
  73.             {
  74.                 newRoutes.put(route.getId(), route.clone(newNetwork, newSimulator));
  75.             }
  76.             newRouteMap.put(gtuType, newRoutes);
  77.         }
  78.         newNetwork.setRawRouteMap(newRouteMap);

  79.         // clone the traffic lights
  80.         for (InvisibleObjectInterface io : network.getInvisibleObjectMap().values())
  81.         {
  82.             InvisibleObjectInterface clonedIO = io.clone(newSimulator, newNetwork);
  83.             newNetwork.addInvisibleObject(clonedIO);
  84.         }

  85.         // TODO clone the visible objects

  86.         // clone the GTUTypes
  87.         for (GTUType gtuType : network.getGtuTypes().values())
  88.         {
  89.             if (gtuType.getParent() == null)
  90.             {
  91.                 new GTUType(gtuType.getId(), newNetwork);
  92.             }
  93.         }
  94.         for (GTUType gtuType : network.getGtuTypes().values())
  95.         {
  96.             if (gtuType.getParent() != null)
  97.             {
  98.                 new GTUType(gtuType.getId(), gtuType.getParent());
  99.             }
  100.         }

  101.         // clone the LinkTypes
  102.         for (LinkType linkType : network.getLinkTypes().values())
  103.         {
  104.             if (linkType.getParent() != null)
  105.             {
  106.                 new LinkType(linkType.getId(), linkType.getParent(), new GTUCompatibility<>(linkType.getCompatibility()),
  107.                         newNetwork);
  108.             }
  109.         }

  110.         // clone the LaneTypes
  111.         for (LaneType laneType : network.getLaneTypes().values())
  112.         {
  113.             if (laneType.getParent() != null)
  114.             {
  115.                 new LaneType(laneType.getId(), laneType.getParent(), new GTUCompatibility<>(laneType.getCompatibility()),
  116.                         newNetwork);
  117.             }
  118.         }

  119.         return newNetwork;
  120.     }

  121.     /**
  122.      * Remove all objects and animation in the road network.
  123.      * @param network OTSRoadNetwork; the network to destroy
  124.      * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator of the old network
  125.      */
  126.     public static void destroy(final OTSRoadNetwork network, final SimulatorInterface.TimeDoubleUnit simulator)
  127.     {
  128.         OTSRoadNetworkUtils.destroy(network, simulator);
  129.     }

  130. }