Rule {sched} | R Documentation |
Scheduling rule class.
Description
Scheduling rule class.
Scheduling rule class.
Details
This class represents a scheduling rule, used to limit the number of events during a certain lap of time.
Methods
Public methods
Method new()
Initializer.
Usage
Rule$new(n = 3L, lap = 1)
Arguments
n
Number of events during a time lap.
lap
Duration of a time lap, in seconds.
Returns
Nothing.
Examples
# Create a rule object with default parameters r <- Rule$new() # Create a rule object with 5 events allowed each second (default time) r2 <- Rule$new(5L) # Create a rule object with 5 events allowed each 3 seconds r3 <- Rule$new(5L, 3)
Method getN()
Gets the number of events allowed during a lap time.
Usage
Rule$getN()
Returns
Returns the number of events as an integer.
Examples
r <- Rule$new() #' Get the allowed number of events for a rule print(r$getN())
Method getLapTime()
Gets the lap time.
The number of seconds during which N events are allowed.
Usage
Rule$getLapTime()
Returns
Returns Lap time as a numeric.
Examples
# Create a rule object with default parameters r <- Rule$new() #' Get the configured lap time for a rule print(r$getLapTime())
Method print()
Displays information about this instance.
Usage
Rule$print()
Returns
Nothing.
Examples
# Create a rule object with default parameters r <- Rule$new() # Print information about a rule object print(r)
Method wait()
Wait (sleep) until a new event is allowed.
Usage
Rule$wait(do_sleep = TRUE)
Arguments
do_sleep
Debug parameter that turns off the call to Sys.sleep(). Use only for testing.
Returns
The time passed to wait, in seconds.
Examples
# Create a rule object that allows 3 events each 0.02 seconds r <- Rule$new(3, 0.02) #' Loop for generating 20 events i <- 0 # event index while (i < 20) { # Wait until next event is allowed wait_time <- r$wait() print(paste("We have waited", wait_time, "second(s) and are now allowed to process event number", i)) i <- i + 1 }
Method clone()
The objects of this class are cloneable with this method.
Usage
Rule$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
# Create a new Rule object:
rule <- sched::Rule$new(n=1,lap=0.2) # 1 event allowed each 2 seconds
# Wait to be allowed to process with first event:
rule$wait() # The first event will be allowed directly, no waiting time.
# Process your first event here
rule$wait() # The second event will be delayed 0.2 seconds. This time
# includes the time passed between the first call to wait() and
# this one.
# Process your second event here
## ------------------------------------------------
## Method `Rule$new`
## ------------------------------------------------
# Create a rule object with default parameters
r <- Rule$new()
# Create a rule object with 5 events allowed each second (default time)
r2 <- Rule$new(5L)
# Create a rule object with 5 events allowed each 3 seconds
r3 <- Rule$new(5L, 3)
## ------------------------------------------------
## Method `Rule$getN`
## ------------------------------------------------
r <- Rule$new()
#' Get the allowed number of events for a rule
print(r$getN())
## ------------------------------------------------
## Method `Rule$getLapTime`
## ------------------------------------------------
# Create a rule object with default parameters
r <- Rule$new()
#' Get the configured lap time for a rule
print(r$getLapTime())
## ------------------------------------------------
## Method `Rule$print`
## ------------------------------------------------
# Create a rule object with default parameters
r <- Rule$new()
# Print information about a rule object
print(r)
## ------------------------------------------------
## Method `Rule$wait`
## ------------------------------------------------
# Create a rule object that allows 3 events each 0.02 seconds
r <- Rule$new(3, 0.02)
#' Loop for generating 20 events
i <- 0 # event index
while (i < 20) {
# Wait until next event is allowed
wait_time <- r$wait()
print(paste("We have waited", wait_time,
"second(s) and are now allowed to process event number", i))
i <- i + 1
}
[Package sched version 1.0.1 Index]