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