Skip to content

Commit

Permalink
Fixed module inventory to handle dynamic modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jrawbits committed Apr 11, 2023
1 parent b087839 commit e54af1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
7 changes: 7 additions & 0 deletions sources/framework/VESnapshot/R/Dynamic.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ getDynamicField <- function(AllSpecs_ls=NA,Cache=FALSE) {
rm(list=ls(dynamic.env),envir=dynamic.env)
}

# If AllSpecs_ls is not a list, we're probably doing documentation,
# not a model run. Just return a list.
if ( ! is.list(AllSpecs_ls ) ) {
dynamic.env$Message <- "No AllSpecs_ls: not running within a model"
return( list() )
}

# General instructions for building a dynamic Specification function:

# If Specs is TRUE in the module specification structure, then AllSpecs_ls will be passed into the function
Expand Down
5 changes: 2 additions & 3 deletions sources/framework/VESnapshot/R/Snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ getSnapshotFields <- function(AllSpecs_ls=NA,Instance=character(0), Cache=FALSE)

# Check AllSpecs_ls
if ( ! is.list(AllSpecs_ls) || length(AllSpecs_ls)==0 ) {
stop(
visioneval::writeLog( "Snapshot can only be run if items are created first by other modules",Level="error")
)
visioneval::writeLog( "Snapshot running without a model",Level="info" )
return( list() )
}

# Make sure Instance can be used as a list element name
Expand Down
13 changes: 10 additions & 3 deletions sources/framework/visioneval/R/initialization.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,22 @@ loadModelState <- function(FileName=NULL,envir=NULL) {
#' parameters are passed (see \code{readModelState}).
#'
#' @param envir An R environment with assigned Datastore functions and a ModelState_ls
#' @param stopOnError if TRUE (default) stop if model state is not initialized else return NULL
#' @param ... If there are any parameters, this quietly becomes a call to readModelState(...) which
#' can subset, read a different file, load into an environment, etc.
#' @return The model state list
#' @export
getModelState <- function(envir=NULL,...) {
getModelState <- function(envir=NULL,stopOnError=TRUE,...) {
if ( ! missing(...) ) return(readModelState(envir=envir,...))
if ( is.null(envir) ) envir <- modelEnvironment()
if ( ! "ModelState_ls" %in% ls(envir=envir) ) stop("getModelState: ModelState is not initialized.")
return(envir$ModelState_ls)
if ( ! "ModelState_ls" %in% ls(envir=envir) ) {
if ( stopOnError ){
traceback(1)
browser()
stop("getModelState: ModelState is not initialized.")
}
}
return(envir$ModelState_ls) # Returns NULL if not stopOnError
}

#SET (UPDATE) MODEL STATE
Expand Down
8 changes: 3 additions & 5 deletions sources/framework/visioneval/R/module.R
Original file line number Diff line number Diff line change
Expand Up @@ -897,10 +897,8 @@ applyLinearModel <- function(Model_ls,
#' @param ModuleName a string identifying the module name.
#' @param PackageName a string identifying the package name.
#' @param NameRegistryList, if FALSE read and write the NameRegistryFile, if TRUE just compose a list for this module
#' @param NameRegistryDir a string identifying the path to the directory
#' where the name registry file is located.
#' @return TRUE if successful. Has a side effect of updating the VisionEval
#' name registry.
#' @param NameRegistryDir a string identifying the path to the directory where the name registry file is located.
#' @return TRUE if successful. Has a side effect of updating the VisionEval name registry if NameRegistryList is FALSE
#' @export
writeVENameRegistry <- function(ModuleName, PackageName, NameRegistryList = FALSE, NameRegistryDir = NULL) {
if ( ! NameRegistryList ) {
Expand Down Expand Up @@ -1349,7 +1347,7 @@ getModuleSpecs <- function(ModuleName, PackageName, AllSpecs_ls=NA, Instance=cha
if ( wantAllSpecs && ! is.list(AllSpecs_ls) ) {
# The provisional AllSpecs_ls is provided as an argument when getModuleSpecs is called while AllSpecs_ls is being built,
# and it will contain all the specifications processed up to the point we encounter this module.
AllSpecs_ls <- getModelState(envir=envir)$AllSpecs_ls
AllSpecs_ls <- getModelState(envir=envir,stopOnError=FALSE)$AllSpecs_ls
}
wantInstance <- "Instance" %in% specFormals

Expand Down

0 comments on commit e54af1b

Please sign in to comment.