Skip to content

Commit

Permalink
Merge pull request #1310 from NASA-AMMOS/bug/unit-annotation-crashing…
Browse files Browse the repository at this point in the history
…-constraints

Fixed an issue where constraint runs would fail against activities wi…
  • Loading branch information
cohansen authored Feb 1, 2024
2 parents 02d06a5 + 795b547 commit 1a0b9a8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,53 @@ void constraintsSucceedOneViolation() throws IOException {
assertTrue(constraintResult.gaps().isEmpty());
}

@Test
void constraintsSucceedAgainstVariantWithUnits() throws IOException {
hasura.deleteActivity(planId, activityId);
hasura.deleteConstraint(constraintId);
var fruitConstraintName = "fruit_equals_3";
var fruitConstraintId = hasura.insertPlanConstraint(
fruitConstraintName,
planId,
"export default (): Constraint => Real.Resource(\"/fruit\").equal(3)",
"");
hasura.insertActivity(
planId,
"PeelBanana",
"1h",
Json.createObjectBuilder().add("peelDirection", "fromStem").build());

hasura.awaitSimulation(planId);
final var constraintsResponses = hasura.checkConstraints(planId);
assertEquals(1, constraintsResponses.size());

// Check the Response
final var constraintResponse = constraintsResponses.get(0);
assertTrue(constraintResponse.success());
assertEquals(fruitConstraintId, constraintResponse.constraintId());
assertEquals(fruitConstraintName, constraintResponse.constraintName());
assertEquals("plan", constraintResponse.type());
// Check the Result
assertTrue(constraintResponse.result().isPresent());
final var constraintResult = constraintResponse.result().get();
// Resources
final var resources = constraintResult.resourceIds();
assertEquals(1, resources.size());
assertTrue(resources.contains("/fruit"));

// Violation
assertEquals(1, constraintResult.violations().size());
final var violation = constraintResult.violations().get(0);
assertEquals(1, violation.windows().size());

final long activityOffset = 60 * 60 * 1000000L; // 1h in micros

assertEquals(0, violation.windows().get(0).start());
assertEquals(activityOffset, violation.windows().get(0).end());
// Gaps
assertTrue(constraintResult.gaps().isEmpty());
}

@Test
void constraintsSucceedNoViolations() throws IOException {
// Delete activity to avoid violation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ private ArrayList<ActivityType> expectedActivityTypesBanananation() {
activityTypes.add(new ActivityType(
"PeelBanana",
Map.of("peelDirection",
new Parameter(0, new ValueSchemaVariant(List.of(
new Variant("fromStem", "fromStem"),
new Variant("fromTip", "fromTip")))))));
new Parameter(0,
new ValueSchemaMeta(Map.of("unit", Json.createObjectBuilder(Map.of("value", "direction")).build()),
new ValueSchemaVariant(List.of(
new Variant("fromStem", "fromStem"),
new Variant("fromTip", "fromTip"))))))));
activityTypes.add(new ActivityType("PickBanana", Map.of("quantity", new Parameter(0, VALUE_SCHEMA_INT))));
activityTypes.add(new ActivityType("RipenBanana", Map.of()));
activityTypes.add(new ActivityType("ThrowBanana", Map.of("speed", new Parameter(0, VALUE_SCHEMA_REAL))));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.nasa.jpl.aerie.banananation.activities;

import gov.nasa.jpl.aerie.banananation.Mission;
import gov.nasa.jpl.aerie.contrib.metadata.Unit;
import gov.nasa.jpl.aerie.merlin.framework.annotations.ActivityType;
import gov.nasa.jpl.aerie.merlin.framework.annotations.ActivityType.EffectModel;
import gov.nasa.jpl.aerie.merlin.framework.annotations.Export.Parameter;
Expand All @@ -25,6 +26,7 @@ public enum PeelDirectionEnum {
}

@Parameter
@Unit("direction")
public PeelDirectionEnum peelDirection = PeelDirectionEnum.fromStem;

@EffectModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private JsonParseResult<ValueSchema> parseVariant(final JsonObject obj) {
productP
.field("type", literalP("variant"))
.field("variants", listP(variantP))
.rest()
.map(
untuple((type, variants) -> ValueSchema.ofVariant(variants)),
$ -> tuple(Unit.UNIT, $.asVariant().get()));
Expand Down

0 comments on commit 1a0b9a8

Please sign in to comment.