Skip to content

Commit c655409

Browse files
committed
Formats outside @require
1 parent fbde6c3 commit c655409

11 files changed

+394
-371
lines changed

src/DOT/Dot.jl

+4-73
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module DOT
22

3-
using GraphIO.ParserCombinator.Parsers
3+
using Requires
44
using Graphs
55
using Graphs: AbstractGraphFormat
66

@@ -10,79 +10,10 @@ export DOTFormat
1010

1111
struct DOTFormat <: AbstractGraphFormat end
1212

13-
function savedot(io::IO, g::Graphs.AbstractGraph, gname::String = "")
14-
isdir = Graphs.is_directed(g)
15-
println(io,(isdir ? "digraph " : "graph ") * gname * " {")
16-
for i in Graphs.vertices(g)
17-
println(io,"\t" * string(i))
13+
function __init__()
14+
@require ParserCombinator="fae87a5f-d1ad-5cf0-8f61-c941e1580b46" begin
15+
include("Dot_conditional.jl")
1816
end
19-
if isdir
20-
for u in Graphs.vertices(g)
21-
out_nbrs = Graphs.outneighbors(g, u)
22-
length(out_nbrs) == 0 && continue
23-
println(io, "\t" * string(u) * " -> {" * join(out_nbrs,',') * "}")
24-
end
25-
else
26-
for e in Graphs.edges(g)
27-
source = string(Graphs.src(e))
28-
dest = string(Graphs.dst(e))
29-
println(io, "\t" * source * " -- " * dest)
30-
end
31-
end
32-
println(io,"}")
33-
return 1
3417
end
3518

36-
function savedot_mult(io::IO, graphs::Dict)
37-
ng = 0
38-
for (gname, g) in graphs
39-
ng += savedot(io, g, gname)
40-
end
41-
return ng
42-
end
43-
44-
function _dot_read_one_graph(pg::Parsers.DOT.Graph)
45-
isdir = pg.directed
46-
nvg = length(Parsers.DOT.nodes(pg))
47-
nodedict = Dict(zip(collect(Parsers.DOT.nodes(pg)), 1:nvg))
48-
if isdir
49-
g = Graphs.DiGraph(nvg)
50-
else
51-
g = Graphs.Graph(nvg)
52-
end
53-
for es in Parsers.DOT.edges(pg)
54-
s = nodedict[es[1]]
55-
d = nodedict[es[2]]
56-
add_edge!(g, s, d)
57-
end
58-
return g
59-
end
60-
61-
_name(pg::Parsers.DOT.Graph) =
62-
pg.id !== nothing ? pg.id.id :
63-
Parsers.DOT.StringID(pg.directed ? "digraph" : "graph")
64-
65-
function loaddot(io::IO, gname::String)
66-
p = Parsers.DOT.parse_dot(read(io, String))
67-
for pg in p
68-
_name(pg) == gname && return _dot_read_one_graph(pg)
69-
end
70-
error("Graph $gname not found")
71-
end
72-
73-
function loaddot_mult(io::IO)
74-
p = Parsers.DOT.parse_dot(read(io, String))
75-
graphs = Dict{String,AbstractGraph}()
76-
77-
for pg in p
78-
graphs[_name(pg)] = _dot_read_one_graph(pg)
79-
end
80-
return graphs
81-
end
82-
83-
loadgraph(io::IO, gname::String, ::DOTFormat) = loaddot(io, gname)
84-
loadgraphs(io::IO, ::DOTFormat) = loaddot_mult(io)
85-
savegraph(io::IO, g::AbstractGraph, gname::String, ::DOTFormat) = savedot(io, g, gname)
86-
savegraph(io::IO, d::Dict, ::DOTFormat) = savedot_mult(io, d)
87-
8819
end #module

src/DOT/Dot_conditional.jl

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using .ParserCombinator.Parsers
2+
3+
function savedot(io::IO, g::Graphs.AbstractGraph, gname::String = "")
4+
isdir = Graphs.is_directed(g)
5+
println(io,(isdir ? "digraph " : "graph ") * gname * " {")
6+
for i in Graphs.vertices(g)
7+
println(io,"\t" * string(i))
8+
end
9+
if isdir
10+
for u in Graphs.vertices(g)
11+
out_nbrs = Graphs.outneighbors(g, u)
12+
length(out_nbrs) == 0 && continue
13+
println(io, "\t" * string(u) * " -> {" * join(out_nbrs,',') * "}")
14+
end
15+
else
16+
for e in Graphs.edges(g)
17+
source = string(Graphs.src(e))
18+
dest = string(Graphs.dst(e))
19+
println(io, "\t" * source * " -- " * dest)
20+
end
21+
end
22+
println(io,"}")
23+
return 1
24+
end
25+
26+
function savedot_mult(io::IO, graphs::Dict)
27+
ng = 0
28+
for (gname, g) in graphs
29+
ng += savedot(io, g, gname)
30+
end
31+
return ng
32+
end
33+
34+
function _dot_read_one_graph(pg::Parsers.DOT.Graph)
35+
isdir = pg.directed
36+
nvg = length(Parsers.DOT.nodes(pg))
37+
nodedict = Dict(zip(collect(Parsers.DOT.nodes(pg)), 1:nvg))
38+
if isdir
39+
g = Graphs.DiGraph(nvg)
40+
else
41+
g = Graphs.Graph(nvg)
42+
end
43+
for es in Parsers.DOT.edges(pg)
44+
s = nodedict[es[1]]
45+
d = nodedict[es[2]]
46+
add_edge!(g, s, d)
47+
end
48+
return g
49+
end
50+
51+
_name(pg::Parsers.DOT.Graph) =
52+
pg.id !== nothing ? pg.id.id :
53+
Parsers.DOT.StringID(pg.directed ? "digraph" : "graph")
54+
55+
function loaddot(io::IO, gname::String)
56+
p = Parsers.DOT.parse_dot(read(io, String))
57+
for pg in p
58+
_name(pg) == gname && return _dot_read_one_graph(pg)
59+
end
60+
error("Graph $gname not found")
61+
end
62+
63+
function loaddot_mult(io::IO)
64+
p = Parsers.DOT.parse_dot(read(io, String))
65+
graphs = Dict{String,AbstractGraph}()
66+
67+
for pg in p
68+
graphs[_name(pg)] = _dot_read_one_graph(pg)
69+
end
70+
return graphs
71+
end
72+
73+
loadgraph(io::IO, gname::String, ::DOTFormat) = loaddot(io, gname)
74+
loadgraphs(io::IO, ::DOTFormat) = loaddot_mult(io)
75+
savegraph(io::IO, g::AbstractGraph, gname::String, ::DOTFormat) = savedot(io, g, gname)
76+
savegraph(io::IO, d::Dict, ::DOTFormat) = savedot_mult(io, d)

src/GEXF/Gexf.jl

+4-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module GEXF
22

3-
using GraphIO.EzXML
3+
using Requires
44
using Graphs
55
using Graphs: AbstractGraph, AbstractGraphFormat
66

@@ -10,46 +10,11 @@ export GEXFFormat
1010

1111
# TODO: implement readgexf
1212
struct GEXFFormat <: AbstractGraphFormat end
13-
"""
14-
savegexf(f, g, gname)
1513

16-
Write a graph `g` with name `gname` to an IO stream `io` in the
17-
[Gexf](http://gexf.net/format/) format. Return 1 (number of graphs written).
18-
"""
19-
function savegexf(io::IO, g::Graphs.AbstractGraph, gname::String)
20-
xdoc = XMLDocument()
21-
xroot = setroot!(xdoc, ElementNode("gexf"))
22-
xroot["xmlns"] = "http://www.gexf.net/1.2draft"
23-
xroot["version"] = "1.2"
24-
xroot["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
25-
xroot["xsi:schemaLocation"] = "http://www.gexf.net/1.2draft/gexf.xsd"
26-
27-
xmeta = addelement!(xroot, "meta")
28-
addelement!(xmeta, "description", gname)
29-
xg = addelement!(xroot, "graph")
30-
strdir = is_directed(g) ? "directed" : "undirected"
31-
xg["defaultedgetype"] = strdir
32-
33-
xnodes = addelement!(xg, "nodes")
34-
for i in 1:nv(g)
35-
xv = addelement!(xnodes, "node")
36-
xv["id"] = "$(i-1)"
37-
end
38-
39-
xedges = addelement!(xg, "edges")
40-
m = 0
41-
for e in Graphs.edges(g)
42-
xe = addelement!(xedges, "edge")
43-
xe["id"] = "$m"
44-
xe["source"] = "$(src(e)-1)"
45-
xe["target"] = "$(dst(e)-1)"
46-
m += 1
14+
function __init__()
15+
@require EzXML="8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" begin
16+
include("Gexf_conditional.jl")
4717
end
48-
49-
prettyprint(io, xdoc)
50-
return 1
5118
end
5219

53-
savegraph(io::IO, g::AbstractGraph, gname::String, ::GEXFFormat) = savegexf(io, g, gname)
54-
5520
end #module

src/GEXF/Gexf_conditional.jl

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using .EzXML
2+
3+
"""
4+
savegexf(f, g, gname)
5+
6+
Write a graph `g` with name `gname` to an IO stream `io` in the
7+
[Gexf](http://gexf.net/format/) format. Return 1 (number of graphs written).
8+
"""
9+
function savegexf(io::IO, g::Graphs.AbstractGraph, gname::String)
10+
xdoc = XMLDocument()
11+
xroot = setroot!(xdoc, ElementNode("gexf"))
12+
xroot["xmlns"] = "http://www.gexf.net/1.2draft"
13+
xroot["version"] = "1.2"
14+
xroot["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
15+
xroot["xsi:schemaLocation"] = "http://www.gexf.net/1.2draft/gexf.xsd"
16+
17+
xmeta = addelement!(xroot, "meta")
18+
addelement!(xmeta, "description", gname)
19+
xg = addelement!(xroot, "graph")
20+
strdir = is_directed(g) ? "directed" : "undirected"
21+
xg["defaultedgetype"] = strdir
22+
23+
xnodes = addelement!(xg, "nodes")
24+
for i in 1:nv(g)
25+
xv = addelement!(xnodes, "node")
26+
xv["id"] = "$(i-1)"
27+
end
28+
29+
xedges = addelement!(xg, "edges")
30+
m = 0
31+
for e in Graphs.edges(g)
32+
xe = addelement!(xedges, "edge")
33+
xe["id"] = "$m"
34+
xe["source"] = "$(src(e)-1)"
35+
xe["target"] = "$(dst(e)-1)"
36+
m += 1
37+
end
38+
39+
prettyprint(io, xdoc)
40+
return 1
41+
end
42+
43+
savegraph(io::IO, g::AbstractGraph, gname::String, ::GEXFFormat) = savegexf(io, g, gname)

src/GML/Gml.jl

+4-85
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,19 @@
11
module GML
22

3-
using GraphIO.ParserCombinator.Parsers
3+
using Requires
44
using Graphs
55
using Graphs: AbstractGraphFormat
66

77
import Graphs: loadgraph, loadgraphs, savegraph
88

99
export GMLFormat
1010

11-
1211
struct GMLFormat <: AbstractGraphFormat end
1312

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

10019
end # module

0 commit comments

Comments
 (0)