View Javadoc
1   package org.opentrafficsim.demo.network.xml;
2   
3   import java.awt.Dimension;
4   import java.io.Serializable;
5   import java.net.URISyntaxException;
6   import java.net.URL;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  import javax.swing.SwingUtilities;
11  import javax.xml.bind.JAXBException;
12  import javax.xml.parsers.ParserConfigurationException;
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.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.geometry.OTSGeometryException;
22  import org.opentrafficsim.core.gtu.GTUException;
23  import org.opentrafficsim.core.network.NetworkException;
24  import org.opentrafficsim.draw.core.OTSDrawingException;
25  import org.opentrafficsim.road.network.OTSRoadNetwork;
26  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
27  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
28  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
29  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
30  import org.xml.sax.SAXException;
31  
32  import nl.tudelft.simulation.dsol.SimRuntimeException;
33  import nl.tudelft.simulation.language.DSOLException;
34  
35  /**
36   * Four stop demo
37   * <p>
38   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
40   * <p>
41   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Dec 1, 2015 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
44   * @author Wouter Schakel
45   */
46  public class FourStop extends OTSSimulationApplication<OTSModelInterface>
47  {
48      /** */
49      private static final long serialVersionUID = 1L;
50  
51      /**
52       * @param model the model
53       * @param animationPanel the animation panel
54       * @throws OTSDrawingException on drawing error
55       */
56      public FourStop(final OTSModelInterface model, final OTSAnimationPanel animationPanel) throws OTSDrawingException
57      {
58          super(model, animationPanel);
59      }
60  
61      /**
62       * Main program.
63       * @param args String[]; the command line arguments (not used)
64       * @throws SimRuntimeException should never happen
65       */
66      public static void main(final String[] args) throws SimRuntimeException
67      {
68          SwingUtilities.invokeLater(new Runnable()
69          {
70              @Override
71              public void run()
72              {
73                  try
74                  {
75                      OTSAnimator simulator = new OTSAnimator("FourStop");
76                      TestXMLModel xmlModel = new TestXMLModel(simulator);
77                      simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel);
78                      OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(),
79                              new Dimension(800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
80                      new FourStop(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 String toString()
93      {
94          return "FourStop []";
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 TestXMLModel extends AbstractOTSModel
110     {
111         /** */
112         private static final long serialVersionUID = 20141121L;
113 
114         /** the network. */
115         private OTSRoadNetwork network = null;
116 
117         /**
118          * @param simulator the simulator
119          */
120         TestXMLModel(final OTSSimulatorInterface simulator)
121         {
122             super(simulator);
123         }
124 
125         /** {@inheritDoc} */
126         @Override
127         public final void constructModel() throws SimRuntimeException
128         {
129             try
130             {
131                 URL xmlURL = URLResource.getResource("/xml/4-stop-3-1.xml");
132                 this.network = new OTSRoadNetwork("4-stop-3-1", true);
133                 XmlNetworkLaneParser.build(xmlURL, this.network, getSimulator(), true);
134             }
135             catch (NetworkException | ParserConfigurationException | SAXException | GTUException | OTSGeometryException
136                     | JAXBException | URISyntaxException | XmlParserException exception)
137             {
138                 exception.printStackTrace();
139             }
140         }
141 
142         /** {@inheritDoc} */
143         @Override
144         public OTSRoadNetwork getNetwork()
145         {
146             return this.network;
147         }
148 
149         /** {@inheritDoc} */
150         @Override
151         public final String toString()
152         {
153             return "TestXMLModel [simulator=" + this.simulator + "]";
154         }
155 
156         /** {@inheritDoc} */
157         @Override
158         public Serializable getSourceId()
159         {
160             return "TestXMLModel";
161         }
162 
163     }
164 
165 }