Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline system initialization refactor #1629

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

DavidLegg
Copy link
Contributor

DRAFT pending #1589 , on which this PR builds.

  • Tickets addressed: N/A
  • Review: By commit
  • Merge strategy: Merge (no squash)

Description

Refactors the initialization of streamline to an explicit call to StreamlineSystem.init. This new method is responsible for building all the singletons used by streamline, rather than putting that responsibility on the Registrar. This promotes a better separation of concerns, especially as we add more singleton-type functionality to initialize.

This also standardizes re-initialization behavior. Re-initializing an already-initialized system will simply rebuild all the singletons. While this would be wasteful if abused in production, it supports testing scenarios where multiple models may be built and re-built as tests are run.

Finally, after merging in #1589, this adds a globally-visible absolute clock, and relatedly the StreamlineSystem.currentInstant() method to get the current time as an Instant.

Verification

Existing unit tests were updated to call StreamlineSystem.init, and they continue to function properly.
The streamline-demo model was also updated to call this method, and was tested manually in a local aerie deployment. It continues to function correctly as well.

Documentation

IMPORTANT: This is a breaking change!
All projects that use streamline should change

var registrar = new Registrar(aerieRegistrar, errorBehavior);

into

StreamlineSystem.init(InitArgs.builder()
    .baseRegistrar(aerieRegistrar)
    .errorBehavior(errorBehavior)
    .planStart(planStart)
    .build());
var registrar = Registration.REGISTRAR;

when constructing their model. This will properly initialize the full streamline system.

Additionally, currentTime() has moved from Resources to StreamlineSystem.

Finally, while not a breaking change, this does expose StreamlineSystem.currentInstant(), which returns the current absolute time as an Instant. This is something I expect many might find useful.

Future work

N/A

David Legg added 5 commits October 29, 2024 18:01
Adds "Instant" counterparts to Clock and VariableClock, which report absolute times as Java instants, but still step time according to an Aerie duration.
Also adds some minimal interoperability between absolute and relative clocks, and some useful derivations including comparisons.

Using these, also adds a global Instant-based clock to Resources, along with exposing both the duration- and instant-based clocks as resources.

In doing so, we add a new parameter for the plan's start time to Resources.init().
This in turn required refactoring some unit tests, and I took the opportunity to clean up the test construction a little bit as well.
This revealed a way to correctly initialize Resources, i.e., to call Resources.init() exactly once per initialization of each test model.
As a result, I refactored the clock handling to remove the awkward reinitialization pattern, since that pattern was added to handle test code.
Refactors streamline's init process, centralizing singletons and initialization into StreamlineSystem.
Using the absolute-timed InstantClock, add a new global simulation clock created by StreamlineSystem.init.
Also, refactor the arguments to StreamlineSystem.init into an InitArgs object with an accompanying builder.
This lets us write a "testBuilder" constructor that fills in as many init args with defaults as it can, while letting us override parameters if needed.
This pattern will extend well if we need to add more parameters in the future.

Using the global simulation clock, removes the "plan start time" parameter from the overload of DiscreteResources.precomputed that uses Instant keys.
This guarantees that absolute-timed precomputed resources are synced with the global clock.
This also creates a name collision with the version of precomputed that uses Duration keys, so the one with Instant keys was renamed to precomputed$.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant