From bcca44238a294c1a033d8650941f6a3ebd32a016 Mon Sep 17 00:00:00 2001 From: fchrstou Date: Fri, 27 Jan 2023 00:18:47 +0100 Subject: [PATCH 1/2] Use traits for (un)directed types --- Project.toml | 1 + src/MetaGraphsNext.jl | 1 + src/graphs.jl | 10 +++++++++- src/metadigraph.jl | 25 ++----------------------- src/metaundigraph.jl | 26 +++----------------------- test/runtests.jl | 19 +++++++++++++++++++ 6 files changed, 35 insertions(+), 47 deletions(-) diff --git a/Project.toml b/Project.toml index ded761b..970ba00 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.4.0" [deps] Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" +SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" [compat] Graphs = "1.4.1" diff --git a/src/MetaGraphsNext.jl b/src/MetaGraphsNext.jl index bed524a..2b60daa 100644 --- a/src/MetaGraphsNext.jl +++ b/src/MetaGraphsNext.jl @@ -2,6 +2,7 @@ module MetaGraphsNext using JLD2 using Graphs +using SimpleTraits export MetaGraph, MetaDiGraph, MetaUndirectedGraph export label_for, code_for, set_data diff --git a/src/graphs.jl b/src/graphs.jl index 5df88d2..1e10d97 100644 --- a/src/graphs.jl +++ b/src/graphs.jl @@ -41,6 +41,14 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph) issubset(meta_graph.graph, h.graph) end +function Graphs.is_directed(meta_graph::MetaGraph) + Graphs.is_directed(meta_graph.graph) +end + +function Graphs.is_directed(::Type{<:MetaGraph{Code, Label, Graph}}) where {Code,Label,Graph<:AbstractGraph} + Graphs.is_directed(Graph) +end + ## Link between graph codes and metagraph labels """ @@ -205,7 +213,7 @@ function Graphs.induced_subgraph( new_graph, code_map end -function Graphs.reverse(meta_graph::MetaDiGraph) +@traitfn function Graphs.reverse(meta_graph::MetaGraph::IsDirected) edge_data = meta_graph.edge_data reverse_edge_data = empty(edge_data) for (label_1, label_2) in keys(edge_data) diff --git a/src/metadigraph.jl b/src/metadigraph.jl index ebada61..fa93426 100644 --- a/src/metadigraph.jl +++ b/src/metadigraph.jl @@ -1,26 +1,5 @@ -""" - MetaDiGraph +@traitfn Graphs.SimpleDiGraph(meta_graph::MetaGraph::(IsDirected)) = meta_graph.graph -A `MetaGraph` whose underlying graph is of type `Graphs.SimpleDiGraph`. -""" -const MetaDiGraph = MetaGraph{<:Any, <:Any, <:SimpleDiGraph} - -function Graphs.SimpleDiGraph(meta_graph::MetaDiGraph) - meta_graph.graph -end - -function Graphs.is_directed(::Type{<:MetaDiGraph}) - true -end -function Graphs.is_directed(::MetaDiGraph) - true -end - -function arrange( - ::MetaDiGraph, - label_1, - label_2, - _..., -) +@traitfn function arrange(::AG::IsDirected, label_1, label_2, _drop...) where {T, AG <: AbstractGraph{T}} label_1, label_2 end diff --git a/src/metaundigraph.jl b/src/metaundigraph.jl index 81b1bf8..1c2bc0f 100644 --- a/src/metaundigraph.jl +++ b/src/metaundigraph.jl @@ -1,22 +1,6 @@ -""" - MetaUndirectedGraph +@traitfn Graphs.SimpleGraph(meta_graph::MetaGraph::(!IsDirected)) = meta_graph.graph -A `MetaGraph` whose underlying graph is of type `Graphs.SimpleGraph`. -""" -const MetaUndirectedGraph = MetaGraph{<:Any, <:Any, <:SimpleGraph} - -Graphs.SimpleGraph(meta_graph::MetaUndirectedGraph) = meta_graph.graph - -Graphs.is_directed(::Type{<:MetaUndirectedGraph}) = false -Graphs.is_directed(::MetaUndirectedGraph) = false - -function arrange( - ::MetaUndirectedGraph, - label_1, - label_2, - code_1, - code_2, -) +@traitfn function arrange(::AG::(!IsDirected), label_1, label_2, code_1, code_2) where {T, AG <: AbstractGraph{T}} if code_1 < code_2 (label_1, label_2) else @@ -24,10 +8,6 @@ function arrange( end end -function arrange( - meta_graph::MetaUndirectedGraph, - label_1, - label_2, -) +@traitfn function arrange(meta_graph::AG::(!IsDirected), label_1, label_2) where {T, AG <: AbstractGraph{T}} arrange(meta_graph, label_1, label_2, code_for(meta_graph, label_1), code_for(meta_graph, label_2)) end diff --git a/test/runtests.jl b/test/runtests.jl index c9497d5..0eb7a6f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,11 +2,15 @@ using Documenter using MetaGraphsNext using Test using Graphs +using SimpleTraits @testset "MetaGraphsNext" begin doctest(MetaGraphsNext) colors = MetaGraph( Graph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors") + @test istrait(IsDirected{typeof(colors)}) == is_directed(colors) == false + @test SimpleGraph(colors) isa SimpleGraph + @test_throws MethodError SimpleDiGraph(colors) labels = [:red, :yellow, :blue] values = ["warm", "warm", "cool"] @@ -23,4 +27,19 @@ using Graphs for label in labels @test label_for(colors, code_for(colors, label)) == label end + @test MetaGraphsNext.arrange(colors, :yellow, :blue) == (:blue, :yellow) + + + # directed MetaGraph + dcolors = MetaGraph( SimpleDiGraph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors") + @test istrait(IsDirected{typeof(dcolors)}) == is_directed(dcolors) == true + @test SimpleDiGraph(dcolors) isa SimpleDiGraph + @test_throws MethodError SimpleGraph(dcolors) + labels = [:red, :yellow, :blue] + values = ["warm", "warm", "cool"] + for (label, value) in zip(labels, values) + dcolors[label] = value + end + dcolors[:red, :yellow] = :redyellow + @test MetaGraphsNext.arrange(dcolors, :yellow, :blue) == (:yellow, :blue) end From a1ef56f20cc39cf2d79e3fd6c61836b6b9c647bc Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:10:45 +0100 Subject: [PATCH 2/2] Static trait dispatch --- src/graphs.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/graphs.jl b/src/graphs.jl index 1056311..4eb85ba 100644 --- a/src/graphs.jl +++ b/src/graphs.jl @@ -41,14 +41,16 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph) return issubset(meta_graph.graph, h.graph) end -function Graphs.is_directed(meta_graph::MetaGraph) - return Graphs.is_directed(meta_graph.graph) +function Graphs.is_directed( + ::MetaGraph{Code,Label,Graph} +) where {Code,Label,Graph<:AbstractGraph} + return is_directed(Graph) end function Graphs.is_directed( ::Type{<:MetaGraph{Code,Label,Graph}} ) where {Code,Label,Graph<:AbstractGraph} - return Graphs.is_directed(Graph) + return is_directed(Graph) end ## Link between graph codes and metagraph labels