Class CodeGraph<G extends CodeGraph<G,N,E>,N extends CodeNode<G,N,E>,E extends CodeEdge<G,N,E>>
- java.lang.Object
-
- it.unive.lisa.util.datastructures.graph.code.CodeGraph<G,N,E>
-
- Type Parameters:
G- the type of this graphN- the type ofCodeNodes in this graphE- the type ofCodeEdges in this graph
- All Implemented Interfaces:
Graph<G,N,E>
- Direct Known Subclasses:
CFG
public abstract class CodeGraph<G extends CodeGraph<G,N,E>,N extends CodeNode<G,N,E>,E extends CodeEdge<G,N,E>> extends java.lang.Object implements Graph<G,N,E>
AGraphthat contains a list of nodes, backed by aNodeList. The characteristic of this graph is that a nodes are mostly sequential, and can be almost perfectly stored as a list. TheNodeListbacking this graph also supports custom edges.
Note that this class does not defineObject.equals(Object)norObject.hashCode(), since we leave the decision to be unique instances to implementers.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEdge(E edge)Adds an edge to this graph.voidaddNode(N node)Adds the given node to the set of nodes.voidaddNode(N node, boolean entrypoint)Adds the given node to the set of nodes, optionally marking this as entrypoint (that is, root).booleancontainsEdge(E edge)Yieldstrueif the given edge is contained in this graph.booleancontainsNode(N node)Yieldstrueif the given node is contained in this graph.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.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.java.util.Collection<E>getEdgesConnecting(N source, N destination)Yields all edges connecting the two given nodes, if any.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, roots of the graph.java.util.Collection<E>getIngoingEdges(N node)Yields the ingoing edges to the given node.NodeList<G,N,E>getNodeList()Yields the node list backing this graph.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.voidpreSimplify(N node)Callback that is invoked on a node before simplifying it.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.SerializableGraphtoSerializableGraph(java.util.function.BiFunction<G,N,SerializableValue> descriptionGenerator)Yields an instance ofSerializableGraphbuilt from this one.java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface it.unive.lisa.util.datastructures.graph.Graph
accept, getCycleEntries, toSerializableGraph
-
-
-
-
Constructor Detail
-
CodeGraph
protected CodeGraph(E sequentialSingleton)
Builds the graph.- Parameters:
sequentialSingleton- an instance of an edge of this list that can be used to invokeCodeEdge.newInstance(CodeNode, CodeNode)to obtain instances of sequential edges
-
CodeGraph
protected CodeGraph(java.util.Collection<N> entrypoints, NodeList<G,N,E> nodes)
Builds the graph.- Parameters:
entrypoints- the nodes of this graph that will be reachable from other graphsnodes- the list of nodes contained in this graph
-
CodeGraph
protected CodeGraph(G other)
Clones the given graph.- Parameters:
other- the original graph
-
-
Method Detail
-
getNodeList
public NodeList<G,N,E> getNodeList()
Yields the node list backing this graph.- Returns:
- the list
-
getEntrypoints
public java.util.Collection<N> getEntrypoints()
Description copied from interface:GraphYields the nodes of this graph that are entrypoints, that is, roots of the graph. This usually contains the first node of this graph, but might also contain other ones.
-
getNodes
public java.util.Collection<N> getNodes()
Description copied from interface:GraphYields the set of nodes of this graph.
-
getEdges
public java.util.Collection<E> getEdges()
Description copied from interface:GraphYields the set of edges of this graph.
-
addNode
public void addNode(N node)
Description copied from interface:GraphAdds the given node to the set of nodes. This is equivalent to invokingGraph.addNode(Node, boolean)withfalseas second parameter.
-
addNode
public void addNode(N node, boolean entrypoint)
Description copied from interface:GraphAdds the given node to the set of nodes, optionally marking this as entrypoint (that is, root).
-
addEdge
public void addEdge(E edge)
Description copied from interface:GraphAdds an edge to this graph.
-
getNodesCount
public int getNodesCount()
Description copied from interface:GraphYields the total number of nodes of this graph.
-
getEdgesCount
public int getEdgesCount()
Description copied from interface:GraphYields the total number of edges of this graph.
-
containsNode
public boolean containsNode(N node)
Description copied from interface:GraphYieldstrueif the given node is contained in this graph.
-
containsEdge
public boolean containsEdge(E edge)
Description copied from interface:GraphYieldstrueif the given edge is contained in this graph.
-
getEdgeConnecting
public E getEdgeConnecting(N source, N destination)
Description copied from interface:GraphYields 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. If more than one edge connects the two nodes, this method returns one of them arbitrarily (but consistently: successive calls with the same parameters will always return the same edge). To retrieve all such edges, useGraph.getEdgesConnecting(Node, Node).
-
getEdgesConnecting
public java.util.Collection<E> getEdgesConnecting(N source, N destination)
Description copied from interface:GraphYields all edges connecting the two given nodes, if any. Yields an empty collection if no edge exists, or if one of the two nodes is not inside this graph.
-
getIngoingEdges
public java.util.Collection<E> getIngoingEdges(N node)
Description copied from interface:GraphYields the ingoing edges to the given node.
-
getOutgoingEdges
public java.util.Collection<E> getOutgoingEdges(N node)
Description copied from interface:GraphYields the outgoing edges from the given node.
-
followersOf
public java.util.Collection<N> followersOf(N node)
Description copied from interface:GraphYields 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.
-
predecessorsOf
public java.util.Collection<N> predecessorsOf(N node)
Description copied from interface:GraphYields 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.
-
toSerializableGraph
public SerializableGraph toSerializableGraph(java.util.function.BiFunction<G,N,SerializableValue> descriptionGenerator)
Description copied from interface:GraphYields an instance ofSerializableGraphbuilt from this one. IfdescriptionGeneratoris notnull,SerializableNodeDescriptionfor each node will be generated using it.
-
isEqualTo
public boolean isEqualTo(G graph)
Description copied from interface:GraphChecks if this graph is effectively equal to the given one, that is, if they have the same structure while potentially being different instances.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
simplify
public 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 toCodeEdge.isUnconditional().- Parameters:
target- the class of theCodeNodethat 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
public void preSimplify(N node)
Callback that is invoked on a node before simplifying it.- Parameters:
node- the node about to be simplified
-
-