Code: Select all
public class GephiBuilder {
ProjectController pc;
Workspace ws;
GraphModel gm;
DirectedGraph directedGraph;
DynamicGraph dynamicGraph;
static Logger logger = Logger.getLogger(GephiBuilder.class);
/**
*
*/
public GephiBuilder() { }
/**
*
*/
public void newGraph() {
logger.info("Creating new gephi objects.");
pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
ws = pc.getCurrentWorkspace();
gm = Lookup.getDefault().lookup(GraphController.class).getModel();
logger.info("Creating a directed graph.");
directedGraph = gm.getDirectedGraph();
}
/**
*
*/
public void addNode(String id, String label) {
logger.info("Adding a node. Id: " + id + " Label: " + label);
Node n = gm.factory().newNode(id);
n.getNodeData().setLabel(label);
n.getNodeData().setSize((float) 2.5);
directedGraph.addNode(n);
}
public void AddEdge(String startNodeId,
String endNodeId, float weight,
boolean directed) throws MissingGephiNodeExcpetion {
//Check to make sure that nodes have been created. Error if true
if(!nodeExists(startNodeId)) {
throw new
MissingGephiNodeExcpetion("Node " + startNodeId + " was not found in the graph.");
}
if(!nodeExists(endNodeId)) {
throw new
MissingGephiNodeExcpetion("Node " + endNodeId + " was not found in the graph.");
}
Node start = directedGraph.getNode(startNodeId);
Node end = directedGraph.getNode(endNodeId);
logger.info("Adding an edge.");
logger.info("Edge start: " + start.getId() + " end: " + end.getId());
addEdge(start, end, weight, directed);
}
/**
*
*/
public void addEdge(Node start, Node end, float weight, boolean directed) {
Edge e = gm.factory().newEdge(start, end, weight, directed);
directedGraph.addEdge(e);
}
/**
*
*/
public void buildGraph() {
//DynamicModel dynamicModel = Lookup.getDefault().lookup(DynamicController.class).getModel();
//dynamicGraph = new DynamicGraph();
}
public File exportGraph() {
File retvalue = new File("io_gexf.gexf");
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
GraphExporter exporter = (GraphExporter) ec.getExporter("gexf");
try {
ec.exportFile(retvalue);
return retvalue;
} catch (IOException ex) {
return null;
}
}
/**
*
*/
public void renderGraph() {
ImportController ic = Lookup.getDefault().lookup(ImportController.class);
Container container = null;
try{
container = ic.importFile(exportGraph());
}
catch(Exception ex) {
return;
}
ic.process(container, new DefaultProcessor(), ws);
//Preview configuration
PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
PreviewModel previewModel = previewController.getModel();
previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.WHITE));
previewModel.getProperties().putValue(PreviewProperty.EDGE_CURVED, Boolean.FALSE);
previewModel.getProperties().putValue(PreviewProperty.EDGE_OPACITY, 50);
previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f);
previewModel.getProperties().putValue(PreviewProperty.BACKGROUND_COLOR, Color.BLACK);
previewController.refreshPreview();
//New Processing target, get the PApplet
ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET);
PApplet applet = target.getApplet();
applet.init();
//Refresh the preview and reset the zoom
previewController.render(target);
target.refresh();
target.resetZoom();
//Add the applet to a JFrame and display
JFrame frame = new JFrame("Test Preview");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applet, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private boolean nodeExists(String nodeId) {
if(directedGraph.getNode(nodeId) != null)
return true;
else
return false;
}
}
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2" xmlns:viz="http://www.gexf.net/1.2draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd">
<meta lastmodifieddate="2013-07-14">
<creator>Gephi 0.8.1</creator>
<description></description>
</meta>
<graph defaultedgetype="directed" mode="static">
<nodes>
<node id="1.2.1" label="Test 2.1">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="221.06403" y="311.56934" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.2.2" label="Test 2.2">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="332.10828" y="243.84601" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.0" label="Test">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="-259.47906" y="159.45837" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.4" label="Test 4">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="-308.87735" y="-52.75769" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.3" label="Test 3">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="397.72437" y="129.328" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.2" label="Test 2">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="-208.681" y="-37.868225" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
<node id="1.1" label="Test 1">
<attvalues></attvalues>
<viz:size value="2.5"></viz:size>
<viz:position x="505.15796" y="54.056763" z="0.0"></viz:position>
<viz:color r="153" g="153" b="153"></viz:color>
</node>
</nodes>
<edges>
<edge source="1.0" target="1.4">
<attvalues></attvalues>
</edge>
<edge source="1.0" target="1.3">
<attvalues></attvalues>
</edge>
<edge source="1.0" target="1.2">
<attvalues></attvalues>
</edge>
<edge source="1.0" target="1.1">
<attvalues></attvalues>
</edge>
<edge source="1.2" target="1.2.1">
<attvalues></attvalues>
</edge>
<edge source="1.2" target="1.2.2">
<attvalues></attvalues>
</edge>
</edges>
</graph>
</gexf>