View Javadoc
1   package org.opentrafficsim.demo.network.xml;
2   
3   import java.awt.Dimension;
4   import java.io.IOException;
5   import java.io.Serializable;
6   import java.net.URISyntaxException;
7   import java.net.URL;
8   import java.nio.file.Files;
9   import java.nio.file.Paths;
10  import java.rmi.RemoteException;
11  
12  import javax.naming.NamingException;
13  import javax.swing.SwingUtilities;
14  import javax.xml.bind.JAXBException;
15  import javax.xml.parsers.ParserConfigurationException;
16  
17  import org.djunits.value.vdouble.scalar.Duration;
18  import org.djunits.value.vdouble.scalar.Time;
19  import org.djutils.io.URLResource;
20  import org.opentrafficsim.core.dsol.AbstractOTSModel;
21  import org.opentrafficsim.core.dsol.OTSAnimator;
22  import org.opentrafficsim.core.dsol.OTSModelInterface;
23  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
24  import org.opentrafficsim.core.geometry.OTSGeometryException;
25  import org.opentrafficsim.core.gis.CoordinateTransformWGS84toRDNew;
26  import org.opentrafficsim.core.gtu.GTUException;
27  import org.opentrafficsim.core.network.NetworkException;
28  import org.opentrafficsim.draw.core.OTSDrawingException;
29  import org.opentrafficsim.road.network.OTSRoadNetwork;
30  import org.opentrafficsim.road.network.factory.OTSRoadNetworkUtils;
31  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
32  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
33  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
34  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
35  import org.xml.sax.SAXException;
36  
37  import nl.javel.gisbeans.io.esri.CoordinateTransform;
38  import nl.tudelft.simulation.dsol.SimRuntimeException;
39  import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
40  import nl.tudelft.simulation.language.DSOLException;
41  
42  /**
43   * <p>
44   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
45   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
46   * <p>
47   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
48   * initial version Oct 17, 2014 <br>
49   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
50   */
51  public class TestXMLParserWriteXstream extends OTSSimulationApplication<OTSModelInterface>
52  {
53      /** */
54      private static final long serialVersionUID = 1L;
55  
56      /**
57       * @param model the model
58       * @param animationPanel the animation panel
59       * @throws OTSDrawingException on drawing error
60       */
61      public TestXMLParserWriteXstream(final OTSModelInterface model, final OTSAnimationPanel animationPanel)
62              throws OTSDrawingException
63      {
64          super(model, animationPanel);
65      }
66  
67      /**
68       * Main program.
69       * @param args String[]; the command line arguments (not used)
70       * @throws SimRuntimeException should never happen
71       */
72      public static void main(final String[] args) throws SimRuntimeException
73      {
74          SwingUtilities.invokeLater(new Runnable()
75          {
76              @Override
77              public void run()
78              {
79                  try
80                  {
81                      OTSAnimator simulator = new OTSAnimator("TestXMLParserWriteXstream");
82                      TestXMLModelWriteXStream xmlModel = new TestXMLModelWriteXStream(simulator);
83                      simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel);
84                      OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(),
85                              new Dimension(800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
86                      new TestXMLParserWriteXstream(xmlModel, animationPanel);
87                  }
88                  catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
89                  {
90                      exception.printStackTrace();
91                  }
92              }
93          });
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final String toString()
99      {
100         return "TestXMLParser []";
101     }
102 
103     /**
104      * Model to test the XML parser.
105      * <p>
106      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
107      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
108      * License</a>.
109      * <p>
110      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
111      * initial version un 27, 2015 <br>
112      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
113      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
114      */
115     static class TestXMLModelWriteXStream extends AbstractOTSModel
116     {
117         /** */
118         private static final long serialVersionUID = 20141121L;
119 
120         /** the network. */
121         private OTSRoadNetwork network;
122 
123         /**
124          * @param simulator the simulator
125          */
126         TestXMLModelWriteXStream(final OTSSimulatorInterface simulator)
127         {
128             super(simulator);
129         }
130 
131         /** {@inheritDoc} */
132         @Override
133         public final void constructModel() throws SimRuntimeException
134         {
135             long millis = System.currentTimeMillis();
136             URL xmlURL = URLResource.getResource("/xml/N201.xml");
137             this.network = new OTSRoadNetwork("Example network", true);
138             try
139             {
140                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), false);
141 
142                 System.out.println("parsing took : " + (System.currentTimeMillis() - millis) + " ms");
143 
144                 millis = System.currentTimeMillis();
145                 String xml = OTSRoadNetworkUtils.toXml(this.network);
146                 System.out.println("making XML took : " + (System.currentTimeMillis() - millis) + " ms");
147                 millis = System.currentTimeMillis();
148                 Files.write(Paths.get("e://temp/network.txt"), xml.getBytes());
149                 System.out.println("writing took : " + (System.currentTimeMillis() - millis) + " ms");
150             }
151             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
152                     | URISyntaxException | XmlParserException | IOException | GTUException exception)
153             {
154                 exception.printStackTrace();
155             }
156 
157             URL gisURL = URLResource.getResource("/xml/N201/map.xml");
158             System.err.println("GIS-map file: " + gisURL.toString());
159             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
160             new GisRenderable2D(this.simulator, gisURL, rdto0);
161         }
162 
163         /** {@inheritDoc} */
164         @Override
165         public OTSRoadNetwork getNetwork()
166         {
167             return this.network;
168         }
169 
170         /** {@inheritDoc} */
171         @Override
172         public final String toString()
173         {
174             return "TestXMLModel [simulator=" + this.simulator + "]";
175         }
176 
177         /** {@inheritDoc} */
178         @Override
179         public Serializable getSourceId()
180         {
181             return "TestXMLModelWriteXStream";
182         }
183 
184     }
185 
186 }