Skip to content

Commit

Permalink
Address PR comments: rename filterby -> scope_limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Oct 2, 2024
1 parent 725f4ca commit 9cc505b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
66 changes: 35 additions & 31 deletions src/component_selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,74 @@
# InfrastructureSystemsComponent with Component and SystemData with System

"""
get_components(selector, sys; filterby = nothing)
get_components(selector, sys; scope_limiter = nothing)
Get the components of the `System` that make up the `ComponentSelector`.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_components(selector::ComponentSelector, sys::System; filterby = nothing) =
IS.get_components(selector, sys; filterby = filterby)
get_components(selector::ComponentSelector, sys::System; scope_limiter = nothing) =
IS.get_components(selector, sys; scope_limiter = scope_limiter)

"""
get_components(filterby, selector, sys)
get_components(scope_limiter, selector, sys)
Get the components of the `System` that make up the `ComponentSelector`.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_components(
filterby::Union{Nothing, Function},
scope_limiter::Union{Nothing, Function},
selector::ComponentSelector,
sys::System,
) =
get_components(selector, sys; filterby = filterby)
get_components(selector, sys; scope_limiter = scope_limiter)

# This would be cleaner if `IS.get_components === PSY.get_components` (see
# https://github.com/NREL-Sienna/InfrastructureSystems.jl/issues/388)
IS.get_components(selector::ComponentSelector, sys::System; filterby = nothing) =
IS.get_components(selector, sys.data; filterby = filterby)
IS.get_components(selector::ComponentSelector, sys::System; scope_limiter = nothing) =
IS.get_components(selector, sys.data; scope_limiter = scope_limiter)

"""
get_component(selector, sys; filterby = nothing)
get_component(selector, sys; scope_limiter = nothing)
Get the component of the `System` that makes up the `SingularComponentSelector`; `nothing`
if there is none.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_component(selector::SingularComponentSelector, sys::System; filterby = nothing) =
IS.get_component(selector, sys.data; filterby = filterby)
get_component(selector::SingularComponentSelector, sys::System; scope_limiter = nothing) =
IS.get_component(selector, sys.data; scope_limiter = scope_limiter)

"""
get_component(filterby, selector, sys)
get_component(scope_limiter, selector, sys)
Get the component of the `System` that makes up the `SingularComponentSelector`; `nothing`
if there is none.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_component(
filterby::Union{Nothing, Function},
scope_limiter::Union{Nothing, Function},
selector::SingularComponentSelector,
sys::System,
) =
get_component(selector, sys; filterby = filterby)
get_component(selector, sys; scope_limiter = scope_limiter)

IS.get_component(selector::ComponentSelector, sys::System; filterby = nothing) =
IS.get_component(selector, sys.data; filterby = filterby)
IS.get_component(selector::ComponentSelector, sys::System; scope_limiter = nothing) =
IS.get_component(selector, sys.data; scope_limiter = scope_limiter)

"""
get_groups(selector, sys; filterby = nothing)
get_groups(selector, sys; scope_limiter = nothing)
Get the groups that make up the `ComponentSelector`.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_groups(selector::ComponentSelector, sys::System; filterby = nothing) =
IS.get_groups(selector, sys; filterby = filterby)
get_groups(selector::ComponentSelector, sys::System; scope_limiter = nothing) =
IS.get_groups(selector, sys; scope_limiter = scope_limiter)

"""
get_groups(filterby, selector, sys)
get_groups(scope_limiter, selector, sys)
Get the groups that make up the `ComponentSelector`.
- `filterby`: optional filter function to apply after evaluating the `ComponentSelector`
- `scope_limiter`: optional filter function to limit the scope of components under consideration (e.g., pass `get_available` to only evaluate the `ComponentSelector` on components marked available)
"""
get_groups(filterby::Union{Nothing, Function}, selector::ComponentSelector, sys::System) =
get_groups(selector, sys; filterby = filterby)
get_groups(
scope_limiter::Union{Nothing, Function},
selector::ComponentSelector,
sys::System,
) =
get_groups(selector, sys; scope_limiter = scope_limiter)

# TopologyComponentSelector
# This one is wholly implemented in PowerSystems rather than in InfrastructureSystems because it depends on `PSY.AggregationTopology`
Expand Down Expand Up @@ -109,7 +113,7 @@ IS.default_name(selector::TopologyComponentSelector) =
function IS.get_components(
selector::TopologyComponentSelector,
sys::System;
filterby = nothing,
scope_limiter = nothing,
)
agg_topology = get_component(selector.topology_type, sys, selector.topology_name)
isnothing(agg_topology) &&
Expand All @@ -119,6 +123,6 @@ function IS.get_components(
sys,
agg_topology,
)
isnothing(filterby) && (return components)
return Iterators.filter(filterby, components)
isnothing(scope_limiter) && (return components)
return Iterators.filter(scope_limiter, components)
end
31 changes: 18 additions & 13 deletions test/test_component_selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ end
@test typeof(first(the_components)) == ThermalStandard
@test get_name(first(the_components)) == "Solitude"
@test collect(
get_components(make_selector(gen_sundance), test_sys; filterby = get_available),
get_components(
make_selector(gen_sundance),
test_sys;
scope_limiter = get_available,
),
) ==
Vector{Component}()
@test IS.get_component(test_gen_ent, test_sys; filterby = x -> true) ==
@test IS.get_component(test_gen_ent, test_sys; scope_limiter = x -> true) ==
first(the_components)
@test isnothing(IS.get_component(test_gen_ent, test_sys; filterby = x -> false))
@test isnothing(IS.get_component(test_gen_ent, test_sys; scope_limiter = x -> false))

@test only(get_groups(test_gen_ent, test_sys)) == test_gen_ent
end
Expand Down Expand Up @@ -113,7 +117,7 @@ end
@test all(the_components .== answer)
@test !(
gen_sundance in
collect(get_components(test_sub_ent, test_sys; filterby = get_available)))
collect(get_components(test_sub_ent, test_sys; scope_limiter = get_available)))

# Grouping inherits from `DynamicallyGroupedComponentSelector` and is tested elsewhere
end
Expand Down Expand Up @@ -167,9 +171,10 @@ end

the_components = get_components(ent, test_sys2)
@test all(sort_name!(the_components) .== ans)
@test Set(collect(get_components(ent, test_sys2; filterby = x -> true))) ==
@test Set(collect(get_components(ent, test_sys2; scope_limiter = x -> true))) ==
Set(the_components)
@test length(collect(get_components(ent, test_sys2; filterby = x -> false))) == 0
@test length(collect(get_components(ent, test_sys2; scope_limiter = x -> false))) ==
0
end
end

Expand Down Expand Up @@ -210,11 +215,11 @@ end
@test all(collect(get_components(test_filter_ent, test_sys)) .== answer)
@test !(
gen_sundance in
collect(get_components(test_filter_ent, test_sys; filterby = get_available)))
collect(get_components(test_filter_ent, test_sys; scope_limiter = get_available)))

@test !(
gen_sundance in
collect(get_components(test_filter_ent, test_sys; filterby = get_available)))
collect(get_components(test_filter_ent, test_sys; scope_limiter = get_available)))
end

@testset "Test DynamicallyGroupedComponentSelector grouping" begin
Expand All @@ -238,15 +243,15 @@ end
@test length(
collect(
get_groups(each_selector, test_sys2;
filterby = x -> length(get_name(x)) == 8),
scope_limiter = x -> length(get_name(x)) == 8),
),
) == 2
@test Set(get_name.(get_groups(partition_selector, test_sys2))) ==
Set(["true", "false"])
@test length(
collect(
get_groups(partition_selector, test_sys2;
filterby = x -> length(get_name(x)) == 8),
scope_limiter = x -> length(get_name(x)) == 8),
),
) == 1

Expand All @@ -262,10 +267,10 @@ end

@testset "Test alternative interfaces" begin
selector = make_selector(ThermalStandard, "Solitude")
@test get_components(selector, test_sys; filterby = x -> true) ==
@test get_components(selector, test_sys; scope_limiter = x -> true) ==
get_components(x -> true, selector, test_sys)
@test get_component(selector, test_sys; filterby = x -> true) ==
@test get_component(selector, test_sys; scope_limiter = x -> true) ==
get_component(x -> true, selector, test_sys)
@test get_groups(selector, test_sys; filterby = x -> true) ==
@test get_groups(selector, test_sys; scope_limiter = x -> true) ==
get_groups(x -> true, selector, test_sys)
end

0 comments on commit 9cc505b

Please sign in to comment.