Skip to content

Commit

Permalink
Small timeline QOL updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney authored and skovati committed Sep 9, 2024
1 parent c261313 commit 4305a70
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void run(EditablePlan plan) {
final var connections = lowFruit.starts().shift(Duration.MINUTE.negate())
.connectTo(bites.ends(), false);

for (final var connection: connections.collect()) {
for (final var connection: connections) {
assert connection.to != null;
plan.create(
"GrowBanana",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public void run(@NotNull final EditablePlan plan) {
// This goal will only apply during the Dole mission phase. :)
final var dolePhase = simResults
.resource("/producer", Strings::deserialize)
.highlightEqualTo("Dole");
dolePhase.cache();
.highlightEqualTo("Dole")
.cache();

// Manual recurrence goal: during Dole phase, require a bitebanana every `bitePeriod`.
final var bites = plan.directives("BiteBanana")
.filterByWindows(dolePhase, false)
.collect();

var currentTime = Duration.MIN_VALUE;
for (final var phase: dolePhase.collect()) {
for (final var phase: dolePhase) {
currentTime = Duration.max(currentTime, phase.start);

while (currentTime.plus(bitePeriod).shorterThan(phase.end)) {
Expand Down Expand Up @@ -91,8 +91,7 @@ public void run(@NotNull final EditablePlan plan) {
// So we iteratively find the first time /fruit drops below zero
// and add a grow banana fix it. We then mock the effect of grow banana
// by adding one to /fruit, rather than resimulating, and do it again.
var fruit = simResults.resource("/fruit", Real::deserialize);
fruit.cache();
var fruit = simResults.resource("/fruit", Real::deserialize).cache();

var ranOutAt = fruit.lessThan(0).filterByWindows(dolePhase, true).risingEdges().highlightTrue().collect();
while (!ranOutAt.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ data class BaseTimeline<V: IntervalLike<V>, TL: Timeline<V, TL>>(
private var cached: List<V>? = null
private var cachedOptions: CollectOptions? = null

override fun cache(opts: CollectOptions) {
override fun cache(opts: CollectOptions): TL {
if (cachedOptions == null || !cachedOptions!!.contains(opts)) {
cached = collect(opts)
cachedOptions = opts
}
return specialize()
}

override fun iterator(): Iterator<V> = collect().iterator()

override fun collect(opts: CollectOptions) =
if (cached == null || !cachedOptions!!.contains(opts)) collector(opts)
else ctor(BaseTimeline(ctor, listCollector(cached!!))).collect(opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import gov.nasa.ammos.aerie.procedural.timeline.payloads.IntervalLike
* Should be implemented by composing a raw timeline in your container
* and delegating to it with the `by` keyword. See any timeline for examples.
*/
interface Timeline<V: IntervalLike<V>, THIS: Timeline<V, THIS>> {
interface Timeline<V: IntervalLike<V>, THIS: Timeline<V, THIS>>: Iterable<V> {
/**
* [(DOC)][collect] Evaluates the stack of operations and produces a list of timeline payload objects.
*
Expand Down Expand Up @@ -62,7 +62,7 @@ interface Timeline<V: IntervalLike<V>, THIS: Timeline<V, THIS>> {
/**
* Caches the result of collecting this timeline, to be reused for future collect requests if possible.
*/
fun cache(opts: CollectOptions)
fun cache(opts: CollectOptions): THIS

/**
* [(DOC)][cache] A simplified version of [cache].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface Plan {
/** Total extent of the plan's bounds, whether it was simulated on the full extent or not. */
fun totalBounds(): Interval

/** The total duration of the plan, whether simulated on the full extent or not. */
fun duration() = totalBounds().duration()

/** Convert a time instant to a relative duration (relative to plan start). */
fun toRelative(abs: Instant): Duration
/** Convert a relative duration to a time instant. */
Expand Down

0 comments on commit 4305a70

Please sign in to comment.