Class GexfUtil
java.lang.Object
cloud.opencode.base.graph.serializer.GexfUtil
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordVisual data for graph vertices. -
Method Summary
Modifier and TypeMethodDescriptionImport graph from GEXF 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.readFromFile(Path path) Read graph from GEXF file.readFromFile(Path path, Map<String, Map<String, String>> vertexAttributes, Map<String, GexfUtil.VisualData> visualData) Read graph from GEXF file with attribute and visual data maps.static <V> StringExport graph to GEXF format.static <V> StringtoGexf(Graph<V> graph, Map<V, Map<String, String>> vertexAttributes, Map<V, GexfUtil.VisualData> visualData) Export graph to GEXF format with attributes and visual data.static <V> voidwriteToFile(Graph<V> graph, Path path) Write graph to GEXF file.static <V> voidwriteToFile(Graph<V> graph, Path path, Map<V, Map<String, String>> vertexAttributes, Map<V, GexfUtil.VisualData> visualData) Write graph to GEXF file with attributes and visual data.
-
Method Details
-
toGexf
-
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
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 IOExceptionWrite 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
-
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
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 IOExceptionRead 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 | 如果读取失败
-