Skip to content

Commit

Permalink
ComponentSelector: Implement in(component, selector)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Sep 18, 2024
1 parent a31ff52 commit b11ccf6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/component_selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
Concrete subtypes MUST implement:
- `get_components`
- `get_name`, or use the default by implementing `default_name` and having a `name` field
- `Base.in`, with the invariant that for all systems `s` and all `ComponentSelector`s `e`,
`c in e` iff `c in collect(get_components(e, s))` for all components `c` in `s`. Ideally,
`in` should be evaluable without a reference to a system, but a three-parameter form
in(c, e, s) may be used if absolutely necessary.
Concrete subtypes MAY implement:
- The factory method `select_components` (make sure your signature does not conflict with
Expand Down Expand Up @@ -117,6 +121,15 @@ function get_components(e::SingleComponentSelector, sys::Components; filterby =
return (com === nothing) ? [] : [com]
end

function Base.in(
c::InfrastructureSystemsComponent,
e::SingleComponentSelector;
filterby = nothing,
)
(!isnothing(filterby) && !filterby(c)) && return false
return (c isa e.component_subtype) && (get_name(c) == e.component_name)
end

# ListComponentSelector
"ComponentSelectorSet represented by a list of other ComponentSelectors."
struct ListComponentSelector <: ComponentSelectorSet
Expand Down Expand Up @@ -148,6 +161,15 @@ function get_components(e::ListComponentSelector, sys::Components; filterby = no
return Iterators.flatten(sub_components)
end

function Base.in(
c::InfrastructureSystemsComponent,
e::ListComponentSelector;
filterby = nothing,
)
(!isnothing(filterby) && !filterby(c)) && return false
return any(in.(Ref(c), e.content))
end

# SubtypeComponentSelector
"ComponentSelectorSet represented by a subtype of Component."
struct SubtypeComponentSelector <: ComponentSelectorSet
Expand Down Expand Up @@ -181,6 +203,15 @@ function get_components(e::SubtypeComponentSelector, sys::Components; filterby =
return Iterators.filter(filterby, components)
end

function Base.in(
c::InfrastructureSystemsComponent,
e::SubtypeComponentSelector;
filterby = nothing,
)
(!isnothing(filterby) && !filterby(c)) && return false
return c isa e.component_subtype
end

# FilterComponentSelector
"ComponentSelectorSet represented by a filter function and a subtype of Component."
struct FilterComponentSelector <: ComponentSelectorSet
Expand Down Expand Up @@ -225,6 +256,15 @@ function get_components(e::FilterComponentSelector, sys::Components; filterby =
return Iterators.filter(filterby, components)
end

function Base.in(
c::InfrastructureSystemsComponent,
e::FilterComponentSelector;
filterby = nothing,
)
(!isnothing(filterby) && !filterby(c)) && return false
return (c isa e.component_subtype) && e.filter_fn(c)
end

# Naming
default_name(e::FilterComponentSelector) =
string(e.filter_fn) * NAME_DELIMETER * subtype_to_string(e.component_subtype)
24 changes: 24 additions & 0 deletions test/test_component_selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ end
@test length(
collect(IS.get_components(test_gen_ent, test_sys; filterby = x -> false)),
) == 0

@test IS.TestComponent("Component1", -1) in test_gen_ent
@test !(IS.TestComponent("Component2", -1) in test_gen_ent)
@test !in(IS.TestComponent("Component1", -1), test_gen_ent; filterby = x -> false)
end
end

Expand Down Expand Up @@ -124,6 +128,14 @@ end
@test length(
collect(IS.get_subselectors(test_list_ent, test_sys; filterby = x -> false)),
) == 2

@test IS.AdditionalTestComponent("Component3", -1) in test_list_ent
@test !(IS.AdditionalTestComponent("Component4", -1) in test_list_ent)
@test !in(
IS.AdditionalTestComponent("Component3", -1),
test_list_ent;
filterby = x -> false,
)
end
end

Expand Down Expand Up @@ -173,6 +185,10 @@ end
@test length(
collect(IS.get_subselectors(test_sub_ent, test_sys; filterby = x -> false)),
) == 0

@test IS.TestComponent("Component1", -1) in test_sub_ent
@test !(IS.AdditionalTestComponent("Component1", -1) in test_sub_ent)
@test !in(IS.TestComponent("Component1", -1), test_sub_ent; filterby = x -> false)
end
end

Expand Down Expand Up @@ -255,5 +271,13 @@ end
@test length(
collect(IS.get_subselectors(test_filter_ent, test_sys; filterby = x -> false)),
) == 0

@test IS.TestComponent("Component1", 20) in test_filter_ent
@test !(IS.AdditionalTestComponent("Component1", 0) in test_filter_ent)
@test !in(
IS.TestComponent("Component1", 20),
test_filter_ent;
filterby = x -> false,
)
end
end

0 comments on commit b11ccf6

Please sign in to comment.