Skip to contents

Activity for going backwards to a previous point in the trajectory. Useful to implement loops.

Usage

rollback(.trj, target, times = Inf, check = NULL, ..., tag)

Arguments

.trj

the trajectory object.

target

tag name (previously set with the tag argument in any activity) or amount of activities (of the same or parent trajectories) to roll back (see examples).

times

the number of repetitions until an arrival may continue.

check

a callable object (a function) which must return a boolean. If present, the times parameter is ignored, and the activity uses this function to check whether the rollback must be done or not.

...

unused

tag

activity tag name to perform named rollbacks (see rollback) or just to better identify your activities.

Value

Returns the trajectory object.

Examples

## rollback a specific number of times
traj <- trajectory() %>%
  log_("hello!") %>%
  timeout(1) %>%
  rollback(2, 3)

simmer() %>%
  add_generator("hello_sayer", traj, at(0)) %>%
  run() %>% invisible
#> 0: hello_sayer0: hello!
#> 1: hello_sayer0: hello!
#> 2: hello_sayer0: hello!
#> 3: hello_sayer0: hello!

## same but with a tag as target
traj <- trajectory() %>%
  log_("hello!", tag="msg") %>%
  timeout(1) %>%
  rollback("msg", 3)

simmer() %>%
  add_generator("hello_sayer", traj, at(0)) %>%
  run() %>% invisible
#> 0: hello_sayer0: hello!
#> 1: hello_sayer0: hello!
#> 2: hello_sayer0: hello!
#> 3: hello_sayer0: hello!

## custom check
env <- simmer()

traj <- trajectory() %>%
  set_attribute("var", 0) %>%
  log_(tag="msg", function()
    paste("attribute level is at:", get_attribute(env, "var"))) %>%
  set_attribute("var", 25, mod="+") %>%
  rollback("msg", check=function() get_attribute(env, "var") < 100) %>%
  log_("done")

env %>%
  add_generator("dummy", traj, at(0)) %>%
  run() %>% invisible
#> 0: dummy0: attribute level is at: 0
#> 0: dummy0: attribute level is at: 25
#> 0: dummy0: attribute level is at: 50
#> 0: dummy0: attribute level is at: 75
#> 0: dummy0: done