Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - add try/finally logic to wrap graph reentrant locks for layouts #1487

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ Development and Distribution License("CDDL") (collectively, the
*/
package org.gephi.layout.plugin.force.yifanHu;

import java.util.ArrayList;
import java.util.List;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Graph;
import org.gephi.graph.api.Node;
Expand All @@ -58,6 +56,9 @@ Development and Distribution License("CDDL") (collectively, the
import org.gephi.layout.spi.LayoutProperty;
import org.openide.util.NbBundle;

import java.util.ArrayList;
import java.util.List;

/**
* Hu's basic algorithm
*
Expand Down Expand Up @@ -229,88 +230,111 @@ public void initAlgo() {
return;
}
graph = graphModel.getGraphVisible();
energy = Float.POSITIVE_INFINITY;
for (Node n : graph.getNodes()) {
n.setLayoutData(new ForceVector());
graph.readLock();
try
{
energy = Float.POSITIVE_INFINITY;
for (Node n : graph.getNodes())
{
n.setLayoutData(new ForceVector());
}
progress = 0;
setConverged(false);
setStep(initialStep);
} finally {
graph.readUnlock();
}
progress = 0;
setConverged(false);
setStep(initialStep);
}

@Override
public void endAlgo() {
for (Node n : graph.getNodes()) {
n.setLayoutData(null);
graph.readLock();
try
{
for (Node n : graph.getNodes())
{
n.setLayoutData(null);
}
} finally {
graph.readUnlock();
}
}

@Override
public void goAlgo() {
graph = graphModel.getGraphVisible();
graph.readLock();
Node[] nodes = graph.getNodes().toArray();
for (Node n : nodes) {
if (n.getLayoutData() == null || !(n.getLayoutData() instanceof ForceVector)) {
n.setLayoutData(new ForceVector());
try
{
Node[] nodes = graph.getNodes().toArray();
for (Node n : nodes)
{
if (n.getLayoutData() == null || !(n.getLayoutData() instanceof ForceVector))
{
n.setLayoutData(new ForceVector());
}
}
}

// Evaluates n^2 inter node forces using BarnesHut.
QuadTree tree = QuadTree.buildTree(graph, getQuadTreeMaxLevel());
// Evaluates n^2 inter node forces using BarnesHut.
QuadTree tree = QuadTree.buildTree(graph, getQuadTreeMaxLevel());

// double electricEnergy = 0; ///////////////////////
// double springEnergy = 0; ///////////////////////
BarnesHut barnes = new BarnesHut(getNodeForce());
barnes.setTheta(getBarnesHutTheta());
for (Node node : nodes) {
ForceVector layoutData = node.getLayoutData();
// double electricEnergy = 0; ///////////////////////
// double springEnergy = 0; ///////////////////////
BarnesHut barnes = new BarnesHut(getNodeForce());
barnes.setTheta(getBarnesHutTheta());
for (Node node : nodes)
{
ForceVector layoutData = node.getLayoutData();

ForceVector f = barnes.calculateForce(node, tree);
layoutData.add(f);
// electricEnergy += f.getEnergy();
}
ForceVector f = barnes.calculateForce(node, tree);
layoutData.add(f);
// electricEnergy += f.getEnergy();
}

// Apply edge forces.
// Apply edge forces.

for (Edge e : graph.getEdges()) {
if (!e.getSource().equals(e.getTarget())) {
Node n1 = e.getSource();
Node n2 = e.getTarget();
ForceVector f1 = n1.getLayoutData();
ForceVector f2 = n2.getLayoutData();

ForceVector f = getEdgeForce().calculateForce(n1, n2);
f1.add(f);
f2.subtract(f);
for (Edge e : graph.getEdges())
{
if (!e.getSource().equals(e.getTarget()))
{
Node n1 = e.getSource();
Node n2 = e.getTarget();
ForceVector f1 = n1.getLayoutData();
ForceVector f2 = n2.getLayoutData();

ForceVector f = getEdgeForce().calculateForce(n1, n2);
f1.add(f);
f2.subtract(f);
}
}
}

// Calculate energy and max force.
energy0 = energy;
energy = 0;
double maxForce = 1;
for (Node n : nodes) {
ForceVector force = n.getLayoutData();
// Calculate energy and max force.
energy0 = energy;
energy = 0;
double maxForce = 1;
for (Node n : nodes)
{
ForceVector force = n.getLayoutData();

energy += force.getNorm();
maxForce = Math.max(maxForce, force.getNorm());
}
energy += force.getNorm();
maxForce = Math.max(maxForce, force.getNorm());
}

// Apply displacements on nodes.
for (Node n : nodes) {
if (!n.isFixed()) {
ForceVector force = n.getLayoutData();
// Apply displacements on nodes.
for (Node n : nodes)
{
if (!n.isFixed())
{
ForceVector force = n.getLayoutData();

force.multiply((float) (1.0 / maxForce));
getDisplacement().moveNode(n, force);
force.multiply((float) (1.0 / maxForce));
getDisplacement().moveNode(n, force);
}
}
postAlgo();
} finally {
graph.readUnlock();
}
postAlgo();
// springEnergy = energy - electricEnergy;
// System.out.println("electric: " + electricEnergy + " spring: " + springEnergy);
// System.out.println("energy0 = " + energy0 + " energy = " + energy);
graph.readUnlock();
}


Expand Down
Loading