# This handler prints increases a counter in the state of the
# Simulation object, and schedule another event every 0.1 time unit.
handler = function(time, sim, agent) {
x = getState(sim)
x$counter = x$counter + 1
setState(sim, x)
schedule(agent, newEvent(time + 0.1, handler))
}
# create a new simulation with no agents. but the simulation itself is
# an agent. So we can use all the methods of agent
sim = Simulation$new()
# set the state of the simulation, initialize the counter
sim$state = list(counter = 0)
# schedule a new event at time 0
sim$schedule(Event$new(0, handler))
# add a logger for the counter. Note that, because sim is an R6 class
# to use it in the newStateLogger function, we need to access the
# external pointer using its $get method
sim$addLogger(newStateLogger("counter", sim$get, "counter"))
# run the simulation for 10 time units.
print(sim$run(0:10))
# interestingly, the counts are not exactly in 10 event time unit.
# Firstly, report always happen before event, so event at time 0 is
# not counted in the time interval 0 to 1. Secondly, the event time
# is stored as a numeric value with increments of 0.1, which is
# subject to rounding errors. So some the the integer tiome events
# may be before the reporting and some may be after.
Run the code above in your browser using DataLab