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