1 package org.opentrafficsim.road.network.factory.vissim;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.djunits.unit.LengthUnit;
8 import org.djunits.unit.SpeedUnit;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.opentrafficsim.core.distributions.Generator;
12 import org.opentrafficsim.core.gtu.GTUType;
13 import org.opentrafficsim.core.network.NetworkException;
14 import org.opentrafficsim.core.network.factory.xml.units.Distributions;
15 import org.opentrafficsim.core.network.route.CompleteRoute;
16 import org.opentrafficsim.core.network.route.FixedRouteGenerator;
17 import org.opentrafficsim.core.network.route.Route;
18 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
19 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
20 import org.opentrafficsim.road.network.factory.vissim.CrossSectionElementTag.ElementType;
21 import org.opentrafficsim.road.network.lane.Lane;
22 import org.w3c.dom.NamedNodeMap;
23 import org.w3c.dom.Node;
24 import org.xml.sax.SAXException;
25
26 import nl.tudelft.simulation.dsol.SimRuntimeException;
27 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
28
29
30
31
32
33
34
35
36
37
38 class FillTag implements Serializable
39 {
40
41 private static final long serialVersionUID = 20150723L;
42
43
44 @SuppressWarnings("checkstyle:visibilitymodifier")
45 String laneName = null;
46
47
48 @SuppressWarnings("checkstyle:visibilitymodifier")
49 GTUTag gtuTag = null;
50
51
52 @SuppressWarnings("checkstyle:visibilitymodifier")
53 GTUMixTag gtuMixTag = null;
54
55
56 @SuppressWarnings("checkstyle:visibilitymodifier")
57 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> distanceDist = null;
58
59
60 @SuppressWarnings("checkstyle:visibilitymodifier")
61 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
62
63
64 @SuppressWarnings("checkstyle:visibilitymodifier")
65 int maxGTUs = Integer.MAX_VALUE;
66
67
68 @SuppressWarnings("checkstyle:visibilitymodifier")
69 RouteTag routeTag = null;
70
71
72 @SuppressWarnings("checkstyle:visibilitymodifier")
73 RouteMixTag routeMixTag = null;
74
75
76 @SuppressWarnings("checkstyle:visibilitymodifier")
77 ShortestRouteTag shortestRouteTag = null;
78
79
80 @SuppressWarnings("checkstyle:visibilitymodifier")
81 ShortestRouteMixTag shortestRouteMixTag = null;
82
83
84
85
86
87
88
89
90
91 @SuppressWarnings("checkstyle:needbraces")
92 static void parseFill(final Node node, final VissimNetworkLaneParser parser, final LinkTag linkTag)
93 throws SAXException, NetworkException
94 {
95 NamedNodeMap attributes = node.getAttributes();
96 FillTag fillTag = new FillTag();
97
98 if (attributes.getNamedItem("LANE") == null)
99 {
100 throw new SAXException("FILL: missing attribute LANE" + " for link " + linkTag.name);
101 }
102 String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
103 if (linkTag.roadLayoutTag == null)
104 {
105 throw new NetworkException("FILL: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
106 }
107 CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
108 if (cseTag == null)
109 {
110 throw new NetworkException("FILL: LANE " + laneName + " not found in elements of link " + linkTag.name
111 + " - roadtype " + linkTag.roadLayoutTag.name);
112 }
113 if (cseTag.elementType != ElementType.LANE)
114 {
115 throw new NetworkException("FILL: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
116 + " - roadtype " + linkTag.roadLayoutTag.name);
117 }
118 if (linkTag.generatorTags.containsKey(laneName))
119 {
120 throw new SAXException("FILL for LANE with NAME " + laneName + " defined twice");
121 }
122 fillTag.laneName = laneName;
123
124 if (attributes.getNamedItem("GTU") != null)
125 {
126 String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
127 if (!parser.getGtuTags().containsKey(gtuName))
128 {
129 throw new NetworkException(
130 "FILL: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
131 }
132 fillTag.gtuTag = parser.getGtuTags().get(gtuName);
133 }
134
135 if (attributes.getNamedItem("GTUMIX") != null)
136 {
137 String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
138 if (!parser.getGtuMixTags().containsKey(gtuMixName))
139 {
140 throw new NetworkException(
141 "FILL: LANE " + laneName + " GTUMIX " + gtuMixName + " in link " + linkTag.name + " not defined");
142 }
143 fillTag.gtuMixTag = parser.getGtuMixTags().get(gtuMixName);
144 }
145
146 if (fillTag.gtuTag == null && fillTag.gtuMixTag == null)
147 {
148 throw new SAXException(
149 "FILL: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link " + linkTag.name);
150 }
151
152 if (fillTag.gtuTag != null && fillTag.gtuMixTag != null)
153 {
154 throw new SAXException(
155 "FILL: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName + " of link " + linkTag.name);
156 }
157
158 Node distance = attributes.getNamedItem("DISTANCE");
159 if (distance == null)
160 {
161 throw new SAXException("FILL: missing attribute DISTANCE");
162 }
163 fillTag.distanceDist = Distributions.parseLengthDist(distance.getNodeValue());
164
165 Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
166 if (initialSpeed == null)
167 {
168 throw new SAXException("FILL: missing attribute INITIALSPEED");
169 }
170 fillTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
171
172 Node maxGTU = attributes.getNamedItem("MAXGTU");
173 fillTag.maxGTUs = maxGTU == null ? Integer.MAX_VALUE : Integer.parseInt(maxGTU.getNodeValue().trim());
174
175 int numberRouteTags = 0;
176
177 if (attributes.getNamedItem("ROUTE") != null)
178 {
179 String routeName = attributes.getNamedItem("ROUTE").getNodeValue().trim();
180 if (!parser.getRouteTags().containsKey(routeName))
181 {
182 throw new NetworkException(
183 "FILL: LANE " + laneName + " ROUTE " + routeName + " in link " + linkTag.name + " not defined");
184 }
185 fillTag.routeTag = parser.getRouteTags().get(routeName);
186 numberRouteTags++;
187 }
188
189 if (attributes.getNamedItem("ROUTEMIX") != null)
190 {
191 String routeMixName = attributes.getNamedItem("ROUTEMIX").getNodeValue().trim();
192 if (!parser.getRouteMixTags().containsKey(routeMixName))
193 {
194 throw new NetworkException(
195 "FILL: LANE " + laneName + " ROUTEMIX " + routeMixName + " in link " + linkTag.name + " not defined");
196 }
197 fillTag.routeMixTag = parser.getRouteMixTags().get(routeMixName);
198 numberRouteTags++;
199 }
200
201 if (attributes.getNamedItem("SHORTESTROUTE") != null)
202 {
203 String shortestRouteName = attributes.getNamedItem("SHORTESTROUTE").getNodeValue().trim();
204 if (!parser.getShortestRouteTags().containsKey(shortestRouteName))
205 {
206 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTE " + shortestRouteName + " in link "
207 + linkTag.name + " not defined");
208 }
209 fillTag.shortestRouteTag = parser.getShortestRouteTags().get(shortestRouteName);
210 numberRouteTags++;
211 }
212
213 if (attributes.getNamedItem("SHORTESTROUTEMIX") != null)
214 {
215 String shortestRouteMixName = attributes.getNamedItem("SHORTESTROUTEMIX").getNodeValue().trim();
216 if (!parser.getShortestRouteMixTags().containsKey(shortestRouteMixName))
217 {
218 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTEMIX " + shortestRouteMixName + " in link "
219 + linkTag.name + " not defined");
220 }
221 fillTag.shortestRouteMixTag = parser.getShortestRouteMixTags().get(shortestRouteMixName);
222 numberRouteTags++;
223 }
224
225 if (numberRouteTags > 1)
226 {
227 throw new SAXException(
228 "FILL: multiple ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
229 }
230
231
232
233 linkTag.fillTags.put(laneName, fillTag);
234 }
235
236
237
238
239
240
241
242
243
244
245 static void makeFill(final FillTag fillTag, final VissimNetworkLaneParser parser, final LinkTag linkTag,
246 final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws SimRuntimeException, NetworkException
247 {
248 Lane lane = linkTag.lanes.get(fillTag.laneName);
249 Class<?> gtuClass = LaneBasedIndividualGTU.class;
250 List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
251 for (NodeTag nodeTag : fillTag.routeTag.routeNodeTags)
252 {
253 nodeList.add(parser.getNodeTags().get(nodeTag.name).node);
254 }
255 Generator<Route> rg = new FixedRouteGenerator(
256 new CompleteRoute("fixed route", parser.network.getGtuType(GTUType.DEFAULTS.VEHICLE), nodeList));
257
258
259
260
261
262
263
264 }
265
266
267 @Override
268 public final String toString()
269 {
270 return "FillTag [laneName=" + this.laneName + ", gtuTag=" + this.gtuTag + ", gtuMixTag=" + this.gtuMixTag
271 + ", distanceDist=" + this.distanceDist + ", initialSpeedDist=" + this.initialSpeedDist + ", maxGTUs="
272 + this.maxGTUs + ", routeTag=" + this.routeTag + ", routeMixTag=" + this.routeMixTag + ", shortestRouteTag="
273 + this.shortestRouteTag + ", shortestRouteMixTag=" + this.shortestRouteMixTag + "]";
274 }
275
276 }