Class Network

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

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

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

$LastChangedDate$, @version $Revision$, by $Author$, initial version Jul 22, 2015

Author:
Alexander Verbraeck, Peter Knoppers, Guus Tamminga
  • Field Details

    • EXTENT_MARGIN

      public static final double EXTENT_MARGIN
      Extra clearance around boundaries of network as fraction of width and height.
      See Also:
    • GTU_REMOVE_EVENT

      public static final EventType GTU_REMOVE_EVENT
      The timed event type for pub/sub indicating the removal of a GTU from the network.
      Payload: String gtuId (not an array, just a String)
    • GTU_ADD_EVENT

      public static final EventType GTU_ADD_EVENT
      The timed event type for pub/sub indicating the addition of a GTU to the network.
      Payload: String gtuId (not an array, just a String)
    • ROUTE_REMOVE_EVENT

      public static final EventType ROUTE_REMOVE_EVENT
      The timed event type for pub/sub indicating the removal of a Route for a gtuType.
      Payload: [String gtuTypeId, String routeId]
    • ROUTE_ADD_EVENT

      public static final EventType ROUTE_ADD_EVENT
      The timed event type for pub/sub indicating the addition of a Route for a gtuType.
      Payload: [String gtuTypeId, String routeId]
    • NONLOCATED_OBJECT_REMOVE_EVENT

      public static final EventType NONLOCATED_OBJECT_REMOVE_EVENT
      The timed event type for pub/sub indicating the removal of a NonLocatedObject implementing object.
      Payload: String objectId (not an array, just a String)
    • NONLOCATED_OBJECT_ADD_EVENT

      public static final EventType NONLOCATED_OBJECT_ADD_EVENT
      The timed event type for pub/sub indicating the addition of a NonLocatedObject implementing object.
      Payload: String ObjectId (not an array, just a String)
    • OBJECT_REMOVE_EVENT

      public static final EventType OBJECT_REMOVE_EVENT
      The timed event type for pub/sub indicating the removal of an ObjectInterface implementing object.
      Payload: String objectId (not an array, just a String)
    • OBJECT_ADD_EVENT

      public static final EventType OBJECT_ADD_EVENT
      The timed event type for pub/sub indicating the addition of an ObjectInterface implementing object.
      Payload: String ObjectId (not an array, just a String)
    • NODE_REMOVE_EVENT

      public static final EventType NODE_REMOVE_EVENT
      The timed event type for pub/sub indicating the removal of a Node.
      Payload: String nodeId (not an array, just a String)
    • NODE_ADD_EVENT

      public static final EventType NODE_ADD_EVENT
      The timed event type for pub/sub indicating the addition of a Node.
      Payload: String nodeId (not an array, just a String)
  • Constructor Details

    • Network

      public Network(String id, OtsSimulatorInterface simulator)
      Construction of an empty network.
      Parameters:
      id - the network id.
      simulator - the DSOL simulator engine
  • Method Details

    • getId

      public String getId()
      Description copied from interface: PerceivableContext
      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 PerceivableContext
      Returns:
      the id of the context
    • getSimulator

      public OtsSimulatorInterface getSimulator()
      Return the simulator.
      Returns:
      the simulator
    • getNodeMap

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

      public void addNode(Node node) throws NetworkException
      Register a node in the network.
      Parameters:
      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 void removeNode(Node node) throws NetworkException
      Unregister a node from the network.
      Parameters:
      node - the node to remove from the network.
      Throws:
      NetworkException - if node does not exist in the network.
    • containsNode

      public boolean containsNode(Node node)
      Test whether a node is present in the network.
      Parameters:
      node - the node to search for in the network.
      Returns:
      whether the node is in this network
    • containsNode

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

      public Optional<Node> getNode(String nodeId)
      Retrieve a node with a given id from the network, or null if the id cannot be found.
      Parameters:
      nodeId - the id of the node to search for in the network.
      Returns:
      the node, empty 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 - the GTU type for which to check the connectors
      Returns:
      a list of Centroid nodes that have incoming connectors without corresponding outgoing connectors to the same node or vice versa.
    • getLinkMap

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

      public void addLink(Link link) throws NetworkException
      Register a link in the network.
      Parameters:
      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 void removeLink(Link link) throws NetworkException
      Unregister a link from the network.
      Parameters:
      link - the link to remove from the network.
      Throws:
      NetworkException - if link does not exist in the network.
    • getLink

      public Optional<Link> getLink(Node node1, Node node2)
      Find a link between node1 and node2 and return it if it exists in the network.
      Parameters:
      node1 - first node
      node2 - second node
      Returns:
      the link between node1 and node2 in the network, empty if it does not exist.
    • containsLink

      public boolean containsLink(Link link)
      Test whether a link is present in the network.
      Parameters:
      link - the link to search for in the network.
      Returns:
      whether the link is in this network
    • containsLink

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

      public Optional<Link> getLink(String nodeId1, String nodeId2) throws NetworkException
      Find a link between node1 and node2 and return it if it exists in the network.
      Parameters:
      nodeId1 - id of the first node
      nodeId2 - id of the second node
      Returns:
      the link between node1 and node2 in the network, empty if it does not exist.
      Throws:
      NetworkException - if the node(s) cannot be found by their id
    • getLink

      public Optional<Link> getLink(String linkId)
      Retrieve a node with a given id from the network, or null if the id cannot be found.
      Parameters:
      linkId - the id of the link to search for in the network.
      Returns:
      the link, empty if not present
    • getObjectMap

      public ImmutableMap<String,LocatedObject> getObjectMap()
      Return an immutable map of all ObjectInterface implementing objects in the Network.
      Returns:
      the immutable map of all ObjectInterface implementing objects in the Network
    • getObjectMap

      public <T extends LocatedObject> 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.
      Type Parameters:
      T - type of object
      Parameters:
      objectType - the (sub-)type of ObjectInterface that the returned map is reduced to
      Returns:
      the immutable map of all ObjectInterface implementing objects in the Network that are of the type objectType, or any sub type thereof
    • getObject

      public <T extends LocatedObject> Optional<T> getObject(Class<T> objectType, String objectId)
      Return object of given type with given id.
      Type Parameters:
      T - object type
      Parameters:
      objectType - object type class
      objectId - id of object
      Returns:
      object of given type with given id, empty if no such object
    • addObject

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

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

      public boolean containsObject(LocatedObject object)
      Test whether the object is present in the Network.
      Parameters:
      object - the object that is tested for presence
      Returns:
      whether the object is present in the Network
    • containsObject

      public 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.

      Parameters:
      objectId - the id that is tested for presence
      Returns:
      whether an object with the given id is present in the Network
    • getNonLocatedObjectMap

      public ImmutableMap<String,NonLocatedObject> getNonLocatedObjectMap()
      Return an immutable map of all NonLocatedObject implementing objects in the Network.
      Returns:
      the immutable map of all NonLocatedObject implementing objects in the Network
    • getNonLocatedObjectMap

      public ImmutableMap<String,NonLocatedObject> getNonLocatedObjectMap(Class<NonLocatedObject> objectType)
      Return an immutable map of all NonLocatedObject implementing objects in the network that are of type objectType, or any sub type thereof.
      Parameters:
      objectType - the (sub-)type of NonLocatedObject that the returned map is reduced to
      Returns:
      the immutable map of all NonLocatedObject implementing objects in the Network that are of the type objectType, or any sub type thereof
    • addNonLocatedObject

      public void addNonLocatedObject(NonLocatedObject object) throws NetworkException
      Add a NonLocatedObject implementing object to the Network.
      Parameters:
      object - the object that implements ObjectInterface
      Throws:
      NetworkException - if link already exists in the network, if name of the object is not unique.
    • removeNonLocatedObject

      public void removeNonLocatedObject(NonLocatedObject object) throws NetworkException
      Remove a NonLocatedObject implementing object form the Network.
      Parameters:
      object - the object that implements ObjectInterface
      Throws:
      NetworkException - if the object does not exist in the network.
    • containsNonLocatedObject

      public boolean containsNonLocatedObject(NonLocatedObject object)
      Test whether the NonLocatedObject is present in the Network.
      Parameters:
      object - the object that is tested for presence
      Returns:
      whether the invisible object is present in the Network
    • containsNonLocatedObject

      public boolean containsNonLocatedObject(String objectId)
      Test whether an NonLocatedObject 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.

      Parameters:
      objectId - the id that is tested for presence
      Returns:
      whether an invisible object with the given id is present in the Network
    • getDefinedRouteMap

      public ImmutableMap<String,Route> getDefinedRouteMap(GtuType gtuType)
      Return an immutable map of routes that exist in the network for the GtuType.
      Parameters:
      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 void addRoute(GtuType gtuType, Route route) throws NetworkException
      Add a route to the network.
      Parameters:
      gtuType - the GtuType for which to add a 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 void removeRoute(GtuType gtuType, Route route) throws NetworkException
      Remove the route from the network, e.g. because of road maintenance.
      Parameters:
      gtuType - the GtuType for which to remove a route
      route - the route to remove from the network.
      Throws:
      NetworkException - if route does not exist in the network.
    • containsRoute

      public boolean containsRoute(GtuType gtuType, Route route)
      Determine whether the provided route exists in the network for the given GtuType.
      Parameters:
      gtuType - the GtuType for which to check whether the route exists
      route - the route to check for
      Returns:
      whether the route exists in the network for the given GtuType
    • containsRoute

      public boolean containsRoute(GtuType gtuType, String routeId)
      Determine whether a route with the given id exists in the network for the given GtuType.
      Parameters:
      gtuType - the GtuType for which to check whether the route exists
      routeId - 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 Optional<Route> getRoute(String routeId)
      Returns the route with given id, for any GTU type for which it is given.
      Parameters:
      routeId - route id
      Returns:
      route with given id, empty if no such route is available
    • getRoute

      public Optional<Route> getRoute(GtuType gtuType, String routeId)
      Return the route with the given id in the network for the given GTU type.
      Parameters:
      gtuType - the GTU type for which to retrieve a route based on its id.
      routeId - the route to search for in the network.
      Returns:
      the route, empty if not present
    • getRoutesBetween

      public 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.
      Parameters:
      gtuType - the GtuType for which to retrieve the defined routes
      nodeFrom - the start node.
      nodeTo - the end node.
      Returns:
      if no route can be found, an empty set is returned.
    • getShortestRouteBetween

      public Route getShortestRouteBetween(GtuType gtuType, Node nodeFrom, Node nodeTo) 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. In case the graph for the GtuType has not yet been built, this method will call the buildGraph method.
      Parameters:
      gtuType - the GtuType for which to calculate the shortest route
      nodeFrom - the start node.
      nodeTo - the end node.
      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 Route 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.
      Parameters:
      gtuType - the GtuType for which to calculate the shortest route
      nodeFrom - the start node.
      nodeTo - the end node.
      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 Route 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. This method recalculates the graph.
      Parameters:
      gtuType - the GtuType for which to calculate the shortest route
      nodeFrom - the start node.
      nodeTo - the end node.
      nodesVia - 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 Route 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.
      Parameters:
      gtuType - the GtuType for which to calculate the shortest route
      nodeFrom - the start node.
      nodeTo - the end node.
      nodesVia - a number of nodes that the GTU has to pass between nodeFrom and nodeTo in the given order.
      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.
      IllegalArgumentException - if the from and to node are equal
    • getRouteMap

      public ImmutableMap<GtuType,Map<String,Route>> getRouteMap()
      Get route map.
      Returns:
      a defensive copy of the routeMap.
    • setRawRouteMap

      public void setRawRouteMap(Map<GtuType,Map<String,Route>> newRouteMap)
      Set route map.
      Parameters:
      newRouteMap - the routeMap to set, only to be used in the 'network' package for cloning.
    • addGTU

      public void addGTU(Gtu gtu)
      Description copied from interface: PerceivableContext
      Add a GTU to the network.
      Specified by:
      addGTU in interface PerceivableContext
      Parameters:
      gtu - the GTU to add
    • removeGTU

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

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

      public Optional<Gtu> getGTU(String gtuId)
      Description copied from interface: PerceivableContext
      Get a GTU in the model.
      Specified by:
      getGTU in interface PerceivableContext
      Parameters:
      gtuId - the id of the GTU
      Returns:
      a GTU as registered in the current model, empty when the id could not be found.
    • getGTUs

      public Set<Gtu> getGTUs()
      Description copied from interface: PerceivableContext
      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 boolean containsGtuId(String gtuId)
      Description copied from interface: PerceivableContext
      Test whether a GTU ID is registered in the network.
      Specified by:
      containsGtuId in interface PerceivableContext
      Parameters:
      gtuId - 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
    • toString

      public String toString()
      Overrides:
      toString in class Object