Skip to content

Commit

Permalink
close #160: implement when_activated convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Aug 6, 2018
1 parent 94a5b1e commit 9b97374
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export(trajectory)
export(trap)
export(untrap)
export(wait)
export(when_activated)
export(wrap)
importFrom(R6,R6Class)
importFrom(Rcpp,evalCpp)
Expand Down
29 changes: 29 additions & 0 deletions R/generators.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,32 @@ from_to <- function(start_time, stop_time, dist, arrive=TRUE, every=NULL) {
}
}
}

#' @rdname generators
#' @param n number of arrivals to generate when activated.
#'
#' @details \code{\link{when_activated}} sets up an initially inactive generator
#' which generates \code{n} arrivals each time it is activated from any
#' trajectory using the activity \code{\link{activate}}.
#' @export
#' @examples
#' # triggering arrivals on demand from a trajectory
#' t1 <- trajectory() %>%
#' activate("dummy")
#'
#' simmer() %>%
#' add_generator("dummy", t0, when_activated()) %>%
#' add_generator("trigger", t1, at(2)) %>%
#' run() %>%
#' get_mon_arrivals()
#'
when_activated <- function(n=1) {
first <- TRUE
function() {
if (first) {
first <<- FALSE
return(-1)
}
c(rep(0, n), -1)
}
}
19 changes: 19 additions & 0 deletions man/generators.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9b97374

Please sign in to comment.