Newer
Older
import tfm.graphs.cfg.CFG;
import tfm.graphs.pdg.PDG;
import tfm.graphs.sdg.SDG;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
* Represents a node in the various graphs ({@link CFG CFG},
* {@link PDG PDG} and {@link SDG SDG}),
* including its AST representation and the connections it has to other nodes
* in the same graph. It can hold a string of characters that will be used
* to represent it.
* @param <N> The type of the AST represented by this node.
*/
public class GraphNode<N extends Node> implements Comparable<GraphNode<?>> {
private final int id;
private final String instruction;
private final N astNode;
private final Set<String> declaredVariables;
private final Set<String> definedVariables;
private final Set<String> usedVariables;
GraphNode(int id, String instruction, @NotNull N astNode) {
astNode,
Utils.emptySet(),
Utils.emptySet(),
Utils.emptySet()
);
if (astNode instanceof Statement) {
extractVariables((Statement) astNode);
}
GraphNode(
Collection<String> declaredVariables,
Collection<String> definedVariables,
Collection<String> usedVariables
this.declaredVariables = new HashSet<>(declaredVariables);
this.definedVariables = new HashSet<>(definedVariables);
this.usedVariables = new HashSet<>(usedVariables);
private void extractVariables(@NotNull Statement statement) {
.setOnVariableDeclarationListener(this.declaredVariables::add)
.setOnVariableDefinitionListener(this.definedVariables::add)
.setOnVariableUseListener(this.usedVariables::add)
return String.format("GraphNode{id: %s, instruction: '%s', astNodeType: %s}",
getInstruction(),
getAstNode().getClass().getSimpleName()
}
public void addDeclaredVariable(String variable) {
declaredVariables.add(variable);
}
public void addDefinedVariable(String variable) {
definedVariables.add(variable);
}
public void addUsedVariable(String variable) {
usedVariables.add(variable);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
GraphNode<?> other = (GraphNode<?>) o;
return Objects.equals(getId(), other.getId())
&& Objects.equals(getInstruction(), other.getInstruction())
@Override
public int hashCode() {
return Objects.hash(getId(), getInstruction(), getAstNode());
public Set<String> getDeclaredVariables() {
return declaredVariables;
}
public Set<String> getDefinedVariables() {
return definedVariables;
}
public Set<String> getUsedVariables() {
return usedVariables;
}
public String getInstruction() {
return instruction;
@Override
public int compareTo(@NotNull GraphNode<?> o) {
return Integer.compare(id, o.id);
}