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                      animationPanel.enableSimulationControlButtons();
89                  }
90                  catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
91                  {
92                      exception.printStackTrace();
93                  }
94              }
95          });
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public final String toString()
101     {
102         return "TestXMLParser []";
103     }
104 
105     /**
106      * Model to test the XML parser.
107      * <p>
108      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
109      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
110      * License</a>.
111      * <p>
112      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
113      * initial version un 27, 2015 <br>
114      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
115      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
116      */
117     static class TestXMLModelWriteXStream extends AbstractOTSModel
118     {
119         /** */
120         private static final long serialVersionUID = 20141121L;
121 
122         /** the network. */
123         private OTSRoadNetwork network;
124 
125         /**
126          * @param simulator the simulator
127          */
128         TestXMLModelWriteXStream(final OTSSimulatorInterface simulator)
129         {
130             super(simulator);
131         }
132 
133         /** {@inheritDoc} */
134         @Override
135         public final void constructModel() throws SimRuntimeException
136         {
137             long millis = System.currentTimeMillis();
138             URL xmlURL = URLResource.getResource("/xml/N201.xml");
139             this.network = new OTSRoadNetwork("Example network", true, getSimulator());
140             try
141             {
142                 XmlNetworkLaneParser.build(xmlURL, this.network, false);
143 
144                 System.out.println("parsing took : " + (System.currentTimeMillis() - millis) + " ms");
145 
146                 millis = System.currentTimeMillis();
147                 String xml = OTSRoadNetworkUtils.toXml(this.network);
148                 System.out.println("making XML took : " + (System.currentTimeMillis() - millis) + " ms");
149                 millis = System.currentTimeMillis();
150                 Files.write(Paths.get("e://temp/network.txt"), xml.getBytes());
151                 System.out.println("writing took : " + (System.currentTimeMillis() - millis) + " ms");
152             }
153             catch (NetworkException | ParserConfigurationException | SAXException | OTSGeometryException | JAXBException
154                     | URISyntaxException | XmlParserException | IOException | GTUException | TrafficControlException exception)
155             {
156                 exception.printStackTrace();
157             }
158 
159             URL gisURL = URLResource.getResource("/xml/N201/map.xml");
160             System.err.println("GIS-map file: " + gisURL.toString());
161             CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
162             new GisRenderable2D(this.simulator, gisURL, rdto0);
163         }
164 
165         /** {@inheritDoc} */
166         @Override
167         public OTSRoadNetwork getNetwork()
168         {
169             return this.network;
170         }
171 
172         /** {@inheritDoc} */
173         @Override
174         public final String toString()
175         {
176             return "TestXMLModel [simulator=" + this.simulator + "]";
177         }
178 
179         /** {@inheritDoc} */
180         @Override
181         public Serializable getSourceId()
182         {
183             return "TestXMLModelWriteXStream";
184         }
185 
186     }
187 
188 }