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

Jd/add infos #316

Merged
merged 47 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
16f87d2
add info types
jd-lara Nov 21, 2023
bb09978
generalize containers
jd-lara Nov 21, 2023
390e9b1
add geographic info as example
jd-lara Nov 21, 2023
f91f19e
fixes to compat
jd-lara Nov 29, 2023
24d2cc2
integrate info to components
jd-lara Nov 29, 2023
592a804
WIP integrating infos closer in the system data
jd-lara Nov 29, 2023
fda144e
add some additional functionality for geo info
jd-lara Nov 29, 2023
e5812b4
add info interface
jd-lara Nov 29, 2023
b532839
improve info interface
jd-lara Nov 29, 2023
caa18ef
WIP add testing
jd-lara Nov 29, 2023
d48e685
fix push! call
jd-lara Nov 29, 2023
db109e8
fix a very old typo
jd-lara Nov 29, 2023
d14f4bf
update printing methods
jd-lara Nov 29, 2023
47d1c70
add more info capabilities
jd-lara Nov 29, 2023
626cf93
add component and info removal
jd-lara Nov 29, 2023
d400498
update the methods for geo infos
jd-lara Nov 29, 2023
41fc858
improve testing and docs
jd-lara Nov 29, 2023
97b5f63
fix typo
jd-lara Nov 30, 2023
ec52c29
fix details
jd-lara Nov 30, 2023
597160e
implement system interface
jd-lara Nov 30, 2023
91f4e95
use types directly
jd-lara Nov 30, 2023
b4a3d4b
rename info -> supplemental_attribute
jd-lara Dec 4, 2023
d8c1439
Update src/InfrastructureSystems.jl
jd-lara Dec 12, 2023
904066f
Update src/component.jl
jd-lara Dec 12, 2023
a1e881b
name replace
jd-lara Dec 12, 2023
db36a70
fix more comments
jd-lara Dec 12, 2023
89ddbe6
fix more comments
jd-lara Dec 12, 2023
0d561e6
implement additional methods
jd-lara Dec 12, 2023
f875522
address comments in getters
jd-lara Dec 12, 2023
89ea1b4
remane geographic information object
jd-lara Dec 12, 2023
c87ebed
new methods and renames
jd-lara Dec 12, 2023
6be70b8
rename to GeographicInfo
jd-lara Dec 12, 2023
008204c
use try catch with removal
jd-lara Dec 12, 2023
f2855b1
formatter
jd-lara Dec 12, 2023
fc75db6
Update src/system_data.jl
jd-lara Dec 12, 2023
be0e6c5
fix almost all tests
jd-lara Dec 12, 2023
01c7911
address PR comments
jd-lara Dec 18, 2023
7a8de74
add time series testing
jd-lara Dec 18, 2023
4e2d04a
update formatter action
jd-lara Dec 18, 2023
cd73f60
address PR comments
jd-lara Dec 21, 2023
7c31557
address comments
jd-lara Dec 21, 2023
0f6b54d
address comments
jd-lara Dec 21, 2023
155ea39
use detach methods
jd-lara Dec 21, 2023
3a0d1b7
address PR comments
jd-lara Dec 21, 2023
423874f
more pr comments
jd-lara Dec 21, 2023
1192753
fix serialization
jd-lara Dec 21, 2023
deebdbc
formatter
jd-lara Dec 21, 2023
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
10 changes: 8 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
Expand All @@ -38,6 +37,13 @@ Mustache = "1"
PrettyTables = "^1.3, 2"
StructTypes = "^1.9"
TerminalLoggers = "~0.1"
TimeSeries = "~0.22, 0.23"
TimeSeries = "0.23, 0.24"
YAML = "~0.4"
julia = "^1.6"
Dates = "1"
InteractiveUtils = "1"
Logging = "1"
Pkg = "1"
Random = "1"
TOML = "1"
UUIDs = "1"
23 changes: 23 additions & 0 deletions src/InfrastructureSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ Base type for auxillary structs. These should not be stored in a system.
"""
abstract type DeviceParameter <: InfrastructureSystemsType end

"""
Base type for structs that store infos

Required interface functions for subtypes:

- get_internal()

Optional interface functions:

- get_time_series_container()
- get_components_uuid()
- get_uuid()

Subtypes may contain time series. Which requires

- get_time_series_container()
"""
abstract type InfrastructureSystemsInfo <: InfrastructureSystemsType end

"""
Return the internal time_series storage container or nothing, if the type doesn't store
time series.
Expand Down Expand Up @@ -92,7 +111,11 @@ include("forecasts.jl")
include("static_time_series.jl")
include("time_series_container.jl")
include("time_series_parser.jl")
include("containers.jl")
include("info.jl")
include("infos.jl")
include("components.jl")
include("geographic_info.jl")
include("generated/includes.jl")
include("single_time_series.jl")
include("deterministic_single_time_series.jl")
Expand Down
70 changes: 70 additions & 0 deletions src/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function clear_time_series!(component::InfrastructureSystemsComponent)
@debug "Cleared time_series in $(get_name(component))." _group =
LOG_GROUP_TIME_SERIES
end
return
end

function _get_columns(start_time, count, ts_metadata::ForecastMetadata)
Expand Down Expand Up @@ -585,6 +586,7 @@ function prepare_for_removal!(component::InfrastructureSystemsComponent)
set_time_series_storage!(component, nothing)
clear_time_series!(component)
@debug "cleared all time series data from" _group = LOG_GROUP_SYSTEM get_name(component)
return
end

"""
Expand Down Expand Up @@ -802,6 +804,7 @@ function clear_time_series_storage!(component::InfrastructureSystemsComponent)
end
end
end
return
end

function set_time_series_storage!(
Expand All @@ -812,6 +815,7 @@ function set_time_series_storage!(
if !isnothing(container)
set_time_series_storage!(container, storage)
end
return
end

function _get_time_series_storage(component::InfrastructureSystemsComponent)
Expand Down Expand Up @@ -868,3 +872,69 @@ function assign_new_uuid!(component::InfrastructureSystemsComponent)
set_uuid!(get_internal(component), new_uuid)
return
end

"""
Attaches an info to a component
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
"""
function attach_info!(
component::InfrastructureSystemsComponent,
info::T,
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
) where {T <: InfrastructureSystemsInfo}
component_uuid = get_uuid(component)

if component_uuid ∈ info.components_uuid
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
throw(
ArgumentError(
"Info type $T with UUID $(get_uuid(info)) already attached to component $(get_name(component))",
),
)
end

push!(info.components_uuid, component_uuid)
infos_container = get_infos_container(component)

if !haskey(infos_container, T)
infos_container[T] = Set{T}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a Dict (keyed by UUID) instead of Set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about what the interface should be, if we want to retrieve an attribute from the component It should be by type since the specific UUID wouldn't be known by the user like the component name.

end
push!(infos_container[T], info)
@debug "Info type $T with UUID $(get_uuid(info)) stored in component $(get_name(component))"
return
end

"""
Return true if the component has infos.
"""
function has_infos(component::InfrastructureSystemsComponent)
container = get_infos_container(component)
return !isempty(container)
end

function clear_infos!(component::InfrastructureSystemsComponent)
container = get_infos_container(component)
for info_set in values(container)
for i in info_set
delete!(get_components_uuid(i), get_uuid(component))
end
end
empty!(container)
@debug "Cleared infos in $(get_name(component))."
return
end

function remove_info!(
component::InfrastructureSystemsComponent,
info::T,
) where {T <: InfrastructureSystemsInfo}
container = get_infos_container(component)
if !haskey(container, T)
throw(
ArgumentError("info type $T is not stored in component $(get_name(component))"),
)
end
delete!(get_components_uuid(info), get_uuid(component))
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
delete!(container[T], info)
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
if isempty(container[T])
pop!(container, T)
end
return
end
43 changes: 9 additions & 34 deletions src/components.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

const ComponentsByType = Dict{DataType, Dict{String, <:InfrastructureSystemsComponent}}

struct Components
struct Components <: InfrastructureSystemsContainer
data::ComponentsByType
time_series_storage::TimeSeriesStorage
validation_descriptors::Vector
end

get_display_string(::Components) = "components"

function Components(time_series_storage::TimeSeriesStorage, validation_descriptors=nothing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function Components(time_series_storage::TimeSeriesStorage, validation_descriptors=nothing)
function Components(
time_series_storage::TimeSeriesStorage,
validation_descriptors = nothing,
)

if isnothing(validation_descriptors)
validation_descriptors = Vector()
Expand All @@ -15,11 +17,6 @@ function Components(time_series_storage::TimeSeriesStorage, validation_descripto
return Components(ComponentsByType(), time_series_storage, validation_descriptors)
end

function serialize(components::Components)
# time_series_storage and validation_descriptors are serialized elsewhere.
return [serialize(x) for y in values(components.data) for x in values(y)]
end

function _add_component!(
components::Components,
component::T;
Expand Down Expand Up @@ -91,6 +88,7 @@ function check_component(components::Components, comp::InfrastructureSystemsComp
if !validate_struct(comp)
throw(InvalidValue("$(summary(comp)) is invalid"))
end
return
end

"""
Expand Down Expand Up @@ -268,7 +266,7 @@ Call collect on the result if an array is desired.
# Arguments

- `T`: component type
- `components::Components`: Components of the sytem
- `components::Components`: Components of the system
- `filter_func::Union{Nothing, Function} = nothing`: Optional function that accepts a component
of type T and returns a Bool. Apply this function to each component and only return components
where the result is true.
Expand Down Expand Up @@ -321,39 +319,15 @@ end
See also: [`get_components`](@ref)
"""
function iterate_components(components::Components)
Channel() do channel
for comp_dict in values(components.data)
for component in values(comp_dict)
put!(channel, component)
end
end
end
iterate_container(components)
end

function iterate_components_with_time_series(components::Components)
Channel() do channel
for comp_dict in values(components.data)
for component in values(comp_dict)
if has_time_series(component)
put!(channel, component)
end
end
end
end
iterate_container_with_time_series(components)
end

function get_num_components(components::Components)
count = 0
for components in values(components.data)
count += length(components)
end
return count
end

function clear_time_series!(components::Components)
for component in iterate_components_with_time_series(components)
clear_time_series!(component)
end
return get_num_members(components)
end

function is_attached(
Expand Down Expand Up @@ -388,6 +362,7 @@ function set_name!(
set_name_internal!(component, name)
components.data[T][name] = component
@debug "Changed the name of component $(summary(component))" _group = LOG_GROUP_SYSTEM
return
end

function compare_values(x::Components, y::Components; compare_uuids=false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function compare_values(x::Components, y::Components; compare_uuids=false)
function compare_values(x::Components, y::Components; compare_uuids = false)

Expand Down
46 changes: 46 additions & 0 deletions src/containers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
abstract type InfrastructureSystemsContainer end

function serialize(container::InfrastructureSystemsContainer)
# time_series_storage and validation_descriptors are serialized elsewhere.
return [serialize(x) for y in values(container.data) for x in values(y)]
end

"""
Iterates over all data in the container.
"""
function iterate_container(container::InfrastructureSystemsContainer)
Channel() do channel
for m_dict in values(container.data)
for member in values(m_dict)
put!(channel, member)
end
end
end
end

function iterate_container_with_time_series(container::InfrastructureSystemsContainer)
Channel() do channel
for m_dict in values(container.data)
for member in values(m_dict)
if has_time_series(member)
put!(channel, member)
end
end
end
end
end

function get_num_members(container::InfrastructureSystemsContainer)
count = 0
for members in values(container.data)
count += length(members)
end
return count
end

function clear_time_series!(container::InfrastructureSystemsContainer)
for member in iterate_components_with_time_series(container)
clear_time_series!(member)
end
return
end
33 changes: 33 additions & 0 deletions src/geographic_info.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Base type for structs that store infos

Required interface functions for subtypes:

- get_internal()
- get_components_uuid()
- get_time_series_container()

Subtypes may contain time series, if no time series container is implemented return nothing
"""
struct InfrastructureSystemsGeo <: InfrastructureSystemsInfo
geo_json::Dict{String, Any}
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
components_uuid::Set{UUIDs.UUID}
internal::InfrastructureSystemsInternal
end

function InfrastructureSystemsGeo(;
geo_json::Dict{String, Any}=Dict{String, Any}(),
components_uuid::Set{UUIDs.UUID}=Set{UUIDs.UUID}(),
)
return InfrastructureSystemsGeo(
geo_json,
components_uuid,
InfrastructureSystemsInternal(),
)
end

get_geo_json(geo::InfrastructureSystemsGeo) = geo.geo_json
get_internal(geo::InfrastructureSystemsGeo) = geo.internal
get_uuid(geo::InfrastructureSystemsGeo) = get_uuid(get_internal(geo))
get_time_series_container(::InfrastructureSystemsGeo) = nothing
get_components_uuid(geo::InfrastructureSystemsGeo) = geo.components_uuid
Loading
Loading