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