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