Class OTSNetwork

java.lang.Object
org.djutils.event.EventProducer
org.opentrafficsim.core.network.OTSNetwork
All Implemented Interfaces:
Serializable, EventProducerInterface, Identifiable, Definitions, Network, PerceivableContext
Direct Known Subclasses:
ExpansionNetwork

public class OTSNetwork
extends EventProducer
implements Network, PerceivableContext, Serializable
A Network consists of a set of links. Each link has, in its turn, a start node and an end node.

Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

$LastChangedDate: 2020-02-04 16:40:30 +0100 (Tue, 04 Feb 2020) $, @version $Revision: 6252 $, by $Author: pknoppers $, initial version Jul 22, 2015

Author:
Alexander Verbraeck, Peter Knoppers, Guus Tamminga
See Also:
Serialized Form
  • Field Details

  • Constructor Details

    • OTSNetwork

      public OTSNetwork​(String id, boolean addDefaultTypes)
      Construction of an empty network.
      Parameters:
      id - String; the network id.
      addDefaultTypes - add the default GTUTypes and LinkTypes, or not
  • Method Details

    • getId

      public final String getId()
      Get a descriptive Id of the perceivable context (e.g., useful for debugging purposes).
      Specified by:
      getId in interface Identifiable
      Specified by:
      getId in interface Network
      Specified by:
      getId in interface PerceivableContext
      Returns:
      String; the id
    • getNodeMap

      public final ImmutableMap<String,​Node> getNodeMap()
      Provide an immutable map of node ids to nodes in the network.
      Specified by:
      getNodeMap in interface Network
      Returns:
      an immutable map of nodes.
    • addNode

      public final void addNode​(Node node) throws NetworkException
      Register a node in the network.
      Specified by:
      addNode in interface Network
      Parameters:
      node - Node; the node to add to the network.
      Throws:
      NetworkException - if node already exists in the network, or if name of the node is not unique.
    • removeNode

      public final void removeNode​(Node node) throws NetworkException
      Unregister a node from the network.
      Specified by:
      removeNode in interface Network
      Parameters:
      node - Node; the node to remove from the network.
      Throws:
      NetworkException - if node does not exist in the network.
    • containsNode

      public final boolean containsNode​(Node node)
      Test whether a node is present in the network.
      Specified by:
      containsNode in interface Network
      Parameters:
      node - Node; the node to search for in the network.
      Returns:
      whether the node is in this network
    • containsNode

      public final boolean containsNode​(String nodeId)
      Test whether a node with a given id is present in the network.
      Specified by:
      containsNode in interface Network
      Parameters:
      nodeId - String; the id of the node to search for in the network.
      Returns:
      whether the node is in this network
    • getNode

      public final Node getNode​(String nodeId)
      Retrieve a node with a given id from the network, or null if the id cannot be found.
      Specified by:
      getNode in interface Network
      Parameters:
      nodeId - String; the id of the node to search for in the network.
      Returns:
      the node or null if not present
    • getUnbalancedCentroids

      public List<Node> getUnbalancedCentroids​(GTUType gtuType)
      Return a list of Centroid nodes that have incoming connectors without corresponding outgoing connectors to the same node or vice versa (which can be fully okay, especially when the lanes are a dead end, or when lanes / links only go in a single direction).
      Parameters:
      gtuType - GTUType; the GTU type for which to check the connectors
      Returns:
      List<Node>; a list of Centroid nodes that have incoming connectors without corresponding outgoing connectors to the same node or vice versa.
    • getLinkMap

      public final ImmutableMap<String,​Link> getLinkMap()
      Provide an immutable map of link ids to links in the network.
      Specified by:
      getLinkMap in interface Network
      Returns:
      the an immutable map of links.
    • addLink

      public final void addLink​(Link link) throws NetworkException
      Register a link in the network.
      Specified by:
      addLink in interface Network
      Parameters:
      link - Link; the link to add to the network.
      Throws:
      NetworkException - if link already exists in the network, if name of the link is not unique, or if the start node or the end node of the link are not registered in the network.
    • removeLink

      public final void removeLink​(Link link) throws NetworkException
      Unregister a link from the network.
      Specified by:
      removeLink in interface Network
      Parameters:
      link - Link; the link to remove from the network.
      Throws:
      NetworkException - if link does not exist in the network.
    • getLink

      public final Link getLink​(Node node1, Node node2)
      Find a link between node1 and node2 and return it if it exists in the network. If not, return null.
      Specified by:
      getLink in interface Network
      Parameters:
      node1 - Node; first node
      node2 - Node; second node
      Returns:
      the link between node1 and node2 in the network or null if it does not exist.
    • getLink

      public final Link getLink​(String nodeId1, String nodeId2) throws NetworkException
      Find a link between node1 and node2 and return it if it exists in the network. If not, return null.
      Specified by:
      getLink in interface Network
      Parameters:
      nodeId1 - String; id of the first node
      nodeId2 - String; id of the second node
      Returns:
      the link between node1 and node2 in the network or null if it does not exist.
      Throws:
      NetworkException - if the node(s) cannot be found by their id
    • containsLink

      public final boolean containsLink​(Link link)
      Test whether a link is present in the network.
      Specified by:
      containsLink in interface Network
      Parameters:
      link - Link; the link to search for in the network.
      Returns:
      whether the link is in this network
    • containsLink

      public final boolean containsLink​(String linkId)
      Test whether a link with a given id is present in the network.
      Specified by:
      containsLink in interface Network
      Parameters:
      linkId - String; the id of the link to search for in the network.
      Returns:
      whether the link is in this network
    • getLink

      public final Link getLink​(String linkId)
      Retrieve a node with a given id from the network, or null if the id cannot be found.
      Specified by:
      getLink in interface Network
      Parameters:
      linkId - String; the id of the link to search for in the network.
      Returns:
      the link or null if not present
    • getObjectMap

      public final ImmutableMap<String,​ObjectInterface> getObjectMap()
      Return an immutable map of all ObjectInterface implementing objects in the Network.
      Specified by:
      getObjectMap in interface Network
      Returns:
      ImmutableMap<String, ObjectInterface>; the immutable map of all ObjectInterface implementing objects in the Network
    • getObjectMap

      public final <T extends ObjectInterface> ImmutableMap<String,​T> getObjectMap​(Class<T> objectType)
      Return an immutable map of all ObjectInterface implementing objects in the network that are of type objectType, or any sub type thereof.
      Specified by:
      getObjectMap in interface Network
      Type Parameters:
      T - type of object
      Parameters:
      objectType - Class<T>; the (sub-)type of ObjectInterface that the returned map is reduced to
      Returns:
      ImmutableMap<String, ObjectInterface>; the immutable map of all ObjectInterface implementing objects in the Network that are of the type objectType, or any sub type thereof
    • getObject

      public final <T extends ObjectInterface> T getObject​(Class<T> objectType, String objectId)
      Return object of given type with given id.
      Specified by:
      getObject in interface Network
      Type Parameters:
      T - object type
      Parameters:
      objectType - T; object type class
      objectId - String; id of object
      Returns:
      T; object of given type with given id, null if no such object
    • addObject

      public final void addObject​(ObjectInterface object) throws NetworkException
      Add an ObjectInterface implementing object to the Network.
      Specified by:
      addObject in interface Network
      Parameters:
      object - ObjectInterface; the object that implements ObjectInterface
      Throws:
      NetworkException - if link already exists in the network, if name of the object is not unique.
    • removeObject

      public final void removeObject​(ObjectInterface object) throws NetworkException
      Remove an ObjectInterface implementing object form the Network.
      Specified by:
      removeObject in interface Network
      Parameters:
      object - ObjectInterface; the object that implements ObjectInterface
      Throws:
      NetworkException - if the object does not exist in the network.
    • containsObject

      public final boolean containsObject​(ObjectInterface object)
      Test whether the object is present in the Network.
      Specified by:
      containsObject in interface Network
      Parameters:
      object - ObjectInterface; the object that is tested for presence
      Returns:
      boolean; whether the object is present in the Network
    • containsObject

      public final boolean containsObject​(String objectId)
      Test whether an object with the given id is present in the Network.

      Note that the objectId should be the fullId of the object, including any additions such as lane ids, link ids, etc.

      Specified by:
      containsObject in interface Network
      Parameters:
      objectId - String; the id that is tested for presence
      Returns:
      boolean; whether an object with the given id is present in the Network
    • getInvisibleObjectMap

      public final ImmutableMap<String,​InvisibleObjectInterface> getInvisibleObjectMap()
      Return an immutable map of all InvisibleObject implementing objects in the Network.
      Specified by:
      getInvisibleObjectMap in interface Network
      Returns:
      ImmutableMap<String, ObjectInterface>; the immutable map of all ObjectInterface implementing objects in the Network
    • getInvisibleObjectMap

      public final ImmutableMap<String,​InvisibleObjectInterface> getInvisibleObjectMap​(Class<InvisibleObjectInterface> objectType)
      Return an immutable map of all InvisibleObject implementing objects in the network that are of type objectType, or any sub type thereof.
      Specified by:
      getInvisibleObjectMap in interface Network
      Parameters:
      objectType - Class<InvisibleObjectInterface>; the (sub-)type of InvisibleObject that the returned map is reduced to
      Returns:
      ImmutableMap<String, InvisibleObject>; the immutable map of all InvisibleObject implementing objects in the Network that are of the type objectType, or any sub type thereof
    • addInvisibleObject

      public final void addInvisibleObject​(InvisibleObjectInterface object) throws NetworkException
      Add an ObjectInterface implementing object to the Network.
      Specified by:
      addInvisibleObject in interface Network
      Parameters:
      object - InvisibleObjectInterface; the object that implements ObjectInterface
      Throws:
      NetworkException - if link already exists in the network, if name of the object is not unique.
    • removeInvisibleObject

      public final void removeInvisibleObject​(InvisibleObjectInterface object) throws NetworkException
      Remove an ObjectInterface implementing object form the Network.
      Specified by:
      removeInvisibleObject in interface Network
      Parameters:
      object - InvisibleObjectInterface; the object that implements ObjectInterface
      Throws:
      NetworkException - if the object does not exist in the network.
    • containsInvisibleObject

      public final boolean containsInvisibleObject​(InvisibleObjectInterface object)
      Test whether the invisible object is present in the Network.
      Specified by:
      containsInvisibleObject in interface Network
      Parameters:
      object - InvisibleObjectInterface; the object that is tested for presence
      Returns:
      boolean; whether the invisible object is present in the Network
    • containsInvisibleObject

      public final boolean containsInvisibleObject​(String objectId)
      Test whether an invisible object with the given id is present in the Network.

      Note that the objectId should be the fullId of the object, including any additions such as lane ids, link ids, etc.

      Specified by:
      containsInvisibleObject in interface Network
      Parameters:
      objectId - String; the id that is tested for presence
      Returns:
      boolean; whether an invisible object with the given id is present in the Network
    • getDefinedRouteMap

      public final ImmutableMap<String,​Route> getDefinedRouteMap​(GTUType gtuType)
      Return an immutable map of routes that exist in the network for the GTUType.
      Specified by:
      getDefinedRouteMap in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to retrieve the defined routes
      Returns:
      an immutable map of routes in the network for the given GTUType, or an empty Map if no routes are defined for the given GTUType.
    • addRoute

      public final void addRoute​(GTUType gtuType, Route route) throws NetworkException
      Add a route to the network.
      Specified by:
      addRoute in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to add a route
      route - Route; the route to add to the network.
      Throws:
      NetworkException - if route already exists in the network, if name of the route is not unique, if one of the nodes of the route are not registered in the network.
    • removeRoute

      public final void removeRoute​(GTUType gtuType, Route route) throws NetworkException
      Remove the route from the network, e.g. because of road maintenance.
      Specified by:
      removeRoute in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to remove a route
      route - Route; the route to remove from the network.
      Throws:
      NetworkException - if route does not exist in the network.
    • containsRoute

      public final boolean containsRoute​(GTUType gtuType, Route route)
      Determine whether the provided route exists in the network for the given GTUType.
      Specified by:
      containsRoute in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to check whether the route exists
      route - Route; the route to check for
      Returns:
      whether the route exists in the network for the given GTUType
    • containsRoute

      public final boolean containsRoute​(GTUType gtuType, String routeId)
      Determine whether a route with the given id exists in the network for the given GTUType.
      Specified by:
      containsRoute in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to check whether the route exists
      routeId - String; the id of the route to check for
      Returns:
      whether a route with the given id exists in the network for the given GTUType
    • getRoute

      public final Route getRoute​(String routeId)
      Returns the route with given id or null if no such route is available.
      Parameters:
      routeId - String; route id
      Returns:
      route with given id or null if no such route is available
    • getRoute

      public final Route getRoute​(GTUType gtuType, String routeId)
      Return the route with the given id in the network for the given GTUType, or null if it the route with the id does not exist.
      Specified by:
      getRoute in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to retrieve a route based on its id.
      routeId - String; the route to search for in the network.
      Returns:
      the route or null if not present
    • getRoutesBetween

      public final Set<Route> getRoutesBetween​(GTUType gtuType, Node nodeFrom, Node nodeTo)
      Return the the shortest route between two nodes in the network, via a list of intermediate nodes. If no path exists from the start node to the end node via the intermediate nodes in the network, null is returned.
      Specified by:
      getRoutesBetween in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to retrieve the defined routes
      nodeFrom - Node; the start node.
      nodeTo - Node; the end node.
      Returns:
      a set with routes from the start Node to the end Node in the network; if no route can be found, an empty set is returned.
    • buildGraph

      public final void buildGraph​(GTUType gtuType)
      (Re)build the underlying graph for the given GTUType.
      Specified by:
      buildGraph in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to (re)build the graph
    • getShortestRouteBetween

      public final CompleteRoute getShortestRouteBetween​(GTUType gtuType, Node nodeFrom, Node nodeTo, LinkWeight linkWeight) throws NetworkException
      Calculate the shortest route between two nodes in the network. If no path exists from the start node to the end node in the network, null is returned. This method returns a CompleteRoute, which includes all nodes to get from start to end. This method recalculates the graph.
      Specified by:
      getShortestRouteBetween in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to calculate the shortest route
      nodeFrom - Node; the start node.
      nodeTo - Node; the end node.
      linkWeight - LinkWeight; link weight.
      Returns:
      the shortest route from the start Node to the end Node in the network. If no path exists from the start node to the end node in the network, null is returned.
      Throws:
      NetworkException - in case nodes cannot be added to the route, e.g. because they are not directly connected. This can be the case when the links in the network have changed, but the graph has not been rebuilt.
    • getShortestRouteBetween

      public final CompleteRoute getShortestRouteBetween​(GTUType gtuType, Node nodeFrom, Node nodeTo, List<Node> nodesVia) throws NetworkException
      Calculate the shortest route between two nodes in the network, via a list of intermediate nodes. If no path exists from the start node to the end node via the intermediate nodes in the network, null is returned. This method returns a CompleteRoute, which includes all nodes to get from start to end. In case the graph for the GTUType has not yet been built, this method will call the buildGraph method.
      Specified by:
      getShortestRouteBetween in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to calculate the shortest route
      nodeFrom - Node; the start node.
      nodeTo - Node; the end node.
      nodesVia - List<Node>; a number of nodes that the GTU has to pass between nodeFrom and nodeTo in the given order.
      Returns:
      the shortest route between two nodes in the network, via the intermediate nodes. If no path exists from the start node to the end node via the intermediate nodes in the network, null is returned.
      Throws:
      NetworkException - in case nodes cannot be added to the route, e.g. because they are not directly connected. This can be the case when the links in the network have changed, but the graph has not been rebuilt.
    • getShortestRouteBetween

      public final CompleteRoute getShortestRouteBetween​(GTUType gtuType, Node nodeFrom, Node nodeTo, List<Node> nodesVia, LinkWeight linkWeight) throws NetworkException
      Calculate the shortest route between two nodes in the network, via a list of intermediate nodes. If no path exists from the start node to the end node via the intermediate nodes in the network, null is returned. This method returns a CompleteRoute, which includes all nodes to get from start to end. This method recalculates the graph.
      Specified by:
      getShortestRouteBetween in interface Network
      Parameters:
      gtuType - GTUType; the GTUType for which to calculate the shortest route
      nodeFrom - Node; the start node.
      nodeTo - Node; the end node.
      nodesVia - List<Node>; a number of nodes that the GTU has to pass between nodeFrom and nodeTo in the given order.
      linkWeight - LinkWeight; link weight.
      Returns:
      the shortest route between two nodes in the network, via the intermediate nodes. If no path exists from the start node to the end node via the intermediate nodes in the network, null is returned.
      Throws:
      NetworkException - in case nodes cannot be added to the route, e.g. because they are not directly connected. This can be the case when the links in the network have changed, but the graph has not been rebuilt.
    • getRouteMap

      public final ImmutableMap<GTUType,​Map<String,​Route>> getRouteMap()
      Returns:
      a defensive copy of the routeMap.
    • setRawRouteMap

      public final void setRawRouteMap​(Map<GTUType,​Map<String,​Route>> newRouteMap)
      Parameters:
      newRouteMap - Map<GTUType,Map<String,Route>>; the routeMap to set, only to be used in the 'network' package for cloning.
    • getLinkGraphs

      public final ImmutableMap<GTUType,​SimpleDirectedWeightedGraph<Node,​LinkEdge<Link>>> getLinkGraphs()
      Returns:
      linkGraphs; only to be used in the 'network' package for cloning.
    • addDefaultLinkTypes

      public void addDefaultLinkTypes()
      Add the default LinkTypes that have been defined in the enum LinkType.DEFAULTS to the network. It is not necessary to call this method on every network; when the LinkTypes are for instance defined in an XML file, adding the default types might not be needed.
      Specified by:
      addDefaultLinkTypes in interface Definitions
    • addLinkType

      public void addLinkType​(LinkType linkType)
      Add a Link type to the map. This method is automatically called from the LinkType constructor.
      Specified by:
      addLinkType in interface Definitions
      Parameters:
      linkType - the LinkType to add
    • getLinkType

      public LinkType getLinkType​(String linkId)
      Retrieve a defined LinkType based on its id.
      Specified by:
      getLinkType in interface Definitions
      Parameters:
      linkId - the id to search for
      Returns:
      the LinkType or null in case it could not be found
    • getLinkType

      public LinkType getLinkType​(LinkType.DEFAULTS linkEnum)
      Retrieve a defined default LinkType based on its enum.
      Specified by:
      getLinkType in interface Definitions
      Parameters:
      linkEnum - the enum to search for
      Returns:
      the LinkType or null in case it could not be found
    • getLinkTypes

      public ImmutableMap<String,​LinkType> getLinkTypes()
      Retrieve a safe copy of the map of defined LinkTypes in this network.
      Specified by:
      getLinkTypes in interface Definitions
      Returns:
      the map of defined LinkTypes
    • addDefaultGtuTypes

      public void addDefaultGtuTypes()
      Add the default GTU Types that have been defined in the enum GTUType.DEFAULTS to the network. It is not necessary to call this method on every network; when the GTUTypes are for instance defined in an XML file, adding the default types might not be needed.
      Specified by:
      addDefaultGtuTypes in interface Definitions
    • addGtuType

      public void addGtuType​(GTUType gtuType)
      Add a GTU type to the map. This method is automatically called from the GTUType constructor.
      Specified by:
      addGtuType in interface Definitions
      Parameters:
      gtuType - the GTUType to add
    • getGtuType

      public GTUType getGtuType​(String gtuId)
      Retrieve a defined GTUType based on its id.
      Specified by:
      getGtuType in interface Definitions
      Parameters:
      gtuId - the id to search for
      Returns:
      the GTUType or null in case it could not be found
    • getGtuType

      public GTUType getGtuType​(GTUType.DEFAULTS gtuEnum)
      Retrieve a defined default GTUType based on its enum.
      Specified by:
      getGtuType in interface Definitions
      Parameters:
      gtuEnum - the enum to search for
      Returns:
      the GTUType or null in case it could not be found
    • getGtuTypes

      public ImmutableMap<String,​GTUType> getGtuTypes()
      Retrieve a safe copy of the map of defined GTUTypes in this network.
      Specified by:
      getGtuTypes in interface Definitions
      Returns:
      the map of defined GTUTypes
    • addGTU

      public final void addGTU​(GTU gtu)
      Add a GTU to the network.
      Specified by:
      addGTU in interface PerceivableContext
      Parameters:
      gtu - GTU; the GTU to add
    • removeGTU

      public final void removeGTU​(GTU gtu)
      Remove a GTU from the network.
      Specified by:
      removeGTU in interface PerceivableContext
      Parameters:
      gtu - GTU; the GTU to remove
    • containsGTU

      public final boolean containsGTU​(GTU gtu)
      Test whether a GTU is registered in the network.
      Specified by:
      containsGTU in interface PerceivableContext
      Parameters:
      gtu - GTU; the GTU to search for
      Returns:
      whether the network contains this GTU
    • getGTU

      public final GTU getGTU​(String gtuId)
      Get a GTU in the model.
      Specified by:
      getGTU in interface PerceivableContext
      Parameters:
      gtuId - String; the id of the GTU
      Returns:
      a GTU as registered in the current model, or null when the id could not be found.
    • getGTUs

      public final Set<GTU> getGTUs()
      Get an overview of the GTUs in the model. The set returned is a defensive copy.
      Specified by:
      getGTUs in interface PerceivableContext
      Returns:
      a set of GTUs as registered in the current model.
    • containsGtuId

      public final boolean containsGtuId​(String gtuId)
      Test whether a GTU ID is registered in the network.
      Specified by:
      containsGtuId in interface PerceivableContext
      Parameters:
      gtuId - String; the GTU ID to search for
      Returns:
      whether the network contains a GTU with this ID
    • getExtent

      public Rectangle2D.Double getExtent()
      Calculate the extent of the network based on the network objects' locations and return the dimensions.
      Returns:
      Rectangle2D.Double; the extent of the network
    • getSourceId

      public Serializable getSourceId()
      Specified by:
      getSourceId in interface EventProducerInterface
      Specified by:
      getSourceId in class EventProducer
    • toString

      public final String toString()
      Overrides:
      toString in class Object