Skip to content

Use traits for (un)directed types #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name = "MetaGraphsNext"
uuid = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
version = "0.4.1"
version = "0.5.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"
JLD2 = "0.1.11, 0.2, 0.3, 0.4"
SimpleTraits = "0.9"
julia = "1.6"

[extras]
Expand Down
6 changes: 3 additions & 3 deletions src/MetaGraphsNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module MetaGraphsNext

using JLD2
using Graphs
using SimpleTraits

export MetaGraph, MetaDiGraph, MetaUndirectedGraph
export MetaGraph
export label_for, code_for, set_data!
export weighttype, default_weight, get_weight_function
export MGFormat, DOTFormat

include("metagraph.jl")
include("metaundigraph.jl")
include("metadigraph.jl")
include("directedness.jl")
include("graphs.jl")
include("dict_utils.jl")
include("weights.jl")
Expand Down
34 changes: 34 additions & 0 deletions src/directedness.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
arrange(graph, label_1, label_2)

Sort two vertex labels in a default order (useful to uniquely express undirected edges).
"""
function arrange end

@traitfn function arrange(
::AG::IsDirected, label_1, label_2, _drop...
) where {T,AG<:AbstractGraph{T}}
return label_1, label_2
end

@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

@traitfn function arrange(
meta_graph::AG::(!IsDirected), label_1, label_2
) where {T,AG<:AbstractGraph{T}}
return arrange(
meta_graph,
label_1,
label_2,
code_for(meta_graph, label_1),
code_for(meta_graph, label_2),
)
end
14 changes: 13 additions & 1 deletion src/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
return issubset(meta_graph.graph, h.graph)
end

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 is_directed(Graph)
end

## Link between graph codes and metagraph labels

"""
Expand Down Expand Up @@ -210,7 +222,7 @@ function Graphs.induced_subgraph(
return 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
21 changes: 0 additions & 21 deletions src/metadigraph.jl

This file was deleted.

18 changes: 10 additions & 8 deletions src/metagraph.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
"""
MetaGraph{Code<:Integer,Label,Graph,VertexData,EdgeData,GraphData,WeightFunction,Weight<:Real} <: AbstractGraph{Code}
MetaGraph{
Code<:Integer,
Label,
Graph,
VertexData,
EdgeData,
GraphData,
WeightFunction,
Weight<:Real
} <: AbstractGraph{Code}

A graph type with custom vertex labels containing vertex-, edge- and graph-level metadata.

Expand Down Expand Up @@ -65,13 +74,6 @@ function MetaGraph(
)
end

"""
arrange(graph, label_1, label_2)

Sort two vertex labels in a default order (useful to uniquely express undirected edges).
"""
function arrange end

function Base.zero(
meta_graph::MetaGraph{Code,Label,Graph,VertexData,EdgeData}
) where {Code,Label,Graph,VertexData,EdgeData}
Expand Down
29 changes: 0 additions & 29 deletions src/metaundigraph.jl

This file was deleted.

21 changes: 0 additions & 21 deletions test/labels_codes.jl

This file was deleted.

34 changes: 34 additions & 0 deletions test/labels_codes_directedness.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# undirected MetaGraph
colors = MetaGraph(
Graph(); VertexData=String, EdgeData=Symbol, graph_data="graph_of_colors"
)
@test istrait(IsDirected{typeof(colors)}) == is_directed(colors) == false
labels = [:red, :yellow, :blue]
values = ["warm", "warm", "cool"]
for (label, value) in zip(labels, values)
colors[label] = value
end
for label in labels
@test label_for(colors, code_for(colors, label)) == label
end
#delete an entry and test again
rem_vertex!(colors, 1)
popfirst!(labels)
popfirst!(values)
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
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)
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Documenter
using Graphs
using JuliaFormatter
using MetaGraphsNext
using SimpleTraits
using Test

@testset verbose = true "MetaGraphsNext" begin
Expand All @@ -26,7 +27,7 @@ using Test
include(joinpath("tutorial", "3_files.jl"))
end
end
@testset verbose = true "Labels and codes" begin
include("labels_codes.jl")
@testset verbose = true "Labels, codes, directedness" begin
include("labels_codes_directedness.jl")
end
end
4 changes: 0 additions & 4 deletions test/tutorial/2_graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ eltype(cities)
#-
edgetype(cities)
@test edgetype(cities) == Graphs.SimpleEdge{Int} #src
#-
SimpleGraph(cities)

# We can check the set of vertices:

Expand Down Expand Up @@ -121,8 +119,6 @@ rock_paper_scissors[:paper, :rock] = "paper beats rock";

is_directed(rock_paper_scissors)
@test is_directed(rock_paper_scissors) #src
#-
SimpleDiGraph(rock_paper_scissors)

# Directed graphs can be reversed:

Expand Down