View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.core.gtu.RelativePosition;
6   import org.opentrafficsim.core.network.NetworkException;
7   import org.w3c.dom.NamedNodeMap;
8   import org.w3c.dom.Node;
9   import org.w3c.dom.NodeList;
10  import org.xml.sax.SAXException;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * <p>
17   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
18   * initial version Jul 23, 2015 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   */
21  class SensorTag implements Serializable
22  {
23      /** */
24      private static final long serialVersionUID = 20150723L;
25  
26      /** Name. */
27      @SuppressWarnings("checkstyle:visibilitymodifier")
28      String name = null;
29  
30      /** length. */
31      @SuppressWarnings("checkstyle:visibilitymodifier")
32      String length = null;
33  
34      /** laneLocation. */
35      @SuppressWarnings("checkstyle:visibilitymodifier")
36      String laneName = null;
37  
38      /** linkLocation. */
39      @SuppressWarnings("checkstyle:visibilitymodifier")
40      String linkName = null;
41  
42      /** No. */
43      @SuppressWarnings("checkstyle:visibilitymodifier")
44      String no = null;
45  
46      /** PortNo. */
47      @SuppressWarnings("checkstyle:visibilitymodifier")
48      String portNo = null;
49  
50      /** sc. */
51      @SuppressWarnings("checkstyle:visibilitymodifier")
52      String sc = null;
53  
54      /** type. */
55      @SuppressWarnings("checkstyle:visibilitymodifier")
56      String type = null;
57  
58      /** Position of the sink on the link, relative to the design line, stored as a string to parse when the length is known. */
59      @SuppressWarnings("checkstyle:visibilitymodifier")
60      String positionStr = null;
61  
62      /** Class name of the Sensor. */
63      @SuppressWarnings("checkstyle:visibilitymodifier")
64      String className = null;
65  
66      /** Trigger position of the GTU (FRONT, REAR, etc.). */
67      @SuppressWarnings("checkstyle:visibilitymodifier")
68      RelativePosition.TYPE triggerPosition;
69  
70      /** is the sensor really located on this link? */
71      Boolean activeOnThisLink = true;
72  
73      /**
74       * @param sensorTag SensorTag;
75       */
76      public SensorTag(SensorTag sensorTag)
77      {
78          this.name = sensorTag.name;
79          this.length = sensorTag.length;
80          this.laneName = sensorTag.laneName;
81          this.linkName = sensorTag.linkName;
82          this.no = sensorTag.no;
83          this.portNo = sensorTag.portNo;
84          this.sc = sensorTag.sc;
85          this.type = sensorTag.type;
86          this.positionStr = sensorTag.positionStr;
87          this.className = sensorTag.className;
88          this.triggerPosition = sensorTag.triggerPosition;
89      }
90  
91      /**
92       *
93       */
94      public SensorTag()
95      {
96          // TODO Auto-generated constructor stub
97      }
98  
99      /**
100      * Parse the SENSOR tag.
101      * @param nodeList NodeList; the SENSOR node to parse
102      * @param parser VissimNetworkLaneParser; the parser with the lists of information
103      * @throws SAXException when parsing of the tag fails
104      * @throws NetworkException when parsing of the tag fails
105      */
106     @SuppressWarnings("checkstyle:needbraces")
107     static void parseSensor(final NodeList nodeList, final VissimNetworkLaneParser parser) throws SAXException, NetworkException
108     {
109 
110         for (Node linksNode : XMLParser.getNodes(nodeList, "detectors"))
111         {
112 
113             for (Node node : XMLParser.getNodes(linksNode.getChildNodes(), "detector"))
114             {
115                 NamedNodeMap attributes = node.getAttributes();
116                 // make a link with its attributes
117                 SensorTag sensorTag = new SensorTag();
118 
119                 if (attributes.getNamedItem("name") == null)
120                 {
121                     throw new SAXException("Sensor: missing attribute: name");
122                 }
123                 sensorTag.name = attributes.getNamedItem("name").getNodeValue().trim();
124 
125                 if (attributes.getNamedItem("lane") == null)
126                 {
127                     throw new SAXException("Sensor: missing attribute: lane");
128                 }
129                 String laneLink = attributes.getNamedItem("lane").getNodeValue().trim();
130                 String[] laneLinkInfo = laneLink.split("\\s+");
131                 sensorTag.linkName = laneLinkInfo[0];
132                 sensorTag.laneName = laneLinkInfo[1];
133 
134                 if (attributes.getNamedItem("length") == null)
135                 {
136                     throw new SAXException("Sensor: missing attribute: length");
137                 }
138                 sensorTag.length = attributes.getNamedItem("length").getNodeValue().trim();
139 
140                 if (attributes.getNamedItem("no") == null)
141                 {
142                     throw new SAXException("Sensor: missing attribute: no");
143                 }
144                 sensorTag.no = attributes.getNamedItem("no").getNodeValue().trim();
145 
146                 if (attributes.getNamedItem("portNo") == null)
147                 {
148                     throw new SAXException("Sensor: missing attribute: portNo");
149                 }
150                 sensorTag.portNo = attributes.getNamedItem("portNo").getNodeValue().trim();
151 
152                 if (attributes.getNamedItem("sc") == null)
153                 {
154                     throw new SAXException("Sensor: missing attribute: sc");
155                 }
156                 sensorTag.sc = attributes.getNamedItem("sc").getNodeValue().trim();
157 
158                 if (attributes.getNamedItem("type") == null)
159                 {
160                     throw new SAXException("Sensor: missing attribute: type");
161                 }
162                 sensorTag.type = attributes.getNamedItem("type").getNodeValue().trim();
163 
164                 if (attributes.getNamedItem("pos") == null)
165                 {
166                     throw new SAXException("Sensor: missing attribute: pos");
167                 }
168                 sensorTag.positionStr = attributes.getNamedItem("pos").getNodeValue().trim();
169 
170                 parser.getSensorTags().put(sensorTag.no, sensorTag);
171 
172             }
173         }
174 
175     }
176 
177     /**
178      * @param trigger String; String of the trigger position such as FRONT
179      * @param sensorTag SensorTag; the sensor tag for error messages
180      * @param laneName String; the lane name for error messages
181      * @return a relative position type, such as RelatievPostition.FRONT
182      * @throws SAXException when the trigger position did not exist
183      */
184     static RelativePosition.TYPE parseTriggerPosition(final String trigger, final SensorTag sensorTag, final String laneName)
185             throws SAXException
186     {
187         switch (trigger)
188         {
189             case "FRONT":
190                 return RelativePosition.FRONT;
191 
192             case "CENTER":
193                 return RelativePosition.CENTER;
194 
195             case "DRIVER":
196                 return RelativePosition.DRIVER;
197 
198             case "REAR":
199                 return RelativePosition.REAR;
200 
201             case "REFERENCE":
202                 return RelativePosition.REFERENCE;
203 
204             default:
205                 throw new SAXException("SENSOR: wrong type of TRIGGER for sensor " + sensorTag.name + " on lane " + laneName);
206         }
207     }
208 
209     /** {@inheritDoc} */
210     @Override
211     public final String toString()
212     {
213         return "SensorTag [name=" + this.name + ", positionStr=" + this.positionStr + ", className=" + this.className
214                 + ", triggerPosition=" + this.triggerPosition + "]";
215     }
216 }