Skip to content

Commit

Permalink
Merge pull request #180 from psrenergy/jg/validation
Browse files Browse the repository at this point in the history
New validation
  • Loading branch information
guilhermebodin authored Oct 4, 2023
2 parents 8f67d77 + c1cabae commit 5206029
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 180 deletions.
196 changes: 108 additions & 88 deletions src/OpenStudy/study_openinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,30 @@ function get_parm(
default::T = _default_value(T),
dim1::Union{Integer, Nothing} = nothing,
dim2::Union{Integer, Nothing} = nothing,
validate::Bool = true,
)::T where {T}
# Retrieve attribute metadata
attribute_struct = get_attribute_struct(data, collection, attribute)

# Basic checks
_check_dim(attribute_struct, collection, attribute, dim1, dim2)
_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
if validate
attribute_struct = get_attribute_struct(data, collection, attribute)
_check_dim(attribute_struct, collection, attribute, dim1, dim2)
_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
dim = get_attribute_dim(attribute_struct)
else
if dim2 !== nothing && dim2 > 0
dim = 2
elseif dim1 !== nothing && dim1 > 0
dim = 1
else
dim = 0
end
end
_check_element_range(data, collection, index)

# This is assumed to be a mutable dictionary
element = _get_element(data, collection, index)

# Format according to dimension
dim = get_attribute_dim(attribute_struct)
key = _get_attribute_key(attribute, dim, 1 => dim1, 2 => dim2)

# Here, a choice is made to return a default
Expand All @@ -548,31 +557,33 @@ function get_parm_1d(
index::Integer,
::Type{T};
default::T = _default_value(T),
validate::Bool = true,
)::Vector{T} where {T}
attribute_struct = get_attribute_struct(data, collection, attribute)

dim = get_attribute_dim(attribute_struct)

if dim != 1
if dim == 0
error("""
if validate
attribute_struct = get_attribute_struct(data, collection, attribute)
dim = get_attribute_dim(attribute_struct)
if dim != 1
if dim == 0
error(
"""
Attribute '$attribute' from collection '$colllection' has no dimensions.
Consider using `get_parm` instead.
""")
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(attribute_struct.dim) dimensions.
Consider using `get_parm_$(attribute_struct.dim)d` instead.
""",
)
""",
)
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(attribute_struct.dim) dimensions.
Consider using `get_parm_$(attribute_struct.dim)d` instead.
""",
)
end
end
_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
_check_element_range(data, collection, index)
end

_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
_check_element_range(data, collection, index)

dim1 = get_attribute_dim1(data, collection, attribute, index)

element = _get_element(data, collection, index)
Expand All @@ -599,31 +610,33 @@ function get_parm_2d(
index::Integer,
::Type{T};
default::T = _default_value(T),
validate::Bool = true,
)::Matrix{T} where {T}
attribute_struct = get_attribute_struct(data, collection, attribute)

dim = get_attribute_dim(attribute_struct)

if dim != 2
if dim == 0
error("""
if validate
attribute_struct = get_attribute_struct(data, collection, attribute)
dim = get_attribute_dim(attribute_struct)
if dim != 2
if dim == 0
error(
"""
Attribute '$attribute' from collection '$colllection' has no dimensions.
Consider using `get_parm` instead.
""")
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
""",
)
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
end
end
_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
_check_element_range(data, collection, index)
end

_check_type(attribute_struct, T, collection, attribute)
_check_parm(attribute_struct, collection, attribute)
_check_element_range(data, collection, index)

dim1 = get_attribute_dim1(data, collection, attribute, index)
dim2 = get_attribute_dim2(data, collection, attribute, index)

Expand All @@ -632,7 +645,7 @@ function get_parm_2d(
out = Matrix{T}(undef, dim1, dim2)

for i in 1:dim1, j in 1:dim2
key = _get_attribute_key(attribute, dim, 1 => i, 2 => j)
key = _get_attribute_key(attribute, 2, 1 => i, 2 => j)

out[i, j] = if haskey(element, key)
_cast(T, element[key], default)
Expand Down Expand Up @@ -790,12 +803,15 @@ function get_vector(
dim1::Union{Integer, Nothing} = nothing,
dim2::Union{Integer, Nothing} = nothing,
default::T = _default_value(T),
validate::Bool = true,
) where {T}
attribute_struct = get_attribute_struct(data, collection, attribute)

_check_dim(attribute_struct, collection, attribute, dim1, dim2)
_check_vector(attribute_struct, collection, attribute)
_check_type(attribute_struct, T, collection, attribute)
if validate
_check_dim(attribute_struct, collection, attribute, dim1, dim2)
_check_type(attribute_struct, T, collection, attribute)
_check_vector(attribute_struct, collection, attribute)
end
_check_element_range(data, collection, index)

dim = get_attribute_dim(attribute_struct)
Expand All @@ -817,39 +833,41 @@ function get_vector_1d(
index::Integer,
::Type{T};
default::T = _default_value(T),
validate::Bool = true,
) where {T}
attribute_struct = get_attribute_struct(data, collection, attribute)

dim = get_attribute_dim(attribute_struct)

if dim != 1
if dim == 0
error("""
if validate
attribute_struct = get_attribute_struct(data, collection, attribute)
dim = get_attribute_dim(attribute_struct)
if dim != 1
if dim == 0
error(
"""
Attribute '$attribute' from collection '$colllection' has no dimensions.
Consider using `get_parm` instead.
""")
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
""",
)
else
error(
"""
Attribute '$attribute' from collection '$colllection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
end
end
_check_type(attribute_struct, T, collection, attribute)
_check_vector(attribute_struct, collection, attribute)
_check_element_range(data, collection, index)
end

_check_vector(attribute_struct, collection, attribute)
_check_type(attribute_struct, T, collection, attribute)
_check_element_range(data, collection, index)

dim1 = get_attribute_dim1(data, collection, attribute, index)

element = _get_element(data, collection, index)

out = Vector{Vector{T}}(undef, dim1)

for i in 1:dim1
key = _get_attribute_key(attribute, dim, 1 => i)
key = _get_attribute_key(attribute, 1, 1 => i)

out[i] = if haskey(element, key)
_cast_vector(T, element[key], default)
Expand All @@ -868,29 +886,31 @@ function get_vector_2d(
index::Integer,
::Type{T};
default::T = _default_value(T),
validate::Bool = true,
) where {T}
attribute_struct = get_attribute_struct(data, collection, attribute)

dim = get_attribute_dim(attribute_struct)

if dim != 2
if dim == 0
error("""
if validate
attribute_struct = get_attribute_struct(data, collection, attribute)
dim = get_attribute_dim(attribute_struct)
if dim != 2
if dim == 0
error(
"""
Attribute '$attribute' from collection '$colllection' has no dimensions.
Consider using `get_parm` instead.
""")
else
error(
"""
Attribute '$attribute' from collection '$collection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
""",
)
else
error(
"""
Attribute '$attribute' from collection '$collection' has $(dim) dimensions.
Consider using `get_parm_$(dim)d` instead.
""",
)
end
end
_check_type(attribute_struct, T, collection, attribute)
_check_vector(attribute_struct, collection, attribute)
end

_check_vector(attribute_struct, collection, attribute)
_check_type(attribute_struct, T, collection, attribute)
_check_element_range(data, collection, index)

dim1 = get_attribute_dim1(data, collection, attribute, index)
Expand All @@ -901,7 +921,7 @@ function get_vector_2d(
out = Matrix{Vector{T}}(undef, dim1, dim2)

for i in 1:dim1, j in 1:dim2
key = _get_attribute_key(attribute, dim, 1 => i, 2 => j)
key = _get_attribute_key(attribute, 2, 1 => i, 2 => j)

out[i, j] = if haskey(element, key)
_cast_vector(T, element[key], default)
Expand Down
7 changes: 5 additions & 2 deletions src/OpenStudy/vector_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function mapped_vector(
map_key = collection, # reference for PSRMap pointer, if empty use class name
filters = String[], # for calling just within a subset instead of the full call
default = _default_value(T),
validate::Bool = true,
) where {T} #<: Union{Float64, Int32}
if has_graf_file(data, collection, attribute)
if isnothing(data.mapper)
Expand All @@ -30,8 +31,10 @@ function mapped_vector(

attribute_struct = get_attribute_struct(data, collection, attribute)

_check_type(attribute_struct, T, collection, attribute)
_check_vector(attribute_struct, collection, attribute)
if validate
_check_type(attribute_struct, T, collection, attribute)
_check_vector(attribute_struct, collection, attribute)
end
_check_dim(attribute_struct, collection, attribute, dim1, dim2)

dim = get_attribute_dim(attribute_struct)
Expand Down
6 changes: 3 additions & 3 deletions src/PMD/PMD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function _load_model!(
loaded_files,
model_template,
relation_mapper;
verbose
verbose,
)
end
else
Expand All @@ -123,7 +123,7 @@ function _load_model!(
loaded_files,
model_template,
relation_mapper;
verbose
verbose,
)
end
end
Expand All @@ -149,7 +149,7 @@ function load_model(
loaded_files,
model_template,
relation_mapper;
verbose
verbose,
)

return data_struct, loaded_files
Expand Down
13 changes: 8 additions & 5 deletions src/PMD/model_template.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using JSON


"""
ModelTemplate
struct ModelTemplate
map::Dict{String, Set{String}}
inv::Dict{String, String}
map::Dict{String, Set{String}}
inv::Dict{String, String}
end
Data structure to store the model template information.
The keys of `map` contain the class name, for instance, `PSRHydroPlant`, the values
contain the models names in the pmd files.
"""
struct ModelTemplate
map::Dict{String, Set{String}}
inv::Dict{String, String}

ModelTemplate() = new(Dict{String, Set{String}}(), Dict{String, String}())
function ModelTemplate(
map::Dict{String, Set{String}} = Dict{String, Set{String}}(),
inv::Dict{String, String} = Dict{String, String}(),
)
return new(map, inv)
end
end

function Base.push!(mt::ModelTemplate, ps::Pair{String, String}...)
Expand Down
Loading

0 comments on commit 5206029

Please sign in to comment.