View Javadoc
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.gtu.GTUType;
12  import org.opentrafficsim.core.network.NetworkException;
13  import org.opentrafficsim.core.network.factory.xml.units.Distributions;
14  import org.opentrafficsim.core.network.route.CompleteRoute;
15  import org.opentrafficsim.core.network.route.FixedRouteGenerator;
16  import org.opentrafficsim.core.network.route.RouteGenerator;
17  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
18  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
19  import org.opentrafficsim.road.network.factory.xml.CrossSectionElementTag.ElementType;
20  import org.opentrafficsim.road.network.lane.Lane;
21  import org.w3c.dom.NamedNodeMap;
22  import org.w3c.dom.Node;
23  import org.xml.sax.SAXException;
24  
25  import nl.tudelft.simulation.dsol.SimRuntimeException;
26  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
27  
28  /**
29   * <p>
30   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
31   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32   * <p>
33   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
34   * initial version Jul 23, 2015 <br>
35   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
36   */
37  class FillTag implements Serializable
38  {
39      /** */
40      private static final long serialVersionUID = 20150723L;
41  
42      /** Lane name. */
43      @SuppressWarnings("checkstyle:visibilitymodifier")
44      String laneName = null;
45  
46      /** GTU tag. */
47      @SuppressWarnings("checkstyle:visibilitymodifier")
48      GTUTag gtuTag = null;
49  
50      /** GTU mix tag. */
51      @SuppressWarnings("checkstyle:visibilitymodifier")
52      GTUMixTag gtuMixTag = null;
53  
54      /** Inter-vehicle distance. */
55      @SuppressWarnings("checkstyle:visibilitymodifier")
56      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> distanceDist = null;
57  
58      /** Initial speed. */
59      @SuppressWarnings("checkstyle:visibilitymodifier")
60      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
61  
62      /** Maximmum number of generated GTUs. */
63      @SuppressWarnings("checkstyle:visibilitymodifier")
64      int maxGTUs = Integer.MAX_VALUE;
65  
66      /** Route tag. */
67      @SuppressWarnings("checkstyle:visibilitymodifier")
68      RouteTag routeTag = null;
69  
70      /** Route mix tag. */
71      @SuppressWarnings("checkstyle:visibilitymodifier")
72      RouteMixTag routeMixTag = null;
73  
74      /** Shortest route tag. */
75      @SuppressWarnings("checkstyle:visibilitymodifier")
76      ShortestRouteTag shortestRouteTag = null;
77  
78      /** Shortest route mix tag. */
79      @SuppressWarnings("checkstyle:visibilitymodifier")
80      ShortestRouteMixTag shortestRouteMixTag = null;
81  
82      /**
83       * Parse the FILL tag.
84       * @param node the FILL node to parse
85       * @param parser the parser with the lists of information
86       * @param linkTag the parent LINK tag
87       * @throws SAXException when parsing of the tag fails
88       * @throws NetworkException when parsing of the tag fails
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         // TODO GTUColorer
199 
200         linkTag.fillTags.put(laneName, fillTag);
201     }
202 
203     /**
204      * Make a fill of a Lane.
205      * @param fillTag XML tag for the generator to build
206      * @param parser the parser with the lists of information
207      * @param linkTag the parent LINK tag
208      * @param simulator the simulator to schedule GTU generation
209      * @throws SimRuntimeException in case of simulation problems building the car generator
210      * @throws NetworkException when route generator cannot be instantiated
211      */
212     static void makeFill(final FillTag fillTag, final XmlNetworkLaneParser parser, final LinkTag linkTag,
213             final DEVSSimulatorInterface.TimeDoubleUnit 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.VEHICLE, nodeList));
223 
224         // TODO create a FILL
225 
226         // TODO GTUMix
227         // TODO RouteMix
228         // TODO ShortestRoute
229         // TODO ShortestRouteMix
230     }
231 
232     /** {@inheritDoc} */
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 }