Skip to content

Commit 418f52a

Browse files
authored
Merge branch 'master' into delete
2 parents 3ed6b2c + 555b493 commit 418f52a

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-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
"""

src/core/types/model.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ mutable struct Model <: AbstractModel
2424
function Model(m::Model)
2525
return new(deepcopy(m.md), nothing)
2626
end
27+
28+
# Create a model from a ModelInstance (temporary for explore call)
29+
function Model(mi::ModelInstance)
30+
return new(deepcopy(mi.md), deepcopy(mi))
31+
end
2732
end
2833

2934
"""

src/explorer/explore.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ function explore(m::Model; title = "Electron")
5252

5353
end
5454

55+
function explore(mi::ModelInstance; title = "Electron")
56+
m = Model(mi)
57+
m.md.dirty = false # we need this to get explorer working, but it's a hack and should be temporary!
58+
explore(m)
59+
end
60+
5561
"""
5662
explore(sim_inst::SimulationInstance; title="Electron", model_index::Int = 1, scen_name::Union{Nothing, String} = nothing, results_output_dir::Union{Nothing, String} = nothing)
5763

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)