1 package org.opentrafficsim.draw.network;
2
3 import java.rmi.RemoteException;
4 import java.util.LinkedHashSet;
5 import java.util.Set;
6
7 import javax.naming.NamingException;
8
9 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
10 import org.opentrafficsim.core.network.Link;
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.OTSNetwork;
15 import org.opentrafficsim.core.network.OTSNetworkUtils;
16 import org.opentrafficsim.core.network.OTSNode;
17 import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
18
19 import nl.tudelft.simulation.dsol.animation.Locatable;
20 import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
21 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
22 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23 import nl.tudelft.simulation.naming.context.ContextInterface;
24 import nl.tudelft.simulation.naming.context.util.ContextUtil;
25
26
27
28
29
30
31
32
33
34 public final class OTSNetworkAnimationUtils
35 {
36
37 private OTSNetworkAnimationUtils()
38 {
39
40 }
41
42
43
44
45
46
47
48
49
50
51 @SuppressWarnings("checkstyle:designforextension")
52 public static OTSNetwork clone(final OTSNetwork network, final String newId,
53 final SimulatorInterface.TimeDoubleUnit oldSimulator, final OTSSimulatorInterface newSimulator)
54 throws NetworkException
55 {
56 OTSNetwork newNetwork = OTSNetworkUtils.clone(network, newId, newSimulator);
57
58
59 for (Link oldLink : network.getLinkMap().values())
60 {
61 OTSLink newLink = (OTSLink) newNetwork.getLink(oldLink.getId());
62 cloneAnimation(oldLink, newLink, oldSimulator, newSimulator);
63 }
64
65
66 for (Node oldNode : network.getNodeMap().values())
67 {
68 OTSNode newNode = (OTSNode) newNetwork.getNode(oldNode.getId());
69 cloneAnimation(oldNode, newNode, oldSimulator, newSimulator);
70 }
71
72
73
74 return newNetwork;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @SuppressWarnings("checkstyle:designforextension")
90 public static <T extends Locatable> void cloneAnimation(final Locatable oldSource, final T newSource,
91 final SimulatorInterface.TimeDoubleUnit oldSimulator, final SimulatorInterface.TimeDoubleUnit newSimulator)
92 {
93 if (!(oldSimulator instanceof AnimatorInterface) || !(newSimulator instanceof AnimatorInterface))
94 {
95 return;
96 }
97
98 try
99 {
100 ContextInterface context =
101 ContextUtil.lookupOrCreateSubContext(oldSimulator.getReplication().getContext(), "animation/2D");
102 for (Object element : context.values())
103 {
104 @SuppressWarnings("unchecked")
105 Renderable2DInterface<T> animationObject = (Renderable2DInterface<T>) element;
106 T locatable = animationObject.getSource();
107 if (oldSource.equals(locatable) && animationObject instanceof ClonableRenderable2DInterface)
108 {
109 ((ClonableRenderable2DInterface<T>) animationObject).clone(newSource, newSimulator);
110 }
111 }
112 }
113 catch (NamingException | RemoteException exception)
114 {
115 System.err.println("Error when cloning animation objects for object " + oldSource);
116 }
117 }
118
119
120
121
122
123
124 @SuppressWarnings("checkstyle:designforextension")
125 public static void destroy(final OTSNetwork network, final SimulatorInterface.TimeDoubleUnit simulator)
126 {
127 Set<Renderable2DInterface<?>> animationObjects = new LinkedHashSet<>();
128 try
129 {
130 ContextInterface context =
131 ContextUtil.lookupOrCreateSubContext(simulator.getReplication().getContext(), "animation/2D");
132 for (Object element : context.values())
133 {
134 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
135 animationObjects.add(animationObject);
136 }
137
138 for (Renderable2DInterface<?> ao : animationObjects)
139 {
140 try
141 {
142 ao.destroy();
143 }
144 catch (Exception e)
145 {
146
147 }
148 }
149 }
150 catch (NamingException | RemoteException exception)
151 {
152 System.err.println("Error when destroying animation objects");
153 }
154
155
156 OTSNetworkUtils.destroy(network);
157 }
158
159
160
161
162
163
164 @SuppressWarnings("checkstyle:designforextension")
165 public static void removeAnimation(final Class<?> clazz, final SimulatorInterface.TimeDoubleUnit oldSimulator)
166 {
167 if (!(oldSimulator instanceof AnimatorInterface))
168 {
169 return;
170 }
171
172 try
173 {
174 ContextInterface context =
175 ContextUtil.lookupOrCreateSubContext(oldSimulator.getReplication().getContext(), "animation/2D");
176 for (Object element : context.values())
177 {
178 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
179 Locatable locatable = animationObject.getSource();
180 if (clazz.isAssignableFrom(locatable.getClass()))
181 {
182 animationObject.destroy();
183 }
184 }
185 }
186 catch (NamingException | RemoteException exception)
187 {
188 System.err.println("Error when destroying animation objects for class " + clazz.getSimpleName());
189 }
190 }
191
192 }