VissimANMNetworkLaneParser.java

  1. package org.opentrafficsim.road.network.factory.vissim;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;

  8. import javax.naming.NamingException;
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;

  12. import nl.tudelft.simulation.dsol.SimRuntimeException;

  13. import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
  14. import org.opentrafficsim.core.geometry.OTSGeometryException;
  15. import org.opentrafficsim.core.gtu.GTUException;
  16. import org.opentrafficsim.core.gtu.GTUType;
  17. import org.opentrafficsim.core.network.NetworkException;
  18. import org.opentrafficsim.core.network.OTSNetwork;
  19. import org.opentrafficsim.road.network.factory.XMLParser;
  20. import org.opentrafficsim.road.network.lane.LaneType;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Node;
  23. import org.w3c.dom.NodeList;
  24. import org.xml.sax.SAXException;

  25. /**
  26.  * <p>
  27.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  28.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  29.  * <p>
  30.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  31.  * initial version Jul 23, 2015 <br>
  32.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  33.  */
  34. public class VissimANMNetworkLaneParser
  35. {
  36.     // /** Global values from the GLOBAL tag. */
  37.     // @SuppressWarnings("visibilitymodifier")
  38.     // protected GlobalTag globalTag;
  39.     //
  40.     // /** The UNprocessed nodes for further reference. */
  41.     // @SuppressWarnings("visibilitymodifier")
  42.     // protected Map<String, NodeTag> nodeTags = new HashMap<>();
  43.     //
  44.     // /** The UNprocessed links for further reference. */
  45.     // @SuppressWarnings("visibilitymodifier")
  46.     // protected Map<String, LinkTag> linkTags = new HashMap<>();
  47.     //
  48.     // /** The gtu tags for further reference. */
  49.     // @SuppressWarnings("visibilitymodifier")
  50.     // protected Map<String, GTUTag> gtuTags = new HashMap<>();
  51.     //
  52.     // /** The gtumix tags for further reference. */
  53.     // @SuppressWarnings("visibilitymodifier")
  54.     // protected Map<String, GTUMixTag> gtuMixTags = new HashMap<>();
  55.     //
  56.     // /** The route tags for further reference. */
  57.     // @SuppressWarnings("visibilitymodifier")
  58.     // protected Map<String, RouteTag> routeTags = new HashMap<>();
  59.     //
  60.     // /** The route mix tags for further reference. */
  61.     // @SuppressWarnings("visibilitymodifier")
  62.     // protected Map<String, RouteMixTag> routeMixTags = new HashMap<>();
  63.     //
  64.     // /** The shortest route tags for further reference. */
  65.     // @SuppressWarnings("visibilitymodifier")
  66.     // protected Map<String, ShortestRouteTag> shortestRouteTags = new HashMap<>();
  67.     //
  68.     // /** The shortest route mix tags for further reference. */
  69.     // @SuppressWarnings("visibilitymodifier")
  70.     // protected Map<String, ShortestRouteMixTag> shortestRouteMixTags = new HashMap<>();
  71.     //
  72.     // /** The road type tags for further reference. */
  73.     // @SuppressWarnings("visibilitymodifier")
  74.     // protected Map<String, RoadTypeTag> roadTypeTags = new HashMap<>();

  75.     /** The GTUTypes that have been created. public to make it accessible from LaneAttributes. */
  76.     @SuppressWarnings("visibilitymodifier")
  77.     public Map<String, GTUType> gtuTypes = new HashMap<>();

  78.     /** The LaneTypes that have been created. */
  79.     @SuppressWarnings("visibilitymodifier")
  80.     protected Map<String, LaneType> laneTypes = new HashMap<>();

  81.     /** The no traffic LaneType. */
  82.     @SuppressWarnings("visibilitymodifier")
  83.     protected static LaneType noTrafficLaneType = new LaneType("NOTRAFFIC");

  84.     /** The simulator for creating the animation. Null if no animation needed. */
  85.     @SuppressWarnings("visibilitymodifier")
  86.     protected OTSDEVSSimulatorInterface simulator;

  87.     /**
  88.      * @param simulator the simulator for creating the animation. Null if no animation needed.
  89.      */
  90.     public VissimANMNetworkLaneParser(final OTSDEVSSimulatorInterface simulator)
  91.     {
  92.         this.simulator = simulator;
  93.         this.laneTypes.put(noTrafficLaneType.getId(), noTrafficLaneType);
  94.     }

  95.     /**
  96.      * @param url the file with the network in the agreed xml-grammar.
  97.      * @return the network with Nodes, Links, and Lanes.
  98.      * @throws NetworkException in case of parsing problems.
  99.      * @throws SAXException in case of parsing problems.
  100.      * @throws ParserConfigurationException in case of parsing problems.
  101.      * @throws IOException in case of file reading problems.
  102.      * @throws NamingException in case the animation context cannot be found
  103.      * @throws GTUException in case of a problem with creating the LaneBlock (which is a GTU right now)
  104.      * @throws OTSGeometryException when construction of a lane contour or offset design line fails
  105.      * @throws SimRuntimeException when simulator cannot be used to schedule GTU generation
  106.      */
  107.     @SuppressWarnings("checkstyle:needbraces")
  108.     public final OTSNetwork build(final URL url) throws NetworkException, ParserConfigurationException, SAXException,
  109.         IOException, NamingException, GTUException, OTSGeometryException, SimRuntimeException
  110.     {
  111.         if (url.getFile().length() > 0 && !(new File(url.getFile()).exists()))
  112.             throw new SAXException("XmlNetworkLaneParser.build: File url.getFile() does not exist");

  113.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  114.         factory.setNamespaceAware(true);
  115.         factory.setXIncludeAware(true);
  116.         DocumentBuilder builder = factory.newDocumentBuilder();
  117.         Document document = builder.parse(url.openStream());
  118.         NodeList networkNodeList = document.getDocumentElement().getChildNodes();

  119.         if (!document.getDocumentElement().getNodeName().equals("NETWORK"))
  120.             throw new SAXException(
  121.                 "XmlNetworkLaneParser.build: XML document does not start with an NETWORK tag, found "
  122.                     + document.getDocumentElement().getNodeName() + " instead");

  123.         // there should be some definitions using DEFINITIONS tags (could be more than one due to include files)
  124.         List<Node> definitionNodes = XMLParser.getNodes(networkNodeList, "DEFINITIONS");

  125.         if (definitionNodes.size() == 0)
  126.             throw new SAXException("XmlNetworkLaneParser.build: XML document does not have a DEFINITIONS tag");

  127.         // make the GTUTypes ALL and NONE to get started
  128.         this.gtuTypes.put("ALL", GTUType.ALL);
  129.         this.gtuTypes.put("NONE", GTUType.NONE);

  130.         // parse the DEFINITIONS tags
  131.         // for (Node definitionNode : definitionNodes)
  132.         // GlobalTag.parseGlobal(definitionNode.getChildNodes(), this);
  133.         // for (Node definitionNode : definitionNodes)
  134.         // GTUTag.parseGTUs(definitionNode.getChildNodes(), this);
  135.         // for (Node definitionNode : definitionNodes)
  136.         // GTUMixTag.parseGTUMix(definitionNode.getChildNodes(), this);
  137.         // for (Node definitionNode : definitionNodes)
  138.         // LaneTypeTag.parseLaneTypes(definitionNode.getChildNodes(), this);
  139.         // for (Node definitionNode : definitionNodes)
  140.         // CompatibilityTag.parseCompatibilities(definitionNode.getChildNodes(), this);
  141.         // for (Node definitionNode : definitionNodes)
  142.         // RoadTypeTag.parseRoadTypes(definitionNode.getChildNodes(), this);

  143.         // parse the NETWORK tag
  144.         // NodeTag.parseNodes(networkNodeList, this);
  145.         // RouteTag.parseRoutes(networkNodeList, this);
  146.         // ShortestRouteTag.parseShortestRoutes(networkNodeList, this);
  147.         // RouteMixTag.parseRouteMix(networkNodeList, this);
  148.         // ShortestRouteMixTag.parseShortestRouteMix(networkNodeList, this);
  149.         // LinkTag.parseLinks(networkNodeList, this);

  150.         // process nodes and links to calculate coordinates and positions
  151.         // Links.calculateNodeCoordinates(this);
  152.         // for (LinkTag linkTag : this.linkTags.values())
  153.         // Links.buildLink(linkTag, this, this.simulator);
  154.         // for (LinkTag linkTag : this.linkTags.values())
  155.         // Links.applyRoadTypeToLink(linkTag, this, this.simulator);

  156.         // process the routes
  157.         // for (RouteTag routeTag : this.routeTags.values())
  158.         // routeTag.makeRoute();
  159.         // TODO shortestRoute, routeMix, ShortestRouteMix

  160.         // store the structure information in the network
  161.         return makeNetwork(url.toString());
  162.     }

  163.     /**
  164.      * @param name the name of the network
  165.      * @return the OTSNetwork with the static information about the network
  166.      * @throws NetworkException if items cannot be added to the Network
  167.      */
  168.     private OTSNetwork makeNetwork(final String name) throws NetworkException
  169.     {
  170.         OTSNetwork network = new OTSNetwork(name);
  171.         // for (NodeTag nodeTag : this.nodeTags.values())
  172.         // {
  173.         // network.addNode(nodeTag.node);
  174.         // }
  175.         // for (LinkTag linkTag : this.linkTags.values())
  176.         // {
  177.         // network.addLink(linkTag.link);
  178.         // }
  179.         // for (RouteTag routeTag : this.routeTags.values())
  180.         // {
  181.         // // TODO Make routes GTU specific. See what to do with GTUType.ALL for routes
  182.         // network.addRoute(GTUType.ALL, routeTag.route);
  183.         // }
  184.         return network;
  185.     }

  186. }