View Javadoc
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   * <p>
31   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
33   * <p>
34   * $LastChangedDate: 2019-03-26 15:54:58 +0100 (Tue, 26 Mar 2019) $, @version $Revision: 5190 $, by $Author: wjschakel $,
35   * initial version Jul 23, 2015 <br>
36   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
37   */
38  class FillTag implements Serializable
39  {
40      /** */
41      private static final long serialVersionUID = 20150723L;
42  
43      /** Lane name. */
44      @SuppressWarnings("checkstyle:visibilitymodifier")
45      String laneName = null;
46  
47      /** GTU tag. */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      GTUTag gtuTag = null;
50  
51      /** GTU mix tag. */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      GTUMixTag gtuMixTag = null;
54  
55      /** Inter-vehicle distance. */
56      @SuppressWarnings("checkstyle:visibilitymodifier")
57      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> distanceDist = null;
58  
59      /** Initial speed. */
60      @SuppressWarnings("checkstyle:visibilitymodifier")
61      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
62  
63      /** Maximmum number of generated GTUs. */
64      @SuppressWarnings("checkstyle:visibilitymodifier")
65      int maxGTUs = Integer.MAX_VALUE;
66  
67      /** Route tag. */
68      @SuppressWarnings("checkstyle:visibilitymodifier")
69      RouteTag routeTag = null;
70  
71      /** Route mix tag. */
72      @SuppressWarnings("checkstyle:visibilitymodifier")
73      RouteMixTag routeMixTag = null;
74  
75      /** Shortest route tag. */
76      @SuppressWarnings("checkstyle:visibilitymodifier")
77      ShortestRouteTag shortestRouteTag = null;
78  
79      /** Shortest route mix tag. */
80      @SuppressWarnings("checkstyle:visibilitymodifier")
81      ShortestRouteMixTag shortestRouteMixTag = null;
82  
83      /**
84       * Parse the FILL tag.
85       * @param node Node; the FILL node to parse
86       * @param parser VissimNetworkLaneParser; the parser with the lists of information
87       * @param linkTag LinkTag; the parent LINK tag
88       * @throws SAXException when parsing of the tag fails
89       * @throws NetworkException when parsing of the tag fails
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         // TODO GTUColorer
232 
233         linkTag.fillTags.put(laneName, fillTag);
234     }
235 
236     /**
237      * Make a fill of a Lane.
238      * @param fillTag FillTag; XML tag for the generator to build
239      * @param parser VissimNetworkLaneParser; the parser with the lists of information
240      * @param linkTag LinkTag; the parent LINK tag
241      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator to schedule GTU generation
242      * @throws SimRuntimeException in case of simulation problems building the car generator
243      * @throws NetworkException when route generator cannot be instantiated
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         // TODO create a FILL
259 
260         // TODO GTUMix
261         // TODO RouteMix
262         // TODO ShortestRoute
263         // TODO ShortestRouteMix
264     }
265 
266     /** {@inheritDoc} */
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 }