Skip to content

Commit 0b9a769

Browse files
authored
Merge pull request #35 from filchristou/fixIssue29
Allow Dict to set prop indices
2 parents b7a1070 + fd1ee3f commit 0b9a769

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/MetaGraphs.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,17 @@ end
300300

301301
function set_props!(g::AbstractMetaGraph, v::Integer, d::Dict)
302302
if has_vertex(g, v)
303-
if length(intersect(keys(d), g.indices)) != 0
304-
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
305-
elseif !_hasdict(g, v)
303+
for (prop,val) in d
304+
index_available(g, v, prop, val) || error("':$prop' index already contains $val")
305+
end
306+
if !_hasdict(g, v)
306307
g.vprops[v] = d
307308
else
308309
merge!(g.vprops[v], d)
309310
end
311+
for prop in intersect(keys(d), g.indices)
312+
g.metaindex[prop][d[prop]] = v
313+
end
310314
return true
311315
end
312316
return false
@@ -368,6 +372,17 @@ function default_index_value(v::Integer, prop::Symbol, index_values::Set{Any}; e
368372
return val
369373
end
370374

375+
"""
376+
index_available(g, v, prop, val)
377+
378+
Check whether `prop` with `val` is available to be used as indexing for node `v`.
379+
First check whether `prop` is already an index. If not, check whether `val`
380+
already appears as an index in any node. If not, finally check whether `prop`
381+
and `val` indeed describe a property of node `v`.
382+
"""
383+
function index_available(g::AbstractMetaGraph, v::Integer, prop::Symbol, val::Any)
384+
!in(prop, g.indices) || !haskey(g.metaindex[prop], val) || (haskey(g.vprops, v) && haskey(g.vprops[v], prop) && g.vprops[v][prop] == val)
385+
end
371386
"""
372387
set_indexing_prop!(g, prop)
373388
set_indexing_prop!(g, v, prop, val)

test/metagraphs.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,27 @@ end
470470
@test_throws ErrorException set_indexing_prop!(dG, 4, :name, "dgnode_3")
471471
@test_throws ErrorException set_prop!(G, 4, :name, "gnode_3")
472472
@test_throws ErrorException set_prop!(dG, 4, :name, "dgnode_3")
473-
@test_throws ErrorException set_props!(G, 5, Dict(:name => "name", :other_name => "something"))
474-
@test_throws ErrorException set_props!(dG, 5, Dict(:name => "name", :other_name => "something"))
473+
474+
@test MetaGraphs.index_available(G, 7, :name, "gnode_8") == false
475+
@test MetaGraphs.index_available(G, 7, :name, "gnode_7") == true
476+
@test MetaGraphs.index_available(G, 7, :another_prop, "gnode_8") == true
477+
@test MetaGraphs.index_available(G, 7, :name, "gnode_8-anothername") == true
478+
479+
@test MetaGraphs.index_available(dG, 7, :name, "dgnode_8") == false
480+
@test MetaGraphs.index_available(dG, 7, :name, "dgnode_7") == true
481+
@test MetaGraphs.index_available(dG, 7, :another_prop, "dgnode_8") == true
482+
@test MetaGraphs.index_available(dG, 7, :name, "dgnode_8-anothername") == true
483+
484+
@test_throws ErrorException set_props!(G, 11, Dict(:name => "gnode_3", :other_name => "something11"))
485+
@test_throws ErrorException set_props!(dG,11, Dict(:name => "dgnode_3", :other_name => "something11"))
486+
@test_throws KeyError get_prop(G, 11, :other_name)
487+
@test_throws KeyError get_prop(dG, 11, :other_name)
488+
489+
@test set_props!(G, 5, Dict(:name => "name5", :other_name => "something"))
490+
@test set_props!(dG, 5, Dict(:name => "dname5", :other_name => "something"))
491+
@test G["name5", :name] == 5
492+
@test dG["dname5", :name] == 5
493+
475494

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

0 commit comments

Comments
 (0)