Class GraphMLUtil

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

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

Utility class for importing and exporting graphs in GraphML format. GraphML is an XML-based file format for graphs.

用于导入和导出GraphML格式图的工具类。GraphML是一种基于XML的图文件格式。

Features | 主要功能:

  • Export graphs to GraphML XML | 导出图到GraphML XML
  • Import graphs from GraphML XML | 从GraphML XML导入图
  • Support for vertex/edge attributes | 支持顶点/边属性
  • Support for edge weights | 支持边权重
  • File and string I/O | 文件和字符串I/O

Usage Examples | 使用示例:

// Export to GraphML string
String graphml = GraphMLUtil.toGraphML(graph);

// Export to file
GraphMLUtil.writeToFile(graph, Path.of("graph.graphml"));

// Import from string
Graph<String> graph = GraphMLUtil.fromGraphML(graphmlString);

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

// Export with custom vertex attributes
Map<String, Map<String, String>> vertexAttrs = new HashMap<>();
vertexAttrs.put("A", Map.of("label", "Node A", "color", "red"));
String graphml = GraphMLUtil.toGraphML(graph, vertexAttrs, null);

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

    • toGraphML

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

      public static <V> String toGraphML(Graph<V> graph, Map<V, Map<String,String>> vertexAttributes, Map<String, Map<String,String>> edgeAttributes)
      Export graph to GraphML format with attributes. 将图导出为带属性的GraphML格式。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      vertexAttributes - vertex attributes (vertex -> key -> value) | 顶点属性
      edgeAttributes - edge attributes (from:to -> key -> value) | 边属性
      Returns:
      GraphML XML string | GraphML XML字符串
    • writeToFile

      public static <V> void writeToFile(Graph<V> graph, Path path) throws IOException
      Write graph to GraphML file. 将图写入GraphML文件。
      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<String, Map<String,String>> edgeAttributes) throws IOException
      Write graph to GraphML file with attributes. 将带属性的图写入GraphML文件。
      Type Parameters:
      V - the vertex type | 顶点类型
      Parameters:
      graph - the graph to export | 要导出的图
      path - the file path | 文件路径
      vertexAttributes - vertex attributes | 顶点属性
      edgeAttributes - edge attributes | 边属性
      Throws:
      IOException - if writing fails | 如果写入失败
    • fromGraphML

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

      public static Graph<String> fromGraphML(String graphml, Map<String, Map<String,String>> vertexAttributes, Map<String, Map<String,String>> edgeAttributes)
      Import graph from GraphML string with attribute maps. 从带属性映射的GraphML字符串导入图。
      Parameters:
      graphml - the GraphML XML string | GraphML XML字符串
      vertexAttributes - output map for vertex attributes | 顶点属性输出映射
      edgeAttributes - output map for edge attributes | 边属性输出映射
      Returns:
      the imported graph | 导入的图
    • readFromFile

      public static Graph<String> readFromFile(Path path) throws IOException
      Read graph from GraphML file. 从GraphML文件读取图。
      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, Map<String,String>> edgeAttributes) throws IOException
      Read graph from GraphML file with attribute maps. 从带属性映射的GraphML文件读取图。
      Parameters:
      path - the file path | 文件路径
      vertexAttributes - output map for vertex attributes | 顶点属性输出映射
      edgeAttributes - output map for edge attributes | 边属性输出映射
      Returns:
      the imported graph | 导入的图
      Throws:
      IOException - if reading fails | 如果读取失败