Class GraphModifierImpl
- java.lang.Object
-
- org.goplanit.utils.event.EventProducerImpl
-
- org.goplanit.graph.modifier.GraphModifierImpl
-
- All Implemented Interfaces:
EventProducer
,GraphModifierEventProducer
,GraphModifier<Vertex,Edge>
,TopologicalModifier
public class GraphModifierImpl extends EventProducerImpl implements GraphModifier<Vertex,Edge>
Apply modifications to the graph in an integrated fashion.While graphs are assumed to have a managed id for all their entities it is not a given that the edges, vertices, etc. are the primary containers where these are uniquely tracked. For example a subgraph might only contain a subset of vertices. Therefore, whenever modifications are made to a graph, the incoker of these changes should be aware whether or not the graph entities containers are the primary containers or not. If so, and the ids used are expected to remain contiguous (directly) after completion of the modification, additional effort is required by invoking
recreateManagedEntitiesIds()
to ensure that all entity containers on the graph are triggered to perform an update of their internally managed ids.- Author:
- markr
-
-
Field Summary
Fields Modifier and Type Field Description protected UntypedGraph<?,?>
theGraph
the graph to modify-
Fields inherited from class org.goplanit.utils.event.EventProducerImpl
listeners
-
-
Constructor Summary
Constructors Constructor Description GraphModifierImpl(UntypedGraph<?,?> theGraph)
Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(GraphModifierListener listener)
Register listener for all its supported types fired by the graph modifiervoid
addListener(GraphModifierListener listener, GraphModifierEventType eventType)
Register listeners for events fired by the graph modifier<Ex extends Edge>
ExbreakEdgeAt(Vertex vertexToBreakAt, Ex edgeToBreak, PlanitJtsCrsUtils geoUtils)
Break the passed in edge by inserting the passed in vertex in between.<Ex extends Edge>
Map<Long,Pair<Ex,Ex>>breakEdgesAt(List<Ex> edgesToBreak, Vertex vertexToBreakAt, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
Break the passed in edges by inserting the passed in vertex in between.void
fireEvent(EventListener eventListener, Event event)
Let derived class deal with the handling of the listener, where based on the derived event implementation the listener's concrete class can be determined which in turn allows for calling the right event callback method which is unknown at this base level since this event mechanism does not force a particular notification method signature on its listener interfaceUntypedGraph<?,?>
getGraph()
Access to graphprotected Set<Vertex>
processSubNetworkVertex(Vertex referenceVertex)
helper function for subnetwork identification (deliberately NOT recursive to avoid stack overflow on large networks)void
recreateManagedEntitiesIds()
This method will recreate all ids of the graph's components, e.g., vertices, edges, etc.void
removeDanglingSubGraphs(Integer belowSize, Integer aboveSize, boolean alwaysKeepLargest)
remove any dangling subgraphs below a given size from the graph if they exist and subsequently reorder the internal ids if neededvoid
removeEdge(Edge edge)
Remove an edge by removing it from the graph and the vertices it is connected to.void
removeListener(GraphModifierListener listener)
Remove listener for all event types it is registered forvoid
removeListener(GraphModifierListener listener, GraphModifierEventType eventType)
Remove listener for given event typevoid
removeSubGraph(Set<? extends Vertex> subGraphToRemove)
remove the subgraph identified by the passed in verticesvoid
removeSubGraphOf(Vertex referenceVertex)
remove the (sub)graph in which the passed in vertex resides.void
removeVertex(Vertex vertex)
Remove a vertex by removing it from the graph and the edges it is connected to.void
reset()
reset all state related information of the instanceprotected static void
updateBrokenEdgeGeometry(Edge brokenEdge, Vertex vertexBrokenAt)
update the geometry of the broken edge, knowing at what vertex it was broken from a previously longer edge-
Methods inherited from class org.goplanit.utils.event.EventProducerImpl
addListener, addListener, addListener, addListener, fireEvent, getEventTypesWithListeners, hasListener, hasListeners, numberOfListeners, removeAllListeners, removeListener, removeListener
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.goplanit.utils.graph.modifier.GraphModifier
breakEdgesAt, removeDanglingSubGraphs
-
Methods inherited from interface org.goplanit.utils.graph.modifier.event.GraphModifierEventProducer
removeAllListeners
-
-
-
-
Field Detail
-
theGraph
protected final UntypedGraph<?,?> theGraph
the graph to modify
-
-
Constructor Detail
-
GraphModifierImpl
public GraphModifierImpl(UntypedGraph<?,?> theGraph)
Constructor- Parameters:
theGraph
- to use
-
-
Method Detail
-
updateBrokenEdgeGeometry
protected static void updateBrokenEdgeGeometry(Edge brokenEdge, Vertex vertexBrokenAt)
update the geometry of the broken edge, knowing at what vertex it was broken from a previously longer edge- Parameters:
brokenEdge
- the broken edgevertexBrokenAt
- the vertex it was broken at
-
processSubNetworkVertex
protected Set<Vertex> processSubNetworkVertex(Vertex referenceVertex)
helper function for subnetwork identification (deliberately NOT recursive to avoid stack overflow on large networks)- Parameters:
referenceVertex
- to process- Returns:
- all vertices in the subnetwork connected to passed in reference vertex
-
getGraph
public UntypedGraph<?,?> getGraph()
Access to graph- Returns:
- the underlying graph
-
fireEvent
public void fireEvent(EventListener eventListener, Event event)
Let derived class deal with the handling of the listener, where based on the derived event implementation the listener's concrete class can be determined which in turn allows for calling the right event callback method which is unknown at this base level since this event mechanism does not force a particular notification method signature on its listener interfacemake public so derived classes can access it as well
- Specified by:
fireEvent
in classEventProducerImpl
- Parameters:
eventListener
- to notify for the eventevent
- to process for the listener
-
removeVertex
public void removeVertex(Vertex vertex)
Remove a vertex by removing it from the graph and the edges it is connected to. Any registered events fro vertex removal will be triggered.- Specified by:
removeVertex
in interfaceGraphModifier<Vertex,Edge>
- Parameters:
vertex
- to remove
-
removeEdge
public void removeEdge(Edge edge)
Remove an edge by removing it from the graph and the vertices it is connected to. Any registered events for edge removal will be triggered.- Specified by:
removeEdge
in interfaceGraphModifier<Vertex,Edge>
- Parameters:
edge
- to remove
-
removeDanglingSubGraphs
public void removeDanglingSubGraphs(Integer belowSize, Integer aboveSize, boolean alwaysKeepLargest)
remove any dangling subgraphs below a given size from the graph if they exist and subsequently reorder the internal ids if needed- Specified by:
removeDanglingSubGraphs
in interfaceGraphModifier<Vertex,Edge>
- Parameters:
belowSize
- remove subgraphs below the given sizeaboveSize
- remove subgraphs above the given size (typically set to maximum value)alwaysKeepLargest
- indicate if the largest of the subgraphs is always to be kept even if it does not match the criteria
-
removeSubGraph
public void removeSubGraph(Set<? extends Vertex> subGraphToRemove)
remove the subgraph identified by the passed in vertices- Specified by:
removeSubGraph
in interfaceGraphModifier<Vertex,Edge>
- Parameters:
subGraphToRemove
- the one to remove
-
removeSubGraphOf
public void removeSubGraphOf(Vertex referenceVertex)
remove the (sub)graph in which the passed in vertex resides. Apply reordering of internal ids of remaining network.- Specified by:
removeSubGraphOf
in interfaceGraphModifier<Vertex,Edge>
- Parameters:
referenceVertex
- to identify subnetwork by
-
breakEdgesAt
public <Ex extends Edge> Map<Long,Pair<Ex,Ex>> breakEdgesAt(List<Ex> edgesToBreak, Vertex vertexToBreakAt, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
Break the passed in edges by inserting the passed in vertex in between. After completion the original edges remain as (VertexA,VertexToBreakAt), and new edges are inserted for (VertexToBreakAt,VertexB).- Specified by:
breakEdgesAt
in interfaceGraphModifier<Vertex,Edge>
- Type Parameters:
Ex
- edge type- Parameters:
edgesToBreak
- the links to breakvertexToBreakAt
- the node to break atcrs
- required to update edge lengths- Returns:
- affectedEdges the list of all result edges of the breaking of links by their original link id
-
breakEdgeAt
public <Ex extends Edge> Ex breakEdgeAt(Vertex vertexToBreakAt, Ex edgeToBreak, PlanitJtsCrsUtils geoUtils)
Break the passed in edge by inserting the passed in vertex in between. After completion the original edge remains as (VertexA,VertexToBreakAt), and new edges are inserted for (VertexToBreakAt,VertexB).- Specified by:
breakEdgeAt
in interfaceGraphModifier<Vertex,Edge>
- Type Parameters:
Ex
- edge type- Parameters:
vertexToBreakAt
- the node to break atedgeToBreak
- the link to breakgeoUtils
- required to update edge lengths- Returns:
- newlyCreatedEdge
-
recreateManagedEntitiesIds
public void recreateManagedEntitiesIds()
This method will recreate all ids of the graph's components, e.g., vertices, edges, etc. but only when the containers used for them are the primary ManagedIdEntities containers, i.e., when the graph is responsible of uniquely tracking all entities by their managed id. If not, for example, if this is a subgraph reusing parts of the main graph, it will not recreate the ids.The reasoning is that if we would recreate ids of the container while the container does not contain all = let's say - vertices, their managedId is no longer guaranteed to be unique which can lead to issues
Method can be used in conjunctions with the removal of parts of the graph and the result is required to have unique contiguous ids
Should fire #RecreatedGraphEntitiesManagedIdsEvent after it has been executed
- Specified by:
recreateManagedEntitiesIds
in interfaceGraphModifier<Vertex,Edge>
-
reset
public void reset()
reset all state related information of the instance- Specified by:
reset
in interfaceGraphModifier<Vertex,Edge>
-
addListener
public void addListener(GraphModifierListener listener)
Register listener for all its supported types fired by the graph modifier- Specified by:
addListener
in interfaceGraphModifierEventProducer
- Parameters:
listener
- to register
-
addListener
public void addListener(GraphModifierListener listener, GraphModifierEventType eventType)
Register listeners for events fired by the graph modifier- Specified by:
addListener
in interfaceGraphModifierEventProducer
- Parameters:
listener
- to registereventType
- to register listener for
-
removeListener
public void removeListener(GraphModifierListener listener, GraphModifierEventType eventType)
Remove listener for given event type- Specified by:
removeListener
in interfaceGraphModifierEventProducer
- Parameters:
listener
- to removeeventType
- to unregister listener for
-
removeListener
public void removeListener(GraphModifierListener listener)
Remove listener for all event types it is registered for- Specified by:
removeListener
in interfaceGraphModifierEventProducer
- Parameters:
listener
- to remove
-
-