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