Process priority queue
Description
Section titled “Description”Creates a priority queue in form of an R6 class, that manages the correct process execution order.
Methods
Section titled “Methods”Public methods
Section titled “Public methods”metaRangePriorityQueue$new()
metaRangePriorityQueue$execute_next_process()
metaRangePriorityQueue$enqueue()
metaRangePriorityQueue$dequeue()
metaRangePriorityQueue$sort_future_queue()
metaRangePriorityQueue$update()
metaRangePriorityQueue$is_empty()
metaRangePriorityQueue$get_queue()
metaRangePriorityQueue$get_future_queue()
metaRangePriorityQueue$get_current_index()
metaRangePriorityQueue$print()
Method new()
Section titled “Method new()”Creates a new metaRangePriorityQueue object. Note: No reason to call this as user. The priority queue is created automatically when a simulation is created.
metaRangePriorityQueue$new()
Returns
Section titled “Returns”<metaRangePriorityQueue>
A metaRangePriorityQueue object.
Examples
Section titled “Examples”# Only for illustration purposes.pr_queue <- metaRangePriorityQueue$new()pr_queue
Method execute_next_process()
Section titled “Method execute_next_process()”Executes the next process in the queue. No reason to call this as user. The next process is executed automatically, when the previous process is finished.
metaRangePriorityQueue$execute_next_process(verbose)
Arguments
Section titled “Arguments”verbose
:<logical>
Should timing and process information be printed when the process is executed?
Returns
Section titled “Returns”<logical>``TRUE
if the next process has been executed,
FALSE
if not, in which case the queue is empty.
Examples
Section titled “Examples”# Only for illustration purposes.pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$execute_next_process(verbose = TRUE)
Method enqueue()
Section titled “Method enqueue()”Add a process to the (future) queue.
Users should only use this method if they added a process to the simulation
via the add_process method of the simulation object with the argument
queue = FALSE
. Otherwise the process is added to the queue automatically.
metaRangePriorityQueue$enqueue(process)
Arguments
Section titled “Arguments”process
:<metaRangeProcess>
A metaRangeProcess that should be added to the queue.
Returns
Section titled “Returns”<boolean>``TRUE
on success FALSE
on failure.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$get_future_queue()
Method dequeue()
Section titled “Method dequeue()”Remove a process from the (future) queue. Useful to remove a process from the queue if it is no longer needed. E.g. if a species went extinct.
metaRangePriorityQueue$dequeue(PID = NULL)
Arguments
Section titled “Arguments”PID
:<string>
the ID of the process, that should be dequeued.
Returns
Section titled “Returns”<boolean>``TRUE
on success FALSE
on failure.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$dequeue(pr$get_PID())pr_queue$get_future_queue()
Method sort_future_queue()
Section titled “Method sort_future_queue()”Sort the (future) queue based on the execution priority. This method is called automatically when a process is added to the queue. Note: No reason to call this as user.
metaRangePriorityQueue$sort_future_queue()
Returns
Section titled “Returns”<invisible self>
.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$sort_future_queue()# at least no error
Method update()
Section titled “Method update()”Update and reset the queue. This method is called automatically at the end of each time step. Note: No reason to call this as user.
metaRangePriorityQueue$update()
Returns
Section titled “Returns”<invisible self>
.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_queue()
Method is_empty()
Section titled “Method is_empty()”Check if the queue is empty.
metaRangePriorityQueue$is_empty()
Returns
Section titled “Returns”<boolean>``TRUE
if queue is empty FALSE
otherwise.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()stopifnot(pr_queue$is_empty())
Method get_queue()
Section titled “Method get_queue()”Return the current queue.
metaRangePriorityQueue$get_queue()
Returns
Section titled “Returns”<named int vector>
The current queue.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_queue()
Method get_future_queue()
Section titled “Method get_future_queue()”Return the future queue.
metaRangePriorityQueue$get_future_queue()
Returns
Section titled “Returns”<named int vector>
The future queue.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$get_future_queue()
Method get_current_index()
Section titled “Method get_current_index()”Get the index of the process that will be executed next.
metaRangePriorityQueue$get_current_index()
Returns
Section titled “Returns”<integer>
The index.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_current_index()
Method print()
Section titled “Method print()”Prints information about the queue to the console.
metaRangePriorityQueue$print()
Returns
Section titled “Returns”<invisible self>
.
Examples
Section titled “Examples”pr_queue <- metaRangePriorityQueue$new()pr_queue$print()
<metaRangePriorityQueue>
A metaRangePriorityQueue object.
Examples
Section titled “Examples”## ------------------------------------------------## Method `metaRangePriorityQueue$new`## ------------------------------------------------
# Only for illustration purposes.pr_queue <- metaRangePriorityQueue$new()pr_queue
## ------------------------------------------------## Method `metaRangePriorityQueue$execute_next_process`## ------------------------------------------------
# Only for illustration purposes.pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$execute_next_process(verbose = TRUE)
## ------------------------------------------------## Method `metaRangePriorityQueue$enqueue`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$get_future_queue()
## ------------------------------------------------## Method `metaRangePriorityQueue$dequeue`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$dequeue(pr$get_PID())pr_queue$get_future_queue()
## ------------------------------------------------## Method `metaRangePriorityQueue$sort_future_queue`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$sort_future_queue()# at least no error
## ------------------------------------------------## Method `metaRangePriorityQueue$update`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_queue()
## ------------------------------------------------## Method `metaRangePriorityQueue$is_empty`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()stopifnot(pr_queue$is_empty())
## ------------------------------------------------## Method `metaRangePriorityQueue$get_queue`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_queue()
## ------------------------------------------------## Method `metaRangePriorityQueue$get_future_queue`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$get_future_queue()
## ------------------------------------------------## Method `metaRangePriorityQueue$get_current_index`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr <- metaRangeProcess$new("A", "1", \() {message("test")}, 1, new.env())pr_queue$enqueue(pr)pr_queue$update()pr_queue$get_current_index()
## ------------------------------------------------## Method `metaRangePriorityQueue$print`## ------------------------------------------------
pr_queue <- metaRangePriorityQueue$new()pr_queue$print()