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
16
17
18
19
20
21
22 public final class OTSNetworkUtils
23 {
24
25 private OTSNetworkUtils()
26 {
27
28 }
29
30
31
32
33
34
35
36
37
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
47 for (Node node : network.getNodeMap().values())
48 {
49 ((OTSNode) node).clone1(newNetwork, newSimulator);
50 }
51
52
53 for (Link oldLink : network.getLinkMap().values())
54 {
55 ((OTSLink) oldLink).clone(newNetwork, newSimulator);
56 }
57
58
59 for (Node oldNode : network.getNodeMap().values())
60 {
61 ((OTSNode) oldNode).clone2(newNetwork, newSimulator);
62 }
63
64
65 for (GTUType gtuType : network.getLinkGraphs().keySet())
66 {
67 newNetwork.buildGraph(gtuType);
68 }
69
70
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
84 for (InvisibleObjectInterface io : network.getInvisibleObjectMap().values())
85 {
86 InvisibleObjectInterface clonedIO = io.clone(newSimulator, newNetwork);
87 newNetwork.addInvisibleObject(clonedIO);
88 }
89
90
91
92 return newNetwork;
93 }
94
95
96
97
98
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 }