Add Schedule dataclass and refactor EventGenerator#3250
Conversation
Introduce a Schedule dataclass (interval, start, end, count) to represent recurring timing and refactor EventGenerator to consume a Schedule instead of separate interval/stop parameters. EventGenerator now stores schedule, uses schedule.interval (callable supported), and evaluates schedule.count/end for stop conditions. Simplified start() and stop() to immediate start/stop behavior (start/after/at and stop(at/after/count) parameters removed) and removed internal _max_count/_end_time fields. Also updated event enqueueing to use model._event_list.add_event. This centralizes scheduling logic and simplifies the API: callers must be updated for the changed constructor and start/stop semantics.
Add Schedule import and a new TestSchedule suite to validate default, custom, and callable interval behavior. Refactor EventGenerator tests to accept a Schedule object instead of raw interval/at/after/stop args: update initialization, start/stop semantics, schedule end/count handling, callable-interval evaluation, functools.partial usage, and priority ordering. Rename and reorganize several test cases and adjust simulation timings accordingly; remove some legacy error cases now superseded by Schedule-based API.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
As indicated also in #3145, I am not convinced by this for data collection. But I agree that this might be cleaner to use over a long list of keyword arguments. I'll try to review later this weekend. |
|
Even if not useful for anything other than event scheduling, from this refactor a |
Similar to Scenario, so this is for me the prime reason why I still like this PR. |
quaquel
left a comment
There was a problem hiding this comment.
one clarifying question, but looks fine to me otherwise.
|
@quaquel conceptually, do you think |
I can see arguments for this but also against this. It depends a bit on how we wan tot use Schedule. At the moment, it is purely used for repeated scheduling of events on the event list. If you want to reuse a schedule object, having the priority frozen in it might be a bit restrictive. At the same time, your argument also holds and creating a Schedule instance is not a big deal, removing the need to extensively reuse them. So, I am fine either way, with a slight preference for including it. |
|
You could essentially do stuff like: step_start = Schedule(interval=1.0, priority=10)
step_end = Schedule(interval=1.0, priority=0) |
Summary
This PR introduces a
Scheduledataclass to centralize recurring event configuration and refactorsEventGeneratorto use it. The change simplifies the event-generation API, consolidates stop/start behavior, and improves clarity around recurring execution rules.A new stable API for creating events and running will still be introduced in another PR, this PR just builds the foundation for that. See EwoutH@440fdbe for an initial concept.
Motive
Previously,
EventGeneratorhandled interval logic, start timing, and stop conditions (at,after,count) internally. This created a more complex API surface and scattered scheduling logic across multiple methods.Implementation
A new
Scheduledataclass was introduced to define recurring execution behavior (interval,start,end, andcount). Theintervalfield supports both fixed numeric values and callables evaluated against the model at runtime.EventGeneratorwas refactored to depend onScheduleinstead of storing interval and stop-condition state internally. The generator now reads execution limits directly from the schedule when determining whether to reschedule events. Thestart()method was simplified to useschedule.startor default tomodel.time + interval, andstop()now performs only immediate cancellation.Tests were updated to reflect the new API, and a new test suite for
Schedulewas added. Existing behavior such as callable intervals, execution counting, and priority ordering remains covered.Usage Examples
Additional Notes
Scheduleobject can be used for other things too, like data collection schedules (see AddDataRecorderfor reactive Data Storage andDatasetConfigfor Configuration #3145 (comment), cc @codebreaker32).EventGeneratorby replacing theintervalargument withSchedule.Scheduleinstead of configured at runtime.start()activates scheduling,stop()cancels immediately.