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
40
41
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
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
61 @Override
62 public final String shortName() {
63 return "TestXMLModel";
64 }
65
66
67 @Override
68 public final String description() {
69 return "TestXMLModel";
70 }
71
72
73 @Override
74 public final void stopTimersThreads() {
75 super.stopTimersThreads();
76 }
77
78
79 @Override
80 protected final JPanel makeCharts(final SimpleSimulatorInterface simulator) {
81 return null;
82 }
83
84
85 @Override
86 protected final OTSModelInterface makeModel(final GTUColorer colorer) {
87 return new VissimImport();
88 }
89
90
91 @Override
92 protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle() {
93
94 return new Rectangle2D.Double(162000, 384500, 2000, 2000);
95 }
96
97
98 @Override
99 public final String toString() {
100 return "TestVissimParser []";
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 class VissimImport implements OTSModelInterface {
119
120 private static final long serialVersionUID = 20141121L;
121
122
123 private OTSDEVSSimulatorInterface simulator;
124
125
126 private OTSNetwork network = new OTSNetwork("test Vissim network");
127
128
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
143 e1.printStackTrace();
144 }
145 File outputFile = new File(classLoader.getResource("testEindhoven1.xml").getFile());
146
147
148
149
150
151
152
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
166
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
178 @Override
179 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
180
181 {
182 return this.simulator;
183 }
184
185
186 @Override
187 public final String toString() {
188 return "TestVissimParser [simulator=" + this.simulator + "]";
189 }
190
191 }
192
193 }