Skip to content

Commit

Permalink
Merge pull request #182 from psrenergy/pr/read-defaults
Browse files Browse the repository at this point in the history
Fix `load_study`
  • Loading branch information
guilhermebodin authored Oct 18, 2023
2 parents f7c708d + e113f9c commit fe79487
Show file tree
Hide file tree
Showing 7 changed files with 973 additions and 832 deletions.
30 changes: 29 additions & 1 deletion src/OpenStudy/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function is_vector_relation(relation::PMD.RelationType)
end

"""
_has_relation_attribute(relations::Vector{PMD.Relation}, relation_attribute::String)
_has_relation_attribute(relations::Dict{String, PMD.Relation}, relation_attribute::String)
Returns true if there is a relation with attribute 'relation_attribute' in a 'Vector{PMD.Relation}'
"""
Expand All @@ -24,6 +24,34 @@ function _has_relation_attribute(
return false
end

function _has_relation_attribute(
relations::Dict{String, Dict{String, PMD.Relation}},
relation_attribute::String,
)
for (target, relations) in relations
if _has_relation_attribute(relations, relation_attribute)
return true
end
end
return false
end

function _has_relation_attribute(
relations::Dict{String, Dict{String, Dict{String, PSRClassesInterface.PMD.Relation}}},
source::String,
relation_attribute::String,
)
if !haskey(relations, source)
return false
end
for (source, source_relations) in relations[source]
if _has_relation_attribute(source_relations, relation_attribute)
return true
end
end
return false
end

"""
_has_relation_type(relations::Vector{PMD.Relation}, relation_type::PMD.RelationType)
Expand Down
5 changes: 5 additions & 0 deletions src/OpenStudy/study_openinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ function load_study(
data_path = "",
pmd_files = String[],
path_pmds = PMD._PMDS_BASE_PATH,
rectify_json_data::Bool = false,
log_file::Union{AbstractString, Nothing} = nothing,
verbose = true,
extra_config_file::String = "",
Expand Down Expand Up @@ -347,6 +348,10 @@ function load_study(
relation_mapper = relation_mapper,
)

if rectify_json_data
_rectify_study_data!(data)
end

if add_transformers_to_series
_merge_psr_transformer_and_psr_serie!(data)
end
Expand Down
13 changes: 12 additions & 1 deletion src/modification_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,10 @@ end

function _cast_element!(data::Data, collection::String, element::Dict{String, Any})
for (attribute, value) in element
if _has_relation_attribute(data.relation_mapper, collection, attribute) ||
attribute == "reference_id"
continue
end
T = get_attribute_type(data, collection, attribute)

if is_vector_attribute(data, collection, attribute)
Expand Down Expand Up @@ -806,6 +810,14 @@ function delete_element!(data::Data, collection::String, index::Int)
return nothing
end

function _rectify_study_data!(data::Data)
for (collection, elements) in data.raw
for element in 1:length(elements)
_cast_element!(data, collection, data.raw[collection][element])
end
end
end

summary(io::IO, args...) = print(io, summary(args...))

function summary(data::Data)
Expand Down Expand Up @@ -929,7 +941,6 @@ function _build_index!(data::Data)
end
end
end

return nothing
end

Expand Down
Loading

0 comments on commit fe79487

Please sign in to comment.