# 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
}
Run the code above in your browser using DataLab