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

Functionality for using nested variable and event lists #100

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
57 changes: 47 additions & 10 deletions R/simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,20 @@ simulation_loop <- function(
events = list(),
processes = list(),
timesteps
) {
) {
if (timesteps <= 0) {
stop('End timestep must be > 0')
}
for (t in seq_len(timesteps)) {
for (process in processes) {
execute_any_process(process, t)
}
for (event in events) {
event$.process()
}
for (variable in variables) {
variable$.update()
}
for (event in events) {
event$.tick()
}

invisible(lapply(events, process_events))

invisible(lapply(variables, update_variables))

invisible(lapply(events, tick_events))
}
}

Expand All @@ -67,3 +64,43 @@ execute_any_process <- function(p, t) {
p(t)
}
}


#' Update variables
#'
#' Recursive approach to updating all variables in nested list.
#'
#' @inheritParams simulation_loop
update_variables <- function(variables){
if("R6" %in% class(variables)){
variables$.update()
} else {
lapply(variables, update_variables)
}
}

#' Process events
#'
#' Recursive approach to processing all events in nested list.
#'
#' @inheritParams simulation_loop
process_events <- function(events){
if("R6" %in% class(events)){
events$.process()
} else {
lapply(events, process_events)
}
}

#' Tick events
#'
#' Recursive approach to ticking all events in nested list.
#'
#' @inheritParams simulation_loop
tick_events <- function(events){
if("R6" %in% class(events)){
events$.tick()
} else {
lapply(events, tick_events)
}
}