diff --git a/src/core/connections.jl b/src/core/connections.jl index 4a5071bf7..35b1e725e 100644 --- a/src/core/connections.jl +++ b/src/core/connections.jl @@ -37,19 +37,6 @@ function disconnect_param!(obj::AbstractCompositeComponentDef, comp_name::Symbol disconnect_param!(obj, comp, param_name) end -""" - disconnect_param!(obj::AbstractCompositeComponentDef, comp_path::ComponentPath, param_name::Symbol) - -Remove any parameter connections for a given parameter `param_name` in the component identified by -`comp_path` which must be under the composite `obj`. -""" -function disconnect_param!(obj::AbstractCompositeComponentDef, comp_path::ComponentPath, param_name::Symbol) - if (comp_def = find_comp(obj, comp_path)) === nothing - return - end - disconnect_param!(obj, comp_def, param_name) -end - # Default string, string unit check function verify_units(unit1::AbstractString, unit2::AbstractString) = (unit1 == unit2) @@ -129,7 +116,7 @@ function connect_param!(obj::AbstractCompositeComponentDef, comp_def::AbstractCo end """ - connect_param!(obj::AbstractCompositeComponentDef, + _connect_param!(obj::AbstractCompositeComponentDef, dst_comp_path::ComponentPath, dst_par_name::Symbol, src_comp_path::ComponentPath, src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; @@ -142,18 +129,18 @@ check match units between the two. The `offset` argument indicates the offset b the destination and the source ie. the value would be `1` if the destination component parameter should only be calculated for the second timestep and beyond. """ -function connect_param!(obj::AbstractCompositeComponentDef, +function _connect_param!(obj::AbstractCompositeComponentDef, dst_comp_path::ComponentPath, dst_par_name::Symbol, src_comp_path::ComponentPath, src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0) - # remove any existing connections for this dst parameter - disconnect_param!(obj, dst_comp_path, dst_par_name) # calls dirty!() - dst_comp_def = compdef(obj, dst_comp_path) src_comp_def = compdef(obj, src_comp_path) + # remove any existing connections for this dst parameter + disconnect_param!(obj, dst_comp_def, dst_par_name) # calls dirty!() + # @info "dst_comp_def: $dst_comp_def" # @info "src_comp_def: $src_comp_def" @@ -242,7 +229,7 @@ function connect_param!(obj::AbstractCompositeComponentDef, dst_comp_name::Symbol, dst_par_name::Symbol, src_comp_name::Symbol, src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0) - connect_param!(obj, ComponentPath(obj, dst_comp_name), dst_par_name, + _connect_param!(obj, ComponentPath(obj, dst_comp_name), dst_par_name, ComponentPath(obj, src_comp_name), src_var_name, backup; ignoreunits=ignoreunits, offset=offset) end diff --git a/src/core/defs.jl b/src/core/defs.jl index ae2ac58e4..3476c9ee0 100644 --- a/src/core/defs.jl +++ b/src/core/defs.jl @@ -456,19 +456,6 @@ function set_param!(md::ModelDef, comp_name::Symbol, value_dict::Dict{Symbol, An end end -""" - set_param!(md::ModelDef, comp_path::ComponentPath, param_name::Symbol, - value_dict::Dict{Symbol, Any}; dims=nothing) - -Call `set_param!()` with `param_name` and a value dict in which `value_dict[param_name]` references -the value of parameter `param_name`. -""" -function set_param!(md::ModelDef, comp_name::Symbol, value_dict::Dict{Symbol, Any}, - param_name::Symbol; dims=nothing) - value = value_dict[param_name] - set_param!(md, comp_name, param_name, value, dims=dims) -end - function set_param!(md::ModelDef, comp_name::Symbol, param_name::Symbol, value; dims=nothing) set_param!(md, comp_name, param_name, param_name, value, dims=dims) end diff --git a/src/core/model.jl b/src/core/model.jl index 5c705f74c..bf8356d1c 100644 --- a/src/core/model.jl +++ b/src/core/model.jl @@ -35,38 +35,34 @@ is_built(mm::MarginalModel) = (is_built(mm.base) && is_built(mm.modified)) @delegate add_connector_comps!(m::Model) => md """ - connect_param!(m::Model, dst_comp_path::ComponentPath, dst_par_name::Symbol, src_comp_path::ComponentPath, - src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0) + connect_param!(m::Model, dst_comp_name::Symbol, dst_par_name::Symbol, src_comp_name::Symbol, src_var_name::Symbol, + backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0) -Bind the parameter `dst_par_name` of one component `dst_comp_path` of model `md` -to a variable `src_var_name` in another component `src_comp_path` of the same model +Bind the parameter `dst_par_name` of one component `dst_comp_name` of model `m` +to a variable `src_var_name` in another component `src_comp_name` of the same model using `backup` to provide default values and the `ignoreunits` flag to indicate the need to check match units between the two. The `offset` argument indicates the offset between the destination and the source ie. the value would be `1` if the destination component parameter should only be calculated for the second timestep and beyond. """ -@delegate connect_param!(m::Model, - dst_comp_path::ComponentPath, dst_par_name::Symbol, - src_comp_path::ComponentPath, src_var_name::Symbol, - backup::Union{Nothing, Array}=nothing; - ignoreunits::Bool=false, offset::Int=0) => md - @delegate connect_param!(m::Model, dst_comp_name::Symbol, dst_par_name::Symbol, src_comp_name::Symbol, src_var_name::Symbol, backup::Union{Nothing, Array}=nothing; ignoreunits::Bool=false, offset::Int=0) => md -@delegate connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol) => md - -@delegate connect_param!(m::Model, dst::AbstractString, src::AbstractString, backup::Union{Nothing, Array}=nothing; - ignoreunits::Bool=false, offset::Int=0) => md +""" + connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol) +Bind the parameter `param_name` in the component `comp_name` of model `m` to the external parameter +`ext_param_name` already present in the model's list of external parameters. +""" +@delegate connect_param!(m::Model, comp_name::Symbol, param_name::Symbol, ext_param_name::Symbol) => md """ connect_param!(m::Model, dst::Pair{Symbol, Symbol}, src::Pair{Symbol, Symbol}, backup::Array; ignoreunits::Bool=false) -Bind the parameter `dst[2]` of one component `dst[1]` of model `md` +Bind the parameter `dst[2]` of one component `dst[1]` of model `m` to a variable `src[2]` in another component `src[1]` of the same model using `backup` to provide default values and the `ignoreunits` flag to indicate the need to check match units between the two. The `offset` argument indicates the offset @@ -80,7 +76,12 @@ function connect_param!(m::Model, dst::Pair{Symbol, Symbol}, src::Pair{Symbol, S connect_param!(m.md, dst[1], dst[2], src[1], src[2], backup; ignoreunits=ignoreunits, offset=offset) end -@delegate disconnect_param!(m::Model, comp_path::ComponentPath, param_name::Symbol) => md +""" + disconnect_param!(m::Model, comp_name::Symbol, param_name::Symbol) + +Remove any parameter connections for a given parameter `param_name` in a given component +`comp_def` in model `m`. +""" @delegate disconnect_param!(m::Model, comp_name::Symbol, param_name::Symbol) => md # TBD: these may not be needed as delegators diff --git a/src/core/references.jl b/src/core/references.jl index 69264b353..04627554b 100644 --- a/src/core/references.jl +++ b/src/core/references.jl @@ -28,7 +28,7 @@ end Connect two components as `connect_param!(dst, dst_name, src, src_name)`. """ function connect_param!(dst::ComponentReference, dst_name::Symbol, src::ComponentReference, src_name::Symbol) - connect_param!(parent(dst), pathof(dst), dst_name, pathof(src), src_name) + _connect_param!(parent(dst), pathof(dst), dst_name, pathof(src), src_name) end """ @@ -37,7 +37,7 @@ end Connect two components with the same name as `connect_param!(dst, src, name)`. """ function connect_param!(dst::ComponentReference, src::ComponentReference, name::Symbol) - connect_param!(parent(dst), pathof(dst), name, pathof(src), name) + _connect_param!(parent(dst), pathof(dst), name, pathof(src), name) end """ @@ -81,5 +81,5 @@ Connect two components as `comp_ref[var_name] = var_ref`. function Base.setindex!(comp_ref::ComponentReference, var_ref::VariableReference, vname::Symbol) _same_composite(comp_ref, var_ref) || error("Can't connect variables defined in different composite trees") - connect_param!(parent(comp_ref), pathof(comp_ref), vname, pathof(var_ref), var_name(var_ref)) + _connect_param!(parent(comp_ref), pathof(comp_ref), vname, pathof(var_ref), var_name(var_ref)) end diff --git a/test/test_references.jl b/test/test_references.jl index 7b13b0595..4df0c2c90 100644 --- a/test/test_references.jl +++ b/test/test_references.jl @@ -38,4 +38,18 @@ run(m) @test m[:B, :p1] == 5 @test m[:B, :p2] == collect(1:10) +# Test `connect_param!` methods for ComponentReferences + +connect_param!(refB, :p2, refA, :v1) +run(m) +@test m[:B, :p2] == collect(1:10) + +@defcomp C begin + v1 = Parameter(index = [time]) +end +refC = add_comp!(m, C) +connect_param!(refC, refA, :v1) +run(m) +@test m[:C, :v1] == collect(1:10) + end \ No newline at end of file