Skip to content

Commit 2c7c276

Browse files
committed
update predefineds. fix nimbleModels. fix package class generation with R fields.
1 parent b3475b9 commit 2c7c276

14 files changed

Lines changed: 67 additions & 41 deletions

nCompiler/R/NC.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ nClass <- function(classname,
217217
Rpublic = NULL # indicates to leave quote(Rpublic)
218218
)
219219

220+
# Either we must store this or rebuilding it when writing package code.
221+
# To store it, we would need to retain the Rpublic from this function's environment.
222+
# Currently the strategy is to rebuild it.
223+
# internals$main_class_code <- main_class_code
224+
220225
## Build the R6 class generator
221226
NCgenerator <- eval(main_class_code)
222227
Cpub_generator <- eval(Cpub_class_code)
@@ -261,7 +266,7 @@ make_uncompiled_Cpub_class_code <- function(classname,
261266

262267
make_nClass_code <- function(internals,
263268
Cpublic = NULL, # if nFunctions (called from nClass), create method. If R function (called from WP_writeRinterfaces), use that ()
264-
Rpublic = NULL # If NULL (called from nClass), use quote(Rpbulic. If provided (from WP_writeRinterfaces), deparse.)
269+
Rpublic = NULL # If NULL (called from nClass), use quote(Rpublic). If provided (from WP_writeRinterfaces), deparse.
265270
) {
266271
classname <- internals$classname
267272
inheritQ <- internals$inheritQ
@@ -298,7 +303,7 @@ make_nClass_code <- function(internals,
298303

299304
Rpublic_code <- quote(Rpublic)
300305
if(!is.null(Rpublic)) {
301-
parsedcopy <- \(f) {ans <- substitute(\() BODY, list(BODY=body(f))) |> removeSource(); if(!is.null(formals(f))) ans[[2]] <- formals(f)}
306+
parsedcopy <- \(f) {ans <- substitute(\() BODY, list(BODY=body(f))) |> removeSource(); if(!is.null(formals(f))) ans[[2]] <- formals(f); ans}
302307
Rpublic_code_list <- Rpublic |> lapply(\(x) if(is.function(x)) parsedcopy(x) else x)
303308
Rpublic_code <- do.call("call", c("list",
304309
Rpublic_code_list))

nCompiler/R/nCompile.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,17 @@ WP_writeRinterfaces <- function(units, unitTypes, interfaces, returnNames, packa
11141114
inherit_obj <- units[[i]] # NCgenerator from which the CncGen_code will inherit
11151115
NCI_inherit <- NCinternals(inherit_obj)
11161116

1117+
# deparsed_main_class <- NCI_inherit$main_class_code |> deparse()
1118+
RpublicMethodNames <- NCI_inherit$RpublicNames[
1119+
NCI_inherit$RpublicNames %in% names(units[[i]]$public_methods)]
1120+
RpublicFieldNames <- NCI_inherit$RpublicNames[
1121+
NCI_inherit$RpublicNames %in% names(units[[i]]$public_fields)]
1122+
#N.B: Cpublic fields are build from NCI_inherit$fieldNames.
11171123
deparsed_main_class <- make_nClass_code(
11181124
internals = NCI_inherit,
11191125
Cpublic = units[[i]]$public_methods[NCI_inherit$methodNames],
1120-
Rpublic = units[[i]]$public_methods[NCI_inherit$RpublicNames]
1126+
Rpublic = c(units[[i]]$public_methods[RpublicMethodNames],
1127+
units[[i]]$public_fields[RpublicFieldNames])
11211128
) |> deparse()
11221129
deparsed_main_class[1] <- paste0(
11231130
generator_name, ' <- ', deparsed_main_class[1]

nCompiler/R/nimbleModels.R

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ nodeInstr_nClass <- nClass(
2424
file.path("nodeInstr_nC")),
2525
compileInfo=list(interface="full",
2626
createFromR = TRUE,
27-
exportName = "nodeInstr_nClass"
27+
exportName = "nodeInstr_nClass_new",
28+
packageNames = c(uncompiled = "nodeInstr_nClass", compiled="nodeInstr_nClass_C")
2829
)
2930
)
3031

@@ -38,15 +39,16 @@ calcInstr_nClass <- nClass(
3839
file.path("calcInstr_nC")),
3940
compileInfo=list(interface="full",
4041
createFromR = TRUE,
41-
# The Hincludes should be picked up automatically but I think it's not
42+
# The Hincludes should be picked up automatically but I think it's not
4243
# because it is in the nList type and that is not being scanned for needed nClasses.
4344
# These do need to be in "" not <>, for case of nCompile(...., package=TRUE)
4445
Hincludes = '"nodeInstr_nClass_c_.h"',
4546
# In the format here, needed_units is a list with either objects (nFunction or nClass (generators),
4647
# or names. If names, we will use scoping to look them up and decide what they are.
4748
# The list can mix objects and names of nClasses and nFunctions.
4849
needed_units = list("nodeInstr_nClass"),
49-
exportName = "calcInstr_nClass"
50+
exportName = "calcInstr_nClass_new",
51+
packageNames = c(uncompiled="calcInstr_nClass", compiled="calcInstr_nClass_C")
5052
)
5153
)
5254

@@ -60,7 +62,8 @@ calcInstrList_nClass <- nClass(
6062
compileInfo=list(interface="full",
6163
createFromR = TRUE,
6264
Hincludes = '"calcInstr_nClass_c_.h"',
63-
exportName = "calcInstrList_nClass",
65+
exportName = "calcInstrList_nClass_new",
66+
packageNames = c(uncompiled = "calcInstrList_nClass", compiled = "calcInstrList_nClass_C"),
6467
needed_units = list("calcInstr_nClass")
6568
)
6669
)
@@ -85,7 +88,9 @@ nodeFxnBase_nClass <- nClass(
8588
file.path("nodeFxnBase_nC")),
8689
compileInfo=list(interface="full",
8790
createFromR = FALSE,
88-
exportName = "nodeFxnBase_nClass")
91+
exportName = "nodeFxnBase_nClass_new",
92+
packageNames = c(uncompiled="nodeFxnBase_nClass", compiled="nodeFxnBase_nClass_C")
93+
)
8994
)
9095

9196
# nCompile(nodeFxnBase_nClass, control=list(generate_predefined=TRUE))
@@ -118,15 +123,16 @@ modelBase_nClass <- nClass(
118123
cppLiteral('Rprintf("modelBase_nClass calculate (should not see this)\\n");'); return(0)},
119124
virtual=TRUE
120125
)
121-
)
126+
)
122127
),
123128
# See comment above about needing to ensure a virtual destructor
124129
predefined = quote(system.file(file.path("include","nCompiler", "predef"), package="nCompiler") |> file.path("modelBase_nC")),
125130
compileInfo=list(interface="full",
126131
createFromR = FALSE,
127132
Hincludes = c('"nodeFxnBase_nClass_c_.h"', '"calcInstrList_nClass_c_.h"'), # do we need "<nodeFxnBase_nClass_c_.h>" too?
128133
needed_units = list("nodeFxnBase_nClass","calcInstrList_nClass"), #do we need nodeFxnBase_nClass here too?
129-
exportName = "modelBase_nClass"
134+
exportName = "modelBase_nClass_new",
135+
packageNames = c(uncompiled="modelBase_nClass", compiled="modelBase_nClass_C")
130136
)
131137
)
132138

@@ -147,7 +153,7 @@ nm_addModelDollarSign <- function(expr, exceptionNames = character(0)) {
147153
if(expr[[1]] == '$'){
148154
expr[2] <- lapply(expr[2], function(listElement) nm_addModelDollarSign(listElement, exceptionNames))
149155
return(expr)
150-
}
156+
}
151157
if(expr[[1]] == 'returnType')
152158
return(expr)
153159
if(length(expr) > 1) {
@@ -189,8 +195,12 @@ make_node_nClass <- function(varInfo = list(),
189195
CpublicVars <- names(symbolList) |> lapply(\(x) eval(substitute(quote(T(symbolList$NAME)),
190196
list(NAME=as.name(x)))))
191197
names(CpublicVars) <- names(symbolList)
198+
# This is a kluge to have a model field in the Cpublic_obj,
199+
# needed for uncompiled purposes, and for compiled purposes
200+
# we instead use references to model variables. So
201+
# the declared type here is arbitrary.
192202
initFun <- function(){}
193-
203+
194204
if(numVars > 0) {
195205
ctorArgNames <- paste0(names(symbolList), '_')
196206
# List used when generating C++ constructor code to allow direct initializers, necessary for references.
@@ -206,8 +216,10 @@ make_node_nClass <- function(varInfo = list(),
206216

207217
# Rpublic method to set the model pointer/reference.
208218
setModel <- function(model) {
209-
if(!isCompiled())
219+
if(!isCompiled()) {
210220
self$model <- model
221+
#private$Cpublic_obj$model <- model
222+
}
211223
else
212224
warning("setModel called on compiled object; no action taken")
213225
}
@@ -232,9 +244,10 @@ make_node_nClass <- function(varInfo = list(),
232244
)
233245
) |> structure(names = classname),
234246
CpublicVars,
247+
list(model = "RcppList"),
235248
methods
236249
),
237-
RPUBLIC = list(model = NULL,
250+
RPUBLIC = list(#model = NULL,
238251
setModel = setModel),
239252
CLASSNAME = classname,
240253
BASECLASS = baseclass
@@ -350,6 +363,7 @@ makeModel_nClass <- function(varInfo,
350363
# It is not very easy to set debug onto the initialize function, so
351364
# here is a magic flag.
352365
if(isTRUE(.GlobalEnv$.debugModelInit)) browser()
366+
super$initialize()
353367
if(isCompiled())
354368
self$setup_node_mgmt_from_names(self$nodeObjNames)
355369
if(!isCompiled()) {
@@ -358,7 +372,7 @@ makeModel_nClass <- function(varInfo,
358372
self[[nodeObj]]$setModel(self)
359373
}
360374
}
361-
375+
362376
# First expand any provided or default sizes
363377
# To-Do possibly merge the argument sizes and defaultSizes by element.
364378
if(missing(sizes)) sizes <- self$defaultSizes
@@ -372,7 +386,7 @@ makeModel_nClass <- function(varInfo,
372386
baseclass <- paste0("modelClass_<", classname, ">")
373387
# CpublicNodeFuns has elements like "node_1 = quote(nodeFxn_1())"
374388
# We provide it in Cpublic to declare C++ member variables with types.
375-
# We also place the list itself in the class so that we can look up for uncompiled execution
389+
# We also place the list itself in the class so that we can look up for uncompiled execution
376390
# the objects that need to be created in initialize.
377391
# If we someday make type declarations and initializations more automatic, we can avoid this duplication.
378392
ans <- substitute(
@@ -390,9 +404,9 @@ makeModel_nClass <- function(varInfo,
390404
),
391405
list(OPDEFS = opDefs,
392406
# A list of individual elements
393-
RPUBLIC = list(initialize=initialize,
407+
RPUBLIC = list(initialize=initialize,
394408
nodeObjNames = nodeObjNames,
395-
nodeObjName_2_nodeIndex = nodeObjName_2_nodeIndex,
409+
nodeObjName_2_nodeIndex = nodeObjName_2_nodeIndex,
396410
defaultSizes = sizes,
397411
defaultInits = inits,
398412
CpublicNodeFuns = CpublicNodeFuns),
@@ -446,12 +460,12 @@ make_stoch_sim_line <- function(LHSrep, RHSrep) {
446460
if(is.null(sim_code)) stop("Could not find simulation ('r') function for ", BUGSdistName)
447461
RHSrep[[1]] <- sim_code
448462
# scoot all named arguments right 1 position
449-
if(length(RHSrep) > 1) {
463+
if(length(RHSrep) > 1) {
450464
for(i in (length(RHSrep)+1):3) {
451465
RHSrep[i] <- RHSrep[i-1]
452466
names(RHSrep)[i] <- names(RHSrep)[i-1]
453-
}
454-
}
467+
}
468+
}
455469
RHSrep[[2]] <- 1
456470
names(RHSrep)[2] <- ''
457471
sim_line <- substitute(
@@ -542,7 +556,7 @@ make_node_methods_from_declInfo <- function(declInfo) {
542556
make_node_method_nFxn("sim_one", NULL),
543557
calc_one = (function(idx) {DETERMCALC; return(invisible(0))}) |>
544558
make_node_method_nFxn("calc_one"),
545-
calcDiff_one = (function(idx) {calc_one(idx);return(invisible(0))}) |>
559+
calcDiff_one = (function(idx) {calc_one(idx);return(invisible(0))}) |>
546560
make_node_method_nFxn("calcDiff_one"),
547561
getLogProb_one = (function(idx) {return(0)}) |>
548562
make_node_method_nFxn("getLogProb_one")
@@ -559,7 +573,7 @@ make_node_methods_from_declInfo <- function(declInfo) {
559573
make_node_method_nFxn("sim_one", NULL),
560574
calc_one = (function(idx) { STOCHCALC; return(invisible(LOGPROB)) }) |>
561575
make_node_method_nFxn("calc_one"),
562-
calcDiff_one = (function(idx) {STOCHCALC_DIFF; LocalAns_ <- LocalNewLogProb_ - LOGPROB;
576+
calcDiff_one = (function(idx) {STOCHCALC_DIFF; LocalAns_ <- LocalNewLogProb_ - LOGPROB;
563577
LOGPROB <- LocalNewLogProb_; return(invisible(LocalAns_))}) |>
564578
make_node_method_nFxn("calcDiff_one"),
565579
getLogProb_one = (function(idx) { return(LOGPROB) }) |>

nCompiler/inst/include/.DS_Store

6 KB
Binary file not shown.

nCompiler/inst/include/nCompiler/predef/calcInstrList_nC/calcInstrList_nClass_cppContent.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ using namespace Rcpp;
1717
RESET_EIGEN_ERRORS
1818
}
1919

20-
// [[Rcpp::export(name = "new_calcInstrList_nClass")]]
20+
// [[Rcpp::export(name = "calcInstrList_nClass_new")]]
2121
SEXP new_calcInstrList_nClass ( ) {
2222
RESET_EIGEN_ERRORS
2323
return CREATE_NEW_NCOMP_OBJECT(calcInstrList_nClass);;
2424
}
2525

26-
// [[Rcpp::export(name = "set_CnClass_env_new_calcInstrList_nClass")]]
26+
// [[Rcpp::export(name = "set_CnClass_env_calcInstrList_nClass_new")]]
2727
void set_CnClass_env_calcInstrList_nClass ( SEXP env ) {
2828
RESET_EIGEN_ERRORS
2929
SET_CNCLASS_ENV(calcInstrList_nClass, env);;
3030
}
3131

32-
// [[Rcpp::export(name = "get_CnClass_env_new_calcInstrList_nClass")]]
32+
// [[Rcpp::export(name = "get_CnClass_env_calcInstrList_nClass_new")]]
3333
Rcpp::Environment get_CnClass_env_calcInstrList_nClass ( ) {
3434
RESET_EIGEN_ERRORS
3535
return GET_CNCLASS_ENV(calcInstrList_nClass);;

nCompiler/inst/include/nCompiler/predef/calcInstrList_nC/calcInstrList_nClass_manifest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
list(saved_at = structure(1765437409.8481, class = c("POSIXct",
1+
list(saved_at = structure(1772141341.98022, class = c("POSIXct",
22
"POSIXt")), packet_name = "calcInstrList_nClass", elements = c("preamble",
33
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles"
44
), files = list(preamble = "calcInstrList_nClass_preamble.cpp",

nCompiler/inst/include/nCompiler/predef/calcInstr_nC/calcInstr_nClass_cppContent.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ using namespace Rcpp;
1717
RESET_EIGEN_ERRORS
1818
}
1919

20-
// [[Rcpp::export(name = "new_calcInstr_nClass")]]
20+
// [[Rcpp::export(name = "calcInstr_nClass_new")]]
2121
SEXP new_calcInstr_nClass ( ) {
2222
RESET_EIGEN_ERRORS
2323
return CREATE_NEW_NCOMP_OBJECT(calcInstr_nClass);;
2424
}
2525

26-
// [[Rcpp::export(name = "set_CnClass_env_new_calcInstr_nClass")]]
26+
// [[Rcpp::export(name = "set_CnClass_env_calcInstr_nClass_new")]]
2727
void set_CnClass_env_calcInstr_nClass ( SEXP env ) {
2828
RESET_EIGEN_ERRORS
2929
SET_CNCLASS_ENV(calcInstr_nClass, env);;
3030
}
3131

32-
// [[Rcpp::export(name = "get_CnClass_env_new_calcInstr_nClass")]]
32+
// [[Rcpp::export(name = "get_CnClass_env_calcInstr_nClass_new")]]
3333
Rcpp::Environment get_CnClass_env_calcInstr_nClass ( ) {
3434
RESET_EIGEN_ERRORS
3535
return GET_CNCLASS_ENV(calcInstr_nClass);;

nCompiler/inst/include/nCompiler/predef/calcInstr_nC/calcInstr_nClass_manifest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
list(saved_at = structure(1765437403.89444, class = c("POSIXct",
1+
list(saved_at = structure(1772141071.19335, class = c("POSIXct",
22
"POSIXt")), packet_name = "calcInstr_nClass", elements = c("preamble",
33
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles"
44
), files = list(preamble = "calcInstr_nClass_preamble.cpp", cppContent = "calcInstr_nClass_cppContent.cpp",

nCompiler/inst/include/nCompiler/predef/modelBase_nC/modelBase_nClass_cppContent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ return(0.0);
2626
RESET_EIGEN_ERRORS
2727
}
2828

29-
// [[Rcpp::export(name = "set_CnClass_env_new_modelBase_nClass")]]
29+
// [[Rcpp::export(name = "set_CnClass_env_modelBase_nClass_new")]]
3030
void set_CnClass_env_modelBase_nClass ( SEXP env ) {
3131
RESET_EIGEN_ERRORS
3232
SET_CNCLASS_ENV(modelBase_nClass, env);;
3333
}
3434

35-
// [[Rcpp::export(name = "get_CnClass_env_new_modelBase_nClass")]]
35+
// [[Rcpp::export(name = "get_CnClass_env_modelBase_nClass_new")]]
3636
Rcpp::Environment get_CnClass_env_modelBase_nClass ( ) {
3737
RESET_EIGEN_ERRORS
3838
return GET_CNCLASS_ENV(modelBase_nClass);;

nCompiler/inst/include/nCompiler/predef/modelBase_nC/modelBase_nClass_manifest.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
list(saved_at = structure(1765437502.8499, class = c("POSIXct",
1+
list(saved_at = structure(1772143550.30016, class = c("POSIXct",
22
"POSIXt")), packet_name = "modelBase_nClass", elements = c("preamble",
33
"cppContent", "hContent", "filebase", "post_cpp_compiler", "copyFiles"
44
), files = list(preamble = "modelBase_nClass_preamble.cpp", cppContent = "modelBase_nClass_cppContent.cpp",

0 commit comments

Comments
 (0)