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