Skip to content

Commit a78df01

Browse files
author
brunomnsilva
committed
Pessimistic approach to high-rate calls to SmartGraphPanel#update().
Fixed SmartGraphPanel#getBounds() when panel is empty.
1 parent fe3adc5 commit a78df01

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

releases/JavaFXSmartGraph-0.9.jar

99 Bytes
Binary file not shown.

releases/JavaFXSmartGraph-0.9.zip

256 Bytes
Binary file not shown.

src/com/brunomnsilva/smartgraph/graph/DigraphEdgeList.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public DigraphEdgeList() {
3535

3636

3737
@Override
38-
public Collection<Edge<E, V>> incidentEdges(Vertex<V> inbound) throws InvalidVertexException {
38+
public synchronized Collection<Edge<E, V>> incidentEdges(Vertex<V> inbound) throws InvalidVertexException {
3939
checkVertex(inbound);
4040

4141
List<Edge<E, V>> incidentEdges = new ArrayList<>();
@@ -49,7 +49,7 @@ public Collection<Edge<E, V>> incidentEdges(Vertex<V> inbound) throws InvalidVer
4949
}
5050

5151
@Override
52-
public Collection<Edge<E, V>> outboundEdges(Vertex<V> outbound) throws InvalidVertexException {
52+
public synchronized Collection<Edge<E, V>> outboundEdges(Vertex<V> outbound) throws InvalidVertexException {
5353
checkVertex(outbound);
5454

5555
List<Edge<E, V>> outboundEdges = new ArrayList<>();
@@ -127,7 +127,7 @@ public int numEdges() {
127127
}
128128

129129
@Override
130-
public Collection<Vertex<V>> vertices() {
130+
public synchronized Collection<Vertex<V>> vertices() {
131131
List<Vertex<V>> list = new ArrayList<>();
132132
vertices.values().forEach((v) -> {
133133
list.add(v);
@@ -136,7 +136,7 @@ public Collection<Vertex<V>> vertices() {
136136
}
137137

138138
@Override
139-
public Collection<Edge<E, V>> edges() {
139+
public synchronized Collection<Edge<E, V>> edges() {
140140
List<Edge<E, V>> list = new ArrayList<>();
141141
edges.values().forEach((e) -> {
142142
list.add(e);
@@ -145,7 +145,7 @@ public Collection<Edge<E, V>> edges() {
145145
}
146146

147147
@Override
148-
public Vertex<V> opposite(Vertex<V> v, Edge<E, V> e) throws InvalidVertexException, InvalidEdgeException {
148+
public synchronized Vertex<V> opposite(Vertex<V> v, Edge<E, V> e) throws InvalidVertexException, InvalidEdgeException {
149149
checkVertex(v);
150150
MyEdge edge = checkEdge(e);
151151

src/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,24 @@ private void insertNodes() {
474474
Edge<E, V> firstEdge = incidentEdges.iterator().next();
475475
Vertex<V> opposite = theGraph.opposite(vertex, firstEdge);
476476
SmartGraphVertexNode<V> existing = vertexNodes.get(opposite);
477-
478-
/* TODO: fix -- the placing point can be set out of bounds*/
479-
Point2D p = UtilitiesPoint2D.rotate(existing.getPosition().add(50.0, 50.0),
480-
existing.getPosition(), Math.random() * 360);
481-
482-
x = p.getX();
483-
y = p.getY();
484-
477+
478+
if(existing == null) {
479+
/*
480+
Updates may be coming too fast and we can get out of sync.
481+
The opposite vertex exists in the (di)graph, but we have not yet
482+
created it for the panel. Therefore, its position is unknown,
483+
so place the vertex representation in the middle.
484+
*/
485+
x = mx;
486+
y = my;
487+
} else {
488+
/* TODO: fix -- the placing point can be set out of bounds*/
489+
Point2D p = UtilitiesPoint2D.rotate(existing.getPosition().add(50.0, 50.0),
490+
existing.getPosition(), Math.random() * 360);
491+
492+
x = p.getX();
493+
y = p.getY();
494+
}
485495
}
486496

487497
SmartGraphVertexNode newVertex = new SmartGraphVertexNode<>(vertex,
@@ -506,6 +516,15 @@ private void insertNodes() {
506516
SmartGraphVertexNode<V> graphVertexOut = vertexNodes.get(u);
507517
SmartGraphVertexNode<V> graphVertexIn = vertexNodes.get(v);
508518

519+
/*
520+
Updates may be coming too fast and we can get out of sync.
521+
Skip and wait for another update call, since they will surely
522+
be coming at this pace.
523+
*/
524+
if(graphVertexIn == null || graphVertexOut == null) {
525+
continue;
526+
}
527+
509528
graphVertexOut.addAdjacentVertex(graphVertexIn);
510529
graphVertexIn.addAdjacentVertex(graphVertexOut);
511530

@@ -600,7 +619,9 @@ private void removeVertice(SmartGraphVertexNode v) {
600619
private Bounds getPlotBounds() {
601620
double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE,
602621
maxX = Double.MIN_VALUE, maxY = Double.MIN_VALUE;
603-
622+
623+
if(vertexNodes.size() == 0) return new BoundingBox(0, 0, getWidth(), getHeight());
624+
604625
for (SmartGraphVertexNode<V> v : vertexNodes.values()) {
605626
minX = Math.min(minX, v.getCenterX());
606627
minY = Math.min(minY, v.getCenterY());

0 commit comments

Comments
 (0)