RoadNetworkUtils.java

  1. package org.opentrafficsim.road.network.factory;

  2. import java.io.OutputStream;
  3. import java.io.Writer;

  4. import org.djutils.event.EventProducer;
  5. import org.opentrafficsim.road.network.RoadNetwork;
  6. import org.opentrafficsim.road.network.lane.object.detector.Detector;

  7. import com.thoughtworks.xstream.XStream;

  8. import nl.tudelft.simulation.naming.context.JvmContext;

  9. /**
  10.  * <p>
  11.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  13.  * </p>
  14.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  16.  */
  17. public final class RoadNetworkUtils
  18. {

  19.     /** Do not instantiate. */
  20.     private RoadNetworkUtils()
  21.     {
  22.         // do not instantiate.
  23.     }

  24.     /**
  25.      * Make a copy of the network, without GTUs and listeners.
  26.      * @param network RoadNetwork; the network to copy
  27.      * @return a copy of the network
  28.      */
  29.     public static RoadNetwork copy(final RoadNetwork network)
  30.     {
  31.         XStream xstream = new XStream();
  32.         return (RoadNetwork) xstream.fromXML(toXml(network));
  33.     }

  34.     /**
  35.      * Create an xml-version of the network.
  36.      * @param network RoadNetwork; the network to create an xml-version from
  37.      * @return an xml-string with the network
  38.      */
  39.     public static String toXml(final RoadNetwork network)
  40.     {
  41.         XStream xstream = new XStream();
  42.         xstream.omitField(RoadNetwork.class, "gtuMap"); // no GTUs
  43.         xstream.omitField(EventProducer.class, "listeners"); // no listeners
  44.         xstream.omitField(JvmContext.class, "atomicName"); // no JvmContext
  45.         xstream.omitField(JvmContext.class, "elements"); // no JvmContext
  46.         xstream.omitField(Detector.class, "simulator"); // no reference to a simulator
  47.         return xstream.toXML(network);
  48.     }

  49.     /**
  50.      * Create an xml-version of the network.
  51.      * @param network RoadNetwork; the network to create an xml-version from
  52.      * @param out OutputStream; the stream to write the xml-string with the network to
  53.      */
  54.     public static void toXml(final RoadNetwork network, final OutputStream out)
  55.     {
  56.         XStream xstream = new XStream();
  57.         xstream.toXML(network, out);
  58.     }

  59.     /**
  60.      * Create an xml-version of the network.
  61.      * @param network RoadNetwork; the network to create an xml-version from
  62.      * @param writer Writer; the writer to write the xml-string with the network to
  63.      */
  64.     public static void toXml(final RoadNetwork network, final Writer writer)
  65.     {
  66.         XStream xstream = new XStream();
  67.         xstream.toXML(network, writer);
  68.     }
  69. }