From 500fa2a302ae776ffcdbae5d39dbc8e1d9662484 Mon Sep 17 00:00:00 2001 From: JoelCourtney Date: Thu, 21 Nov 2024 13:51:42 -0800 Subject: [PATCH] Validate that new directives are within the plan bounds --- .../scheduling/utils/EasyEditablePlanDriver.kt | 17 +++++++++++++---- .../scheduler/plan/InMemoryEditablePlan.kt | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/procedural/scheduling/src/main/kotlin/gov/nasa/ammos/aerie/procedural/scheduling/utils/EasyEditablePlanDriver.kt b/procedural/scheduling/src/main/kotlin/gov/nasa/ammos/aerie/procedural/scheduling/utils/EasyEditablePlanDriver.kt index 545b84738a..7c2f3c4289 100644 --- a/procedural/scheduling/src/main/kotlin/gov/nasa/ammos/aerie/procedural/scheduling/utils/EasyEditablePlanDriver.kt +++ b/procedural/scheduling/src/main/kotlin/gov/nasa/ammos/aerie/procedural/scheduling/utils/EasyEditablePlanDriver.kt @@ -97,11 +97,20 @@ abstract class EasyEditablePlanDriver( protected abstract fun simulateInternal(options: SimulateOptions) /** - * Optional validation hook for new activities. The default implementation does nothing. + * Optional validation hook for new activities. * - * Implementor should throw if the arguments are invalid. + * The default implementation checks if the activity is within the bounds of the plan. The implementor can + * add additional checks by overriding this method and calling `super.validate(directive)`. Implementor + * should throw if the directive is invalid. */ - protected open fun validateArguments(directive: Directive) {} + protected open fun validate(directive: Directive) { + if (directive.startTime > duration()) { + throw Exception("New activity with id ${directive.id.id()} would start after the end of the plan") + } + if (directive.start is DirectiveStart.Absolute && directive.startTime.isNegative) { + throw Exception("New activity with id ${directive.id.id()} would start before the beginning of the plan") + } + } private data class Commit( val diff: List, @@ -153,7 +162,7 @@ abstract class EasyEditablePlanDriver( val resolved = directive.resolve(id, parent) uncommittedChanges.add(Edit.Create(resolved)) - validateArguments(resolved) + validate(resolved) createInternal(resolved) diff --git a/scheduler-driver/src/main/kotlin/gov/nasa/jpl/aerie/scheduler/plan/InMemoryEditablePlan.kt b/scheduler-driver/src/main/kotlin/gov/nasa/jpl/aerie/scheduler/plan/InMemoryEditablePlan.kt index 82cdbf5831..49ca1dbaeb 100644 --- a/scheduler-driver/src/main/kotlin/gov/nasa/jpl/aerie/scheduler/plan/InMemoryEditablePlan.kt +++ b/scheduler-driver/src/main/kotlin/gov/nasa/jpl/aerie/scheduler/plan/InMemoryEditablePlan.kt @@ -47,7 +47,8 @@ data class InMemoryEditablePlan( simulationFacade.simulateWithResults(plan, options.pause.resolve(this)) } - override fun validateArguments(directive: Directive) { + override fun validate(directive: Directive) { + super.validate(directive) lookupActivityType(directive.type).specType.inputType.validateArguments(directive.inner.arguments) }