Skip to contents

Activities for collecting a number of arrivals before they can continue processing and splitting a previously established batch.

Usage

batch(.trj, n, timeout = 0, permanent = FALSE, name = "", rule = NULL,
  ..., tag)

separate(.trj, ..., tag)

Arguments

.trj

the trajectory object.

n

batch size, accepts a numeric or a callable object (a function) which must return a numeric.

timeout

set an optional timer which triggers batches every timeout time units even if the batch size has not been fulfilled, accepts a numeric or a callable object (a function) which must return a numeric (0 = disabled).

permanent

if TRUE, batches cannot be split.

name

optional string. Unnamed batches from different batch activities are independent. However, if you want to feed arrivals from different trajectories into a same batch, you need to specify a common name across all your batch activities.

rule

an optional callable object (a function) which will be applied to every arrival to determine whether it should be included into the batch, thus it must return a boolean.

...

unused.

tag

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

Value

Returns the trajectory object.

Examples

## unnamed batch with a timeout
traj <- trajectory() %>%
  log_("arrived") %>%
  batch(2, timeout=5) %>%
  log_("in a batch") %>%
  timeout(5) %>%
  separate() %>%
  log_("leaving")

simmer() %>%
  add_generator("dummy", traj, at(0:2)) %>%
  run() %>% invisible
#> 0: dummy0: arrived
#> 1: dummy1: arrived
#> 1: batch0: in a batch
#> 2: dummy2: arrived
#> 6: dummy0: leaving
#> 6: dummy1: leaving
#> 7: batch1: in a batch
#> 12: dummy2: leaving

## batching based on some dynamic rule
traj <- trajectory() %>%
  log_("arrived") %>%
  # always FALSE -> no batches
  batch(2, rule=function() FALSE) %>%
  log_("not in a batch") %>%
  timeout(5) %>%
  separate() %>%
  log_("leaving")

simmer() %>%
  add_generator("dummy", traj, at(0:2)) %>%
  run() %>% invisible
#> 0: dummy0: arrived
#> 0: dummy0: not in a batch
#> 1: dummy1: arrived
#> 1: dummy1: not in a batch
#> 2: dummy2: arrived
#> 2: dummy2: not in a batch
#> 5: dummy0: leaving
#> 6: dummy1: leaving
#> 7: dummy2: leaving

## named batch, shared across trajectories
traj0 <- trajectory() %>%
  log_("arrived traj0") %>%
  batch(2, name = "mybatch")

traj1 <- trajectory() %>%
  log_("arrived traj1") %>%
  timeout(1) %>%
  batch(2, name = "mybatch") %>%
  log_("in a batch") %>%
  timeout(2) %>%
  separate() %>%
  log_("leaving traj1")

simmer() %>%
  add_generator("dummy0", traj0, at(0)) %>%
  add_generator("dummy1", traj1, at(0)) %>%
  run() %>% invisible
#> 0: dummy00: arrived traj0
#> 0: dummy10: arrived traj1
#> 1: batch_mybatch: in a batch
#> 3: dummy00: leaving traj1
#> 3: dummy10: leaving traj1