Skip to content

Commit 7516f32

Browse files
daniel-thomclaude
andcommitted
Port #587 review follow-ups: subsystem id remap & UNASSIGNED_ID guards
Bring the integer-id model to parity with PR #587 (the pure-Julia integer-id PR), porting the review-driven fixes that were missing here: - assign_new_id_internal!: also remap the component's id in subsystem membership sets (previously left stale, breaking subsystem lookups after reassignment). - Guard against UNASSIGNED_ID on the manager-direct path when attaching a supplemental attribute and when adding an association, with actionable errors instead of colliding at id 0. - Fix "subystem" typos in system_data.jl and subsystems.jl. - Tests: assert subsystem membership tracks the new id after assign_new_id!; assign ids in the manager-direct supplemental attribute tests. (PR #587's f896870 put owner_category in the Julia metadata-store unique index; here that lives in the Rust store instead — NatLabRockies/infrastore#4.) Full test suite passes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 25d1f55 commit 7516f32

7 files changed

Lines changed: 51 additions & 15 deletions

src/component.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
Assign a new integer id to the component, drawn from the system counter, and update any
3-
references to its old id in the time series store and supplemental attribute associations.
2+
Assign a new integer ID to the component, drawn from the system counter, and update any
3+
references to its old ID in the time series store, supplemental attribute associations, and
4+
subsystem membership sets.
45
"""
56
function assign_new_id_internal!(data, component::InfrastructureSystemsComponent)
67
old_id = get_id(component)
@@ -15,6 +16,13 @@ function assign_new_id_internal!(data, component::InfrastructureSystemsComponent
1516
replace_component_id!(associations, old_id, new_id)
1617
end
1718

19+
for ids in values(data.subsystems)
20+
if old_id in ids
21+
pop!(ids, old_id)
22+
push!(ids, new_id)
23+
end
24+
end
25+
1826
set_id!(get_internal(component), new_id)
1927
return
2028
end

src/subsystems.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function add_subsystem!(data::SystemData, subsystem_name::AbstractString)
77
end
88

99
data.subsystems[subsystem_name] = Set{Int}()
10-
@debug "Added subystem $subsystem_name" _group = LOG_GROUP_SYSTEM
10+
@debug "Added subsystem $subsystem_name" _group = LOG_GROUP_SYSTEM
1111
return
1212
end
1313

@@ -33,7 +33,7 @@ Throws ArgumentError if the subsystem name is not stored.
3333
function remove_subsystem!(data::SystemData, subsystem_name::AbstractString)
3434
_throw_if_not_stored(data, subsystem_name)
3535
container = pop!(data.subsystems, subsystem_name)
36-
@debug "Removed subystem $subsystem_name" length(container) _group = LOG_GROUP_SYSTEM
36+
@debug "Removed subsystem $subsystem_name" length(container) _group = LOG_GROUP_SYSTEM
3737
return
3838
end
3939

@@ -61,7 +61,7 @@ function add_component_to_subsystem!(
6161
end
6262

6363
push!(container, id)
64-
@debug "Added $(summary(component)) to subystem $subsystem_name" _group =
64+
@debug "Added $(summary(component)) to subsystem $subsystem_name" _group =
6565
LOG_GROUP_SYSTEM
6666
return
6767
end
@@ -98,7 +98,7 @@ function remove_component_from_subsystem!(
9898
end
9999

100100
pop!(data.subsystems[subsystem_name], get_id(component))
101-
@debug "Removed $(summary(component)) from subystem $subsystem_name" _group =
101+
@debug "Removed $(summary(component)) from subsystem $subsystem_name" _group =
102102
LOG_GROUP_SYSTEM
103103
return
104104
end
@@ -110,7 +110,7 @@ function remove_component_from_subsystems!(
110110
id = get_id(component)
111111
for (subsystem_name, ids) in data.subsystems
112112
pop!(ids, id, nothing)
113-
@debug "Removed $(summary(component)) from subystem $subsystem_name" _group =
113+
@debug "Removed $(summary(component)) from subsystem $subsystem_name" _group =
114114
LOG_GROUP_SYSTEM
115115
end
116116
return

src/supplemental_attribute_associations.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,20 @@ function add_association!(
128128
attribute::SupplementalAttribute,
129129
)
130130
TimerOutputs.@timeit_debug SYSTEM_TIMERS "add supplemental attribute association" begin
131+
attribute_id = get_id(attribute)
132+
component_id = get_id(component)
133+
if attribute_id == UNASSIGNED_ID || component_id == UNASSIGNED_ID
134+
throw(
135+
ArgumentError(
136+
"cannot associate $(summary(attribute)) with $(summary(component)): " *
137+
"both must have assigned IDs (attach them to `SystemData` first).",
138+
),
139+
)
140+
end
131141
row = (
132-
get_id(attribute),
142+
attribute_id,
133143
string(nameof(typeof(attribute))),
134-
get_id(component),
144+
component_id,
135145
string(nameof(typeof(component))),
136146
)
137147
placeholder = chop(repeat("?,", length(row)))

src/supplemental_attribute_manager.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,21 @@ function _attach_attribute!(
8989
)
9090
end
9191

92+
id = get_id(attribute)
93+
if id == UNASSIGNED_ID
94+
throw(
95+
ArgumentError(
96+
"$(summary(attribute)) has an unassigned ID; attach it through " *
97+
"`add_supplemental_attribute!` on `SystemData` so an ID is assigned first.",
98+
),
99+
)
100+
end
101+
92102
T = typeof(attribute)
93103
if !haskey(mgr.data, T)
94104
mgr.data[T] = Dict{Int, T}()
95105
end
96-
mgr.data[T][get_id(attribute)] = attribute
106+
mgr.data[T][id] = attribute
97107
end
98108

99109
function is_attached(attribute::SupplementalAttribute, mgr::SupplementalAttributeManager)

src/system_data.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mutable struct SystemData <: ComponentContainer
2727
next_component_id::Int
2828
"Next integer id to assign to a supplemental attribute. Independent of the component id stream. Starts at 1."
2929
next_supplemental_attribute_id::Int
30-
"User-defined subystems. Components can be regular or masked."
30+
"User-defined subsystems. Components can be regular or masked."
3131
subsystems::Dict{String, Set{Int}}
3232
supplemental_attribute_manager::SupplementalAttributeManager
3333
time_series_manager::TimeSeriesManager

test/test_supplemental_attributes.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,16 @@ end
184184

185185
@testset "Test iterate_SupplementalAttributeManager" begin
186186
mgr = IS.SupplementalAttributeManager()
187-
geo_supplemental_attribute = IS.GeographicInfo()
188-
component = IS.TestComponent("component1", 5)
187+
geo_supplemental_attribute = _id!(IS.GeographicInfo(), 2)
188+
component = _id!(IS.TestComponent("component1", 5), 1)
189189
IS.add_supplemental_attribute!(mgr, component, geo_supplemental_attribute)
190190
@test length(collect(IS.iterate_supplemental_attributes(mgr))) == 1
191191
end
192192

193193
@testset "Summarize SupplementalAttributeManager" begin
194194
mgr = IS.SupplementalAttributeManager()
195-
geo_supplemental_attribute = IS.GeographicInfo()
196-
component = IS.TestComponent("component1", 5)
195+
geo_supplemental_attribute = _id!(IS.GeographicInfo(), 2)
196+
component = _id!(IS.TestComponent("component1", 5), 1)
197197
IS.add_supplemental_attribute!(mgr, component, geo_supplemental_attribute)
198198
summary(devnull, mgr)
199199
end

test/test_system_data.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,20 @@ end
566566
component = IS.TestComponent(name, 5)
567567
IS.add_component!(data, component)
568568
id1 = IS.get_id(component)
569+
subsystem_name = "subsystem1"
570+
IS.add_subsystem!(data, subsystem_name)
571+
IS.add_component_to_subsystem!(data, subsystem_name, component)
569572
IS.assign_new_id!(data, component)
570573
id2 = IS.get_id(component)
571574
@test id1 != id2
572575
@test IS.get_component(data, id2) === component
573576
@test_throws ArgumentError IS.get_component(data, id1)
574577
@test IS.get_component(IS.TestComponent, data, name).name == name
578+
# The subsystem membership set must track the new ID after reassignment.
579+
@test IS.has_component(data, subsystem_name, component)
580+
@test collect(IS.get_subsystem_components(data, subsystem_name)) == [component]
581+
@test id2 in IS.get_component_ids(data, subsystem_name)
582+
@test !(id1 in IS.get_component_ids(data, subsystem_name))
575583
end
576584

577585
@testset "Test independent integer id streams" begin

0 commit comments

Comments
 (0)