1 package org.opentrafficsim.road.network.factory.xml;
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.dsol.OTSDEVSSimulatorInterface;
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.RouteGenerator;
18 import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
19 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
20 import org.opentrafficsim.road.network.factory.xml.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
28
29
30
31
32
33
34
35
36
37 class FillTag implements Serializable
38 {
39
40 private static final long serialVersionUID = 20150723L;
41
42
43 @SuppressWarnings("checkstyle:visibilitymodifier")
44 String laneName = null;
45
46
47 @SuppressWarnings("checkstyle:visibilitymodifier")
48 GTUTag gtuTag = null;
49
50
51 @SuppressWarnings("checkstyle:visibilitymodifier")
52 GTUMixTag gtuMixTag = null;
53
54
55 @SuppressWarnings("checkstyle:visibilitymodifier")
56 ContinuousDistDoubleScalar.Rel<Length, LengthUnit> distanceDist = null;
57
58
59 @SuppressWarnings("checkstyle:visibilitymodifier")
60 ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
61
62
63 @SuppressWarnings("checkstyle:visibilitymodifier")
64 int maxGTUs = Integer.MAX_VALUE;
65
66
67 @SuppressWarnings("checkstyle:visibilitymodifier")
68 RouteTag routeTag = null;
69
70
71 @SuppressWarnings("checkstyle:visibilitymodifier")
72 RouteMixTag routeMixTag = null;
73
74
75 @SuppressWarnings("checkstyle:visibilitymodifier")
76 ShortestRouteTag shortestRouteTag = null;
77
78
79 @SuppressWarnings("checkstyle:visibilitymodifier")
80 ShortestRouteMixTag shortestRouteMixTag = null;
81
82
83
84
85
86
87
88
89
90 @SuppressWarnings("checkstyle:needbraces")
91 static void parseFill(final Node node, final XmlNetworkLaneParser parser, final LinkTag linkTag)
92 throws SAXException, NetworkException
93 {
94 NamedNodeMap attributes = node.getAttributes();
95 FillTag fillTag = new FillTag();
96
97 if (attributes.getNamedItem("LANE") == null)
98 throw new SAXException("FILL: missing attribute LANE" + " for link " + linkTag.name);
99 String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
100 if (linkTag.roadLayoutTag == null)
101 throw new NetworkException("FILL: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
102 CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
103 if (cseTag == null)
104 throw new NetworkException("FILL: LANE " + laneName + " not found in elements of link " + linkTag.name
105 + " - roadtype " + linkTag.roadLayoutTag.name);
106 if (cseTag.elementType != ElementType.LANE)
107 throw new NetworkException("FILL: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
108 + " - roadtype " + linkTag.roadLayoutTag.name);
109 if (linkTag.generatorTags.containsKey(laneName))
110 throw new SAXException("FILL for LANE with NAME " + laneName + " defined twice");
111 fillTag.laneName = laneName;
112
113 if (attributes.getNamedItem("GTU") != null)
114 {
115 String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
116 if (!parser.gtuTags.containsKey(gtuName))
117 throw new NetworkException(
118 "FILL: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
119 fillTag.gtuTag = parser.gtuTags.get(gtuName);
120 }
121
122 if (attributes.getNamedItem("GTUMIX") != null)
123 {
124 String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
125 if (!parser.gtuMixTags.containsKey(gtuMixName))
126 throw new NetworkException(
127 "FILL: LANE " + laneName + " GTUMIX " + gtuMixName + " in link " + linkTag.name + " not defined");
128 fillTag.gtuMixTag = parser.gtuMixTags.get(gtuMixName);
129 }
130
131 if (fillTag.gtuTag == null && fillTag.gtuMixTag == null)
132 throw new SAXException(
133 "FILL: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link " + linkTag.name);
134
135 if (fillTag.gtuTag != null && fillTag.gtuMixTag != null)
136 throw new SAXException(
137 "FILL: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName + " of link " + linkTag.name);
138
139 Node distance = attributes.getNamedItem("DISTANCE");
140 if (distance == null)
141 throw new SAXException("FILL: missing attribute DISTANCE");
142 fillTag.distanceDist = Distributions.parseLengthDist(distance.getNodeValue());
143
144 Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
145 if (initialSpeed == null)
146 throw new SAXException("FILL: missing attribute INITIALSPEED");
147 fillTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
148
149 Node maxGTU = attributes.getNamedItem("MAXGTU");
150 fillTag.maxGTUs = maxGTU == null ? Integer.MAX_VALUE : Integer.parseInt(maxGTU.getNodeValue().trim());
151
152 int numberRouteTags = 0;
153
154 if (attributes.getNamedItem("ROUTE") != null)
155 {
156 String routeName = attributes.getNamedItem("ROUTE").getNodeValue().trim();
157 if (!parser.routeTags.containsKey(routeName))
158 throw new NetworkException(
159 "FILL: LANE " + laneName + " ROUTE " + routeName + " in link " + linkTag.name + " not defined");
160 fillTag.routeTag = parser.routeTags.get(routeName);
161 numberRouteTags++;
162 }
163
164 if (attributes.getNamedItem("ROUTEMIX") != null)
165 {
166 String routeMixName = attributes.getNamedItem("ROUTEMIX").getNodeValue().trim();
167 if (!parser.routeMixTags.containsKey(routeMixName))
168 throw new NetworkException(
169 "FILL: LANE " + laneName + " ROUTEMIX " + routeMixName + " in link " + linkTag.name + " not defined");
170 fillTag.routeMixTag = parser.routeMixTags.get(routeMixName);
171 numberRouteTags++;
172 }
173
174 if (attributes.getNamedItem("SHORTESTROUTE") != null)
175 {
176 String shortestRouteName = attributes.getNamedItem("SHORTESTROUTE").getNodeValue().trim();
177 if (!parser.shortestRouteTags.containsKey(shortestRouteName))
178 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTE " + shortestRouteName + " in link "
179 + linkTag.name + " not defined");
180 fillTag.shortestRouteTag = parser.shortestRouteTags.get(shortestRouteName);
181 numberRouteTags++;
182 }
183
184 if (attributes.getNamedItem("SHORTESTROUTEMIX") != null)
185 {
186 String shortestRouteMixName = attributes.getNamedItem("SHORTESTROUTEMIX").getNodeValue().trim();
187 if (!parser.shortestRouteMixTags.containsKey(shortestRouteMixName))
188 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTEMIX " + shortestRouteMixName + " in link "
189 + linkTag.name + " not defined");
190 fillTag.shortestRouteMixTag = parser.shortestRouteMixTags.get(shortestRouteMixName);
191 numberRouteTags++;
192 }
193
194 if (numberRouteTags > 1)
195 throw new SAXException(
196 "FILL: multiple ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
197
198
199
200 linkTag.fillTags.put(laneName, fillTag);
201 }
202
203
204
205
206
207
208
209
210
211
212 static void makeFill(final FillTag fillTag, final XmlNetworkLaneParser parser, final LinkTag linkTag,
213 final OTSDEVSSimulatorInterface simulator) throws SimRuntimeException, NetworkException
214 {
215 Lane lane = linkTag.lanes.get(fillTag.laneName);
216 Class<?> gtuClass = LaneBasedIndividualGTU.class;
217 List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
218 for (NodeTag nodeTag : fillTag.routeTag.routeNodeTags)
219 {
220 nodeList.add(parser.nodeTags.get(nodeTag.name).node);
221 }
222 RouteGenerator rg = new FixedRouteGenerator(new CompleteRoute("fixed route", GTUType.ALL, nodeList));
223
224
225
226
227
228
229
230 }
231
232
233 @Override
234 public final String toString()
235 {
236 return "FillTag [laneName=" + this.laneName + ", gtuTag=" + this.gtuTag + ", gtuMixTag=" + this.gtuMixTag
237 + ", distanceDist=" + this.distanceDist + ", initialSpeedDist=" + this.initialSpeedDist + ", maxGTUs="
238 + this.maxGTUs + ", routeTag=" + this.routeTag + ", routeMixTag=" + this.routeMixTag + ", shortestRouteTag="
239 + this.shortestRouteTag + ", shortestRouteMixTag=" + this.shortestRouteMixTag + "]";
240 }
241
242 }