use of org.gephi.graph.api.GraphModel in project gephi by gephi.
the class GraphDistanceNGTest method testDirectedCyclicGraphBetweenness.
@Test
public void testDirectedCyclicGraphBetweenness() {
GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
GraphDistance d = new GraphDistance();
d.initializeStartValues();
DirectedGraph directedGraph = graphModel.getDirectedGraph();
HashMap<Node, Integer> indicies = d.createIndiciesMap(graphModel.getGraph());
HashMap<String, double[]> metricsMap = (HashMap) d.calculateDistanceMetrics(graphModel.getGraph(), indicies, true, false);
double[] betweenness = metricsMap.get(GraphDistance.BETWEENNESS);
Node n1 = directedGraph.getNode("0");
Node n3 = directedGraph.getNode("2");
int index1 = indicies.get(n1);
int index3 = indicies.get(n3);
assertEquals(betweenness[index1], 6.0, TOLERANCE);
assertEquals(betweenness[index3], 6.0, TOLERANCE);
}
use of org.gephi.graph.api.GraphModel in project gephi by gephi.
the class AttributeColumnsControllerImpl method setAttributeValue.
@Override
public boolean setAttributeValue(Object value, Element row, Column column) {
if (!canChangeColumnData(column)) {
return false;
}
Class targetType = column.getTypeClass();
if (value != null && !value.getClass().equals(targetType)) {
try {
GraphModel graphModel = column.getTable().getGraph().getModel();
String stringValue = AttributeUtils.print(value, graphModel.getTimeFormat(), graphModel.getTimeZone());
//Try to convert to target type from string representation
value = AttributeUtils.parse(stringValue, targetType);
} catch (Exception ex) {
//Could not parse
return false;
}
}
if (value == null && !canClearColumnData(column)) {
//Do not set a null value when the column can't have a null value
return false;
} else {
try {
if (value == null) {
row.removeAttribute(column);
} else {
row.setAttribute(column, value);
}
return true;
} catch (Exception e) {
Logger.getLogger("").log(Level.SEVERE, null, e);
return false;
}
}
}
use of org.gephi.graph.api.GraphModel in project gephi by gephi.
the class ManageColumnEstimators method getColumns.
public List<Column> getColumns() {
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
Table table;
if (Lookup.getDefault().lookup(DataTablesController.class).isNodeTableMode()) {
table = graphModel.getNodeTable();
} else {
table = graphModel.getEdgeTable();
}
List<Column> availableColumns = new ArrayList<>();
for (Column column : table) {
if (TimeMap.class.isAssignableFrom(column.getTypeClass())) {
availableColumns.add(column);
}
}
return availableColumns;
}
use of org.gephi.graph.api.GraphModel in project gephi by gephi.
the class ShortestPath method getListeners.
@Override
public ToolEventListener[] getListeners() {
listeners = new ToolEventListener[2];
listeners[0] = new NodeClickEventListener() {
@Override
public void clickNodes(Node[] nodes) {
Node n = nodes[0];
if (sourceNode == null) {
sourceNode = n;
shortestPathPanel.setResult("");
shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status2"));
} else if (n != sourceNode) {
color = shortestPathPanel.getColor();
Node targetNode = n;
GraphController gc = Lookup.getDefault().lookup(GraphController.class);
GraphModel gm = gc.getGraphModel();
AbstractShortestPathAlgorithm algorithm;
if (gm.isDirected()) {
algorithm = new BellmanFordShortestPathAlgorithm(gm.getDirectedGraphVisible(), sourceNode);
} else {
algorithm = new DijkstraShortestPathAlgorithm(gm.getGraphVisible(), sourceNode);
}
algorithm.compute();
double distance;
if ((distance = algorithm.getDistances().get(targetNode)) != Double.POSITIVE_INFINITY) {
targetNode.setColor(color);
VizController.getInstance().selectNode(targetNode);
Edge predecessorEdge = algorithm.getPredecessorIncoming(targetNode);
Node predecessor = algorithm.getPredecessor(targetNode);
while (predecessorEdge != null && predecessor != sourceNode) {
predecessorEdge.setColor(color);
VizController.getInstance().selectEdge(predecessorEdge);
predecessor.setColor(color);
VizController.getInstance().selectNode(predecessor);
predecessorEdge = algorithm.getPredecessorIncoming(predecessor);
predecessor = algorithm.getPredecessor(predecessor);
}
predecessorEdge.setColor(color);
VizController.getInstance().selectEdge(predecessorEdge);
sourceNode.setColor(color);
VizController.getInstance().selectNode(sourceNode);
shortestPathPanel.setResult(NbBundle.getMessage(ShortestPath.class, "ShortestPath.result", distance));
} else {
//No path
shortestPathPanel.setResult(NbBundle.getMessage(ShortestPath.class, "ShortestPath.noresult"));
}
sourceNode = null;
shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status1"));
}
}
};
listeners[1] = new MouseClickEventListener() {
@Override
public void mouseClick(int[] positionViewport, float[] position3d) {
if (sourceNode != null) {
//Cancel
shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status1"));
sourceNode = null;
} else {
VizController.getInstance().resetSelection();
}
}
};
return listeners;
}
use of org.gephi.graph.api.GraphModel in project gephi by gephi.
the class PageRankNGTest method testUndirectedWeightedGraphPageRank.
@Test
public void testUndirectedWeightedGraphPageRank() {
pc.newProject();
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
Node node1 = graphModel.factory().newNode("0");
Node node2 = graphModel.factory().newNode("1");
Node node3 = graphModel.factory().newNode("2");
Node node4 = graphModel.factory().newNode("3");
Node node5 = graphModel.factory().newNode("4");
Node node6 = graphModel.factory().newNode("5");
undirectedGraph.addNode(node1);
undirectedGraph.addNode(node2);
undirectedGraph.addNode(node3);
undirectedGraph.addNode(node4);
undirectedGraph.addNode(node5);
undirectedGraph.addNode(node6);
Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
Edge edge23 = graphModel.factory().newEdge(node2, node3, 0, 10, false);
Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
Edge edge61 = graphModel.factory().newEdge(node6, node1, false);
undirectedGraph.addEdge(edge12);
undirectedGraph.addEdge(edge23);
undirectedGraph.addEdge(edge34);
undirectedGraph.addEdge(edge45);
undirectedGraph.addEdge(edge56);
undirectedGraph.addEdge(edge61);
UndirectedGraph graph = graphModel.getUndirectedGraph();
PageRank pr = new PageRank();
double[] pageRank;
HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
pageRank = pr.calculatePagerank(graph, indicies, false, true, 0.001, 0.85);
int index1 = indicies.get(node1);
int index2 = indicies.get(node2);
int index3 = indicies.get(node3);
int index6 = indicies.get(node6);
double diff = 0.01;
double pr1 = pageRank[index1];
double pr2 = pageRank[index2];
double pr3 = pageRank[index3];
double pr6 = pageRank[index6];
assertTrue(Math.abs(pr2 - pr3) < diff);
assertTrue(pr1 < pr2);
assertTrue(pr1 < pr6);
}
Aggregations