Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check spec for duplicate blocks and allow multiple $PLUGIN #1238

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/Aaaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ block_list <- c("ENV", "PROB", "PARAM", "INIT",
"PREAMBLE", "PRED", "BLOCK", "TRANSIT", "YAML", "NMEXT",
"INPUT")

block_list_single <- c("MAIN", "SET", "GLOBAL", "PREAMBLE", "PRED", "PKMODEL",
"ENV", "CMTN", "INCLUDE", "NAMESPACE", "BLOCK",
"TRANSIT", "YAML")

Reserved_cvar <- c("SOLVERTIME","table","ETA","EPS", "AMT", "CMT",
"ID", "TIME", "EVID","simeps", "self", "simeta",
"NEWIND", "DONE", "CFONSTOP", "DXDTZERO",
Expand Down
21 changes: 11 additions & 10 deletions R/modspec.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,17 @@ check_sim_eta_eps_n <- function(x, spec) {
return(invisible(NULL))
}

check_spec_contents <- function(x, crump = TRUE, warn = TRUE, ...) {
invalid <- setdiff(x,block_list)
valid <- intersect(x,block_list)

if(sum("MAIN" == x) > 1){
stop("Only one $MAIN block allowed in the model.",call.=FALSE)
}

if(sum("SET" == x) > 1) {
stop("Only one $SET block allowed in the model.", call.=FALSE)
check_spec_contents <- function(x, crump = TRUE, warn = TRUE, ...) {
# Check for valid and invalid blocks
invalid <- base::setdiff(x, block_list)
valid <- base::intersect(x, block_list)

# Check for block duplicates where we only allow single
dup_x <- x[duplicated(x)]
dups <- base::intersect(dup_x, block_list_single)
if(length(dups)) {
names(dups) <- rep("*", length(dups))
abort("Multiple blocks found where only one is allowed:", body = dups)
}

if(warn) {
Expand Down
11 changes: 8 additions & 3 deletions R/mread.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
# Collect potential multiples
subr <- collect_subr(spec)
table <- unlist(spec[names(spec)=="TABLE"], use.names = FALSE)
spec[["ODE"]] <- unlist(spec[names(spec)=="ODE"], use.names = FALSE)
if("ODE" %in% names(spec)) {
spec[["ODE"]] <- unlist(spec[names(spec)=="ODE"], use.names = FALSE)
}
if("PLUGIN" %in% names(spec)) {
spec[["PLUGIN"]] <- unlist(spec[names(spec)=="PLUGIN"], use.names = FALSE)
}

# TODO: deprecate audit argument
mread.env[["audit_dadt"]] <-
Expand Down Expand Up @@ -431,7 +436,7 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
x <- update_capture(x, .ren.chr(capture_vars))
build$preclean <- TRUE
}

# Check mod ----
check_pkmodel(x, subr, spec)
check_globals(mread.env[["move_global"]], Cmt(x))
Expand Down Expand Up @@ -555,7 +560,7 @@ mread <- function(model, project = getOption("mrgsolve.project", getwd()),
from = temp_write,
to = build[["compfile"]]
)

if(!compile) return(x)

if(ignore.stdout & !quiet) {
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-modspec.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ for(what in c("THETA", "PARAM", "CMT",
})
}

test_that("multiple blocks allowed or not allowed", {

for(bl in mrgsolve:::block_list_single) {
code <- glue::glue("${bl} end = 5\n${bl} delta = 1\n$PARAM x = 3")
model <- glue::glue("test-multiple-{tolower(bl)}")
expect_error(
mcode(model, code, compile = FALSE),
"Multiple blocks found"
)
}

code <- "$PLUGIN Rcpp\n$PLUGIN BH evtools\n$PARAM x = 3"
expect_silent(
mod <- mcode("test-multiple-plugin", code, compile = FALSE)
)
expect_is(mod, "mrgmod")

code <- "$ODE a = 3\n$ODE b = 55\n$PARAM x = 3"
expect_silent(
mod <- mcode("test-multiple-ode", code, compile = FALSE)
)
expect_is(mod, "mrgmod")

code <- "$TABLE x\n$TABLE y = 55\n$PARAM x = 3\n y = 10"
expect_silent(
mod <- mcode("test-multiple-table", code, compile = FALSE)
)
expect_is(mod, "mrgmod")
})

test_that("Commented model", {
code <- '
// A comment
Expand Down
Loading