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