View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import javax.naming.NamingException;
8   
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.core.network.NetworkException;
11  import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
12  import org.w3c.dom.NamedNodeMap;
13  import org.w3c.dom.Node;
14  import org.xml.sax.SAXException;
15  
16  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
17  
18  /**
19   * <p>
20   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * <p>
23   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
24   * initial version Jul 23, 2015 <br>
25   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   */
27  class JunctionTag implements Serializable
28  {
29      /** */
30      private static final long serialVersionUID = 20150723L;
31  
32      /** Name of the junction. */
33      @SuppressWarnings("checkstyle:visibilitymodifier")
34      String name = null;
35  
36      /** Unique ID within database. */
37      @SuppressWarnings("checkstyle:visibilitymodifier")
38      String id = null;
39  
40      /** A map of connections in the junction */
41      @SuppressWarnings("checkstyle:visibilitymodifier")
42      Map<String, ConnectionTag> connectionTags = new HashMap<String, ConnectionTag>();
43  
44      /** A map of controller in the junction */
45      @SuppressWarnings("checkstyle:visibilitymodifier")
46      Map<String, ControllerTag> controllerTags = new HashMap<String, ControllerTag>();
47  
48      /**
49       * Parse the attributes of the junction tag. The sub-elements are parsed in separate classes.
50       * @param node Node; the junction node to parse
51       * @param parser OpenDriveNetworkLaneParser; the parser with the lists of information
52       * @throws SAXException when parsing of the tag fails
53       * @throws NetworkException when parsing of the tag fails
54       */
55      @SuppressWarnings("checkstyle:needbraces")
56      static void parseJunction(final Node node, final OpenDriveNetworkLaneParser parser) throws SAXException, NetworkException
57      {
58          NamedNodeMap attributes = node.getAttributes();
59          JunctionTag junctionTag = new JunctionTag();
60  
61          Node id = attributes.getNamedItem("id");
62          if (id == null)
63              throw new SAXException("JUNCTION: missing attribute ID");
64          junctionTag.id = id.getNodeValue().trim();
65          if (parser.roadTags.keySet().contains(junctionTag.id))
66              throw new SAXException("JUNCTION: ID " + junctionTag.id + " defined twice");
67  
68          Node name = attributes.getNamedItem("name");
69          if (name == null)
70              throw new SAXException("JUNCTION: missing attribute NAME for ID=" + junctionTag.id);
71          junctionTag.name = name.getNodeValue().trim();
72  
73          for (Node connectionNode : XMLParser.getNodes(node.getChildNodes(), "connection"))
74          {
75              ConnectionTag connectionTag = ConnectionTag.parseConnection(connectionNode, parser);
76              junctionTag.connectionTags.put(connectionTag.connectingRoad, connectionTag);
77          }
78  
79          for (Node connectionNode : XMLParser.getNodes(node.getChildNodes(), "controller"))
80          {
81              ControllerTag controllerTag = ControllerTag.parseController(connectionNode, parser);
82              junctionTag.controllerTags.put(controllerTag.id, controllerTag);
83          }
84  
85          parser.junctionTags.put(junctionTag.id, junctionTag);
86      }
87  
88      /**
89       * @param juncTag JunctionTag; junction tag
90       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; the simulator
91       * @param openDriveNetworkLaneParser OpenDriveNetworkLaneParser; the parser
92       * @throws NamingException when an animation registration fails
93       * @throws NetworkException when the network is inconsistent
94       * @throws GTUException when the traffic light (a GTU at the moment) has an error
95       */
96      public static void createController(JunctionTag juncTag, DEVSSimulatorInterface.TimeDoubleUnit simulator,
97              OpenDriveNetworkLaneParser openDriveNetworkLaneParser) throws GTUException, NetworkException, NamingException
98      {
99          if (juncTag.controllerTags.size() > 0)
100         {
101             Controller controller = new Controller(juncTag.id, simulator);
102 
103             for (ControllerTag controllerTag : juncTag.controllerTags.values())
104             {
105                 int sequence = controllerTag.sequence;
106                 String id = controllerTag.id;
107                 String signalId = openDriveNetworkLaneParser.controllerTags.get(id).controlSignalID;
108 
109                 // AbstractTrafficLight trafficLight = openDriveNetworkLaneParser.trafficLightsBySignals.get(signalId);
110 
111                 for (SimpleTrafficLight trafficLight : openDriveNetworkLaneParser.trafficLightsBySignals.get(signalId))
112                     controller.addTrafficLight(sequence, trafficLight);
113 
114                 /*
115                  * String refId = signalId + ".ref"; if(openDriveNetworkLaneParser.trafficLightsBySignals.containsKey(refId)) {
116                  * AbstractTrafficLight trafficLightRef = openDriveNetworkLaneParser.trafficLightsBySignals.get(refId);
117                  * controller.addTrafficLight(sequence, trafficLightRef); }
118                  */
119             }
120         }
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final String toString()
126     {
127         return "JunctionTag [name=" + this.name + ", id=" + this.id + ", connectionTags=" + this.connectionTags
128                 + ", controllerTags=" + this.controllerTags + "]";
129     }
130 
131 }