}
public void execute(Graph graph, AttributeModel attributeModel) {
//Look if the result column already exist and create it if needed
AttributeTable nodeTable = attributeModel.getNodeTable();
AttributeColumn col = nodeTable.getColumn(AVG_EUCLIDEAN_DISTANCE);
if (col == null) {
col = nodeTable.addColumn(AVG_EUCLIDEAN_DISTANCE, "Average Euclidean Distance", AttributeType.DOUBLE, AttributeOrigin.COMPUTED, 0.0);
}
//Lock to graph. This is important to have consistent results if another
//process is currently modifying it.
graph.readLock();
//Iterate on all nodes
Node[] nodes = graph.getNodes().toArray();
for (Node n : nodes) {
double avg = 0;
int count = 0;
if (useOnlyConnections) {
//Calculate distance with neighbors
for (Node m : graph.getNeighbors(n)) {
double xDist = n.getNodeData().x() - m.getNodeData().x();
double yDist = n.getNodeData().y() - m.getNodeData().y();
double dist = Math.sqrt(xDist * xDist + yDist * yDist);
avg = (dist + avg) / ++count;
}
} else {
//Calculate distance with all other nodes
for (Node m : nodes) {
if (n != m) {
double xDist = n.getNodeData().x() - m.getNodeData().x();
double yDist = n.getNodeData().y() - m.getNodeData().y();
double dist = Math.sqrt(xDist * xDist + yDist * yDist);
avg = (dist + avg) / ++count;
}
}
}
//Store the average in the node attribute
n.getAttributes().setValue(col.getIndex(), avg);
}
graph.readUnlock();
}