java生成gexf的工具gexf4j的用法

github地址

gexf4j

maven依赖

 <dependency>
      <groupId>it.uniroma1.dis.wsngroup.gexf4j</groupId>
      <artifactId>gexf4j</artifactId>
      <version>1.0.0</version>
    </dependency>

demo代码

package com.neusoft;

import it.uniroma1.dis.wsngroup.gexf4j.core.EdgeType;
import it.uniroma1.dis.wsngroup.gexf4j.core.Gexf;
import it.uniroma1.dis.wsngroup.gexf4j.core.Graph;
import it.uniroma1.dis.wsngroup.gexf4j.core.Mode;
import it.uniroma1.dis.wsngroup.gexf4j.core.Node;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.Attribute;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeClass;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeList;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeType;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.GexfImpl;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.StaxGraphWriter;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.data.AttributeListImpl;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;


public class StaticGexfGraph {

    public static void main(String[] args) {
        Gexf gexf = new GexfImpl();
        Calendar date = Calendar.getInstance();
        
        gexf.getMetadata()
            .setLastModified(date.getTime())
            .setCreator("Gephi.org")
            .setDescription("A Web network");
        gexf.setVisualization(true);

        Graph graph = gexf.getGraph();
        graph.setDefaultEdgeType(EdgeType.UNDIRECTED).setMode(Mode.STATIC);
        
        AttributeList attrList = new AttributeListImpl(AttributeClass.NODE);
        graph.getAttributeLists().add(attrList);
        
        Attribute attUrl = attrList.createAttribute("class", AttributeType.INTEGER, "Class");
        Attribute attIndegree = attrList.createAttribute("pageranks", AttributeType.DOUBLE, "PageRank");

     
        
        Node gephi = graph.createNode("0");
        gephi
            .setLabel("郝大通")
            .getAttributeValues()
                .addValue(attUrl, "3")
                .addValue(attIndegree, "0.14658");

        
        Node webatlas = graph.createNode("1");
        webatlas
            .setLabel("郝大通")
                .getAttributeValues()
                .addValue(attUrl, "3")
                .addValue(attIndegree, "0.14658");

        gephi.connectTo("0", webatlas).setWeight(0.8f);

        StaxGraphWriter graphWriter = new StaxGraphWriter();
        File f = new File("static_graph_sample.gexf");
        Writer out;
        try {
            out =  new FileWriter(f, false);
            graphWriter.writeToStream(gexf, out, "UTF-8");
            System.out.println(f.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

生成的gexf文件

<?xml version='1.0' encoding='UTF-8'?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.2draft/viz" version="1.2">
 <meta lastmodifieddate="2018-09-17">
  <creator>Gephi.org</creator>
  <description>A Web network</description>
 </meta>
 <graph defaultedgetype="undirected" idtype="string" mode="static">
  <attributes class="node" mode="static">
   <attribute id="class" title="Class" type="integer"/>
   <attribute id="pageranks" title="PageRank" type="double"/>
  </attributes>
  <nodes count="2">
   <node id="0" label="郝大通">
    <attvalues>
     <attvalue for="class" value="3"/>
     <attvalue for="pageranks" value="0.14658"/>
    </attvalues>
   </node>
   <node id="1" label="郝大通">
    <attvalues>
     <attvalue for="class" value="3"/>
     <attvalue for="pageranks" value="0.14658"/>
    </attvalues>
   </node>
  </nodes>
  <edges count="1">
   <edge id="0" source="0" target="1" type="undirected" weight="0.8"/>
  </edges>
 </graph>
</gexf>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,355评论 3 283
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,140评论 25 709
  • 今天很开心! 今天和自己的小部员聚餐,过程与结果出乎我的意料,无比和谐与温馨。简单的说就是非常开心! 其实开始知道...
    会子cc阅读 1,400评论 0 0
  • 昨天在地铁上看到的一幕,使我想要写一篇文章。列车进站停靠之后,乘客有序进入车厢,前面的乘客把空余的位置填满之后,...
    Appler哆啦A梦阅读 2,844评论 0 0
  • 起初你是不存在的 因为我还没有和你妈妈在一起 当某年某月某天某时 两个细胞的碰撞 你产生了 你开始成为一个寄生虫 ...
    布老头和他的家人们阅读 1,625评论 0 0

友情链接更多精彩内容