Package org.opentrafficsim.core.network
Class Network
java.lang.Object
org.djutils.event.LocalEventProducer
org.opentrafficsim.core.network.Network
- All Implemented Interfaces:
Identifiable,EventProducer,PerceivableContext
- Direct Known Subclasses:
ExpansionNetwork
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleExtra clearance around boundaries of network as fraction of width and height.static final EventTypeThe timed event type for pub/sub indicating the addition of a GTU to the network.static final EventTypeThe timed event type for pub/sub indicating the removal of a GTU from the network.static final EventTypeThe timed event type for pub/sub indicating the addition of a Link.static final EventTypeThe timed event type for pub/sub indicating the removal of a Link.static final EventTypeThe timed event type for pub/sub indicating the addition of a Node.static final EventTypeThe timed event type for pub/sub indicating the removal of a Node.static final EventTypeThe timed event type for pub/sub indicating the addition of a NonLocatedObject implementing object.static final EventTypeThe timed event type for pub/sub indicating the removal of a NonLocatedObject implementing object.static final EventTypeThe timed event type for pub/sub indicating the addition of an ObjectInterface implementing object.static final EventTypeThe timed event type for pub/sub indicating the removal of an ObjectInterface implementing object.static final EventTypeThe timed event type for pub/sub indicating the addition of a Route for a gtuType.static final EventTypeThe timed event type for pub/sub indicating the removal of a Route for a gtuType.Fields inherited from interface org.djutils.event.EventProducer
FIRST_POSITION, LAST_POSITION -
Constructor Summary
ConstructorsConstructorDescriptionNetwork(String id, OtsSimulatorInterface simulator) Construction of an empty network. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a GTU to the network.voidRegister a link in the network.voidRegister a node in the network.voidaddNonLocatedObject(NonLocatedObject object) Add a NonLocatedObject implementing object to the Network.voidaddObject(LocatedObject object) Add an ObjectInterface implementing object to the Network.voidAdd a route to the network.booleancontainsGTU(Gtu gtu) Test whether a GTU is registered in the network.booleancontainsGtuId(String gtuId) Test whether a GTU ID is registered in the network.booleancontainsLink(String linkId) Test whether a link with a given id is present in the network.booleancontainsLink(Link link) Test whether a link is present in the network.booleancontainsNode(String nodeId) Test whether a node with a given id is present in the network.booleancontainsNode(Node node) Test whether a node is present in the network.booleancontainsNonLocatedObject(String objectId) Test whether an NonLocatedObject object with the given id is present in the Network.booleanTest whether the NonLocatedObject is present in the Network.booleancontainsObject(String objectId) Test whether an object with the given id is present in the Network.booleancontainsObject(LocatedObject object) Test whether the object is present in the Network.booleancontainsRoute(GtuType gtuType, String routeId) Determine whether a route with the given id exists in the network for the given GtuType.booleancontainsRoute(GtuType gtuType, Route route) Determine whether the provided route exists in the network for the given GtuType.getDefinedRouteMap(GtuType gtuType) Return an immutable map of routes that exist in the network for the GtuType.Calculate the extent of the network based on the network objects' locations and return the dimensions.Get a GTU in the model.getGTUs()Get an overview of the GTUs in the model.getId()Get a descriptive Id of the perceivable context (e.g., useful for debugging purposes).Retrieve a node with a given id from the network, or null if the id cannot be found.Find a link between node1 and node2 and return it if it exists in the network.Find a link between node1 and node2 and return it if it exists in the network.Provide an immutable map of link ids to links in the network.Retrieve a node with a given id from the network, or null if the id cannot be found.Provide an immutable map of node ids to nodes in the network.Return an immutable map of all NonLocatedObject implementing objects in the Network.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.<T extends LocatedObject>
Optional<T>Return object of given type with given id.Return an immutable map of all ObjectInterface implementing objects in the Network.<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.Returns the route with given id, for any GTU type for which it is given.Return the route with the given id in the network for the given GTU type.Get route map.getRoutesBetween(GtuType gtuType, Node nodeFrom, Node nodeTo) Return the the shortest route between two nodes in the network, via a list of intermediate nodes.getShortestRouteBetween(GtuType gtuType, Node nodeFrom, Node nodeTo) Calculate the shortest route between two nodes in the network.Calculate the shortest route between two nodes in the network, via a list of intermediate nodes.getShortestRouteBetween(GtuType gtuType, Node nodeFrom, Node nodeTo, List<Node> nodesVia, LinkWeight linkWeight) Calculate the shortest route between two nodes in the network, via a list of intermediate nodes.getShortestRouteBetween(GtuType gtuType, Node nodeFrom, Node nodeTo, LinkWeight linkWeight) Calculate the shortest route between two nodes in the network.Return the simulator.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).voidRemove a GTU from the network.voidremoveLink(Link link) Unregister a link from the network.voidremoveNode(Node node) Unregister a node from the network.voidRemove a NonLocatedObject implementing object form the Network.voidremoveObject(LocatedObject object) Remove an ObjectInterface implementing object form the Network.voidremoveRoute(GtuType gtuType, Route route) Remove the route from the network, e.g. because of road maintenance.voidSet route map.toString()Methods inherited from class org.djutils.event.LocalEventProducer
getEventListenerMapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djutils.event.EventProducer
addListener, addListener, addListener, addListener, fireEvent, fireEvent, fireEvent, fireTimedEvent, fireTimedEvent, fireTimedEvent, fireUnverifiedEvent, fireUnverifiedEvent, fireUnverifiedTimedEvent, fireUnverifiedTimedEvent, getEventListenerMap, getEventTypesWithListeners, getListenerReferences, hasListeners, numberOfListeners, removeAllListeners, removeAllListeners, removeListener
-
Field Details
-
EXTENT_MARGIN
public static final double EXTENT_MARGINExtra clearance around boundaries of network as fraction of width and height.- See Also:
-
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
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
The timed event type for pub/sub indicating the removal of a Route for a gtuType.
Payload: [String gtuTypeId, String routeId] -
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
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
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
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
The timed event type for pub/sub indicating the addition of an ObjectInterface implementing object.
Payload: String ObjectId (not an array, just a String) -
LINK_REMOVE_EVENT
The timed event type for pub/sub indicating the removal of a Link.
Payload: String linkId (not an array, just a String) -
LINK_ADD_EVENT
The timed event type for pub/sub indicating the addition of a Link.
Payload: String linkId (not an array, just a String) -
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
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
Construction of an empty network.- Parameters:
id- the network id.simulator- the DSOL simulator engine
-
-
Method Details
-
getId
Description copied from interface:PerceivableContextGet a descriptive Id of the perceivable context (e.g., useful for debugging purposes).- Specified by:
getIdin interfaceIdentifiable- Specified by:
getIdin interfacePerceivableContext- Returns:
- the id of the context
-
getSimulator
Return the simulator.- Returns:
- the simulator
-
getNodeMap
Provide an immutable map of node ids to nodes in the network.- Returns:
- an immutable map of nodes.
-
addNode
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
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
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
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
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
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
Provide an immutable map of link ids to links in the network.- Returns:
- the an immutable map of links.
-
addLink
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
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
Find a link between node1 and node2 and return it if it exists in the network.- Parameters:
node1- first nodenode2- second node- Returns:
- the link between node1 and node2 in the network, empty if it does not exist.
-
containsLink
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
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
Find a link between node1 and node2 and return it if it exists in the network.- Parameters:
nodeId1- id of the first nodenodeId2- 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
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
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
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
Return object of given type with given id.- Type Parameters:
T- object type- Parameters:
objectType- object type classobjectId- id of object- Returns:
- object of given type with given id, empty if no such object
-
addObject
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
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
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
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
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
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
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
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
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
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
Add a route to the network.- Parameters:
gtuType- the GtuType for which to add a routeroute- 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
Remove the route from the network, e.g. because of road maintenance.- Parameters:
gtuType- the GtuType for which to remove a routeroute- the route to remove from the network.- Throws:
NetworkException- if route does not exist in the network.
-
containsRoute
Determine whether the provided route exists in the network for the given GtuType.- Parameters:
gtuType- the GtuType for which to check whether the route existsroute- the route to check for- Returns:
- whether the route exists in the network for the given GtuType
-
containsRoute
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 existsrouteId- 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
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
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
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 routesnodeFrom- 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 routenodeFrom- 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 routenodeFrom- 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 routenodeFrom- 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 routenodeFrom- 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
Get route map.- Returns:
- a defensive copy of the routeMap.
-
setRawRouteMap
Set route map.- Parameters:
newRouteMap- the routeMap to set, only to be used in the 'network' package for cloning.
-
addGTU
Description copied from interface:PerceivableContextAdd a GTU to the network.- Specified by:
addGTUin interfacePerceivableContext- Parameters:
gtu- the GTU to add
-
removeGTU
Description copied from interface:PerceivableContextRemove a GTU from the network.- Specified by:
removeGTUin interfacePerceivableContext- Parameters:
gtu- the GTU to remove
-
containsGTU
Description copied from interface:PerceivableContextTest whether a GTU is registered in the network.- Specified by:
containsGTUin interfacePerceivableContext- Parameters:
gtu- the GTU to search for- Returns:
- whether the network contains this GTU
-
getGTU
Description copied from interface:PerceivableContextGet a GTU in the model.- Specified by:
getGTUin interfacePerceivableContext- 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
Description copied from interface:PerceivableContextGet an overview of the GTUs in the model. The set returned is a defensive copy.- Specified by:
getGTUsin interfacePerceivableContext- Returns:
- a set of GTUs as registered in the current model.
-
containsGtuId
Description copied from interface:PerceivableContextTest whether a GTU ID is registered in the network.- Specified by:
containsGtuIdin interfacePerceivableContext- Parameters:
gtuId- the GTU ID to search for- Returns:
- whether the network contains a GTU with this ID
-
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
-