Skip to content

Commit

Permalink
feat: allow storing multiple finalizer function for a single Virtual,…
Browse files Browse the repository at this point in the history
… which are executed in reverse (inner-most template first) order
  • Loading branch information
Stefan Strömer authored and sstroemer committed Oct 23, 2024
1 parent 5b41e81 commit 5a83e58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/IESopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ function _build_model!(model::JuMP.Model; callbacks::Union{Nothing, Dict})
end
end

# TODO: rework storage of Virtuals/templates/... since we can actually access them as first-class CoreComponents
# without having to store them separately
# Call finalization functions of all Core Templates.
for (name, entry) in _iesopt(model).results._templates
entry.finalize(entry.virtual)
@info "Finalizing Virtuals"
for component in corder
component isa Virtual || continue
for i in reverse(eachindex(component._finalizers))
component._finalizers[i](component)
end
end

# Construct relevant ETDF constraints.
Expand Down
1 change: 1 addition & 0 deletions src/core/virtual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ might expect (and want) to be able to interact with "my_storage_foo". Since the

# [Internal] =======================================================================================================
_parameters = Dict{String, Any}()
_finalizers::Vector{Base.Callable} = Base.Callable[]

# [External] =======================================================================================================
# -
Expand Down
38 changes: 20 additions & 18 deletions src/templates/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,29 @@ function _parse_noncore_component!(
parameters[k] = v
end

# Write the final version of parameters into the Virtual.
virtual = _iesopt(model).model.components[cname]
merge!(virtual._parameters, parameters)
if haskey(template.yaml, "functions")
@warn "It is highly recommended NOT to use `functions` in single component templates that just \"rename\" another template" template = template.name maxlog = 1

# Validate and then prepare.
template.functions[:validate](virtual) || @critical "Template validation failed" component = cname
template.functions[:prepare](virtual)
# Write the final version of parameters into the Virtual.
virtual = _iesopt(model).model.components[cname]
merge!(virtual._parameters, parameters)

# Add an entry for finalization.
_iesopt(model).results._templates[cname] =
(finalize=template.functions[:finalize], virtual=virtual) # parameters=parameters, items=Vector{Any}())
# Validate and then prepare.
template.functions[:validate](virtual) || @critical "Template validation failed" component = cname
template.functions[:prepare](virtual)

# Convert data types that do not "render" well to strings using JSON.
# Example:
# `Dict{String, Any}("electricity" => 1)` will just be rendered as `"Dict{String, Any}("electricity" => 1)"`,
# which then messes with replacement in the YAML parsing.
# Add an entry for finalization.
push!(virtual._finalizers, template.functions[:finalize])

# This would result in modifying the original `parameters` dictionary, which is not desired.
# Therefore, we keep a copy (not a deep copy!) and modify that, only duplicating potential json-ed items.
parameters = copy(virtual._parameters)
# Convert data types that do not "render" well to strings using JSON.
# Example:
# `Dict{String, Any}("electricity" => 1)` will just be rendered as `"Dict{String, Any}("electricity" => 1)"`,
# which then messes with replacement in the YAML parsing.

# This would result in modifying the original `parameters` dictionary, which is not desired.
# Therefore, we keep a copy (not a deep copy!) and modify that, only duplicating potential json-ed items.
parameters = copy(virtual._parameters)
end

# Convert data types that do not "render" well to strings using JSON.
# Example:
Expand Down Expand Up @@ -202,8 +205,7 @@ function _parse_container!(
template.functions[:prepare](virtual)

# Add an entry for finalization.
_iesopt(model).results._templates[name] =
(finalize=template.functions[:finalize], virtual=virtual) # parameters=parameters, items=Vector{Any}())
push!(virtual._finalizers, template.functions[:finalize])

# Convert data types that do not "render" well to strings using JSON.
# Example:
Expand Down

0 comments on commit 5a83e58

Please sign in to comment.