View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.LengthUnit;
6   import org.djunits.unit.SpeedUnit;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djunits.value.vdouble.scalar.Speed;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.core.gtu.GTUType;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.factory.xml.units.Distributions;
13  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
14  import org.w3c.dom.NamedNodeMap;
15  import org.w3c.dom.Node;
16  import org.w3c.dom.NodeList;
17  import org.xml.sax.SAXException;
18  
19  /**
20   * GTU Tag.
21   *
22   * <pre>
23   * {@code
24    <xsd:element name="GTU">
25      <xsd:complexType>
26        <xsd:attribute name="NAME" type="xsd:string" use="required" />
27        <xsd:attribute name="GTUTYPE" type="xsd:string" use="required" />
28        <xsd:attribute name="LENGTH" type="LENGTHDISTTYPE" use="required" />
29        <xsd:attribute name="WIDTH" type="LENGTHDISTTYPE" use="required" />
30        <xsd:attribute name="MAXSPEED" type="SPEEDDISTTYPE" use="required" />
31        <xsd:attribute ref="xml:base" />
32      </xsd:complexType>
33    </xsd:element>
34   * }
35   * </pre>
36   * <p>
37   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
39   * <p>
40   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
41   * initial version Jul 23, 2015 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   */
44  class GTUTag implements Serializable
45  {
46      /** */
47      private static final long serialVersionUID = 20150723L;
48  
49      /** Name. */
50      @SuppressWarnings("checkstyle:visibilitymodifier")
51      String name = null;
52  
53      /** Type. */
54      @SuppressWarnings("checkstyle:visibilitymodifier")
55      GTUType gtuType = null;
56  
57      /** GTU length. */
58      @SuppressWarnings("checkstyle:visibilitymodifier")
59      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> lengthDist = null;
60  
61      /** GTU width. */
62      @SuppressWarnings("checkstyle:visibilitymodifier")
63      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> widthDist = null;
64  
65      /** Maximum speed. */
66      @SuppressWarnings("checkstyle:visibilitymodifier")
67      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maxSpeedDist = null;
68  
69      /**
70       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
71       * @param parser VissimNetworkLaneParser; the parser with the lists of information
72       * @throws SAXException when parsing of GTU tag fails
73       * @throws NetworkException when parsing of GTU tag fails
74       * @throws GTUException if GTUType defined twice
75       */
76      @SuppressWarnings("checkstyle:needbraces")
77      static void parseGTUs(final NodeList nodeList, final VissimNetworkLaneParser parser)
78              throws SAXException, NetworkException, GTUException
79      {
80          for (Node node : XMLParser.getNodes(nodeList, "GTU"))
81          {
82              NamedNodeMap attributes = node.getAttributes();
83              GTUTag gtuTag = new GTUTag();
84  
85              Node name = attributes.getNamedItem("NAME");
86              if (name == null)
87              {
88                  throw new SAXException("GTU: missing attribute NAME");
89              }
90              gtuTag.name = name.getNodeValue().trim();
91              if (parser.getGtuTags().keySet().contains(gtuTag.name))
92              {
93                  throw new SAXException("GTU: NAME " + gtuTag.name + " defined twice");
94              }
95  
96              Node gtuType = attributes.getNamedItem("GTUTYPE");
97              if (gtuType == null)
98              {
99                  throw new SAXException("GTU: missing attribute GTUTYPE");
100             }
101             if (!parser.getGtuTypes().containsKey(gtuType.getNodeValue().trim()))
102             {
103                 throw new SAXException("GTU: GTUTYPE " + gtuType.getNodeValue().trim() + " not defined");
104             }
105             gtuTag.gtuType = parser.getGtuTypes().get(gtuType.getNodeValue().trim());
106 
107             Node length = attributes.getNamedItem("LENGTH");
108             if (length == null)
109             {
110                 throw new SAXException("GTU: missing attribute LENGTH");
111             }
112             gtuTag.lengthDist = Distributions.parseLengthDist(length.getNodeValue());
113 
114             Node width = attributes.getNamedItem("WIDTH");
115             if (width == null)
116             {
117                 throw new SAXException("GTU: missing attribute WIDTH");
118             }
119             gtuTag.widthDist = Distributions.parseLengthDist(width.getNodeValue());
120 
121             Node maxSpeed = attributes.getNamedItem("MAXSPEED");
122             if (maxSpeed == null)
123             {
124                 throw new SAXException("GTU: missing attribute LENGTH");
125             }
126             gtuTag.maxSpeedDist = Distributions.parseSpeedDist(maxSpeed.getNodeValue());
127 
128             parser.getGtuTags().put(gtuTag.name, gtuTag);
129         }
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public final String toString()
135     {
136         return "GTUTag [name=" + this.name + ", gtuType=" + this.gtuType + ", lengthDist=" + this.lengthDist + ", widthDist="
137                 + this.widthDist + ", followingModel=" + ", maxSpeedDist=" + this.maxSpeedDist + "]";
138     }
139 
140 }