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