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.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.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  
28  /**
29   * <p>
30   * Copyright (c) 2013-2017 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: 2017-04-29 13:29:16 +0200 (Sat, 29 Apr 2017) $, @version $Revision: 3573 $, 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 VissimNetworkLaneParser 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          {
99              throw new SAXException("FILL: missing attribute LANE" + " for link " + linkTag.name);
100         }
101         String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
102         if (linkTag.roadLayoutTag == null)
103         {
104             throw new NetworkException("FILL: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
105         }
106         CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
107         if (cseTag == null)
108         {
109             throw new NetworkException("FILL: LANE " + laneName + " not found in elements of link " + linkTag.name
110                     + " - roadtype " + linkTag.roadLayoutTag.name);
111         }
112         if (cseTag.elementType != ElementType.LANE)
113         {
114             throw new NetworkException("FILL: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
115                     + " - roadtype " + linkTag.roadLayoutTag.name);
116         }
117         if (linkTag.generatorTags.containsKey(laneName))
118         {
119             throw new SAXException("FILL for LANE with NAME " + laneName + " defined twice");
120         }
121         fillTag.laneName = laneName;
122 
123         if (attributes.getNamedItem("GTU") != null)
124         {
125             String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
126             if (!parser.getGtuTags().containsKey(gtuName))
127             {
128                 throw new NetworkException(
129                         "FILL: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
130             }
131             fillTag.gtuTag = parser.getGtuTags().get(gtuName);
132         }
133 
134         if (attributes.getNamedItem("GTUMIX") != null)
135         {
136             String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
137             if (!parser.getGtuMixTags().containsKey(gtuMixName))
138             {
139                 throw new NetworkException(
140                         "FILL: LANE " + laneName + " GTUMIX " + gtuMixName + " in link " + linkTag.name + " not defined");
141             }
142             fillTag.gtuMixTag = parser.getGtuMixTags().get(gtuMixName);
143         }
144 
145         if (fillTag.gtuTag == null && fillTag.gtuMixTag == null)
146         {
147             throw new SAXException(
148                     "FILL: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link " + linkTag.name);
149         }
150 
151         if (fillTag.gtuTag != null && fillTag.gtuMixTag != null)
152         {
153             throw new SAXException(
154                     "FILL: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName + " of link " + linkTag.name);
155         }
156 
157         Node distance = attributes.getNamedItem("DISTANCE");
158         if (distance == null)
159         {
160             throw new SAXException("FILL: missing attribute DISTANCE");
161         }
162         fillTag.distanceDist = Distributions.parseLengthDist(distance.getNodeValue());
163 
164         Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
165         if (initialSpeed == null)
166         {
167             throw new SAXException("FILL: missing attribute INITIALSPEED");
168         }
169         fillTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
170 
171         Node maxGTU = attributes.getNamedItem("MAXGTU");
172         fillTag.maxGTUs = maxGTU == null ? Integer.MAX_VALUE : Integer.parseInt(maxGTU.getNodeValue().trim());
173 
174         int numberRouteTags = 0;
175 
176         if (attributes.getNamedItem("ROUTE") != null)
177         {
178             String routeName = attributes.getNamedItem("ROUTE").getNodeValue().trim();
179             if (!parser.getRouteTags().containsKey(routeName))
180             {
181                 throw new NetworkException(
182                         "FILL: LANE " + laneName + " ROUTE " + routeName + " in link " + linkTag.name + " not defined");
183             }
184             fillTag.routeTag = parser.getRouteTags().get(routeName);
185             numberRouteTags++;
186         }
187 
188         if (attributes.getNamedItem("ROUTEMIX") != null)
189         {
190             String routeMixName = attributes.getNamedItem("ROUTEMIX").getNodeValue().trim();
191             if (!parser.getRouteMixTags().containsKey(routeMixName))
192             {
193                 throw new NetworkException(
194                         "FILL: LANE " + laneName + " ROUTEMIX " + routeMixName + " in link " + linkTag.name + " not defined");
195             }
196             fillTag.routeMixTag = parser.getRouteMixTags().get(routeMixName);
197             numberRouteTags++;
198         }
199 
200         if (attributes.getNamedItem("SHORTESTROUTE") != null)
201         {
202             String shortestRouteName = attributes.getNamedItem("SHORTESTROUTE").getNodeValue().trim();
203             if (!parser.getShortestRouteTags().containsKey(shortestRouteName))
204             {
205                 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTE " + shortestRouteName + " in link "
206                         + linkTag.name + " not defined");
207             }
208             fillTag.shortestRouteTag = parser.getShortestRouteTags().get(shortestRouteName);
209             numberRouteTags++;
210         }
211 
212         if (attributes.getNamedItem("SHORTESTROUTEMIX") != null)
213         {
214             String shortestRouteMixName = attributes.getNamedItem("SHORTESTROUTEMIX").getNodeValue().trim();
215             if (!parser.getShortestRouteMixTags().containsKey(shortestRouteMixName))
216             {
217                 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTEMIX " + shortestRouteMixName + " in link "
218                         + linkTag.name + " not defined");
219             }
220             fillTag.shortestRouteMixTag = parser.getShortestRouteMixTags().get(shortestRouteMixName);
221             numberRouteTags++;
222         }
223 
224         if (numberRouteTags > 1)
225         {
226             throw new SAXException(
227                     "FILL: multiple ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
228         }
229 
230         // TODO GTUColorer
231 
232         linkTag.fillTags.put(laneName, fillTag);
233     }
234 
235     /**
236      * Make a fill of a Lane.
237      * @param fillTag XML tag for the generator to build
238      * @param parser the parser with the lists of information
239      * @param linkTag the parent LINK tag
240      * @param simulator the simulator to schedule GTU generation
241      * @throws SimRuntimeException in case of simulation problems building the car generator
242      * @throws NetworkException when route generator cannot be instantiated
243      */
244     static void makeFill(final FillTag fillTag, final VissimNetworkLaneParser parser, final LinkTag linkTag,
245             final OTSDEVSSimulatorInterface simulator) throws SimRuntimeException, NetworkException
246     {
247         Lane lane = linkTag.lanes.get(fillTag.laneName);
248         Class<?> gtuClass = LaneBasedIndividualGTU.class;
249         List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
250         for (NodeTag nodeTag : fillTag.routeTag.routeNodeTags)
251         {
252             nodeList.add(parser.getNodeTags().get(nodeTag.name).node);
253         }
254         RouteGenerator rg = new FixedRouteGenerator(new CompleteRoute("fixed route", GTUType.ALL, nodeList));
255 
256         // TODO create a FILL
257 
258         // TODO GTUMix
259         // TODO RouteMix
260         // TODO ShortestRoute
261         // TODO ShortestRouteMix
262     }
263 
264     /** {@inheritDoc} */
265     @Override
266     public final String toString()
267     {
268         return "FillTag [laneName=" + this.laneName + ", gtuTag=" + this.gtuTag + ", gtuMixTag=" + this.gtuMixTag
269                 + ", distanceDist=" + this.distanceDist + ", initialSpeedDist=" + this.initialSpeedDist + ", maxGTUs="
270                 + this.maxGTUs + ", routeTag=" + this.routeTag + ", routeMixTag=" + this.routeMixTag + ", shortestRouteTag="
271                 + this.shortestRouteTag + ", shortestRouteMixTag=" + this.shortestRouteMixTag + "]";
272     }
273 
274 }