Activities for seizing/releasing a resource, by name or a previously selected
one. Resources must be defined in the simulation environment (see
add_resource
).
Usage
seize(.trj, resource, amount = 1, continue = NULL, post.seize = NULL,
reject = NULL, ..., tag)
seize_selected(.trj, amount = 1, id = 0, continue = NULL,
post.seize = NULL, reject = NULL, ..., tag)
release(.trj, resource, amount = 1, ..., tag)
release_selected(.trj, amount = 1, id = 0, ..., tag)
release_all(.trj, resource, ..., tag)
release_selected_all(.trj, id = 0, ..., tag)
Arguments
- .trj
the trajectory object.
- resource
the name of the resource.
- amount
the amount to seize/release, accepts either a numeric or a callable object (a function) which must return a numeric.
- continue
a boolean (if
post.seize
ORreject
is defined) or a pair of booleans (ifpost.seize
ANDreject
are defined; if only one value is provided, it will be recycled) to indicate whether these subtrajectories should continue to the next activity in the main trajectory.- post.seize
an optional trajectory object which will be followed after a successful seize.
- reject
an optional trajectory object which will be followed if the arrival is rejected (dropped).
- ...
unused.
- tag
activity tag name to perform named rollbacks (see
rollback
) or just to better identify your activities.- id
selection identifier for nested usage.
Details
Rejection happens when a resource is at full capacity and there is
no room in the queue (either because there is a finite queue_size
and
it is full, or because queue_size=0
and thus it is disabled). In those
cases, the reject
parameter defines a fallback trajectory. Note,
however, that, if the arrival is accepted (either in the queue or in the
server) and then it is dropped afterwards due to preemption or resource
shrinkage, then this trajectory will not be executed. Instead, see
handle_unfinished
for another, more general, method for
handling all kinds of unfinished arrivals.
Examples
## simple seize, delay, then release
traj <- trajectory() %>%
seize("doctor", 1) %>%
timeout(3) %>%
release("doctor", 1)
simmer() %>%
add_resource("doctor", capacity=1) %>%
add_generator("patient", traj, at(0, 1)) %>%
run() %>%
get_mon_resources()
#> resource time server queue capacity queue_size system limit replication
#> 1 doctor 0 1 0 1 Inf 1 Inf 1
#> 2 doctor 1 1 1 1 Inf 2 Inf 1
#> 3 doctor 3 1 0 1 Inf 1 Inf 1
#> 4 doctor 6 0 0 1 Inf 0 Inf 1
## arrival rejection (no space left in the queue)
traj <- trajectory() %>%
log_("arriving...") %>%
seize("doctor", 1) %>%
# the second patient won't reach this point
log_("doctor seized") %>%
timeout(5) %>%
release("doctor", 1)
simmer() %>%
add_resource("doctor", capacity=1, queue_size=0) %>%
add_generator("patient", traj, at(0, 1)) %>%
run() %>% invisible
#> 0: patient0: arriving...
#> 0: patient0: doctor seized
#> 1: patient1: arriving...
## capturing rejection to retry
traj <- trajectory() %>%
log_("arriving...") %>%
seize(
"doctor", 1, continue = FALSE,
reject = trajectory() %>%
log_("rejected!") %>%
# go for a walk and try again
timeout(2) %>%
log_("retrying...") %>%
rollback(amount = 4, times = Inf)) %>%
# the second patient will reach this point after a couple of walks
log_("doctor seized") %>%
timeout(5) %>%
release("doctor", 1) %>%
log_("leaving")
#> Warning: argument 'amount' is deprecated, use 'target' instead
simmer() %>%
add_resource("doctor", capacity=1, queue_size=0) %>%
add_generator("patient", traj, at(0, 1)) %>%
run() %>% invisible
#> 0: patient0: arriving...
#> 0: patient0: doctor seized
#> 1: patient1: arriving...
#> 1: patient1: rejected!
#> 3: patient1: retrying...
#> 3: patient1: rejected!
#> 5: patient1: retrying...
#> 5: patient0: leaving
#> 5: patient1: doctor seized
#> 10: patient1: leaving
## combining post.seize and reject
traj <- trajectory() %>%
log_("arriving...") %>%
seize(
"doctor", 1, continue = c(TRUE, TRUE),
post.seize = trajectory("admitted patient") %>%
log_("admitted") %>%
timeout(5) %>%
release("doctor", 1),
reject = trajectory("rejected patient") %>%
log_("rejected!") %>%
seize("nurse", 1) %>%
timeout(2) %>%
release("nurse", 1)) %>%
# both patients will reach this point, as continue = c(TRUE, TRUE)
timeout(10) %>%
log_("leaving...")
simmer() %>%
add_resource("doctor", capacity=1, queue_size=0) %>%
add_resource("nurse", capacity=10, queue_size=0) %>%
add_generator("patient", traj, at(0, 1)) %>%
run() %>% invisible
#> 0: patient0: arriving...
#> 0: patient0: admitted
#> 1: patient1: arriving...
#> 1: patient1: rejected!
#> 13: patient1: leaving...
#> 15: patient0: leaving...