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