View Javadoc
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.Network;
11  import org.opentrafficsim.core.network.NetworkUtils;
12  
13  import nl.tudelft.simulation.dsol.animation.Locatable;
14  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
15  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
16  import nl.tudelft.simulation.naming.context.ContextInterface;
17  import nl.tudelft.simulation.naming.context.util.ContextUtil;
18  
19  /**
20   * NetworkAnimationUtils can make a deep clone of a network, including animation, and can destroy the animation.
21   * <p>
22   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
26   */
27  public final class NetworkAnimationUtils
28  {
29      /** */
30      private NetworkAnimationUtils()
31      {
32          // utility class
33      }
34  
35      /**
36       * Remove all objects and animation in the network.
37       * @param network Network; the network to destroy
38       * @param simulator OtsSimulatorInterface; the simulator of the old network
39       */
40      @SuppressWarnings("checkstyle:designforextension")
41      public static void destroy(final Network network, final OtsSimulatorInterface simulator)
42      {
43          Set<Renderable2DInterface<?>> animationObjects = new LinkedHashSet<>();
44          try
45          {
46              ContextInterface context =
47                      ContextUtil.lookupOrCreateSubContext(simulator.getReplication().getContext(), "animation/2D");
48              for (Object element : context.values())
49              {
50                  Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
51                  animationObjects.add(animationObject);
52              }
53  
54              for (Renderable2DInterface<?> ao : animationObjects)
55              {
56                  try
57                  {
58                      ao.destroy(simulator);
59                  }
60                  catch (Exception e)
61                  {
62                      //
63                  }
64              }
65          }
66          catch (NamingException | RemoteException exception)
67          {
68              System.err.println("Error when destroying animation objects");
69          }
70  
71          // destroy the network, GTUs, Routes, etc.
72          NetworkUtils.destroy(network);
73      }
74  
75      /**
76       * Remove all animation objects of the given class.
77       * @param clazz Class&lt;?&gt;; the class to remove the animation objects for
78       * @param oldSimulator OtsSimulatorInterface; the old simulator
79       */
80      @SuppressWarnings("checkstyle:designforextension")
81      public static void removeAnimation(final Class<?> clazz, final OtsSimulatorInterface oldSimulator)
82      {
83          if (!(oldSimulator instanceof AnimatorInterface))
84          {
85              return;
86          }
87  
88          try
89          {
90              ContextInterface context =
91                      ContextUtil.lookupOrCreateSubContext(oldSimulator.getReplication().getContext(), "animation/2D");
92              for (Object element : context.values())
93              {
94                  Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
95                  Locatable locatable = animationObject.getSource();
96                  if (clazz.isAssignableFrom(locatable.getClass()))
97                  {
98                      animationObject.destroy(oldSimulator);
99                  }
100             }
101         }
102         catch (NamingException | RemoteException exception)
103         {
104             System.err.println("Error when destroying animation objects for class " + clazz.getSimpleName());
105         }
106     }
107 
108 }