Skip to content

Commit

Permalink
Merge pull request #1509 from NASA-AMMOS/refactor/scheduling-ids-joel
Browse files Browse the repository at this point in the history
Refactor directive IDs in the scheduler
  • Loading branch information
mattdailis authored Aug 6, 2024
2 parents 8fb119b + 2550b77 commit a505cf9
Show file tree
Hide file tree
Showing 81 changed files with 1,385 additions and 1,453 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
package gov.nasa.jpl.aerie.constraints.model;

import gov.nasa.jpl.aerie.constraints.time.Interval;
import gov.nasa.jpl.aerie.merlin.driver.ActivityDirectiveId;
import gov.nasa.jpl.aerie.merlin.driver.ActivityInstanceId;
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public final class ActivityInstance {
public final long id;
public final String type;
public final Map<String, SerializedValue> parameters;
public final Interval interval;

public record ActivityInstance(
ActivityInstanceId instanceId,
String type,
Map<String, SerializedValue> parameters,
Interval interval,
Optional<ActivityDirectiveId> directiveId
) {
public ActivityInstance(
final long id,
final String type,
final Map<String, SerializedValue> parameters,
final Interval interval
long id,
String type,
Map<String, SerializedValue> parameters,
Interval interval
) {
this.type = type;
this.id = id;
this.parameters = parameters;
this.interval = interval;
this(id, type, parameters, interval, Optional.empty());
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof ActivityInstance)) return false;
final var o = (ActivityInstance)obj;

return Objects.equals(this.id, o.id) &&
Objects.equals(this.type, o.type) &&
Objects.equals(this.parameters, o.parameters) &&
Objects.equals(this.interval, o.interval);
public ActivityInstance(
long id,
String type,
Map<String, SerializedValue> parameters,
Interval interval,
Optional<ActivityDirectiveId> directiveId
) {
this(new ActivityInstanceId(id), type, parameters, interval, directiveId);
}

@Override
public int hashCode() {
return Objects.hash(this.id, this.type, this.parameters, this.interval);
public long id() {
return this.instanceId().id();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ActivitySpan(final String activityAlias) {
@Override
public Spans evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new Spans(Segment.of(activity.interval, Optional.of(new Spans.Metadata(activity))));
return new Spans(Segment.of(activity.interval(), Optional.of(new Spans.Metadata(activity))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Windows evaluate(final SimulationResults results, final Interval bounds,
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(bounds, false),
Segment.of(activity.interval, true)
Segment.of(activity.interval(), true)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DiscreteParameter(final String activityAlias, final String parameterName)
public DiscreteProfile evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new DiscreteProfile(
Segment.of(bounds, activity.parameters.get(this.parameterName))
Segment.of(bounds, activity.parameters().get(this.parameterName))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Windows evaluate(final SimulationResults results, final Interval bounds,
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(bounds, false),
Segment.of(Interval.at(activity.interval.end), true)
Segment.of(Interval.at(activity.interval().end), true)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String prettyPrint(final String prefix) {
public record MatchType(String type) implements TriFunction<ActivityInstance, SimulationResults, EvaluationEnvironment, Boolean> {
@Override
public Boolean apply(ActivityInstance act, SimulationResults results, EvaluationEnvironment env) {
return Objects.equals(act.type, type);
return Objects.equals(act.type(), type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public record ForEachActivityViolations(
public ConstraintResult evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
var violations = new ConstraintResult();
for (final var activity : results.activities) {
if (activity.type.equals(this.activityType)) {
if (activity.type().equals(this.activityType)) {
final var newEnvironment = new EvaluationEnvironment(
new HashMap<>(environment.activityInstances()),
environment.spansInstances(),
Expand All @@ -28,7 +28,7 @@ public ConstraintResult evaluate(final SimulationResults results, final Interval

final var newViolations = this.expression.evaluate(results, bounds, newEnvironment);
for (final var violation: newViolations.violations) {
violation.addActivityId(activity.id);
violation.addActivityId(activity.id());
}
violations = ConstraintResult.merge(violations, newViolations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public RealParameter(final String activityAlias, final String parameterName) {
@Override
public LinearProfile evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
final var parameter = activity.parameters.get(this.parameterName);
final var parameter = activity.parameters().get(this.parameterName);
final var value = parameter.asReal().orElseThrow(
() -> new InputMismatchException(
String.format("Activity parameter \"%s\" with value %s cannot be interpreted as real",
this.parameterName,
activity.parameters.get(parameterName).toString())));
activity.parameters().get(parameterName).toString())));

return new LinearProfile(
Segment.of(Interval.FOREVER, new LinearEquation(Duration.ZERO, value, 0.0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ConstraintResult evaluate(SimulationResults results, final Interval bound
for (final var span : reportedSpans) {
if (!Interval.intersect(span.interval(), expandedInterval).isEmpty()) {
violationIntervals.add(span.interval());
span.value().ifPresent(m -> violationActivityIds.add(m.activityInstance().id));
span.value().ifPresent(m -> violationActivityIds.add(m.activityInstance().id()));
}
}
if (this.algorithm == RollingThresholdAlgorithm.ExcessHull || this.algorithm == RollingThresholdAlgorithm.DeficitHull) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Windows evaluate(final SimulationResults results, final Interval bounds,
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(bounds, false),
Segment.of(Interval.at(activity.interval.start), true)
Segment.of(Interval.at(activity.interval().start), true)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public final class SimulatedActivityTest {
public final class ActivityInstanceTest {
@Test
public void testUnspecifiedArgInSimulatedActivity() {
final var schedule = SimulationUtility.buildSchedule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.util.Map;
import java.util.Optional;

public record SimulatedActivity(
public record ActivityInstance(
String type,
Map<String, SerializedValue> arguments,
Instant start,
Duration duration,
SimulatedActivityId parentId,
List<SimulatedActivityId> childIds,
ActivityInstanceId parentId,
List<ActivityInstanceId> childIds,
Optional<ActivityDirectiveId> directiveId,
SerializedValue computedAttributes
) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package gov.nasa.jpl.aerie.merlin.driver;

public record ActivityInstanceId(long id) {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public final class SimulationResults {
public final Duration duration;
public final Map<String, ResourceProfile<RealDynamics>> realProfiles;
public final Map<String, ResourceProfile<SerializedValue>> discreteProfiles;
public final Map<SimulatedActivityId, SimulatedActivity> simulatedActivities;
public final Map<SimulatedActivityId, UnfinishedActivity> unfinishedActivities;
public final Map<ActivityInstanceId, ActivityInstance> simulatedActivities;
public final Map<ActivityInstanceId, UnfinishedActivity> unfinishedActivities;
public final List<Triple<Integer, String, ValueSchema>> topics;
public final Map<Duration, List<EventGraph<EventRecord>>> events;

public SimulationResults(
final Map<String, ResourceProfile<RealDynamics>> realProfiles,
final Map<String, ResourceProfile<SerializedValue>> discreteProfiles,
final Map<SimulatedActivityId, SimulatedActivity> simulatedActivities,
final Map<SimulatedActivityId, UnfinishedActivity> unfinishedActivities,
final Map<ActivityInstanceId, ActivityInstance> simulatedActivities,
final Map<ActivityInstanceId, UnfinishedActivity> unfinishedActivities,
final Instant startTime,
final Duration duration,
final List<Triple<Integer, String, ValueSchema>> topics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record UnfinishedActivity(
String type,
Map<String, SerializedValue> arguments,
Instant start,
SimulatedActivityId parentId,
List<SimulatedActivityId> childIds,
ActivityInstanceId parentId,
List<ActivityInstanceId> childIds,
Optional<ActivityDirectiveId> directiveId
) { }
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import gov.nasa.jpl.aerie.merlin.driver.ActivityDirectiveId;
import gov.nasa.jpl.aerie.merlin.driver.MissionModel.SerializableTopic;
import gov.nasa.jpl.aerie.merlin.driver.SerializedActivity;
import gov.nasa.jpl.aerie.merlin.driver.SimulatedActivity;
import gov.nasa.jpl.aerie.merlin.driver.SimulatedActivityId;
import gov.nasa.jpl.aerie.merlin.driver.ActivityInstance;
import gov.nasa.jpl.aerie.merlin.driver.ActivityInstanceId;
import gov.nasa.jpl.aerie.merlin.driver.resources.SimulationResourceManager;
import gov.nasa.jpl.aerie.merlin.driver.SimulationResults;
import gov.nasa.jpl.aerie.merlin.driver.UnfinishedActivity;
Expand Down Expand Up @@ -712,8 +712,8 @@ public DirectiveDetail getDirectiveDetailsFromSpan(
public record SimulationActivityExtract(
Instant startTime,
Duration duration,
Map<SimulatedActivityId, SimulatedActivity> simulatedActivities,
Map<SimulatedActivityId, UnfinishedActivity> unfinishedActivities
Map<ActivityInstanceId, ActivityInstance> simulatedActivities,
Map<ActivityInstanceId, UnfinishedActivity> unfinishedActivities
) {}

private SpanInfo computeSpanInfo(
Expand Down Expand Up @@ -756,25 +756,25 @@ private HashMap<SpanId, ActivityDirectiveId> spanToActivityDirectiveId(
return activityDirectiveIds;
}

private HashMap<SpanId, SimulatedActivityId> spanToSimulatedActivities(
private HashMap<SpanId, ActivityInstanceId> spanToSimulatedActivities(
final SpanInfo spanInfo
) {
final var activityDirectiveIds = spanToActivityDirectiveId(spanInfo);
final var spanToSimulatedActivityId = new HashMap<SpanId, SimulatedActivityId>(activityDirectiveIds.size());
final var usedSimulatedActivityIds = new HashSet<>();
final var spanToActivityInstanceId = new HashMap<SpanId, ActivityInstanceId>(activityDirectiveIds.size());
final var usedActivityInstanceIds = new HashSet<>();
for (final var entry : activityDirectiveIds.entrySet()) {
spanToSimulatedActivityId.put(entry.getKey(), new SimulatedActivityId(entry.getValue().id()));
usedSimulatedActivityIds.add(entry.getValue().id());
spanToActivityInstanceId.put(entry.getKey(), new ActivityInstanceId(entry.getValue().id()));
usedActivityInstanceIds.add(entry.getValue().id());
}
long counter = 1L;
for (final var span : this.spans.keySet()) {
if (!spanInfo.isActivity(span)) continue;
if (spanToSimulatedActivityId.containsKey(span)) continue;
if (spanToActivityInstanceId.containsKey(span)) continue;

while (usedSimulatedActivityIds.contains(counter)) counter++;
spanToSimulatedActivityId.put(span, new SimulatedActivityId(counter++));
while (usedActivityInstanceIds.contains(counter)) counter++;
spanToActivityInstanceId.put(span, new ActivityInstanceId(counter++));
}
return spanToSimulatedActivityId;
return spanToActivityInstanceId;
}

/**
Expand Down Expand Up @@ -803,30 +803,30 @@ public SimulationActivityExtract computeActivitySimulationResults(
});

// Give every task corresponding to a child activity an ID that doesn't conflict with any root activity.
final var spanToSimulatedActivityId = spanToSimulatedActivities(spanInfo);
final var spanToActivityInstanceId = spanToSimulatedActivities(spanInfo);

final var simulatedActivities = new HashMap<SimulatedActivityId, SimulatedActivity>();
final var unfinishedActivities = new HashMap<SimulatedActivityId, UnfinishedActivity>();
final var simulatedActivities = new HashMap<ActivityInstanceId, ActivityInstance>();
final var unfinishedActivities = new HashMap<ActivityInstanceId, UnfinishedActivity>();
this.spans.forEach((span, state) -> {
if (!spanInfo.isActivity(span)) return;

final var activityId = spanToSimulatedActivityId.get(span);
final var activityId = spanToActivityInstanceId.get(span);
final var directiveId = activityDirectiveIds.get(span);

if (state.endOffset().isPresent()) {
final var inputAttributes = spanInfo.input().get(span);
final var outputAttributes = spanInfo.output().get(span);

simulatedActivities.put(activityId, new SimulatedActivity(
simulatedActivities.put(activityId, new ActivityInstance(
inputAttributes.getTypeName(),
inputAttributes.getArguments(),
startTime.plus(state.startOffset().in(Duration.MICROSECONDS), ChronoUnit.MICROS),
state.endOffset().get().minus(state.startOffset()),
spanToSimulatedActivityId.get(activityParents.get(span)),
spanToActivityInstanceId.get(activityParents.get(span)),
activityChildren
.getOrDefault(span, Collections.emptyList())
.stream()
.map(spanToSimulatedActivityId::get)
.map(spanToActivityInstanceId::get)
.toList(),
(activityParents.containsKey(span)) ? Optional.empty() : Optional.ofNullable(directiveId),
outputAttributes
Expand All @@ -837,11 +837,11 @@ public SimulationActivityExtract computeActivitySimulationResults(
inputAttributes.getTypeName(),
inputAttributes.getArguments(),
startTime.plus(state.startOffset().in(Duration.MICROSECONDS), ChronoUnit.MICROS),
spanToSimulatedActivityId.get(activityParents.get(span)),
spanToActivityInstanceId.get(activityParents.get(span)),
activityChildren
.getOrDefault(span, Collections.emptyList())
.stream()
.map(spanToSimulatedActivityId::get)
.map(spanToActivityInstanceId::get)
.toList(),
(activityParents.containsKey(span)) ? Optional.empty() : Optional.of(directiveId)
));
Expand All @@ -853,7 +853,7 @@ public SimulationActivityExtract computeActivitySimulationResults(
private TreeMap<Duration, List<EventGraph<EventRecord>>> createSerializedTimeline(
final TemporalEventSource combinedTimeline,
final Iterable<SerializableTopic<?>> serializableTopics,
final HashMap<SpanId, SimulatedActivityId> spanToActivities,
final HashMap<SpanId, ActivityInstanceId> spanToActivities,
final HashMap<SerializableTopic<?>, Integer> serializableTopicToId) {
final var serializedTimeline = new TreeMap<Duration, List<EventGraph<EventRecord>>>();
var time = Duration.ZERO;
Expand Down
Loading

0 comments on commit a505cf9

Please sign in to comment.