Skip to content
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

Add getextra with default #182

Merged
merged 3 commits into from
Mar 19, 2024
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
8 changes: 6 additions & 2 deletions src/graph_engine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ getextra(node::NodeData) = node.extra

hasextra(node::NodeData, key::Symbol) = haskey(node.extra, key)
getextra(node::NodeData, key::Symbol) = getindex(node.extra, key)
getextra(node::NodeData, key::Symbol, default) = hasextra(node, key) ? getextra(node, key) : default
setextra!(node::NodeData, key::Symbol, value) = insert!(node.extra, key, value)

"""
Expand All @@ -553,6 +554,9 @@ end
function getextra(node::NodeData, key::NodeDataExtraKey{K, T})::T where {K, T}
return getindex(node.extra, K)::T
end
function getextra(node::NodeData, key::NodeDataExtraKey{K, T}, default::T)::T where {K, T}
return hasextra(node, key) ? (getextra(node, key)::T) : default
end
function setextra!(node::NodeData, key::NodeDataExtraKey{K}, value::T) where {K, T}
return insert!(node.extra, K, value)
end
Expand Down Expand Up @@ -882,8 +886,8 @@ check_variate_compatability(node::NodeLabel, index) =
check_variate_compatability(label::GraphPPL.ProxyLabel, index) = check_variate_compatability(unroll(label), index)

check_variate_compatability(node::ResizableArray{NodeLabel, V, N}, index::Vararg{Int, N}) where {V, N} = isassigned(node, index...)
check_variate_compatability(node::ResizableArray{NodeLabel, V, N}, index::Vararg{Int, M}) where {V, N, M} = error("Index of length $(length(index)) not possible for $N-dimensional vector of random variables")

check_variate_compatability(node::ResizableArray{NodeLabel, V, N}, index::Vararg{Int, M}) where {V, N, M} =
error("Index of length $(length(index)) not possible for $N-dimensional vector of random variables")

check_variate_compatability(node::ResizableArray{NodeLabel, V, N}, index::Nothing) where {V, N} =
error("Cannot call vector of random variables on the left-hand-side by an unindexed statement")
Expand Down
11 changes: 11 additions & 0 deletions test/graph_engine_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ end
nodedata = NodeData(context, properties)

@test !hasextra(nodedata, :a)
@test getextra(nodedata, :a, 2) === 2
@test !hasextra(nodedata, :a) # the default should not add the extra property, only return
setextra!(nodedata, :a, 1)
@test hasextra(nodedata, :a)
@test getextra(nodedata, :a) === 1
@test getextra(nodedata, :a, 2) === 1
@test !hasextra(nodedata, :b)
@test_throws Exception getextra(nodedata, :b)
@test getextra(nodedata, :b, 2) === 2

# In the current implementation it is not possible to update extra properties
@test_throws Exception setextra!(nodedata, :a, 2)
Expand All @@ -142,9 +148,14 @@ end
constkey_c_float = NodeDataExtraKey{:c, Float64}()

@test !@inferred(hasextra(nodedata, constkey_c_float))
@test @inferred(getextra(nodedata, constkey_c_float, 4.0)) === 4.0
@inferred(setextra!(nodedata, constkey_c_float, 3.0))
@test @inferred(hasextra(nodedata, constkey_c_float))
@test @inferred(getextra(nodedata, constkey_c_float)) === 3.0
@test @inferred(getextra(nodedata, constkey_c_float, 4.0)) === 3.0

# The default has a different type from the key (4.0 is Float and 4 is Int), thus the error
@test_throws MethodError getextra(nodedata, constkey_c_float, 4)

constkey_d_int = NodeDataExtraKey{:d, Int64}()

Expand Down
Loading