Newer
Older
import edg.graphlib.Arrow;
import edg.graphlib.Vertex;
import java.util.stream.Collectors;
/**
* A graphlibGraph without cost and data in arcs
* */
public abstract class Graph<NodeType extends Node> extends edg.graphlib.Graph<String, ArcData> {
private static int nextVertexId = 0;
private int id;
static synchronized NodeId getVertexId() {
return new NodeId(nextVertexId++);
}
public int getId() {
return id;
}
@Override
public String toString() {
return String.valueOf(id);
}
}
public Graph() {
super();
}
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@Override
/*
Library fix: if a node had an edge to itself resulted in 2 outgoing nodes instead of 1 outgoing and 1 incoming
*/
public boolean addEdge(Arrow<String, ArcData> arrow) {
Vertex<String, ArcData> from = arrow.getFrom();
Vertex<String, ArcData> to = arrow.getTo();
int cost = arrow.getCost();
ArcData data = arrow.getData();
if (!verticies.contains(from))
throw new IllegalArgumentException("from is not in graph");
if (!verticies.contains(to))
throw new IllegalArgumentException("to is not in graph");
List<Arrow<String, ArcData>> es2 = from.findEdges(to);
for (Arrow<String, ArcData> e2 : es2) {
if (e2 != null && cost == e2.getCost() &&
((data == null && e2.getData() == null) ||
(data != null && data.equals(e2.getData()))))
return false;
}
// FIX
if (Objects.equals(from, to)) {
from.getOutgoingArrows().add(arrow);
from.getIncomingArrows().add(arrow);
} else {
from.addEdge(arrow);
to.addEdge(arrow);
}
edges.add(arrow);
return true;
}
public abstract NodeType addNode(String instruction, Statement statement);
@SuppressWarnings("unchecked")
public Set<NodeType> getNodes() {
.map(vertex -> (NodeType) vertex)
.collect(Collectors.toSet());
}
public Set<Arc<ArcData>> getArcs() {
return getArrows().stream()
.map(arrow -> (Arc<ArcData>) arrow)
.collect(Collectors.toSet());
}
public String toString() {
return getNodes().stream()
.map(Node::toString)
.collect(Collectors.joining(System.lineSeparator()));
}