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 ofCodeNode
s in this graphE
- the type ofCodeEdge
s 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>
AGraph
that 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. TheNodeList
backing 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 void
addEdge(E edge)
Adds an edge to this graph.void
addNode(N node)
Adds the given node to the set of nodes.void
addNode(N node, boolean entrypoint)
Adds the given node to the set of nodes, optionally marking this as entrypoint (that is, root).boolean
containsEdge(E edge)
Yieldstrue
if the given edge is contained in this graph.boolean
containsNode(N node)
Yieldstrue
if 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.E
getEdgeConnecting(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.int
getEdgesCount()
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.int
getNodesCount()
Yields the total number of nodes of this graph.java.util.Collection<E>
getOutgoingEdges(N node)
Yields the outgoing edges from the given node.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.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.void
preSimplify(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.SerializableGraph
toSerializableGraph(java.util.function.BiFunction<G,N,SerializableValue> descriptionGenerator)
Yields an instance ofSerializableGraph
built from this one.java.lang.String
toString()
-
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:Graph
Yields 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:Graph
Yields the set of nodes of this graph.
-
getEdges
public java.util.Collection<E> getEdges()
Description copied from interface:Graph
Yields the set of edges of this graph.
-
addNode
public void addNode(N node)
Description copied from interface:Graph
Adds the given node to the set of nodes. This is equivalent to invokingGraph.addNode(Node, boolean)
withfalse
as second parameter.
-
addNode
public void addNode(N node, boolean entrypoint)
Description copied from interface:Graph
Adds 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:Graph
Adds an edge to this graph.
-
getNodesCount
public int getNodesCount()
Description copied from interface:Graph
Yields the total number of nodes of this graph.
-
getEdgesCount
public int getEdgesCount()
Description copied from interface:Graph
Yields the total number of edges of this graph.
-
containsNode
public boolean containsNode(N node)
Description copied from interface:Graph
Yieldstrue
if the given node is contained in this graph.
-
containsEdge
public boolean containsEdge(E edge)
Description copied from interface:Graph
Yieldstrue
if the given edge is contained in this graph.
-
getEdgeConnecting
public E getEdgeConnecting(N source, N destination)
Description copied from interface:Graph
Yields the edge connecting the two given nodes, if any. Yieldsnull
if 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:Graph
Yields 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:Graph
Yields the ingoing edges to the given node.
-
getOutgoingEdges
public java.util.Collection<E> getOutgoingEdges(N node)
Description copied from interface:Graph
Yields the outgoing edges from the given node.
-
followersOf
public java.util.Collection<N> followersOf(N node)
Description copied from interface:Graph
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. Yieldsnull
if the node is not in this graph.
-
predecessorsOf
public java.util.Collection<N> predecessorsOf(N node)
Description copied from interface:Graph
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. Yieldsnull
if the node is not in this graph.
-
toSerializableGraph
public SerializableGraph toSerializableGraph(java.util.function.BiFunction<G,N,SerializableValue> descriptionGenerator)
Description copied from interface:Graph
Yields an instance ofSerializableGraph
built from this one. IfdescriptionGenerator
is notnull
,SerializableNodeDescription
for each node will be generated using it.
-
isEqualTo
public boolean isEqualTo(G graph)
Description copied from interface: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.
-
toString
public java.lang.String toString()
- Overrides:
toString
in 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 anUnsupportedOperationException
if one of the nodes being simplified has an outgoing edge that is not simplifiable, according toCodeEdge.isUnconditional()
.- Parameters:
target
- the class of theCodeNode
that 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
-
-