Newer
Older
import org.checkerframework.checker.nullness.qual.NonNull;
import tfm.arcs.data.ArcData;
import tfm.graphs.Graph;
import java.util.Optional;
public class Node extends Vertex<String, ArcData> {
private Set<String> definedVariables;
private Set<String> usedVariables;
public <N extends Node> Node(N node) {
this(node.getId(), node.getData(), node.getStatement());
}
public Node(Graph.NodeId id, String representation, @NonNull Statement statement) {
this(id.getId(), representation, statement);
}
private Node(int id, String representation, @NonNull Statement statement) {
super(String.valueOf(id), representation);
this.declaredVariables = new HashSet<>();
this.definedVariables = new HashSet<>();
this.usedVariables = new HashSet<>();
extractVariables(statement);
}
private void extractVariables(@NonNull Statement statement) {
new VariableExtractor()
.setOnVariableDeclarationListener(variable -> this.declaredVariables.add(variable))
.setOnVariableDefinitionListener(variable -> this.definedVariables.add(variable))
.setOnVariableUseListener(variable -> this.usedVariables.add(variable))
.visit(statement);
}
public int getId() {
return Integer.parseInt(getName());
}
public String toString() {
return String.format("Node{id: %s, data: '%s', in: %s, out: %s}",
getName(),
getData(),
getIncomingArrows().stream().map(arrow -> arrow.getFrom().getName()).collect(Collectors.toList()),
getOutgoingArrows().stream().map(arc -> arc.getTo().getName()).collect(Collectors.toList()));
}
}
public Optional<Integer> getFileLineNumber() {
return statement.getBegin().isPresent() ? Optional.of(statement.getBegin().get().line) : Optional.empty();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
return Objects.equals(getData(), other.getData())
&& Objects.equals(getIncomingArrows(), other.getIncomingArrows())
&& Objects.equals(getOutgoingArrows(), other.getOutgoingArrows())
// && Objects.equals(getName(), other.getName()) ID IS ALWAYS UNIQUE, SO IT WILL NEVER BE THE SAME
}
public String toGraphvizRepresentation() {
return String.format("%s[label=\"%s: %s\"];", getId(), getId(), getData());
}
public Set<String> getDeclaredVariables() {
return declaredVariables;
}
public Set<String> getDefinedVariables() {
return definedVariables;
}
public Set<String> getUsedVariables() {
return usedVariables;
}