@@ -837,56 +837,55 @@ appendSymbol(path::Nothing, name::Symbol) = name
837837appendSymbol (path , name:: Symbol ) = :( $ path.$ name )
838838
839839"""
840- modifiedModel = buildSubModels !(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
840+ modifiedModel = buildSubmodels !(model, modelModule, FloatType, TimeType, unitless, buildDict::OrderedDict)
841841
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
844844
845845```
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>)
850848```
851849
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 .
853851The arguments of `<buildFunction>`are:
854852
855- - `subModel `: The returned `buildCode` is merged to `submodel`
853+ - `updatedSubmodel `: A potentially new reference to the updated `submodel`
856854- `FloatType`, `TimeType`: Types used when instantiating `SimulationModel{FloatType,TimeType}`
857855- `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).
862859- `buildOption`: Option used for the generation of `buildCode`.
863860
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 `.
865862"""
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 )
868865 if haskey (model, :_buildFunction )
869866 _buildFunction = model[:_buildFunction ]
870867 if haskey (_buildFunction, :functionName )
871868 buildFunction = _buildFunction[:functionName ]
872869 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"
874871 end
875872 delete! (model, :_buildFunction )
876- quotedPath = Meta. quot (path)
873+ ID = modelPathAsString (pathAST)
874+ quotedPathAST = Meta. quot (pathAST)
877875 if haskey (model, :_buildOption )
878876 buildOption = model[:_buildOption ]
879877 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)) )
881879 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 )))
883881 end
884- return model | buildCode
882+ buildDict[ID] = instantiatedSubmodelStruct
883+ return model
885884 end
886885
887886 for (key,value) in model
888887 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))
890889 end
891890 end
892891 return model
@@ -959,7 +958,7 @@ function instantiateModel(model; modelName="", modelModule=nothing, source=nothi
959958 FloatType <: MonteCarloMeasurements.AbstractParticles ;
960959 baseType (FloatType) else FloatType end # baseType(..) is defined in CodeGeneration.jl
961960 model = deepcopy (model)
962- model = buildSubModels ! (model, modelModule, FloatType, TimeType, unitless, buildDict)
961+ model = buildSubmodels ! (model, modelModule, FloatType, TimeType, unitless, buildDict)
963962
964963 if logModel
965964 @showModel (model)
0 commit comments