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