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