Skip to content
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
21 changes: 18 additions & 3 deletions src/MetaGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,17 @@ end

function set_props!(g::AbstractMetaGraph, v::Integer, d::Dict)
if has_vertex(g, v)
if length(intersect(keys(d), g.indices)) != 0
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
elseif !_hasdict(g, v)
for (prop,val) in d
index_available(g, v, prop, val) || error("':$prop' index already contains $val")
end
if !_hasdict(g, v)
g.vprops[v] = d
else
merge!(g.vprops[v], d)
end
for prop in intersect(keys(d), g.indices)
g.metaindex[prop][d[prop]] = v
end
return true
end
return false
Expand Down Expand Up @@ -368,6 +372,17 @@ function default_index_value(v::Integer, prop::Symbol, index_values::Set{Any}; e
return val
end

"""
index_available(g, v, prop, val)

Check whether `prop` with `val` is available to be used as indexing for node `v`.
First check whether `prop` is already an index. If not, check whether `val`
already appears as an index in any node. If not, finally check whether `prop`
and `val` indeed describe a property of node `v`.
"""
function index_available(g::AbstractMetaGraph, v::Integer, prop::Symbol, val::Any)
!in(prop, g.indices) || !haskey(g.metaindex[prop], val) || (haskey(g.vprops, v) && haskey(g.vprops[v], prop) && g.vprops[v][prop] == val)
end
"""
set_indexing_prop!(g, prop)
set_indexing_prop!(g, v, prop, val)
Expand Down
23 changes: 21 additions & 2 deletions test/metagraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,27 @@ end
@test_throws ErrorException set_indexing_prop!(dG, 4, :name, "dgnode_3")
@test_throws ErrorException set_prop!(G, 4, :name, "gnode_3")
@test_throws ErrorException set_prop!(dG, 4, :name, "dgnode_3")
@test_throws ErrorException set_props!(G, 5, Dict(:name => "name", :other_name => "something"))
@test_throws ErrorException set_props!(dG, 5, Dict(:name => "name", :other_name => "something"))

@test MetaGraphs.index_available(G, 7, :name, "gnode_8") == false
@test MetaGraphs.index_available(G, 7, :name, "gnode_7") == true
@test MetaGraphs.index_available(G, 7, :another_prop, "gnode_8") == true
@test MetaGraphs.index_available(G, 7, :name, "gnode_8-anothername") == true

@test MetaGraphs.index_available(dG, 7, :name, "dgnode_8") == false
@test MetaGraphs.index_available(dG, 7, :name, "dgnode_7") == true
@test MetaGraphs.index_available(dG, 7, :another_prop, "dgnode_8") == true
@test MetaGraphs.index_available(dG, 7, :name, "dgnode_8-anothername") == true

@test_throws ErrorException set_props!(G, 11, Dict(:name => "gnode_3", :other_name => "something11"))
@test_throws ErrorException set_props!(dG,11, Dict(:name => "dgnode_3", :other_name => "something11"))
@test_throws KeyError get_prop(G, 11, :other_name)
@test_throws KeyError get_prop(dG, 11, :other_name)

@test set_props!(G, 5, Dict(:name => "name5", :other_name => "something"))
@test set_props!(dG, 5, Dict(:name => "dname5", :other_name => "something"))
@test G["name5", :name] == 5
@test dG["dname5", :name] == 5


@info("Ignore \"'foo1' is already in index\" warnings")
set_indexing_prop!(G, 50, :name, "another name")
Expand Down