use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class AverageEuclideanDistance method execute.
@Override
public void execute(GraphModel graphModel) {
Graph graph = graphModel.getGraphVisible();
//Look if the result column already exist and create it if needed
Table nodeTable = graphModel.getNodeTable();
Column col = nodeTable.getColumn(AVG_EUCLIDEAN_DISTANCE);
if (col == null) {
col = nodeTable.addColumn(AVG_EUCLIDEAN_DISTANCE, "Average Euclidean Distance", Double.class, Origin.DATA);
}
//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.x() - m.x();
double yDist = n.y() - m.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.x() - m.x();
double yDist = n.y() - m.y();
double dist = Math.sqrt(xDist * xDist + yDist * yDist);
avg = (dist + avg) / ++count;
}
}
}
//Store the average in the node attribute
n.setAttribute(col, avg);
}
graph.readUnlock();
}
use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.
the class AddNodesTool method getListeners.
@Override
public ToolEventListener[] getListeners() {
return new ToolEventListener[] { new MouseClickEventListener() {
@Override
public void mouseClick(int[] positionViewport, float[] position3d) {
//Get current graph
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
Graph graph = gc.getGraphModel().getGraph();
GraphFactory factory = gc.getGraphModel().factory();
//Add node
Node node = factory.newNode();
node.setX(position3d[0]);
node.setY(position3d[1]);
node.setSize(10f);
graph.addNode(node);
}
}, new NodeClickEventListener() {
@Override
public void clickNodes(Node[] nodes) {
//Get mouse position
float[] position3d = VizController.getInstance().getGraphIO().getMousePosition3d();
//Get current graph
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
Graph graph = gc.getGraphModel().getGraph();
GraphFactory factory = gc.getGraphModel().factory();
//Add node
Node node = factory.newNode();
node.setX(position3d[0]);
node.setY(position3d[1]);
node.setSize(10f);
graph.addNode(node);
//Add edges with the clicked nodes
for (Node n : nodes) {
Edge edge = factory.newEdge(node, n);
graph.addEdge(edge);
}
}
} };
}
use of org.gephi.graph.api.Graph in project gephi by gephi.
the class StatisticsControllerImpl method executeDynamic.
private void executeDynamic(DynamicStatistics statistics, DynamicLongTask dynamicLongTask) {
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getGraphModel();
double window = statistics.getWindow();
double tick = statistics.getTick();
GraphView currentView = graphModel.getVisibleView();
Interval bounds = statistics.getBounds();
if (bounds == null) {
if (currentView.isMainView()) {
bounds = graphModel.getTimeBounds();
} else {
bounds = currentView.getTimeInterval();
}
statistics.setBounds(bounds);
}
if (dynamicLongTask != null) {
//Count
int c = (int) ((bounds.getHigh() - window - bounds.getLow()) / tick);
dynamicLongTask.start(c);
}
//Init
statistics.execute(graphModel);
//Loop
for (double low = bounds.getLow(); low <= bounds.getHigh() - window; low += tick) {
double high = low + window;
Graph graph = graphModel.getGraphVisible();
graph.writeLock();
try {
GraphView view = graphModel.createView();
Subgraph g = graphModel.getGraph(view);
TimeIndex<Node> nodeIndex = graphModel.getNodeTimeIndex(currentView);
if (Double.isInfinite(nodeIndex.getMinTimestamp()) && Double.isInfinite(nodeIndex.getMaxTimestamp())) {
for (Node node : graph.getNodes()) {
g.addNode(node);
}
} else {
for (Node node : nodeIndex.get(new Interval(low, high))) {
g.addNode(node);
}
}
TimeIndex<Edge> edgeIndex = graphModel.getEdgeTimeIndex(currentView);
if (Double.isInfinite(edgeIndex.getMinTimestamp()) && Double.isInfinite(edgeIndex.getMaxTimestamp())) {
for (Edge edge : graph.getEdges()) {
if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
g.addEdge(edge);
}
}
} else {
for (Edge edge : edgeIndex.get(new Interval(low, high))) {
if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
g.addEdge(edge);
}
}
}
statistics.loop(g.getView(), new Interval(low, high));
} finally {
graph.writeUnlock();
}
//Cancelled?
if (dynamicLongTask != null && dynamicLongTask.isCancelled()) {
return;
} else if (dynamicLongTask != null) {
dynamicLongTask.progress();
}
}
statistics.end();
model.addReport(statistics);
}
use of org.gephi.graph.api.Graph in project gephi by gephi.
the class ClusteringCoefficient method execute.
@Override
public void execute(GraphModel graphModel) {
isDirected = graphModel.isDirected();
Graph graph = null;
if (isDirected) {
graph = graphModel.getDirectedGraphVisible();
} else {
graph = graphModel.getUndirectedGraphVisible();
}
execute(graph);
}
use of org.gephi.graph.api.Graph in project gephi by gephi.
the class FilterControllerImpl method exportToLabelVisible.
@Override
public void exportToLabelVisible(Query query) {
Graph result;
if (model.getCurrentQuery() == query) {
GraphView view = model.getCurrentResult();
if (view == null) {
return;
}
result = model.getGraphModel().getGraph(view);
} else {
FilterProcessor processor = new FilterProcessor();
result = (Graph) processor.process((AbstractQueryImpl) query, model.getGraphModel());
}
Graph fullGraph = model.getGraphModel().getGraph();
fullGraph.writeLock();
try {
for (Node n : fullGraph.getNodes()) {
boolean inView = result.contains(n);
n.getTextProperties().setVisible(inView);
}
for (Edge e : fullGraph.getEdges()) {
boolean inView = result.contains(e);
e.getTextProperties().setVisible(inView);
}
} finally {
fullGraph.readUnlockAll();
fullGraph.writeUnlock();
}
}
Aggregations