View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.awt.geom.Rectangle2D;
4   import java.io.File;
5   import java.io.IOException;
6   import java.net.MalformedURLException;
7   import java.net.URL;
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 org.djunits.unit.TimeUnit;
16  import org.djunits.value.vdouble.scalar.Duration;
17  import org.djunits.value.vdouble.scalar.Time;
18  import org.opengis.feature.Property;
19  import org.opentrafficsim.base.modelproperties.PropertyException;
20  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
21  import org.opentrafficsim.core.dsol.OTSModelInterface;
22  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
23  import org.opentrafficsim.core.geometry.OTSGeometryException;
24  import org.opentrafficsim.core.gtu.GTUException;
25  import org.opentrafficsim.core.gtu.animation.GTUColorer;
26  import org.opentrafficsim.core.network.NetworkException;
27  import org.opentrafficsim.core.network.OTSNetwork;
28  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
29  import org.opentrafficsim.simulationengine.OTSSimulationException;
30  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
31  import org.xml.sax.SAXException;
32  
33  import nl.tudelft.simulation.dsol.SimRuntimeException;
34  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
35  
36  public class TestVissimParser extends AbstractWrappableAnimation {
37  
38      /**
39       * Main program.
40       * @param args String[]; the command line arguments (not used)
41       * @throws SimRuntimeException should never happen
42       */
43      public static void main(final String[] args) throws SimRuntimeException {
44          SwingUtilities.invokeLater(new Runnable() {
45              @Override
46              public void run() {
47                  try {
48                      TestVissimParser xmlModel = new TestVissimParser();
49                      // 1 hour simulation run for testing
50                      xmlModel.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND), new Duration(
51                          60.0, TimeUnit.MINUTE), new ArrayList<org.opentrafficsim.base.modelproperties.Property<?>>(), null,
52                          true);
53                  } catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception) {
54                      exception.printStackTrace();
55                  }
56              }
57          });
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public final String shortName() {
63          return "TestXMLModel";
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final String description() {
69          return "TestXMLModel";
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final void stopTimersThreads() {
75          super.stopTimersThreads();
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      protected final JPanel makeCharts(final SimpleSimulatorInterface simulator) {
81          return null;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      protected final OTSModelInterface makeModel(final GTUColorer colorer) {
87          return new VissimImport();
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle() {
93          // return new Rectangle2D.Double(-1000, -1000, 2000, 2000);
94          return new Rectangle2D.Double(162000, 384500, 2000, 2000);
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public final String toString() {
100         return "TestVissimParser []";
101     }
102 
103     /**
104      * Model to test the Vissim File Format parser.
105      * <p>
106      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
107      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
108      * License</a>.
109      * <p>
110      * $LastChangedDate: 2016-11-11 18:03:54 +0100 (Fri, 11 Nov 2016) $, @version $Revision: 2547 $, by $Author: gtamminga $,
111      * initial version un 27, 2015 <br>
112      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
113      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
114      */
115     /**
116      * @author P070518
117      */
118     class VissimImport implements OTSModelInterface {
119         /** */
120         private static final long serialVersionUID = 20141121L;
121 
122         /** The simulator. */
123         private OTSDEVSSimulatorInterface simulator;
124 
125         /** The network. */
126         private OTSNetwork network = new OTSNetwork("test Vissim network");
127 
128         /** {@inheritDoc} */
129         @Override
130         public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
131             throws SimRuntimeException {
132 
133             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
134 
135             ClassLoader classLoader = getClass().getClassLoader();
136 
137             File inputFile = new File(classLoader.getResource("ehv_eisen1_VA.inpx").getFile());
138             URL inputUrl = null;
139             try {
140                 inputUrl = new URL(classLoader.getResource("ehv_eisen1_VA.inpx").toString());
141             } catch (MalformedURLException e1) {
142                 // TODO Auto-generated catch block
143                 e1.printStackTrace();
144             }
145             File outputFile = new File(classLoader.getResource("testEindhoven1.xml").getFile());
146 
147             // URL inputUrl = URLResource.getResource(
148             // "C:/Projecten/OTS/ots-parser-vissim/src/main/resources/ehv_eisen1_VA.inpx");
149 
150             // URL url = URLResource.getResource("C:/Projecten/OTS/ots-parser-vissim/src/main/resources/Test-netwerk-12.inpx");
151             // URL url =
152             // URLResource.getResource("C:/Projecten/OTS/ots-parser-vissim/src/main/resources/OTS-Tester-Vissimnetwerk.inpx");
153             VissimNetworkLaneParser nlp = new VissimNetworkLaneParser(this.simulator);
154 
155             try {
156                 this.network = nlp.build(inputUrl, outputFile, network);
157             } catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
158                 | GTUException | OTSGeometryException exception) {
159                 exception.printStackTrace();
160             }
161 
162         }
163 
164         /**
165          * @param property
166          * @return a double
167          */
168         private Double parseDouble(Property property) {
169             if (property.getValue() != null) {
170                 if (property.getValue().toString() != null) {
171                     return Double.parseDouble(property.getValue().toString());
172                 }
173             }
174             return Double.NaN;
175         }
176 
177         /** {@inheritDoc} */
178         @Override
179         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
180 
181         {
182             return this.simulator;
183         }
184 
185         /** {@inheritDoc} */
186         @Override
187         public final String toString() {
188             return "TestVissimParser [simulator=" + this.simulator + "]";
189         }
190 
191     }
192 
193 }