Class AbstractGraph
- All Implemented Interfaces:
Iterable<Node>
,Element
,Graph
,Structure
,AttributeSink
,ElementSink
,Pipe
,Replayable
,Sink
,Source
- Direct Known Subclasses:
AdjacencyListGraph
public abstract class AbstractGraph extends AbstractElement implements Graph, Replayable
This class provides a basic implementation of
Graph
interface, to minimize the effort
required to implement this interface. It provides event management
implementing all the methods of Pipe
. It also
manages strict checking and auto-creation policies, as well as other services
as displaying, reading and writing.
Subclasses have to maintain data structures allowing to efficiently access
graph elements by their id or index and iterating on them. They also have to
maintain coherent indices of the graph elements. When AbstractGraph decides
to add or remove elements, it calls one of the "callbacks"
addNodeCallback(AbstractNode)
,
addEdgeCallback(AbstractEdge)
,
removeNodeCallback(AbstractNode)
,
removeEdgeCallback(AbstractEdge)
, clearCallback()
. The role
of these callbacks is to update the data structures and to re-index elements
if necessary.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.graphstream.graph.implementations.AbstractElement
AbstractElement.AttributeChangeEvent
Nested classes/interfaces inherited from interface org.graphstream.stream.Replayable
Replayable.Controller
-
Constructor Summary
Constructors Constructor Description AbstractGraph(String id)
The same asAbstractGraph(id, true, false)
AbstractGraph(String id, boolean strictChecking, boolean autoCreate)
Creates a new graph. -
Method Summary
Modifier and Type Method Description void
addAttributeSink(AttributeSink sink)
Add a sink for attribute events only.Edge
addEdge(String id, Node from, Node to, boolean directed)
LikeGraph.addEdge(String, String, String, boolean)
but the node references are given instead of node identifiers.void
addElementSink(ElementSink sink)
Add a sink for elements events only.Node
addNode(String id)
Add a node in the graph.void
addSink(Sink sink)
Add a sink for all graph events (attributes and graph elements) coming from this source.Iterable<AttributeSink>
attributeSinks()
Returns an "iterable" ofAttributeSink
objects registered to this graph.void
clear()
Empty the graph completely by removing any references to nodes or edges.void
clearAttributeSinks()
Remove all listener attribute sinks.void
clearElementSinks()
Remove all listener element sinks.void
clearSinks()
Remove all listener sinks.Viewer
display()
Utility method that creates a new graph viewer, and register the graph in it.Viewer
display(boolean autoLayout)
Utility method that creates a new graph viewer, and register the graph in it.void
edgeAdded(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)
An edge was inserted in graph.void
edgeAttributeAdded(String sourceId, long timeId, String edgeId, String attribute, Object value)
A edge attribute was added.void
edgeAttributeChanged(String sourceId, long timeId, String edgeId, String attribute, Object oldValue, Object newValue)
A edge attribute was changed.void
edgeAttributeRemoved(String sourceId, long timeId, String edgeId, String attribute)
A edge attribute was removed.EdgeFactory<? extends Edge>
edgeFactory()
The factory used to create edge instances.void
edgeRemoved(String sourceId, long timeId, String edgeId)
An edge of graph was removed.The nodes the edge connects may already have been removed from the graph.Iterable<ElementSink>
elementSinks()
Returns an "iterable" ofElementSink
objects registered to this graph.Replayable.Controller
getReplayController()
Get a controller to replay the graph.double
getStep()
The current step.void
graphAttributeAdded(String sourceId, long timeId, String attribute, Object value)
A graph attribute was added.void
graphAttributeChanged(String sourceId, long timeId, String attribute, Object oldValue, Object newValue)
A graph attribute was changed.void
graphAttributeRemoved(String sourceId, long timeId, String attribute)
A graph attribute was removed.void
graphCleared(String sourceId, long timeId)
The whole graph was cleared.boolean
isAutoCreationEnabled()
Is the automatic creation of missing elements enabled?.boolean
isStrict()
Is strict checking enabled?Iterator<Node>
iterator()
This implementation returns an iterator over nodes.void
nodeAdded(String sourceId, long timeId, String nodeId)
A node was inserted in the given graph.void
nodeAttributeAdded(String sourceId, long timeId, String nodeId, String attribute, Object value)
A node attribute was added.void
nodeAttributeChanged(String sourceId, long timeId, String nodeId, String attribute, Object oldValue, Object newValue)
A node attribute was changed.void
nodeAttributeRemoved(String sourceId, long timeId, String nodeId, String attribute)
A node attribute was removed.NodeFactory<? extends Node>
nodeFactory()
The factory used to create node instances.void
nodeRemoved(String sourceId, long timeId, String nodeId)
A node was removed from the graph.void
removeAttributeSink(AttributeSink sink)
Remove an attribute sink.Edge
removeEdge(Edge edge)
Removes an edge.Edge
removeEdge(Node node1, Node node2)
Removes an edge between two nodes.void
removeElementSink(ElementSink sink)
Remove an element sink.Node
removeNode(Node node)
Removes a node.void
removeSink(Sink sink)
Remove a sink.void
setAutoCreate(boolean on)
Enable or disable the automatic creation of missing elements.void
setEdgeFactory(EdgeFactory<? extends Edge> ef)
Set the edge factory used to create edges.void
setNodeFactory(NodeFactory<? extends Node> nf)
Set the node factory used to create nodes.void
setStrict(boolean on)
Enable or disable strict checking.void
stepBegins(double time)
Since dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events.void
stepBegins(String sourceId, long timeId, double step)
Since dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events.Methods inherited from class org.graphstream.graph.implementations.AbstractElement
attributeKeys, clearAttributes, getAttribute, getAttribute, getAttributeCount, getFirstAttributeOf, getFirstAttributeOf, getId, getIndex, hasAttribute, hasAttribute, removeAttribute, setAttribute, toString
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.graphstream.graph.Element
attributeKeys, clearAttributes, getArray, getAttribute, getAttribute, getAttributeCount, getFirstAttributeOf, getFirstAttributeOf, getId, getIndex, getLabel, getMap, getNumber, getVector, hasArray, hasAttribute, hasAttribute, hasLabel, hasMap, hasNumber, hasVector, removeAttribute, setAttribute, setAttributes
Methods inherited from interface org.graphstream.graph.Graph
addEdge, addEdge, addEdge, addEdge, addEdge, getEdge, getEdge, getNode, getNode, read, read, removeEdge, removeEdge, removeEdge, removeEdge, removeNode, removeNode, write, write
Methods inherited from interface org.graphstream.graph.Structure
edges, getEdgeCount, getNodeCount, nodes
-
Constructor Details
-
AbstractGraph
The same asAbstractGraph(id, true, false)
- Parameters:
id
- Identifier of the graph- See Also:
AbstractGraph(String, boolean, boolean)
-
AbstractGraph
Creates a new graph. Subclasses must create their node and edge factories and initialize their data structures in their constructors.- Parameters:
id
-strictChecking
-autoCreate
-
-
-
Method Details
-
iterator
This implementation returns an iterator over nodes.- Specified by:
iterator
in interfaceIterable<Node>
- See Also:
Iterable.iterator()
-
nodeFactory
Description copied from interface:Graph
The factory used to create node instances. The factory can be changed to refine the node class generated for this graph.- Specified by:
nodeFactory
in interfaceGraph
- See Also:
Graph.setNodeFactory(NodeFactory)
,Graph.edgeFactory()
-
edgeFactory
Description copied from interface:Graph
The factory used to create edge instances. The factory can be changed to refine the edge class generated for this graph.- Specified by:
edgeFactory
in interfaceGraph
- See Also:
Graph.setEdgeFactory(EdgeFactory)
,Graph.nodeFactory()
-
setNodeFactory
Description copied from interface:Graph
Set the node factory used to create nodes.- Specified by:
setNodeFactory
in interfaceGraph
- Parameters:
nf
- the new NodeFactory
-
setEdgeFactory
Description copied from interface:Graph
Set the edge factory used to create edges.- Specified by:
setEdgeFactory
in interfaceGraph
- Parameters:
ef
- the new EdgeFactory
-
isStrict
public boolean isStrict()Description copied from interface:Graph
Is strict checking enabled? If strict checking is enabled the graph checks for name space conflicts (e.g. insertion of two nodes with the same name), removal of non-existing elements, use of non existing elements (create an edge between two non existing nodes). Graph implementations are free to respect strict checking or not. -
setStrict
public void setStrict(boolean on)Description copied from interface:Graph
Enable or disable strict checking.- Specified by:
setStrict
in interfaceGraph
- Parameters:
on
- True or false.- See Also:
Graph.isStrict()
-
isAutoCreationEnabled
public boolean isAutoCreationEnabled()Description copied from interface:Graph
Is the automatic creation of missing elements enabled?. If strict checking is disabled and auto-creation is enabled, when an edge is created and one or two of its nodes are not already present in the graph, the nodes are automatically created.- Specified by:
isAutoCreationEnabled
in interfaceGraph
- Returns:
- True if enabled.
-
getStep
public double getStep()Description copied from interface:Graph
The current step. -
setAutoCreate
public void setAutoCreate(boolean on)Description copied from interface:Graph
Enable or disable the automatic creation of missing elements.- Specified by:
setAutoCreate
in interfaceGraph
- Parameters:
on
- True or false.- See Also:
Graph.isAutoCreationEnabled()
-
stepBegins
public void stepBegins(double time)Description copied from interface:Graph
Since dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events. So a step is a event that occurs in the graph, it does not modify it but it gives a kind of timestamp that allows the tracking of the progress of the graph over the time.
This kind of event is useful for dynamic algorithms that listen to the dynamic graph and need to measure the time in the graph's evolution.
- Specified by:
stepBegins
in interfaceGraph
- Parameters:
time
- A numerical value that may give a timestamp to track the evolution of the graph over the time.
-
display
Description copied from interface:Graph
Utility method that creates a new graph viewer, and register the graph in it. Notice that this method is a quick way to see a graph, and only this. It can be used to prototype a program, but may be limited. This method automatically launch a graph layout algorithm in its own thread to compute best node positions.- Specified by:
display
in interfaceGraph
- Returns:
- a graph viewer that allows to command the viewer (it often run in another thread).
- See Also:
Viewer
,Graph.display(boolean )
-
display
Description copied from interface:Graph
Utility method that creates a new graph viewer, and register the graph in it. Notice that this method is a quick way to see a graph, and only this. It can be used to prototype a program, but is very limited.- Specified by:
display
in interfaceGraph
- Parameters:
autoLayout
- If true a layout algorithm is launched in its own thread to compute best node positions.- Returns:
- a graph viewer that allows to command the viewer (it often run in another thread).
- See Also:
Viewer
,Graph.display()
-
clear
public void clear()Description copied from interface:Graph
Empty the graph completely by removing any references to nodes or edges. Every attribute is also removed. However, listeners are kept.- Specified by:
clear
in interfaceGraph
- See Also:
Source.clearSinks()
-
addNode
Description copied from interface:Graph
Add a node in the graph.This acts as a factory, creating the node instance automatically (and eventually using the node factory provided). An event is generated toward the listeners. If strict checking is enabled, and a node already exists with this identifier, an
IdAlreadyInUseException
is raised. Else the error is silently ignored and the already existing node is returned.This method is implicitly generic and returns something which extends Node. The return type is the one of the left part of the assignment. For example, in the following call :
ExtendedNode n = graph.addNode("...");
the method will return an ExtendedNode. If no left part exists, method will just return a Node.
-
addEdge
Description copied from interface:Graph
LikeGraph.addEdge(String, String, String, boolean)
but the node references are given instead of node identifiers.- Specified by:
addEdge
in interfaceGraph
- Parameters:
id
- Unique and arbitrary string identifying the edge.from
- The first nodeto
- The second nodedirected
- Is the edge directed?- Returns:
- The newly created edge, an existing edge or
null
- See Also:
Graph.addEdge(String, String, String)
-
removeNode
Description copied from interface:Graph
Removes a node.An event is generated toward the listeners. Note that removing a node may remove all edges it is connected to. In this case corresponding events will also be generated toward the listeners.
This method is implicitly generic and return something which extends Node. The return type is the one of the left part of the assignment. For example, in the following call :
ExtendedNode n = graph.removeNode(...);
the method will return an ExtendedNode. If no left part exists, method will just return a Node.
- Specified by:
removeNode
in interfaceGraph
- Parameters:
node
- The node to be removed- Returns:
- The removed node
-
removeEdge
Description copied from interface:Graph
Removes an edge. An event is sent toward the listeners.This method is implicitly generic and returns something which extends Edge. The return type is the one of the left part of the assignment. For example, in the following call :
ExtendedEdge e = graph.removeEdge(...);
the method will return an ExtendedEdge. If no left part exists, method will just return an Edge.
- Specified by:
removeEdge
in interfaceGraph
- Parameters:
edge
- The edge to be removed- Returns:
- The removed edge
-
removeEdge
Description copied from interface:Graph
Removes an edge between two nodes. LikeGraph.removeEdge(String, String)
but node references are given instead of node identifiers.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
node1
- the first nodenode2
- the second node- Returns:
- the removed edge or
null
if no edge is removed - See Also:
Graph.removeEdge(String, String)
-
attributeSinks
Description copied from interface:Graph
Returns an "iterable" ofAttributeSink
objects registered to this graph.- Specified by:
attributeSinks
in interfaceGraph
- Returns:
- the set of
AttributeSink
under the form of an iterable object.
-
elementSinks
Description copied from interface:Graph
Returns an "iterable" ofElementSink
objects registered to this graph.- Specified by:
elementSinks
in interfaceGraph
- Returns:
- the list of
ElementSink
under the form of an iterable object.
-
addAttributeSink
Description copied from interface:Source
Add a sink for attribute events only. Attribute events include attribute addition change and removal.- Specified by:
addAttributeSink
in interfaceSource
- Parameters:
sink
- The sink to register.
-
addElementSink
Description copied from interface:Source
Add a sink for elements events only. Elements events include, addition and removal of nodes and edges, as well as step events.- Specified by:
addElementSink
in interfaceSource
- Parameters:
sink
- The sink to register.
-
addSink
Description copied from interface:Source
Add a sink for all graph events (attributes and graph elements) coming from this source. This is similar to registering a sink for attributes an another for elements. -
clearAttributeSinks
public void clearAttributeSinks()Description copied from interface:Source
Remove all listener attribute sinks.- Specified by:
clearAttributeSinks
in interfaceSource
-
clearElementSinks
public void clearElementSinks()Description copied from interface:Source
Remove all listener element sinks.- Specified by:
clearElementSinks
in interfaceSource
-
clearSinks
public void clearSinks()Description copied from interface:Source
Remove all listener sinks.- Specified by:
clearSinks
in interfaceSource
-
removeAttributeSink
Description copied from interface:Source
Remove an attribute sink.- Specified by:
removeAttributeSink
in interfaceSource
- Parameters:
sink
- The sink to remove, if it does not exist, this is ignored silently.
-
removeElementSink
Description copied from interface:Source
Remove an element sink.- Specified by:
removeElementSink
in interfaceSource
- Parameters:
sink
- The sink to remove, if it does not exist, this is ignored silently.
-
removeSink
Description copied from interface:Source
Remove a sink.- Specified by:
removeSink
in interfaceSource
- Parameters:
sink
- The sink to remove, if it does not exist, this is ignored silently.
-
edgeAttributeAdded
public void edgeAttributeAdded(String sourceId, long timeId, String edgeId, String attribute, Object value)Description copied from interface:AttributeSink
A edge attribute was added.- Specified by:
edgeAttributeAdded
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the change occurred.edgeId
- Identifier of the edge whose attribute changed.attribute
- The attribute name.value
- The attribute new value.
-
edgeAttributeChanged
public void edgeAttributeChanged(String sourceId, long timeId, String edgeId, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSink
A edge attribute was changed.- Specified by:
edgeAttributeChanged
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the change occurred.edgeId
- Identifier of the edge whose attribute changed.attribute
- The attribute name.oldValue
- The attribute old value.newValue
- The attribute new value.
-
edgeAttributeRemoved
Description copied from interface:AttributeSink
A edge attribute was removed.- Specified by:
edgeAttributeRemoved
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the attribute was removed.edgeId
- Identifier of the edge whose attribute was removed.attribute
- The removed attribute name.
-
graphAttributeAdded
Description copied from interface:AttributeSink
A graph attribute was added.- Specified by:
graphAttributeAdded
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the attribute changed.attribute
- The attribute name.value
- The attribute new value.
-
graphAttributeChanged
public void graphAttributeChanged(String sourceId, long timeId, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSink
A graph attribute was changed.- Specified by:
graphAttributeChanged
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the attribute changed.attribute
- The attribute name.oldValue
- The attribute old value.newValue
- The attribute new value.
-
graphAttributeRemoved
Description copied from interface:AttributeSink
A graph attribute was removed.- Specified by:
graphAttributeRemoved
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the attribute was removed.attribute
- The removed attribute name.
-
nodeAttributeAdded
public void nodeAttributeAdded(String sourceId, long timeId, String nodeId, String attribute, Object value)Description copied from interface:AttributeSink
A node attribute was added.- Specified by:
nodeAttributeAdded
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the change occurred.nodeId
- Identifier of the node whose attribute changed.attribute
- The attribute name.value
- The attribute new value.
-
nodeAttributeChanged
public void nodeAttributeChanged(String sourceId, long timeId, String nodeId, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSink
A node attribute was changed.- Specified by:
nodeAttributeChanged
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the change occurred.nodeId
- Identifier of the node whose attribute changed.attribute
- The attribute name.oldValue
- The attribute old value.newValue
- The attribute new value.
-
nodeAttributeRemoved
Description copied from interface:AttributeSink
A node attribute was removed.- Specified by:
nodeAttributeRemoved
in interfaceAttributeSink
- Parameters:
sourceId
- Identifier of the graph where the attribute was removed.nodeId
- Identifier of the node whose attribute was removed.attribute
- The removed attribute name.
-
edgeAdded
public void edgeAdded(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)Description copied from interface:ElementSink
An edge was inserted in graph.- Specified by:
edgeAdded
in interfaceElementSink
- Parameters:
sourceId
- Identifier of the graph where the edge was added.edgeId
- Identifier of the added edge.fromNodeId
- Identifier of the first node of the edge.toNodeId
- Identifier of the second node of the edge.directed
- If true, the edge is directed.
-
edgeRemoved
Description copied from interface:ElementSink
An edge of graph was removed.The nodes the edge connects may already have been removed from the graph.- Specified by:
edgeRemoved
in interfaceElementSink
- Parameters:
sourceId
- The graph where the edge will be removed.edgeId
- The edge that will be removed.
-
graphCleared
Description copied from interface:ElementSink
The whole graph was cleared. All the nodes, edges and attributes of the graph are removed.- Specified by:
graphCleared
in interfaceElementSink
- Parameters:
sourceId
- The graph cleared.
-
nodeAdded
Description copied from interface:ElementSink
A node was inserted in the given graph.- Specified by:
nodeAdded
in interfaceElementSink
- Parameters:
sourceId
- Identifier of the graph where the node was added.nodeId
- Identifier of the added node.
-
nodeRemoved
Description copied from interface:ElementSink
A node was removed from the graph.- Specified by:
nodeRemoved
in interfaceElementSink
- Parameters:
sourceId
- Identifier of the graph where the node will be removed.nodeId
- Identifier of the removed node.
-
stepBegins
Description copied from interface:ElementSink
Since dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events. So a step is a event that occurs in the graph, it does not modify it but it gives a kind of timestamp that allow the tracking of the progress of the graph over the time.
This kind of event is useful for dynamic algorithms that listen to the dynamic graph and need to measure the time in the graph's evolution.
- Specified by:
stepBegins
in interfaceElementSink
- Parameters:
sourceId
- Identifier of the graph where the step starts.timeId
- A numerical value that may give a timestamp to track the evolution of the graph over the time.
-
getReplayController
Description copied from interface:Replayable
Get a controller to replay the graph.- Specified by:
getReplayController
in interfaceReplayable
- Returns:
- a new replay controller
-