Skip to content

Commit b28f1c5

Browse files
authored
Merge pull request #773 from mimiframework/ref-update-param
Fix set_param for references
2 parents bb186db + b16b6d0 commit b28f1c5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/core/references.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ function set_param!(ref::ComponentReference, name::Symbol, value)
1111
set_param!(parent(ref), compdef, name, unique_name, value)
1212
end
1313

14+
"""
15+
update_param!(ref::ComponentReference, name::Symbol, value)
16+
17+
Update a component parameter as `update_param!(reference, name, value)`.
18+
This uses the unique name :compname_paramname in the model's external parameter list,
19+
and updates the parameter only in the referenced component to that value.
20+
"""
21+
function update_param!(ref::ComponentReference, name::Symbol, value)
22+
compdef = find_comp(ref)
23+
unique_name = Symbol("$(compdef.name)_$name")
24+
update_param!(parent(ref), unique_name, value)
25+
end
26+
1427
"""
1528
Base.setindex!(ref::ComponentReference, value, name::Symbol)
1629
@@ -19,7 +32,13 @@ This creates a unique name :compname_paramname in the model's external parameter
1932
and sets the parameter only in the referenced component to that value.
2033
"""
2134
function Base.setindex!(ref::ComponentReference, value, name::Symbol)
22-
set_param!(ref, name, value)
35+
compdef = find_comp(ref)
36+
unique_name = Symbol("$(compdef.name)_$name")
37+
if has_parameter(parent(ref), unique_name)
38+
update_param!(ref, name, value)
39+
else
40+
set_param!(ref, name, value)
41+
end
2342
end
2443

2544
"""

test/test_references.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ connect_param!(refC, refA, :v1)
5252
run(m)
5353
@test m[:C, :v1] == collect(1:10)
5454

55+
# test `update_param!` for references
56+
refA[:p1] = 10
57+
run(m)
58+
@test m[:foo, :p1] == 10
59+
5560
end

0 commit comments

Comments
 (0)