View Javadoc
1   package org.opentrafficsim.road.test;
2   
3   import java.awt.geom.Rectangle2D;
4   import java.awt.geom.Rectangle2D.Double;
5   import java.io.IOException;
6   import java.net.URL;
7   import java.util.ArrayList;
8   
9   import javax.naming.NamingException;
10  import javax.swing.JPanel;
11  import javax.swing.SwingUtilities;
12  import javax.xml.parsers.ParserConfigurationException;
13  
14  import nl.tudelft.simulation.dsol.SimRuntimeException;
15  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
16  import nl.tudelft.simulation.language.io.URLResource;
17  
18  import org.djunits.unit.TimeUnit;
19  import org.djunits.value.vdouble.scalar.DoubleScalar;
20  import org.djunits.value.vdouble.scalar.Duration;
21  import org.djunits.value.vdouble.scalar.Time;
22  import org.opentrafficsim.core.Throw;
23  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
24  import org.opentrafficsim.core.dsol.OTSModelInterface;
25  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
26  import org.opentrafficsim.core.geometry.OTSGeometryException;
27  import org.opentrafficsim.core.gtu.GTUException;
28  import org.opentrafficsim.core.gtu.animation.GTUColorer;
29  import org.opentrafficsim.core.network.NetworkException;
30  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
31  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
32  import org.opentrafficsim.simulationengine.OTSSimulationException;
33  import org.opentrafficsim.simulationengine.properties.AbstractProperty;
34  import org.opentrafficsim.simulationengine.properties.PropertyException;
35  import org.xml.sax.SAXException;
36  
37  /**
38   * <p>
39   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
40   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
41   * <p>
42   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
43   * initial version Oct 17, 2014 <br>
44   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
45   */
46  public class TestNetwork2 extends AbstractWrappableAnimation
47  {
48      /**
49       * Main program.
50       * @param args String[]; the command line arguments (not used)
51       * @throws SimRuntimeException should never happen
52       */
53      public static void main(final String[] args) throws SimRuntimeException
54      {
55          SwingUtilities.invokeLater(new Runnable()
56          {
57              @Override
58              public void run()
59              {
60                  try
61                  {
62                      TestNetwork2 xmlModel = new TestNetwork2();
63                      // 1 hour simulation run for testing
64                      xmlModel.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
65                          new Duration(60.0, TimeUnit.MINUTE), new ArrayList<AbstractProperty<?>>(), null, true);
66                  }
67                  catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
68                  {
69                      exception.printStackTrace();
70                  }
71              }
72          });
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final String shortName()
78      {
79          return "TestXMLModel";
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final String description()
85      {
86          return "TestXMLModel";
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final void stopTimersThreads()
92      {
93          super.stopTimersThreads();
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      protected final JPanel makeCharts()
99      {
100         return null;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     protected final OTSModelInterface makeModel(final GTUColorer colorer)
106     {
107         return new TestXMLModel();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     protected final Double makeAnimationRectangle()
113     {
114         return new Rectangle2D.Double(-1000, -1000, 2000, 2000);
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public String toString()
120     {
121         return "TestNetwork2 []";
122     }
123 
124     /**
125      * Model to test the XML parser.
126      * <p>
127      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
128      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
129      * License</a>.
130      * <p>
131      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
132      * initial version un 27, 2015 <br>
133      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
134      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
135      */
136     class TestXMLModel implements OTSModelInterface
137     {
138         /** */
139         private static final long serialVersionUID = 20141121L;
140 
141         /** The simulator. */
142         private OTSDEVSSimulatorInterface simulator;
143 
144         /** {@inheritDoc} */
145         @Override
146         public final
147             void
148             constructModel(
149                 final SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> pSimulator)
150                 throws SimRuntimeException
151         {
152             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
153             URL url = URLResource.getResource("/Testnetwork2.x.xml");
154             XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
155             try
156             {
157                 nlp.build(url);
158             }
159             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
160                 | GTUException | OTSGeometryException exception)
161             {
162                 exception.printStackTrace();
163             }
164         }
165 
166         /** {@inheritDoc} */
167         @Override
168         public SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble>
169             getSimulator()
170 
171         {
172             return this.simulator;
173         }
174 
175         /** {@inheritDoc} */
176         @Override
177         public final String toString()
178         {
179             return "TestXMLModel [simulator=" + this.simulator + "]";
180         }
181 
182     }
183 
184 }