Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/MetaGraphsNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module MetaGraphsNext

using JLD2
using Graphs
using SimpleTraits

export MetaGraph, MetaDiGraph, MetaUndirectedGraph
export label_for, code_for, set_data
Expand Down
10 changes: 9 additions & 1 deletion src/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 2 additions & 23 deletions src/metadigraph.jl
Original file line number Diff line number Diff line change
@@ -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
26 changes: 3 additions & 23 deletions src/metaundigraph.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
"""
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
(label_2, label_1)
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
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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