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