@@ -837,56 +837,55 @@ appendSymbol(path::Nothing, name::Symbol) = name
837
837
appendSymbol (path , name:: Symbol ) = :( $ path.$ name )
838
838
839
839
"""
840
- modifiedModel = buildSubModels !(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
840
+ modifiedModel = buildSubmodels !(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
841
841
842
- Traverse `model` and for every `<subModel >` that is a `Model(..)` and has a key-value pair
843
- `:_buildFunction = <buildFunction>` and optionally `:_buildOption=<buildOption>`, call
842
+ Traverse `model` and for every `<submodel >` that is a `Model(..)` and has a key-value pair
843
+ `:_buildFunction = Par(functionName = <buildFunction>) ` and optionally `:_buildOption=<buildOption>`, call
844
844
845
845
```
846
- buildCode = <buildFunction>(<subModel>, modelModule, FloatType::Type, TimeType::Type, unitless::Bool,
847
- buildDict::OrderedDict{String,Any},
848
- modelPath::Union{Expr,Symbol,Nothing},
849
- buildOption = <buildOption>)
846
+ updatedSubmodel = <buildFunction>(submodel, FloatType::Type, TimeType::Type, unitless::Bool,
847
+ ID, pathAST::Union{Expr,Symbol,Nothing}, buildOption = <buildOption>)
850
848
```
851
849
852
- The`buildCode ` is merged to the corresponding `<subModel>` in the calling environment .
850
+ A new `updatedSubmodel ` is generated from `submodel` merged with additional code and then returned .
853
851
The arguments of `<buildFunction>`are:
854
852
855
- - `subModel `: The returned `buildCode` is merged to `submodel`
853
+ - `updatedSubmodel `: A potentially new reference to the updated `submodel`
856
854
- `FloatType`, `TimeType`: Types used when instantiating `SimulationModel{FloatType,TimeType}`
857
855
- `unitless`: Argument `unitless` of `@instantiateModel`.
858
- - `modelPath`: Path upto `<subModel>`, such as: `:( a.b.c )`.
859
- - `buildDict`: Dictionary, that will be stored in the corresponding SimulationModel instance and
860
- that allows to store information about the build-process,
861
- typically with key `string(modelPath)` (if modelPath==Nothing, key="" is used).
856
+ - `ID`: Unique ID to identify the generated submodel (to be used in the code merged into the submodel)
857
+ - `pathAST`: Path upto `<submodel>` as Abstract Syntax Tree, such as: `:( a.b.c )`
858
+ (this path might be used as part of a variable name in the code merged into the submodel).
862
859
- `buildOption`: Option used for the generation of `buildCode`.
863
860
864
- Note, keys `_buildFunction` and `_buildOption` are deleted from the corresponding `<subModel> `.
861
+ Note, keys `_buildFunction` and `_buildOption` have been deleted in the returned `updatedSubmodel `.
865
862
"""
866
- function buildSubModels ! (model:: AbstractDict , modelModule, FloatType:: Type , TimeType:: Type , unitless:: Bool ,
867
- buildDict:: OrderedDict{String,Any} ; path :: Union{Expr,Symbol,Nothing} = nothing )
863
+ function buildSubmodels ! (model:: AbstractDict , modelModule, FloatType:: Type , TimeType:: Type , unitless:: Bool ,
864
+ buildDict:: OrderedDict{String,Any} ; pathAST :: Union{Expr,Symbol,Nothing} = nothing )
868
865
if haskey (model, :_buildFunction )
869
866
_buildFunction = model[:_buildFunction ]
870
867
if haskey (_buildFunction, :functionName )
871
868
buildFunction = _buildFunction[:functionName ]
872
869
else
873
- @error " Model $path has key :_buildFunction but its value has no key :functionName"
870
+ @error " Model $pathAST has key :_buildFunction but its value has no key :functionName"
874
871
end
875
872
delete! (model, :_buildFunction )
876
- quotedPath = Meta. quot (path)
873
+ ID = modelPathAsString (pathAST)
874
+ quotedPathAST = Meta. quot (pathAST)
877
875
if haskey (model, :_buildOption )
878
876
buildOption = model[:_buildOption ]
879
877
delete! (model, :_buildOption )
880
- buildCode = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless, $ buildDict , $ quotedPath , buildOption= $ buildOption)) )
878
+ (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless, $ ID , $ quotedPathAST , buildOption= $ buildOption)) )
881
879
else
882
- buildCode = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless, $ buildDict , $ quotedPath )))
880
+ (model, instantiatedSubmodelStruct) = Core. eval (modelModule, :($ buildFunction ($ model, $ FloatType, $ TimeType, $ unitless, $ ID , $ quotedPathAST )))
883
881
end
884
- return model | buildCode
882
+ buildDict[ID] = instantiatedSubmodelStruct
883
+ return model
885
884
end
886
885
887
886
for (key,value) in model
888
887
if typeof (value) <: OrderedDict && haskey (value, :_class ) && value[:_class ] == :Model
889
- model[key] = buildSubModels ! (value, modelModule, FloatType, TimeType, unitless, buildDict; path = appendSymbol (path ,key))
888
+ model[key] = buildSubmodels ! (value, modelModule, FloatType, TimeType, unitless, buildDict; pathAST = appendSymbol (pathAST ,key))
890
889
end
891
890
end
892
891
return model
@@ -959,7 +958,7 @@ function instantiateModel(model; modelName="", modelModule=nothing, source=nothi
959
958
FloatType <: MonteCarloMeasurements.AbstractParticles ;
960
959
baseType (FloatType) else FloatType end # baseType(..) is defined in CodeGeneration.jl
961
960
model = deepcopy (model)
962
- model = buildSubModels ! (model, modelModule, FloatType, TimeType, unitless, buildDict)
961
+ model = buildSubmodels ! (model, modelModule, FloatType, TimeType, unitless, buildDict)
963
962
964
963
if logModel
965
964
@showModel (model)
0 commit comments