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

Handle relations using element name #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions src/OpenStudy/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@
return relations_as_source, relations_as_target
end

function _get_element_related(data::Data, collection::String, name::String)
idx = _get_index(data, collection, name)
return _get_element_related(data, collection, idx)

Check warning on line 368 in src/OpenStudy/relations.jl

View check run for this annotation

Codecov / codecov/patch

src/OpenStudy/relations.jl#L366-L368

Added lines #L366 - L368 were not covered by tests
end

"""
has_relations(data::Data, collection::String)

Expand Down Expand Up @@ -391,6 +396,11 @@
return false
end

function has_relations(data::Data, collection::String, name::String)
idx = _get_index(data, collection, name)
return has_relations(data, collection, idx)
end

"""
relations_summary(data::Data, collection::String, index::Integer)

Expand Down Expand Up @@ -427,6 +437,11 @@
return
end

function relations_summary(data::Data, collection::String, name::String)
idx = _get_index(data, collection, name)
return relations_summary(data, collection, idx)
end

"""
check_relation_scalar(relation_type::PMD.RelationType)

Expand Down Expand Up @@ -859,6 +874,17 @@
return 0 # for type stability
end

function get_related(
data::Data,
source::String,
target::String,
source_name::String;
relation_type::PMD.RelationType = PMD.RELATION_1_TO_1,
)
idx = _get_index(data, source, source_name)
return get_related(data, source, target, idx; relation_type = relation_type)
end

function get_vector_related(
data::Data,
source::String,
Expand Down Expand Up @@ -893,3 +919,14 @@

return target_index_list
end

function get_vector_related(
data::Data,
source::String,
target::String,
source_name::String,
relation_type::PMD.RelationType = PMD.RELATION_1_TO_N,
)
idx = _get_index(data, source, source_name)
return get_vector_related(data, source, target, idx, relation_type)
end
17 changes: 17 additions & 0 deletions src/OpenStudy/study_openinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@
return nothing
end

"""
_get_index(data::Data, collection::String, name::String)

Returns the index of an element from a collection, based on its name attribute
"""
function _get_index(data::Data, collection::String, name::String)
elements = data.raw[collection]
for idx in eachindex(elements)
if elements[idx]["name"] == name
return idx
end
end
return error(

Check warning on line 238 in src/OpenStudy/study_openinterface.jl

View check run for this annotation

Codecov / codecov/patch

src/OpenStudy/study_openinterface.jl#L238

Added line #L238 was not covered by tests
"Element from collection $(collection) with name $(name) not found.",
)
end

function load_study(
::OpenInterface;
data_path = "",
Expand Down
71 changes: 71 additions & 0 deletions src/modification_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,27 @@ function set_related!(
return nothing
end

function set_related!(
data::Data,
source::String,
target::String,
source_name::String,
target_name::String;
relation_type::PMD.RelationType = PMD.RELATION_1_TO_1,
)
source_idx = _get_index(data, source, source_name)
target_idx = _get_index(data, target, target_name)

return set_related!(
data,
source,
target,
source_idx,
target_idx;
relation_type = relation_type,
)
end

function set_related_by_code!(
data::Data,
source::String,
Expand Down Expand Up @@ -355,6 +376,29 @@ function set_vector_related!(
return nothing
end

function set_vector_related!(
data::Data,
source::String,
target::String,
source_name::String,
target_names::Vector{T},
relation_type::PMD.RelationType = PMD.RELATION_1_TO_N,
) where {T <: String}
source_idx = _get_index(data, source, source_name)
target_indices = Vector{Integer}()
for target_name in target_names
push!(target_indices, _get_index(data, target, target_name))
end
return set_vector_related!(
data,
source,
target,
source_idx,
target_indices,
relation_type,
)
end

function delete_relation!(
data::Data,
source::String,
Expand Down Expand Up @@ -382,6 +426,18 @@ function delete_relation!(
return nothing
end

function delete_relation!(
data::Data,
source::String,
target::String,
source_name::String,
target_name::String,
)
source_idx = _get_index(data, source, source_name)
target_idx = _get_index(data, target, target_name)
return delete_relation!(data, source, target, source_idx, target_idx)
end

function delete_vector_relation!(
data::Data,
source::String,
Expand Down Expand Up @@ -412,6 +468,21 @@ function delete_vector_relation!(
return nothing
end

function delete_vector_relation!(
data::Data,
source::String,
target::String,
source_name::String,
target_names::Vector{String},
)
source_idx = _get_index(data, source, source_name)
target_indices = Vector{Int}()
for target_name in target_names
push!(target_indices, _get_index(data, target, target_name))
end
return delete_vector_relation!(data, source, target, source_idx, target_indices)
end

function Base.show(io::IO, data::Data)
return summary(io, data)
end
Expand Down
54 changes: 50 additions & 4 deletions test/modification_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ function test_api8() #tests delete_relation!

data = PSRI.create_study(PSRI.OpenInterface(); data_path = temp_path)

index1 = PSRI.create_element!(data, "PSRBus")
index2 = PSRI.create_element!(data, "PSRBus")
index1 = PSRI.create_element!(data, "PSRBus", "name" => "Bus1")
index2 = PSRI.create_element!(data, "PSRBus", "name" => "Bus2")

index3 = PSRI.create_element!(data, "PSRSerie")
index3 = PSRI.create_element!(data, "PSRSerie", "name" => "Serie1")

PSRI.set_related!(
data,
Expand Down Expand Up @@ -309,7 +309,7 @@ function test_api8() #tests delete_relation!
@test PSRI.get_map(data, "PSRSerie", "PSRBus"; relation_type = PSRI.PMD.RELATION_TO) ==
[1]

PSRI.delete_relation!(data, "PSRSerie", "PSRBus", index3, index1)
PSRI.delete_relation!(data, "PSRSerie", "PSRBus", "Serie1", "Bus1")
PSRI.delete_relation!(data, "PSRSerie", "PSRBus", index3, index2)

PSRI.write_data(data)
Expand Down Expand Up @@ -375,6 +375,51 @@ function test_api9() #tests delete_vector_relation!
@test map_vec_copy == Vector{Int32}[[]]
end

function test_api10() #tests delete_vector_relation!
temp_path = joinpath(tempdir(), "PSRI_10")
json_path = joinpath(temp_path, "psrclasses.json")

mkpath(temp_path)

data = PSRI.create_study(PSRI.OpenInterface(); data_path = temp_path)

PSRI.create_element!(
data,
"PSRThermalPlant",
"ShutDownCost" => 1.0,
"name" => "Thermal1",
)
PSRI.create_element!(data, "PSRFuel", "name" => "Fuel1")
PSRI.create_element!(data, "PSRFuel", "name" => "Fuel2")
PSRI.create_element!(data, "PSRFuel", "name" => "Fuel3")

PSRI.set_vector_related!(
data,
"PSRThermalPlant",
"PSRFuel",
"Thermal1",
["Fuel1", "Fuel2", "Fuel3"],
)
map_vec = PSRI.get_vector_map(data, "PSRThermalPlant", "PSRFuel")
@test map_vec == Vector{Int32}[[1, 2, 3]]
PSRI.write_data(data)

PSRI.delete_vector_relation!(
data,
"PSRThermalPlant",
"PSRFuel",
"Thermal1",
["Fuel1", "Fuel2", "Fuel3"],
)

PSRI.write_data(data)

data_copy = PSRI.load_study(PSRI.OpenInterface(); data_path = temp_path)

map_vec_copy = PSRI.get_vector_map(data_copy, "PSRThermalPlant", "PSRFuel")
@test map_vec_copy == Vector{Int32}[[]]
end

test_api(PATH_CASE_0)
test_api2()
test_api3()
Expand All @@ -384,3 +429,4 @@ test_api6()
test_api7()
test_api8()
test_api9()
test_api10()
Loading
Loading