|
3 | 3 | struct MGFormat <: AbstractGraphFormat end |
4 | 4 | struct DOTFormat <: AbstractGraphFormat end |
5 | 5 |
|
6 | | -function loadmg(fn::AbstractString) |
7 | | - @load fn g |
8 | | - return g |
| 6 | +function loadmg(args...) |
| 7 | + error("In order to load static graphs from binary files, you need to load the JLD2.jl \ |
| 8 | + package") |
9 | 9 | end |
10 | 10 |
|
11 | | -function savemg(fn::AbstractString, g::AbstractMetaGraph) |
12 | | - @save fn g |
13 | | - return 1 |
| 11 | +function savemg(args...) |
| 12 | + error("In order to save static graphs to binary files, you need to load the JLD2.jl \ |
| 13 | + package") |
14 | 14 | end |
15 | 15 |
|
16 | | -loadgraph(fn::AbstractString, gname::String, ::MGFormat) = loadmg(fn) |
17 | | -savegraph(fn::AbstractString, g::AbstractMetaGraph) = savemg(fn, g) |
18 | | - |
19 | | -# escaping unescaped quotation marks |
20 | | -# i.e. replacing `"`` with `\"` while leaving `\"` as is |
21 | | -escape_quotes(s::AbstractString) = replace(s, r"([^\\])\"" => s"\1\\\\\"") |
22 | | - |
23 | | -# According to the DOT language specification https://graphviz.org/doc/info/lang.html |
24 | | -# we can quote everyhthing that's not an XML/HTML literal |
25 | | -function quote_prop(p::AbstractString) |
26 | | - if occursin(r"<+.*>+$", p) |
27 | | - # The label is an HTML string, no additional quotes here. |
28 | | - return p |
29 | | - else |
30 | | - return "\"" * escape_quotes(p) * "\"" |
31 | | - end |
| 16 | +function savedot(args...) |
| 17 | + error("In order to save static graphs to binary files, you need to load the JLD2.jl \ |
| 18 | + package") |
32 | 19 | end |
33 | | -# if the property value is _not_ a string it cannot be XML/HTML literal, so just put it in quotes |
34 | | -quote_prop(p::Any) = "\"" * escape_quotes(string(p)) * "\"" |
35 | | -# NOTE: down there I only quote property _values_. DOT allows quoting property _names_ too |
36 | | -# I don't do that as long as names are Symbols and can't have spaces and commas and stuff. |
37 | | -# That will break if someone uses a DOT keyword as a property name, as they must be quoted. |
38 | | - |
39 | | -function savedot(io::IO, g::AbstractMetaGraph) |
40 | | - |
41 | | - if is_directed(g) |
42 | | - write(io, "digraph G {\n") |
43 | | - dash = "->" |
44 | | - else |
45 | | - write(io, "graph G {\n") |
46 | | - dash = "--" |
47 | | - end |
48 | | - |
49 | | - for p in props(g) |
50 | | - write(io, "$(p[1])=$(quote_prop(p[2]));\n") |
51 | | - end |
52 | | - |
53 | | - for v in vertices(g) |
54 | | - write(io, "$v") |
55 | | - if length(props(g, v)) > 0 |
56 | | - write(io, " [ ") |
57 | 20 |
|
58 | | - for p in props(g, v) |
59 | | - write(io, "$(p[1])=$(quote_prop(p[2])), ") |
60 | | - end |
61 | | - |
62 | | - write(io, "];") |
63 | | - end |
64 | | - write(io, "\n") |
65 | | - end |
66 | | - |
67 | | - for e in edges(g) |
68 | | - write(io, "$(src(e)) $dash $(dst(e)) [ ") |
69 | | - for p in props(g,e) |
70 | | - write(io, "$(p[1])=$(quote_prop(p[2])), ") |
71 | | - end |
72 | | - write(io, "]\n") |
73 | | - end |
74 | | - write(io, "}\n") |
75 | | -end |
| 21 | +loadgraph(fn::AbstractString, ::String, ::MGFormat) = loadmg(fn) |
| 22 | +savegraph(fn::AbstractString, g::AbstractMetaGraph) = savemg(fn, g) |
76 | 23 |
|
77 | 24 | function savegraph(fn::AbstractString, g::AbstractMetaGraph, ::DOTFormat) |
78 | 25 | open(fn, "w") do fp |
|
0 commit comments