Class GexfUtil

java.lang.Object
cloud.opencode.base.graph.serializer.GexfUtil

public final class GexfUtil extends Object
GEXF Util - GEXF Format Import/Export GEXF工具类 - GEXF格式导入/导出

Utility class for importing and exporting graphs in GEXF format. GEXF (Graph Exchange XML Format) is designed for complex network analysis and visualization tools like Gephi.

用于导入和导出GEXF格式图的工具类。GEXF是为复杂网络分析和可视化工具(如Gephi)设计的格式。

Features | 主要功能:

  • Export graphs to GEXF XML | 导出图到GEXF XML
  • Import graphs from GEXF XML | 从GEXF XML导入图
  • Support for vertex/edge attributes | 支持顶点/边属性
  • Support for edge weights | 支持边权重
  • Support for visualization data (positions, colors, sizes) | 支持可视化数据
  • Compatible with Gephi | 兼容Gephi

Usage Examples | 使用示例:

// Export to GEXF string
String gexf = GexfUtil.toGexf(graph);

// Export to file
GexfUtil.writeToFile(graph, Path.of("graph.gexf"));

// Export with visualization data
Map<String, VisualData> visuals = new HashMap<>();
visuals.put("A", new VisualData(100, 200, 10, "#FF0000"));
String gexf = GexfUtil.toGexf(graph, null, visuals);

// Import from file
Graph<String> graph = GexfUtil.readFromFile(Path.of("graph.gexf"));

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty string for null graph) - 空值安全: 是(null图返回空字符串)

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes (returns empty string for null graph) - 空值安全: 是(null图返回空字符串)

Performance | 性能特性:

  • Time complexity: O(V + E) - 时间复杂度: O(V + E)
  • Space complexity: O(V + E) - 空间复杂度: O(V + E)
Since:
JDK 25, opencode-base-graph V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • toGexf

      public static <V> String toGexf(Graph<V> graph)
      Export graph to GEXF format. 将图导出为GEXF格式。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      Returns:
      GEXF XML string | GEXF XML字符串
    • toGexf

      public static <V> String toGexf(Graph<V> graph, Map<V, Map<String,String>> vertexAttributes, Map<V, GexfUtil.VisualData> visualData)
      Export graph to GEXF format with attributes and visual data. 将图导出为带属性和可视化数据的GEXF格式。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      vertexAttributes - vertex attributes (vertex -> key -> value) | 顶点属性
      visualData - visualization data (vertex -> visual data) | 可视化数据
      Returns:
      GEXF XML string | GEXF XML字符串
    • writeToFile

      public static <V> void writeToFile(Graph<V> graph, Path path) throws IOException
      Write graph to GEXF file. 将图写入GEXF文件。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      path - the file path | 文件路径
      Throws:
      IOException - if writing fails | 如果写入失败
    • writeToFile

      public static <V> void writeToFile(Graph<V> graph, Path path, Map<V, Map<String,String>> vertexAttributes, Map<V, GexfUtil.VisualData> visualData) throws IOException
      Write graph to GEXF file with attributes and visual data. 将带属性和可视化数据的图写入GEXF文件。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      path - the file path | 文件路径
      vertexAttributes - vertex attributes | 顶点属性
      visualData - visualization data | 可视化数据
      Throws:
      IOException - if writing fails | 如果写入失败
    • fromGexf

      public static Graph<String> fromGexf(String gexf)
      Import graph from GEXF string. 从GEXF字符串导入图。
      Parameters:
      gexf - the GEXF XML string | GEXF XML字符串
      Returns:
      the imported graph | 导入的图
    • fromGexf

      public static Graph<String> fromGexf(String gexf, Map<String, Map<String,String>> vertexAttributes, Map<String, GexfUtil.VisualData> visualData)
      Import graph from GEXF string with attribute and visual data maps. 从带属性和可视化数据映射的GEXF字符串导入图。
      Parameters:
      gexf - the GEXF XML string | GEXF XML字符串
      vertexAttributes - output map for vertex attributes | 顶点属性输出映射
      visualData - output map for visual data | 可视化数据输出映射
      Returns:
      the imported graph | 导入的图
    • readFromFile

      public static Graph<String> readFromFile(Path path) throws IOException
      Read graph from GEXF file. 从GEXF文件读取图。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the imported graph | 导入的图
      Throws:
      IOException - if reading fails | 如果读取失败
    • readFromFile

      public static Graph<String> readFromFile(Path path, Map<String, Map<String,String>> vertexAttributes, Map<String, GexfUtil.VisualData> visualData) throws IOException
      Read graph from GEXF file with attribute and visual data maps. 从带属性和可视化数据映射的GEXF文件读取图。
      Parameters:
      path - the file path | 文件路径
      vertexAttributes - output map for vertex attributes | 顶点属性输出映射
      visualData - output map for visual data | 可视化数据输出映射
      Returns:
      the imported graph | 导入的图
      Throws:
      IOException - if reading fails | 如果读取失败