1 package org.opentrafficsim.road.network;
2
3 import java.util.LinkedHashMap;
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.GTUType;
9 import org.opentrafficsim.core.network.Link;
10 import org.opentrafficsim.core.network.LinkType;
11 import org.opentrafficsim.core.network.NetworkException;
12 import org.opentrafficsim.core.network.Node;
13 import org.opentrafficsim.core.network.OTSLink;
14 import org.opentrafficsim.core.network.OTSNode;
15 import org.opentrafficsim.core.network.route.Route;
16 import org.opentrafficsim.core.object.InvisibleObjectInterface;
17 import org.opentrafficsim.road.network.lane.LaneType;
18
19 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20
21
22
23
24
25
26
27
28
29 public final class OTSRoadNetworkUtils
30 {
31
32 private OTSRoadNetworkUtils()
33 {
34
35 }
36
37
38
39
40
41
42
43
44
45
46 @SuppressWarnings("checkstyle:designforextension")
47 public static OTSRoadNetworkRoadNetwork.html#OTSRoadNetwork">OTSRoadNetwork clone(final OTSRoadNetwork network, final String newId,
48 final SimulatorInterface.TimeDoubleUnit oldSimulator, final OTSSimulatorInterface newSimulator)
49 throws NetworkException
50 {
51 OTSRoadNetworketwork.html#OTSRoadNetwork">OTSRoadNetwork newNetwork = new OTSRoadNetwork(newId, false, newSimulator);
52
53
54 for (Node node : network.getNodeMap().values())
55 {
56 ((OTSNode) node).clone1(newNetwork);
57 }
58
59
60 for (Link oldLink : network.getLinkMap().values())
61 {
62 ((OTSLink) oldLink).clone(newNetwork);
63 }
64
65
66 for (Node oldNode : network.getNodeMap().values())
67 {
68 ((OTSNode) oldNode).clone2(newNetwork);
69 }
70
71
72 for (GTUType gtuType : network.getLinkGraphs().keySet())
73 {
74 newNetwork.buildGraph(gtuType);
75 }
76
77
78 Map<GTUType, Map<String, Route>> newRouteMap = new LinkedHashMap<>();
79 for (GTUType gtuType : network.getRouteMap().keySet())
80 {
81 Map<String, Route> newRoutes = new LinkedHashMap<>();
82 for (Route route : network.getRouteMap().get(gtuType).values())
83 {
84 newRoutes.put(route.getId(), route.clone(newNetwork));
85 }
86 newRouteMap.put(gtuType, newRoutes);
87 }
88 newNetwork.setRawRouteMap(newRouteMap);
89
90
91 for (InvisibleObjectInterface io : network.getInvisibleObjectMap().values())
92 {
93 InvisibleObjectInterface clonedIO = io.clone(newSimulator, newNetwork);
94 newNetwork.addInvisibleObject(clonedIO);
95 }
96
97
98
99
100 for (GTUType gtuType : network.getGtuTypes().values())
101 {
102 if (gtuType.getParent() == null)
103 {
104 new GTUType(gtuType.getId(), newNetwork);
105 }
106 }
107 for (GTUType gtuType : network.getGtuTypes().values())
108 {
109 if (gtuType.getParent() != null)
110 {
111 new GTUType(gtuType.getId(), gtuType.getParent());
112 }
113 }
114
115
116 for (LinkType linkType : network.getLinkTypes().values())
117 {
118 if (linkType.getParent() != null)
119 {
120 new LinkType(linkType.getId(), linkType.getParent(), new GTUCompatibility<>(linkType.getCompatibility()),
121 newNetwork);
122 }
123 }
124
125
126 for (LaneType laneType : network.getLaneTypes().values())
127 {
128 if (laneType.getParent() != null)
129 {
130 new LaneType(laneType.getId(), laneType.getParent(), new GTUCompatibility<>(laneType.getCompatibility()),
131 newNetwork);
132 }
133 }
134
135 return newNetwork;
136 }
137
138
139
140
141
142
143 public static void destroy(final OTSRoadNetwork network, final SimulatorInterface.TimeDoubleUnit simulator)
144 {
145 OTSRoadNetworkUtils.destroy(network, simulator);
146 }
147
148 }