Skip to contents

This brick encapsulates a chain of interleaved resources, i.e., the current resource is not released until the next one in the chain is available. An interesting property of such a pattern is that, if one resource is blocked for some reason, the whole chain stops.

Usage

interleave(.trj, resources, task, amount = 1)

Arguments

.trj

the trajectory object.

resources

character vector of resource names.

task

the timeout duration supplied by either passing a numeric or a callable object (a function) which must return a numeric (negative values are automatically coerced to positive).

amount

the amount to seize/release, accepts either a numeric or a callable object (a function) which must return a numeric.

Value

Returns the following chain of activities: seize

(1) > timeout > [seize (token to 2) > release (1) > seize (2) > timeout > release (2) > release (token to 2) > ... (repeat) ] (see examples below). Thus, the total number of activities appended is length(resources) * 3 + (length(resources)-1) * 2.

Details

Both task and amount accept a list of values/functions, instead of a single one, that should be of the same length as resources, so that each value/function is applied to the resource of the same index.

The transition to the second and subsequent resources is guarded by a token, an auxiliary resource whose capacity must be equal to the capacity + queue size of the guarded resource, and its queue size must be infinite. For example, if two resources are provided, c("A", "B"), the auxiliary resource will be named "B_token". If capacity=2 and queue_size=1 for B, then capacity=3 and queue_size=Inf must be the values for B_token. But note that the user is responsible for adding such an auxiliary resource to the simulation environment with the appropriate parameters.

Examples

## These are equivalent:
trajectory() %>%
  interleave(c("A", "B"), c(2, 10), 1)
#> trajectory: anonymous, 8 activities
#> { Activity: Seize        | resource: A, amount: 1 }
#> { Activity: Timeout      | delay: 2 }
#> { Activity: Seize        | resource: B_token, amount: 1 }
#> { Activity: Release      | resource: A, amount: 1 }
#> { Activity: Seize        | resource: B, amount: 1 }
#> { Activity: Timeout      | delay: 10 }
#> { Activity: Release      | resource: B, amount: 1 }
#> { Activity: Release      | resource: B_token, amount: 1 }

trajectory() %>%
  seize("A", 1) %>%
  timeout(2) %>%
  seize("B_token", 1) %>%
  release("A", 1) %>%
  seize("B", 1) %>%
  timeout(10) %>%
  release("B", 1) %>%
  release("B_token", 1)
#> trajectory: anonymous, 8 activities
#> { Activity: Seize        | resource: A, amount: 1 }
#> { Activity: Timeout      | delay: 2 }
#> { Activity: Seize        | resource: B_token, amount: 1 }
#> { Activity: Release      | resource: A, amount: 1 }
#> { Activity: Seize        | resource: B, amount: 1 }
#> { Activity: Timeout      | delay: 10 }
#> { Activity: Release      | resource: B, amount: 1 }
#> { Activity: Release      | resource: B_token, amount: 1 }