You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Inner constructor that performs checks on request
35
34
functionLayer(descriptor::LayerDescriptor{T,U,G}, graph::G, v_V_associations::Bijection{T, <: MultilayerVertex}; check_consistency =true) where {T,U,G}
36
35
if check_consistency
36
+
# Check that the graph type is the same as the one in the descriptor
37
37
typeof(descriptor.null_graph) ==typeof(graph) ||throw(ErrorException("Graph types between the provided `descriptor` and `graph` cannot differ. Found $(typeof(descriptor.null_graph)) and $(typeof(graph))."))
38
-
38
+
# Check that the graph vertices are the same as the ones in the association
39
39
all(vertices(graph) .==sort(domain(v_V_associations))) ||throw(ErrorException("The graph has a different set of vertices w.r.t. the domain of `v_V_associations`. Found $(vertices(graph)) and $(sort(domain(v_V_associations)))."))
- `vertices::Vector{ <: MultilayerVertex}`: The `MultilayerVertex`s of the Layer;
63
62
- `edge_list::Vector{ <: MultilayerEdge}`: The list of `MultilayerEdge`s;
64
63
- `null_graph::G`: the Layer's underlying graph type, which must be passed as a null graph. If it is not, an error will be thrown;
65
-
- `weighttype::Type{U}`: The type of the `MultilayerEdge` weights (evem when the underlying Layer's graph is unweighted, we need to specify a weight type since the `MultilayerGraph`s will always be weighted)
64
+
- `weighttype::Type{U}`: The type of the `MultilayerEdge` weights (even when the underlying Layer's graph is unweighted, we need to specify a weight type since the `MultilayerGraph`s will always be weighted)
66
65
67
66
# KWARGS
68
67
@@ -85,24 +84,24 @@ Constructor for `Layer`.
85
84
- `edge_list::Vector{<:MultilayerEdge}`;
86
85
"""
87
86
functionLayer(descriptor::LayerDescriptor{T}, vertices::Vector{<: MultilayerVertex}, edge_list::Vector{<:MultilayerEdge}) where {T <:Integer}
88
-
87
+
# First check that the vertices are of the correct type
89
88
ifhasproperty(eltype(vertices), :parameters)
90
89
par =eltype(vertices).parameters[1]
91
90
(isnothing(par) || par == descriptor.name) ||throw(ErrorException("`vertices` should be a `Vector{MultilayerVertex{:$(descriptor.name)}}` or a `Vector{MultilayerVertex{nothing}}`. Found $(typeof(vertices))"))
92
91
else
92
+
# if not, throw an error
93
93
throw(ErrorException("`vertices` should be a `Vector{MultilayerVertex{:$(descriptor.name)}}` or a `Vector{MultilayerVertex{nothing}}`. Found $(typeof(vertices))"))
- `vertices::Vector{ <: MultilayerVertex}`: The `MultilayerVertex`s of the Layer
118
117
- `ne::Int64`: The number of edges of the Layer
119
118
- `null_graph::G`: the Layer's underlying graph type, which must be passed as a null graph. If it is not, an error will be thrown.
120
-
- `weighttype::Type{U}`: The type of the `MultilayerEdge` weights (evem when the underlying Layer's graph is unweighted, we need to specify a weight type since the `MultilayerGraph`s will always be weighted);
119
+
- `weighttype::Type{U}`: The type of the `MultilayerEdge` weights (even when the underlying Layer's graph is unweighted, we need to specify a weight type since the `MultilayerGraph`s will always be weighted);
121
120
122
121
# KWARGS
123
122
-` default_vertex_metadata::Function`: Function that takes a `MultilayerVertex` and returns a `Tuple` or a `NamedTuple` containing the vertex metadata. defaults to `mv -> NamedTuple()`;
124
123
- `default_edge_weight::Function`: Function that takes a pair of `MultilayerVertex`s and returns an edge weight of type `weighttype` or `nothing` (which is compatible with unweighted underlying graphs and corresponds to `one(weighttype)` for weighted underlying graphs). Defaults to `(src, dst) -> nothing`;
125
124
- `default_edge_metadata::Function`: Function that takes a pair of `MultilayerVertex`s and returns a `Tuple` or a `NamedTuple` containing the edge metadata, that will be called when `add_edge!(mg,src,dst, args...; kwargs...)` is called without the `metadata` keyword argument, and when generating the edges in this constructor. Defaults to `(src, dst) -> NamedTuple()`;
126
-
- `allow_self_loops::Bool`: whether to allow self loops to be geenrated or not. Deafults to `false`.
125
+
- `allow_self_loops::Bool`: whether to allow self loops to be generated or not. Defaults to `false`.
127
126
"""
128
127
functionLayer(
129
128
name::Symbol,
@@ -142,19 +141,23 @@ function Layer(
142
141
edge_list = MultilayerEdge[]
143
142
144
143
for i in1:ne
144
+
# Generate a random vertex
145
145
rand_vertex_1 =rand(vertices)
146
+
# Generate another random vertex
146
147
rand_vertex_2 =nothing
148
+
# If we don't allow self loops, keep generating until we get a vertex that isn't the same as the previous one.
edge_list = MultilayerEdge[rand() <0.5? me :reverse(me) for me in edge_list]
160
+
edge_list = MultilayerEdge[rand() <0.5? me :reverse(me) for me in edge_list]
158
161
layer =Layer(descriptor, vertices, edge_list)
159
162
160
163
return layer
@@ -193,19 +196,26 @@ end
193
196
Add vertex associated with node `n` to layer `layer`. This method supports the uniform and transparent interfaces. See the [Vertices](@ref) section of the Tutorial.
194
197
"""
195
198
function Graphs.add_vertex!(layer::L, n::Node, args...; kwargs...) where {T, U, G, L <:Layer{T,U,G}}
@@ -237,16 +247,13 @@ function Graphs.rem_vertex!(layer::Layer, mv::MultilayerVertex)
237
247
rem_vertex!(layer, mv.node)
238
248
end
239
249
240
-
241
250
"""
242
251
rem_vertex!(layer::Layer, n::Node)
243
252
244
253
Remove node `n` from `layer`. Modify `layer.v_N_associations` according to how `rem_vertex!` works in [Graph.jl](https://juliagraphs.org/Graphs.jl/dev/core_functions/simplegraphs/#Graphs.SimpleGraphs.rem_vertex!-Tuple{Graphs.SimpleGraphs.AbstractSimpleGraph,%20Integer}).
245
254
"""
246
255
function Graphs.rem_vertex!(layer::Layer, n::Node)
@@ -287,28 +294,29 @@ Graphs.rem_vertex!(layer::L, v::T) where {T,U,G, L <: Layer{T,U,G}} = rem_vertex
287
294
Add edge from vertex `src` to vertex `dst` to layer `layer`. This method supports the uniform and transparent interfaces. See the [Edges](@ref) section of the Tutorial.
288
295
"""
289
296
function Graphs.add_edge!(layer::L, src::MultilayerVertex, dst::MultilayerVertex, args...; kwargs...) where {L <:Layer}
290
-
bare_src =get_bare_mv(src)
291
-
bare_dst =get_bare_mv(dst)
292
-
297
+
# Check if the vertices exist
293
298
!has_vertex(layer, src) &&throw( ErrorException( "Vertex $(src) does not belong to the layer."))
294
299
!has_vertex(layer, dst) &&throw( ErrorException( "Vertex $(dst) does not belong to the layer."))
295
300
301
+
# Check if the edge already exists
296
302
if!has_edge(layer, src, dst)
303
+
# If the edge doesn't exist, add it
304
+
# If the user does not specify any arguments, we use the default values
0 commit comments