public class HeadlessSimple {
public void script() {
//初始化一个工程和工作区间
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
//将各种模型和控制器导入到工作区间中
AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();//导入属性模型
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();//导入图模型
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();//导入预览模型
ImportController importController = Lookup.getDefault().lookup(ImportController.class);//导入导入控制器
FilterController filterController = Lookup.getDefault().lookup(FilterController.class);//导入过滤控制器
RankingController rankingController = Lookup.getDefault().lookup(RankingController.class);//导入排序控制器
//导入文件
Container container;
try {
File file = new File(getClass().getResource("/org/gephi/toolkit/demos/resources/polblogs.gml").toURI());
container = importController.importFile(file);
container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);
//Force DIRECTED
} catch (Exception ex) {
ex.printStackTrace();
return;
}
//将数据加入到图形API
importController.process(container, new DefaultProcessor(), workspace);
//检查图是否已经导入到工作区间中
DirectedGraph graph = graphModel.getDirectedGraph();
System.out.println("Nodes: " + graph.getNodeCount());
System.out.println("Edges: " + graph.getEdgeCount());
//过滤器的设置并将过滤后的结果显示出来
DegreeRangeFilter degreeFilter = new DegreeRangeFilter();
degreeFilter.init(graph);
degreeFilter.setRange(new Range(30, Integer.MAX_VALUE));
//Remove nodes with degree < 30
Query query = filterController.createQuery(degreeFilter);
GraphView view = filterController.filter(query);
graphModel.setVisibleView(view);
//Set the filter result as the visible view
//See visible graph stats
UndirectedGraph graphVisible = graphModel.getUndirectedGraphVisibl
e();
System.out.println("Nodes: " + graphVisible.getNodeCount());
System.out.println("Edges: " + graphVisible.getEdgeCount());
//Run YifanHuLayout for 100 passes - The layout always takes the current visible view
YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f));
layout.setGraphModel(graphModel);
layout.resetPropertiesValues();
layout.setOptimalDistance(200f);
for (int i = 0; i < 100 && layout.canAlgo(); i++) {
layout.goAlgo();
}
//计算中心紧密型
GraphDistance distance = new GraphDistance();
distance.setDirected(true);
distance.execute(graphModel, attributeModel);
//利用层级进行排序,并以颜色的形式展现出来
Ranking degreeRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, Ranking.DEGREE_RANKING);
AbstractColorTransformer
colorTransformer = (AbstractColorTransformer
) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_COLOR);
colorTransformer.setColors(new Color[]{new Color(0xFEF0D9), new Color(0xB30000)});
rankingController.transform(degreeRanking,colorTransformer);
//根据中心紧密度调节大小
AttributeColumn centralityColumn = attributeModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS);
Ranking centralityRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, centralityColumn.getId());
AbstractSizeTransformer sizeTransformer = (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_SIZE);
sizeTransformer.setMinSize(3);
sizeTransformer.setMaxSize(10);
rankingController.transform(centralityRanking,sizeTransformer);
//进行预览
model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
model.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY));
model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f));
model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));
//导出文件
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try {
ec.exportFile(new File("headless_simple.pdf"));
} catch (IOException ex) {
ex.printStackTrace();
return;
}
}