View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.old;
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.xml.old.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: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
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 XmlNetworkLaneParserOld; 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 XmlNetworkLaneParserOld 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              throw new SAXException("FILL: missing attribute LANE" + " for link " + linkTag.name);
100         String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
101         if (linkTag.roadLayoutTag == null)
102             throw new NetworkException("FILL: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
103         CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
104         if (cseTag == null)
105             throw new NetworkException("FILL: LANE " + laneName + " not found in elements of link " + linkTag.name
106                     + " - roadtype " + linkTag.roadLayoutTag.name);
107         if (cseTag.elementType != ElementType.LANE)
108             throw new NetworkException("FILL: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
109                     + " - roadtype " + linkTag.roadLayoutTag.name);
110         if (linkTag.generatorTags.containsKey(laneName))
111             throw new SAXException("FILL for LANE with NAME " + laneName + " defined twice");
112         fillTag.laneName = laneName;
113 
114         if (attributes.getNamedItem("GTU") != null)
115         {
116             String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
117             if (!parser.gtuTags.containsKey(gtuName))
118                 throw new NetworkException(
119                         "FILL: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
120             fillTag.gtuTag = parser.gtuTags.get(gtuName);
121         }
122 
123         if (attributes.getNamedItem("GTUMIX") != null)
124         {
125             String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
126             if (!parser.gtuMixTags.containsKey(gtuMixName))
127                 throw new NetworkException(
128                         "FILL: LANE " + laneName + " GTUMIX " + gtuMixName + " in link " + linkTag.name + " not defined");
129             fillTag.gtuMixTag = parser.gtuMixTags.get(gtuMixName);
130         }
131 
132         if (fillTag.gtuTag == null && fillTag.gtuMixTag == null)
133             throw new SAXException(
134                     "FILL: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link " + linkTag.name);
135 
136         if (fillTag.gtuTag != null && fillTag.gtuMixTag != null)
137             throw new SAXException(
138                     "FILL: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName + " of link " + linkTag.name);
139 
140         Node distance = attributes.getNamedItem("DISTANCE");
141         if (distance == null)
142             throw new SAXException("FILL: missing attribute DISTANCE");
143         fillTag.distanceDist = Distributions.parseLengthDist(distance.getNodeValue());
144 
145         Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
146         if (initialSpeed == null)
147             throw new SAXException("FILL: missing attribute INITIALSPEED");
148         fillTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
149 
150         Node maxGTU = attributes.getNamedItem("MAXGTU");
151         fillTag.maxGTUs = maxGTU == null ? Integer.MAX_VALUE : Integer.parseInt(maxGTU.getNodeValue().trim());
152 
153         int numberRouteTags = 0;
154 
155         if (attributes.getNamedItem("ROUTE") != null)
156         {
157             String routeName = attributes.getNamedItem("ROUTE").getNodeValue().trim();
158             if (!parser.routeTags.containsKey(routeName))
159                 throw new NetworkException(
160                         "FILL: LANE " + laneName + " ROUTE " + routeName + " in link " + linkTag.name + " not defined");
161             fillTag.routeTag = parser.routeTags.get(routeName);
162             numberRouteTags++;
163         }
164 
165         if (attributes.getNamedItem("ROUTEMIX") != null)
166         {
167             String routeMixName = attributes.getNamedItem("ROUTEMIX").getNodeValue().trim();
168             if (!parser.routeMixTags.containsKey(routeMixName))
169                 throw new NetworkException(
170                         "FILL: LANE " + laneName + " ROUTEMIX " + routeMixName + " in link " + linkTag.name + " not defined");
171             fillTag.routeMixTag = parser.routeMixTags.get(routeMixName);
172             numberRouteTags++;
173         }
174 
175         if (attributes.getNamedItem("SHORTESTROUTE") != null)
176         {
177             String shortestRouteName = attributes.getNamedItem("SHORTESTROUTE").getNodeValue().trim();
178             if (!parser.shortestRouteTags.containsKey(shortestRouteName))
179                 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTE " + shortestRouteName + " in link "
180                         + linkTag.name + " not defined");
181             fillTag.shortestRouteTag = parser.shortestRouteTags.get(shortestRouteName);
182             numberRouteTags++;
183         }
184 
185         if (attributes.getNamedItem("SHORTESTROUTEMIX") != null)
186         {
187             String shortestRouteMixName = attributes.getNamedItem("SHORTESTROUTEMIX").getNodeValue().trim();
188             if (!parser.shortestRouteMixTags.containsKey(shortestRouteMixName))
189                 throw new NetworkException("FILL: LANE " + laneName + " SHORTESTROUTEMIX " + shortestRouteMixName + " in link "
190                         + linkTag.name + " not defined");
191             fillTag.shortestRouteMixTag = parser.shortestRouteMixTags.get(shortestRouteMixName);
192             numberRouteTags++;
193         }
194 
195         if (numberRouteTags > 1)
196             throw new SAXException(
197                     "FILL: multiple ROUTE tags defined for Lane with NAME " + laneName + " of link " + linkTag.name);
198 
199         // TODO GTUColorer
200 
201         linkTag.fillTags.put(laneName, fillTag);
202     }
203 
204     /**
205      * Make a fill of a Lane.
206      * @param fillTag FillTag; XML tag for the generator to build
207      * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
208      * @param linkTag LinkTag; the parent LINK tag
209      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator to schedule GTU generation
210      * @throws SimRuntimeException in case of simulation problems building the car generator
211      * @throws NetworkException when route generator cannot be instantiated
212      */
213     static void makeFill(final FillTag fillTag, final XmlNetworkLaneParserOld parser, final LinkTag linkTag,
214             final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws SimRuntimeException, NetworkException
215     {
216         Lane lane = linkTag.lanes.get(fillTag.laneName);
217         Class<?> gtuClass = LaneBasedIndividualGTU.class;
218         List<org.opentrafficsim.core.network.Node> nodeList = new ArrayList<>();
219         for (NodeTag nodeTag : fillTag.routeTag.routeNodeTags)
220         {
221             nodeList.add(parser.nodeTags.get(nodeTag.name).node);
222         }
223         Generator<Route> rg = new FixedRouteGenerator(
224                 new CompleteRoute("fixed route", lane.getNetwork().getGtuType(GTUType.DEFAULTS.VEHICLE), nodeList));
225 
226         // TODO create a FILL
227 
228         // TODO GTUMix
229         // TODO RouteMix
230         // TODO ShortestRoute
231         // TODO ShortestRouteMix
232     }
233 
234     /** {@inheritDoc} */
235     @Override
236     public final String toString()
237     {
238         return "FillTag [laneName=" + this.laneName + ", gtuTag=" + this.gtuTag + ", gtuMixTag=" + this.gtuMixTag
239                 + ", distanceDist=" + this.distanceDist + ", initialSpeedDist=" + this.initialSpeedDist + ", maxGTUs="
240                 + this.maxGTUs + ", routeTag=" + this.routeTag + ", routeMixTag=" + this.routeMixTag + ", shortestRouteTag="
241                 + this.shortestRouteTag + ", shortestRouteMixTag=" + this.shortestRouteMixTag + "]";
242     }
243 
244 }