- java.lang.Object
-
- it.unive.lisa.util.datastructures.graph.Graph<G,N,E>
-
- Type Parameters:
N- the type ofNodes in this graphE- the type ofEdges in this graphG- the type of this graph
- Direct Known Subclasses:
BaseCallGraph,CFG
public abstract class Graph<G extends Graph<G,N,E>,N extends Node<N,E,G>,E extends Edge<N,E,G>> extends java.lang.ObjectA generic graph, backed by anAdjacencyMatrix.
Note that this class does not defineObject.equals(Object)norObject.hashCode(), since we leave the decision to be unique instances to implementers.
-
-
Field Summary
Fields Modifier and Type Field Description protected AdjacencyMatrix<N,E,G>adjacencyMatrixThe adjacency matrix of this graph, mapping nodes to the collection of edges attached to it.protected java.util.Collection<N>entrypointsThe nodes of this graph that are entrypoints, that is, that can be executed from other graphs.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <V> voidaccept(GraphVisitor<G,N,E,V> visitor, V tool)Accepts the givenGraphVisitor.voidaddEdge(E edge)Adds an edge to this graph.voidaddNode(N node)Adds the given node to the set of nodes, optionally setting that as root.voidaddNode(N node, boolean entrypoint)Adds the given node to the set of nodes, optionally marking this as entrypoint (that is, reachable executable from other graphs).voiddump(java.io.Writer writer)Dumps the content of this graph in the given writer, formatted as a dot file.voiddump(java.io.Writer writer, java.util.function.Function<N,java.lang.String> labelGenerator)Dumps the content of this graph in the given writer, formatted as a dot file.java.util.Collection<N>followersOf(N node)Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this control flow graph going from the given node to such node.AdjacencyMatrix<N,E,G>getAdjacencyMatrix()Yields the adjacency matrix backing this graph.EgetEdgeConnecting(N source, N destination)Yields the edge connecting the two given nodes, if any.java.util.Collection<E>getEdges()Yields the set of edges of this graph.intgetEdgesCount()Yields the total number of edges of this graph.java.util.Collection<N>getEntrypoints()Yields the nodes of this graph that are entrypoints, that is, that can be executed from other graphs.java.util.Collection<E>getIngoingEdges(N node)Yields the ingoing edges to the given node.java.util.Collection<N>getNodes()Yields the set of nodes of this graph.intgetNodesCount()Yields the total number of nodes of this graph.java.util.Collection<E>getOutgoingEdges(N node)Yields the outgoing edges from the given node.booleanisEqualTo(G graph)Checks if this graph is effectively equal to the given one, that is, if they have the same structure while potentially being different instances.java.util.Collection<N>predecessorsOf(N node)Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this control flow graph going from such node to the given one.protected voidpreSimplify(N node)Callback that is invoked on a node before simplifying it.protected java.util.Set<N>simplify(java.lang.Class<? extends N> target, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)Simplifies the adjacency matrix beneath this graph, removing all nodes that are instances of<T>and rewriting the edge set accordingly.protected abstract DotGraph<N,E,G>toDot(java.util.function.Function<N,java.lang.String> labelGenerator)Converts this graph to aDotGraphinstance.java.lang.StringtoString()
-
-
-
Constructor Detail
-
Graph
protected Graph()
Builds the graph.
-
Graph
protected Graph(java.util.Collection<N> entrypoints, AdjacencyMatrix<N,E,G> adjacencyMatrix)
Builds the graph.- Parameters:
entrypoints- the nodes of this graph that will be reachable from other graphsadjacencyMatrix- the matrix containing all the nodes and the edges that will be part of this graph
-
Graph
protected Graph(G other)
Clones the given graph.- Parameters:
other- the original graph
-
-
Method Detail
-
getAdjacencyMatrix
public AdjacencyMatrix<N,E,G> getAdjacencyMatrix()
Yields the adjacency matrix backing this graph.- Returns:
- the matrix
-
getEntrypoints
public final java.util.Collection<N> getEntrypoints()
Yields the nodes of this graph that are entrypoints, that is, that can be executed from other graphs. This usually contains the first node of this graph, but might also contain other ones.- Returns:
- the entrypoints of this graph.
-
getNodes
public final java.util.Collection<N> getNodes()
Yields the set of nodes of this graph.- Returns:
- the collection of nodes
-
getEdges
public final java.util.Collection<E> getEdges()
Yields the set of edges of this graph.- Returns:
- the collection of edges
-
addNode
public final void addNode(N node)
Adds the given node to the set of nodes, optionally setting that as root. This is equivalent to invokingaddNode(Node, boolean)withfalseas second parameter.- Parameters:
node- the node to add
-
addNode
public final void addNode(N node, boolean entrypoint)
Adds the given node to the set of nodes, optionally marking this as entrypoint (that is, reachable executable from other graphs). The first node of a graph should always be marked as entrypoint. Besides, nodes that might be reached through jumps from external graphs should be marked as entrypoints as well.- Parameters:
node- the node to addentrypoint- iftruecauses the given node to be considered as an entrypoint.
-
addEdge
public void addEdge(E edge)
Adds an edge to this graph.- Parameters:
edge- the edge to add- Throws:
java.lang.UnsupportedOperationException- if the source or the destination of the given edge are not part of this graph
-
getNodesCount
public final int getNodesCount()
Yields the total number of nodes of this graph.- Returns:
- the number of nodes
-
getEdgesCount
public final int getEdgesCount()
Yields the total number of edges of this graph.- Returns:
- the number of edges
-
getEdgeConnecting
public final E getEdgeConnecting(N source, N destination)
Yields the edge connecting the two given nodes, if any. Yieldsnullif such edge does not exist, or if one of the two nodes is not inside this graph.- Parameters:
source- the source nodedestination- the destination node- Returns:
- the edge connecting
sourcetodestination, ornull
-
getIngoingEdges
public final java.util.Collection<E> getIngoingEdges(N node)
Yields the ingoing edges to the given node.- Parameters:
node- the node- Returns:
- the collection of ingoing edges
-
getOutgoingEdges
public final java.util.Collection<E> getOutgoingEdges(N node)
Yields the outgoing edges from the given node.- Parameters:
node- the node- Returns:
- the collection of outgoing edges
-
followersOf
public final java.util.Collection<N> followersOf(N node)
Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this control flow graph going from the given node to such node. Yieldsnullif the node is not in this graph.- Parameters:
node- the node- Returns:
- the collection of followers
-
predecessorsOf
public final java.util.Collection<N> predecessorsOf(N node)
Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this control flow graph going from such node to the given one. Yieldsnullif the node is not in this graph.- Parameters:
node- the node- Returns:
- the collection of predecessors
-
dump
public void dump(java.io.Writer writer) throws java.io.IOExceptionDumps the content of this graph in the given writer, formatted as a dot file.- Parameters:
writer- the writer where the content will be written- Throws:
java.io.IOException- if an exception happens while writing something to the given writer
-
dump
public void dump(java.io.Writer writer, java.util.function.Function<N,java.lang.String> labelGenerator) throws java.io.IOExceptionDumps the content of this graph in the given writer, formatted as a dot file. The content of each vertex will be enriched by invoking labelGenerator on the vertex itself, to obtain an extra description to be concatenated with the standard call to the vertex'stoString().- Parameters:
writer- the writer where the content will be writtenlabelGenerator- the function used to generate extra labels- Throws:
java.io.IOException- if an exception happens while writing something to the given writer
-
toDot
protected abstract DotGraph<N,E,G> toDot(java.util.function.Function<N,java.lang.String> labelGenerator)
Converts this graph to aDotGraphinstance.
-
isEqualTo
public boolean isEqualTo(G graph)
Checks if this graph is effectively equal to the given one, that is, if they have the same structure while potentially being different instances.- Parameters:
graph- the other graph- Returns:
trueif this graph and the given one are effectively equals
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
simplify
protected final java.util.Set<N> simplify(java.lang.Class<? extends N> target, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)
Simplifies the adjacency matrix beneath this graph, removing all nodes that are instances of<T>and rewriting the edge set accordingly. This method will throw anUnsupportedOperationExceptionif one of the nodes being simplified has an outgoing edge that is not simplifiable, according toEdge.canBeSimplified().- Parameters:
target- the class of theNodethat needs to be simplifiedremovedEdges- the collections of edges that got removed during the simplification, filled by this method (the collection will be cleared before simplifying)replacedEdges- the map of edges that got replaced during the simplification, filled by this method (the map will be cleared before simplifying); each entry refers to a single simplified edge, and is in the form<<ingoing removed, outgoing removed>, added>- Returns:
- the set of nodes that have been simplified
- Throws:
java.lang.UnsupportedOperationException- if there exists at least one node being simplified with an outgoing non-simplifiable edge
-
preSimplify
protected void preSimplify(N node)
Callback that is invoked on a node before simplifying it.- Parameters:
node- the node about to be simplified
-
accept
public <V> void accept(GraphVisitor<G,N,E,V> visitor, V tool)
Accepts the givenGraphVisitor. This method first invokesGraphVisitor.visit(Object, Graph)on this graph, and then proceeds by first invokingGraphVisitor.visit(Object, Graph, Node)on all the nodes in the order they are returned bygetNodes(), and later invokingGraphVisitor.visit(Object, Graph, Edge)on all the edges in the order they are returned bygetEdges(). The visiting stops at the first of such calls that returnfalse.- Type Parameters:
V- the type of auxiliary tool thatvisitorcan use- Parameters:
visitor- the visitor that is visiting theGraphcontaining this graphtool- the auxiliary tool thatvisitorcan use
-
-