Skip to content

Commit bcca442

Browse files
committed
Use traits for (un)directed types
1 parent 71cf31d commit bcca442

File tree

6 files changed

+35
-47
lines changed

6 files changed

+35
-47
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.4.0"
55
[deps]
66
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
77
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
8+
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
89

910
[compat]
1011
Graphs = "1.4.1"

src/MetaGraphsNext.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module MetaGraphsNext
22

33
using JLD2
44
using Graphs
5+
using SimpleTraits
56

67
export MetaGraph, MetaDiGraph, MetaUndirectedGraph
78
export label_for, code_for, set_data

src/graphs.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
4141
issubset(meta_graph.graph, h.graph)
4242
end
4343

44+
function Graphs.is_directed(meta_graph::MetaGraph)
45+
Graphs.is_directed(meta_graph.graph)
46+
end
47+
48+
function Graphs.is_directed(::Type{<:MetaGraph{Code, Label, Graph}}) where {Code,Label,Graph<:AbstractGraph}
49+
Graphs.is_directed(Graph)
50+
end
51+
4452
## Link between graph codes and metagraph labels
4553

4654
"""
@@ -205,7 +213,7 @@ function Graphs.induced_subgraph(
205213
new_graph, code_map
206214
end
207215

208-
function Graphs.reverse(meta_graph::MetaDiGraph)
216+
@traitfn function Graphs.reverse(meta_graph::MetaGraph::IsDirected)
209217
edge_data = meta_graph.edge_data
210218
reverse_edge_data = empty(edge_data)
211219
for (label_1, label_2) in keys(edge_data)

src/metadigraph.jl

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
"""
2-
MetaDiGraph
1+
@traitfn Graphs.SimpleDiGraph(meta_graph::MetaGraph::(IsDirected)) = meta_graph.graph
32

4-
A `MetaGraph` whose underlying graph is of type `Graphs.SimpleDiGraph`.
5-
"""
6-
const MetaDiGraph = MetaGraph{<:Any, <:Any, <:SimpleDiGraph}
7-
8-
function Graphs.SimpleDiGraph(meta_graph::MetaDiGraph)
9-
meta_graph.graph
10-
end
11-
12-
function Graphs.is_directed(::Type{<:MetaDiGraph})
13-
true
14-
end
15-
function Graphs.is_directed(::MetaDiGraph)
16-
true
17-
end
18-
19-
function arrange(
20-
::MetaDiGraph,
21-
label_1,
22-
label_2,
23-
_...,
24-
)
3+
@traitfn function arrange(::AG::IsDirected, label_1, label_2, _drop...) where {T, AG <: AbstractGraph{T}}
254
label_1, label_2
265
end

src/metaundigraph.jl

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
1-
"""
2-
MetaUndirectedGraph
1+
@traitfn Graphs.SimpleGraph(meta_graph::MetaGraph::(!IsDirected)) = meta_graph.graph
32

4-
A `MetaGraph` whose underlying graph is of type `Graphs.SimpleGraph`.
5-
"""
6-
const MetaUndirectedGraph = MetaGraph{<:Any, <:Any, <:SimpleGraph}
7-
8-
Graphs.SimpleGraph(meta_graph::MetaUndirectedGraph) = meta_graph.graph
9-
10-
Graphs.is_directed(::Type{<:MetaUndirectedGraph}) = false
11-
Graphs.is_directed(::MetaUndirectedGraph) = false
12-
13-
function arrange(
14-
::MetaUndirectedGraph,
15-
label_1,
16-
label_2,
17-
code_1,
18-
code_2,
19-
)
3+
@traitfn function arrange(::AG::(!IsDirected), label_1, label_2, code_1, code_2) where {T, AG <: AbstractGraph{T}}
204
if code_1 < code_2
215
(label_1, label_2)
226
else
237
(label_2, label_1)
248
end
259
end
2610

27-
function arrange(
28-
meta_graph::MetaUndirectedGraph,
29-
label_1,
30-
label_2,
31-
)
11+
@traitfn function arrange(meta_graph::AG::(!IsDirected), label_1, label_2) where {T, AG <: AbstractGraph{T}}
3212
arrange(meta_graph, label_1, label_2, code_for(meta_graph, label_1), code_for(meta_graph, label_2))
3313
end

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ using Documenter
22
using MetaGraphsNext
33
using Test
44
using Graphs
5+
using SimpleTraits
56

67
@testset "MetaGraphsNext" begin
78
doctest(MetaGraphsNext)
89

910
colors = MetaGraph( Graph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors")
11+
@test istrait(IsDirected{typeof(colors)}) == is_directed(colors) == false
12+
@test SimpleGraph(colors) isa SimpleGraph
13+
@test_throws MethodError SimpleDiGraph(colors)
1014

1115
labels = [:red, :yellow, :blue]
1216
values = ["warm", "warm", "cool"]
@@ -23,4 +27,19 @@ using Graphs
2327
for label in labels
2428
@test label_for(colors, code_for(colors, label)) == label
2529
end
30+
@test MetaGraphsNext.arrange(colors, :yellow, :blue) == (:blue, :yellow)
31+
32+
33+
# directed MetaGraph
34+
dcolors = MetaGraph( SimpleDiGraph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors")
35+
@test istrait(IsDirected{typeof(dcolors)}) == is_directed(dcolors) == true
36+
@test SimpleDiGraph(dcolors) isa SimpleDiGraph
37+
@test_throws MethodError SimpleGraph(dcolors)
38+
labels = [:red, :yellow, :blue]
39+
values = ["warm", "warm", "cool"]
40+
for (label, value) in zip(labels, values)
41+
dcolors[label] = value
42+
end
43+
dcolors[:red, :yellow] = :redyellow
44+
@test MetaGraphsNext.arrange(dcolors, :yellow, :blue) == (:yellow, :blue)
2645
end

0 commit comments

Comments
 (0)