@@ -30,7 +30,7 @@ nodeFxnBase_nClass <- nClass(
3030 ),
3131 # We haven't dealt with ensuring a virtual destructor when any method is virtual
3232 # For now I did it manually by editing the .h and .cpp
33- predefined = quote(system.file(file.path(" include" ," nCompiler" , " predefined_nClasses" ), package = " nCompiler" ) | >
33+ predefined = quote(system.file(file.path(" include" ," nCompiler" , " predefined_nClasses" ), package = " nCompiler" ) | >
3434 file.path(" nodeFxnBase_nClass" )),
3535 compileInfo = list (interface = " full" ,
3636 createFromR = FALSE )
@@ -76,10 +76,16 @@ modelBase_nClass <- nClass(
7676# # obj <- comp$test$new()
7777# # obj$calculate(NULL)
7878
79- make_node_fun <- function (varInfo ) {
79+ # Turn variables and methods into a nodeFxn nClass
80+ make_node_fun <- function (varInfo = list (),
81+ methods = list (),
82+ classname ) {
8083 # varInfo will be a list (names not used) of name, nDim, sizes.
81- varInfo_2_cppVar <- \(x ) nCompiler ::: symbolCppVar $ new(baseType = nCompiler ::: symbolBasic $ new(type = " double" , nDim = x $ nDim , name = " " )$ genCppVar()$ generate(),
82- ref = TRUE , const = TRUE )
84+ varInfo_2_cppVar <- \(x ) nCompiler ::: symbolBasic $ new(
85+ type = " double" , nDim = x $ nDim , name = " " , isRef = TRUE , isConst = FALSE , interface = FALSE ) # We could in future make some isConst=TRUE, but it might not matter much
86+ # varInfo_2_cppVar <- \(x) nCompiler:::symbolCppVar$new(
87+ # baseType = nCompiler:::symbolBasic$new(type="double", nDim=x$nDim, name="")$genCppVar()$generate(),
88+ # ref=TRUE, const=TRUE)
8389 typeList <- varInfo | > lapply(varInfo_2_cppVar )
8490 names(typeList ) <- varInfo | > lapply(\(x ) x $ name ) | > unlist()
8591
@@ -92,29 +98,59 @@ make_node_fun <- function(varInfo) {
9298 initFun <- function (){}
9399 formals(initFun ) <- structure(as.pairlist(CpublicVars ), names = ctorArgNames )
94100
101+ if (missing(classname ))
102+ classname <- nodeFxnLabelCreator()
103+
104+ baseclass <- paste0(" nodeFxnClass_<" , classname , " >" )
105+
95106# This was a prototype
96- node_dnorm <- substitute(
107+ node_nClass <- substitute(
97108 nClass(
98- classname = " node_dnorm " ,
109+ classname = CLASSNAME ,
99110 Cpublic = CPUBLIC ,
100111 compileInfo = list (
101112 createFromR = FALSE , # Without a default constructor (which we've disabled here), createFromR is impossible
102- nClass_inherit = list (base = " nodeFxnClass_<node_dnorm> " )) # Ideally this line would be obtained from a base nClass, but we insert it directly for now
113+ nClass_inherit = list (base = BASECLASS )) # Ideally this line would be obtained from a base nClass, but we insert it directly for now
103114 ),
104115 list (CPUBLIC = c(
105116 list (
106- node_dnorm = nFunction(
107- initFun ,
108- compileInfo = list (constructor = TRUE , initializers = initializersList )
109- )
117+ nFunction(
118+ initFun ,
119+ compileInfo = list (constructor = TRUE , initializers = initializersList )
120+ )
121+ ) | > structure(names = classname ),
122+ CpublicVars ,
123+ methods
110124 ),
111- CpublicVars
112- )))
113- eval(node_dnorm )
125+ CLASSNAME = classname ,
126+ BASECLASS = baseclass
127+ ))
128+ eval(node_nClass )
114129}
115130# test <- nCompiler:::argType2symbol('CppVar(baseType = argType2Cpp("numericVector"), ref=TRUE, const=TRUE)')
116131
117- makeModel_nClass <- function (varInfo ) {
132+ # Make all the info needed to include a node in a model class.
133+ # The nodeFxn_nClass should be created first.
134+ # Currently it needs to have a name to include in nCompile(). Later we might be able to pass the object itself
135+ # At first drafting this is fairly trivial but could grow in complexity.
136+
137+ make_node_info <- function (membername ,
138+ nodeFxnName ,
139+ classname ,
140+ varInfo = list ()
141+ ) {
142+ ctorArgs <- varInfo | > lapply(\(x ) x $ name ) | > unlist()
143+
144+ list (nodeFxnName = nodeFxnName ,
145+ membername = membername ,
146+ classname = classname ,
147+ ctorArgs = ctorArgs )
148+ }
149+
150+ makeModel_nClass <- function (varInfo ,
151+ nodes = list (),
152+ classname
153+ ) {
118154 # varInfo will be a list (names not used) of name, nDim, sizes.
119155 CpublicModelVars <- varInfo | > lapply(\(x ) paste0(" numericArray(nDim=" ,x $ nDim ," )" ))
120156 names(CpublicModelVars ) <- varInfo | > lapply(\(x ) x $ name ) | > unlist()
@@ -127,6 +163,9 @@ makeModel_nClass <- function(varInfo) {
127163 opDefs $ setup_node_mgmt $ returnType <- nCompiler ::: argType2symbol(quote(void()))
128164 opDefs $ setup_node_mgmt $ labelAbstractTypes $ recurse <- FALSE
129165
166+ if (missing(classname ))
167+ classname <- modelLabelCreator()
168+
130169 CpublicMethods <- list (
131170 do_setup_node_mgmt = nFunction(
132171 name = " call_setup_node_mgmt" ,
@@ -141,29 +180,44 @@ makeModel_nClass <- function(varInfo) {
141180 function (Rlist = ' RcppList' ) {cppLiteral(' modelClass_::resize_from_list(Rlist);' )}
142181 )
143182 )
144- CpublicNodeFuns <- list (
145- beta_node = ' node_dnorm()'
146- )
183+ # nodes will be a list of membername, nodeFxnName, (node) classname, ctorArgs (list)
184+ node_pieces <- nodes | > lapply(\(x ) {
185+ nClass_type <- paste0(x $ nodeFxnName , " ()" )
186+ init_string <- paste0(' nCpp("' , x $ membername , ' ( new ' , x $ classname , ' (' ,
187+ paste0(x $ ctorArgs , collapse = " ," ), ' ))")' )
188+ list (nClass_type = nClass_type ,
189+ init_string = init_string ,
190+ membername = x $ membername )
191+ })
192+ membernames <- node_pieces | > lapply(\(x ) x $ membername ) | > unlist()
193+ CpublicNodeFuns <- node_pieces | > lapply(\(x ) x $ nClass_type ) | > setNames(membernames )
194+ # CpublicNodeFuns <- list(
195+ # beta_node = 'node_dnorm()'
196+ # )
147197 CpublicCtor <- list (
148- mymodel = nFunction(
198+ nFunction(
149199 function (){},
150200 compileInfo = list (constructor = TRUE ,
151- initializers = c(' nCpp("beta_node(new node_dnorm(mu, beta, 1))")' ))
201+ # initializers = c('nCpp("beta_node(new node_dnorm(mu, beta, 1))")'))
202+ initializers = node_pieces | > lapply(\(x ) x $ init_string ) | > unlist())
152203 )
153- )
204+ ) | > structure(names = classname )
205+ baseclass <- paste0(" modelClass_<" , classname , " >" )
154206 ans <- substitute(
155207 nClass(
156- classname = " mymodel " ,
208+ classname = CLASSNAME ,
157209 inherit = modelBase_nClass ,
158210 compileInfo = list (opDefs = OPDEFS ,
159- nClass_inherit = list (base = " modelClass_<mymodel> " )
211+ nClass_inherit = list (base = BASECLASS )
160212 # inherit = list(base = "public modelClass_<mymodel>"),
161213 # Hincludes = "<nCompiler/nClass_interface/post_Rcpp/nCompiler_model_base_devel.h>"
162214 ),
163215 Cpublic = CPUBLIC
164216 ),
165217 list (OPDEFS = opDefs ,
166- CPUBLIC = c(CpublicNodeFuns , CpublicModelVars , CpublicCtor , CpublicMethods ))
218+ CPUBLIC = c(CpublicNodeFuns , CpublicModelVars , CpublicCtor , CpublicMethods ),
219+ CLASSNAME = classname ,
220+ BASECLASS = baseclass )
167221 )
168222 eval(ans , envir = parent.frame())
169223}
0 commit comments