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.value.vdouble.scalar.Frequency;
8   import org.djunits.value.vdouble.scalar.LinearDensity;
9   import org.opentrafficsim.core.network.NetworkException;
10  import org.opentrafficsim.core.network.factory.xml.units.DurationUnits;
11  import org.opentrafficsim.core.network.factory.xml.units.LengthUnits;
12  import org.w3c.dom.NamedNodeMap;
13  import org.w3c.dom.Node;
14  import org.w3c.dom.NodeList;
15  import org.xml.sax.SAXException;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
23   * initial version Jul 23, 2015 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   */
26  class ShortestRouteTag implements Serializable
27  {
28      /** */
29      private static final long serialVersionUID = 20150723L;
30  
31      /** Name. */
32      @SuppressWarnings("checkstyle:visibilitymodifier")
33      String name = null;
34  
35      /** From Node. */
36      @SuppressWarnings("checkstyle:visibilitymodifier")
37      NodeTag from = null;
38  
39      /** Via Nodes. */
40      @SuppressWarnings("checkstyle:visibilitymodifier")
41      List<NodeTag> via = new ArrayList<NodeTag>();
42  
43      /** To Node. */
44      @SuppressWarnings("checkstyle:visibilitymodifier")
45      NodeTag to = null;
46  
47      /** Time unit for the "cost" per time. */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      Frequency costPerTime = null;
50  
51      /** Distance unit for the "cost" per time. */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      LinearDensity costPerDistance = null;
54  
55      /**
56       * Parse the SHORTESTROUTE tag.
57       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
58       * @param parser VissimNetworkLaneParser; the parser with the lists of information
59       * @throws SAXException when parsing of the tag fails
60       * @throws NetworkException when parsing of the tag fails
61       */
62      @SuppressWarnings("checkstyle:needbraces")
63      static void parseShortestRoutes(final NodeList nodeList, final VissimNetworkLaneParser parser)
64              throws SAXException, NetworkException
65      {
66          for (Node node : XMLParser.getNodes(nodeList, "SHORTESTROUTE"))
67          {
68              NamedNodeMap attributes = node.getAttributes();
69              ShortestRouteTag shortestRouteTag = new ShortestRouteTag();
70  
71              if (attributes.getNamedItem("NAME") == null)
72              {
73                  throw new SAXException("SHORTESTROUTE: missing attribute NAME");
74              }
75              shortestRouteTag.name = attributes.getNamedItem("NAME").getNodeValue().trim();
76              if (parser.getRouteTags().keySet().contains(shortestRouteTag.name))
77              {
78                  throw new SAXException("SHORTESTROUTE: NAME " + shortestRouteTag.name + " defined twice");
79              }
80  
81              if (attributes.getNamedItem("FROM") == null)
82              {
83                  throw new SAXException("SHORTESTROUTE: missing attribute FROM");
84              }
85              String fromNode = attributes.getNamedItem("FROM").getNodeValue().trim();
86              if (!parser.getNodeTags().containsKey(fromNode))
87              {
88                  throw new SAXException("SHORTESTROUTE " + shortestRouteTag.name + ": FROM node " + fromNode + " not found");
89              }
90              shortestRouteTag.from = parser.getNodeTags().get(fromNode);
91  
92              if (attributes.getNamedItem("NODELIST") != null)
93              {
94                  String viaNodes = attributes.getNamedItem("NODELIST").getNodeValue().trim();
95                  shortestRouteTag.via = NodeTag.parseNodeList(viaNodes, parser);
96              }
97  
98              if (attributes.getNamedItem("TO") == null)
99              {
100                 throw new SAXException("SHORTESTROUTE: missing attribute TO");
101             }
102             String toNode = attributes.getNamedItem("TO").getNodeValue().trim();
103             if (!parser.getNodeTags().containsKey(toNode.trim()))
104             {
105                 throw new SAXException("SHORTESTROUTE " + shortestRouteTag.name + ": TO node " + toNode + " not found");
106             }
107             shortestRouteTag.to = parser.getNodeTags().get(toNode);
108 
109             Node distanceCost = attributes.getNamedItem("DISTANCECOST");
110             if (distanceCost == null)
111             {
112                 throw new SAXException("SHORTESTROUTE: missing attribute DISTANCECOST");
113             }
114             shortestRouteTag.costPerDistance = LengthUnits.parseLinearDensity(distanceCost.getNodeValue().trim());
115 
116             Node timeCost = attributes.getNamedItem("TIMECOST");
117             if (timeCost == null)
118             {
119                 throw new SAXException("SHORTESTROUTE: missing attribute TIMECOST");
120             }
121             shortestRouteTag.costPerTime = DurationUnits.parseFrequency(timeCost.getNodeValue().trim());
122 
123             parser.getShortestRouteTags().put(shortestRouteTag.name.trim(), shortestRouteTag);
124         }
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public final String toString()
130     {
131         return "ShortestRouteTag [name=" + this.name + ", from=" + this.from + ", via=" + this.via + ", to=" + this.to
132                 + ", costPerTime=" + this.costPerTime + ", costPerDistance=" + this.costPerDistance + "]";
133     }
134 }