|
| 1 | +module GraphIOGMLExt |
| 2 | + |
| 3 | +using Graphs |
| 4 | +import Graphs: loadgraph, loadgraphs, savegraph |
| 5 | + |
| 6 | +@static if isdefined(Base, :get_extension) |
| 7 | + using GraphIO |
| 8 | + using ParserCombinator |
| 9 | + import GraphIO.GML.GMLFormat |
| 10 | +else # not required for julia >= v1.9 |
| 11 | + using ..GraphIO |
| 12 | + using ..ParserCombinator |
| 13 | + import ..GraphIO.GML.GMLFormat |
| 14 | +end |
| 15 | + |
| 16 | +function _gml_read_one_graph(gs, dir) |
| 17 | + nodes = [x[:id] for x in gs[:node]] |
| 18 | + if dir |
| 19 | + g = DiGraph(length(nodes)) |
| 20 | + else |
| 21 | + g = Graph(length(nodes)) |
| 22 | + end |
| 23 | + mapping = Dict{Int,Int}() |
| 24 | + for (i, n) in enumerate(nodes) |
| 25 | + mapping[n] = i |
| 26 | + end |
| 27 | + sds = [(Int(x[:source]), Int(x[:target])) for x in gs[:edge]] |
| 28 | + for (s, d) in (sds) |
| 29 | + add_edge!(g, mapping[s], mapping[d]) |
| 30 | + end |
| 31 | + return g |
| 32 | +end |
| 33 | + |
| 34 | +function loadgml(io::IO, gname::String) |
| 35 | + p = Parsers.GML.parse_dict(read(io, String)) |
| 36 | + for gs in p[:graph] |
| 37 | + dir = Bool(get(gs, :directed, 0)) |
| 38 | + graphname = get(gs, :label, dir ? "digraph" : "graph") |
| 39 | + |
| 40 | + (gname == graphname) && return _gml_read_one_graph(gs, dir) |
| 41 | + end |
| 42 | + return error("Graph $gname not found") |
| 43 | +end |
| 44 | + |
| 45 | +function loadgml_mult(io::IO) |
| 46 | + p = Parsers.GML.parse_dict(read(io, String)) |
| 47 | + graphs = Dict{String,AbstractGraph}() |
| 48 | + for gs in p[:graph] |
| 49 | + dir = Bool(get(gs, :directed, 0)) |
| 50 | + graphname = get(gs, :label, dir ? "digraph" : "graph") |
| 51 | + graphs[graphname] = _gml_read_one_graph(gs, dir) |
| 52 | + end |
| 53 | + return graphs |
| 54 | +end |
| 55 | + |
| 56 | +""" |
| 57 | + savegml(f, g, gname="graph") |
| 58 | +
|
| 59 | +Write a graph `g` with name `gname` to an IO stream `io` in the |
| 60 | +[GML](https://en.wikipedia.org/wiki/Graph_Modelling_Language) format. Return 1. |
| 61 | +""" |
| 62 | +function savegml(io::IO, g::AbstractGraph, gname::String="") |
| 63 | + println(io, "graph") |
| 64 | + println(io, "[") |
| 65 | + length(gname) > 0 && println(io, "label \"$gname\"") |
| 66 | + is_directed(g) && println(io, "directed 1") |
| 67 | + for i in 1:nv(g) |
| 68 | + println(io, "\tnode") |
| 69 | + println(io, "\t[") |
| 70 | + println(io, "\t\tid $i") |
| 71 | + println(io, "\t]") |
| 72 | + end |
| 73 | + for e in edges(g) |
| 74 | + s, t = Tuple(e) |
| 75 | + println(io, "\tedge") |
| 76 | + println(io, "\t[") |
| 77 | + println(io, "\t\tsource $s") |
| 78 | + println(io, "\t\ttarget $t") |
| 79 | + println(io, "\t]") |
| 80 | + end |
| 81 | + println(io, "]") |
| 82 | + return 1 |
| 83 | +end |
| 84 | + |
| 85 | +""" |
| 86 | + savegml_mult(io, graphs) |
| 87 | +Write a dictionary of (name=>graph) to an IO stream `io` Return number of graphs written. |
| 88 | +""" |
| 89 | +function savegml_mult(io::IO, graphs::Dict) |
| 90 | + ng = 0 |
| 91 | + for (gname, g) in graphs |
| 92 | + ng += savegml(io, g, gname) |
| 93 | + end |
| 94 | + return ng |
| 95 | +end |
| 96 | +loadgraph(io::IO, gname::String, ::GMLFormat) = loadgml(io, gname) |
| 97 | +loadgraphs(io::IO, ::GMLFormat) = loadgml_mult(io) |
| 98 | +savegraph(io::IO, g::AbstractGraph, gname::String, ::GMLFormat) = savegml(io, g, gname) |
| 99 | +savegraph(io::IO, d::Dict, ::GMLFormat) = savegml_mult(io, d) |
| 100 | + |
| 101 | +end |
0 commit comments