NetworkAnimationUtils.java

  1. package org.opentrafficsim.draw.network;

  2. import java.rmi.RemoteException;
  3. import java.util.LinkedHashSet;
  4. import java.util.Set;

  5. import javax.naming.NamingException;

  6. import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
  7. import org.opentrafficsim.core.network.Network;
  8. import org.opentrafficsim.core.network.NetworkUtils;

  9. import nl.tudelft.simulation.dsol.animation.Locatable;
  10. import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
  11. import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
  12. import nl.tudelft.simulation.naming.context.ContextInterface;
  13. import nl.tudelft.simulation.naming.context.util.ContextUtil;

  14. /**
  15.  * NetworkAnimationUtils can make a deep clone of a network, including animation, and can destroy the animation.
  16.  * <p>
  17.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  18.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  19.  * </p>
  20.  * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
  21.  */
  22. public final class NetworkAnimationUtils
  23. {
  24.     /** */
  25.     private NetworkAnimationUtils()
  26.     {
  27.         // utility class
  28.     }

  29.     /**
  30.      * Remove all objects and animation in the network.
  31.      * @param network Network; the network to destroy
  32.      * @param simulator OtsSimulatorInterface; the simulator of the old network
  33.      */
  34.     @SuppressWarnings("checkstyle:designforextension")
  35.     public static void destroy(final Network network, final OtsSimulatorInterface simulator)
  36.     {
  37.         Set<Renderable2DInterface<?>> animationObjects = new LinkedHashSet<>();
  38.         try
  39.         {
  40.             ContextInterface context =
  41.                     ContextUtil.lookupOrCreateSubContext(simulator.getReplication().getContext(), "animation/2D");
  42.             for (Object element : context.values())
  43.             {
  44.                 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
  45.                 animationObjects.add(animationObject);
  46.             }

  47.             for (Renderable2DInterface<?> ao : animationObjects)
  48.             {
  49.                 try
  50.                 {
  51.                     ao.destroy(simulator);
  52.                 }
  53.                 catch (Exception e)
  54.                 {
  55.                     //
  56.                 }
  57.             }
  58.         }
  59.         catch (NamingException | RemoteException exception)
  60.         {
  61.             System.err.println("Error when destroying animation objects");
  62.         }

  63.         // destroy the network, GTUs, Routes, etc.
  64.         NetworkUtils.destroy(network);
  65.     }

  66.     /**
  67.      * Remove all animation objects of the given class.
  68.      * @param clazz Class&lt;?&gt;; the class to remove the animation objects for
  69.      * @param oldSimulator OtsSimulatorInterface; the old simulator
  70.      */
  71.     @SuppressWarnings("checkstyle:designforextension")
  72.     public static void removeAnimation(final Class<?> clazz, final OtsSimulatorInterface oldSimulator)
  73.     {
  74.         if (!(oldSimulator instanceof AnimatorInterface))
  75.         {
  76.             return;
  77.         }

  78.         try
  79.         {
  80.             ContextInterface context =
  81.                     ContextUtil.lookupOrCreateSubContext(oldSimulator.getReplication().getContext(), "animation/2D");
  82.             for (Object element : context.values())
  83.             {
  84.                 Renderable2DInterface<?> animationObject = (Renderable2DInterface<?>) element;
  85.                 Locatable locatable = animationObject.getSource();
  86.                 if (clazz.isAssignableFrom(locatable.getClass()))
  87.                 {
  88.                     animationObject.destroy(oldSimulator);
  89.                 }
  90.             }
  91.         }
  92.         catch (NamingException | RemoteException exception)
  93.         {
  94.             System.err.println("Error when destroying animation objects for class " + clazz.getSimpleName());
  95.         }
  96.     }

  97. }