Skip to content

Commit

Permalink
Merge pull request #1507 from NASA-AMMOS/feat/stream-sim-results
Browse files Browse the repository at this point in the history
Stream Simulation Resources
  • Loading branch information
Mythicaeda authored Jul 24, 2024
2 parents 7b13d58 + 6861a47 commit 8d29fd9
Show file tree
Hide file tree
Showing 47 changed files with 1,696 additions and 1,205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* A daemon task that tracks the number of minutes since plan start
*/
public class TimeTrackerDaemon {
private Counter<Integer> minutesElapsed;
private final Counter<Integer> minutesElapsed;

public int getMinutesElapsed() {
return minutesElapsed.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gov.nasa.jpl.aerie.merlin.driver.ActivityDirective;
import gov.nasa.jpl.aerie.merlin.driver.ActivityDirectiveId;
import gov.nasa.jpl.aerie.merlin.driver.CachedEngineStore;
import gov.nasa.jpl.aerie.merlin.driver.CachedSimulationEngine;
import gov.nasa.jpl.aerie.merlin.driver.CheckpointSimulationDriver;
import gov.nasa.jpl.aerie.merlin.driver.DirectiveTypeRegistry;
import gov.nasa.jpl.aerie.merlin.driver.MissionModel;
Expand Down Expand Up @@ -34,17 +35,17 @@
public class FooSimulationDuplicationTest {
CachedEngineStore store;
final private class InfiniteCapacityEngineStore implements CachedEngineStore{
private final Map<SimulationEngineConfiguration, List<CheckpointSimulationDriver.CachedSimulationEngine>> store = new HashMap<>();
private final Map<SimulationEngineConfiguration, List<CachedSimulationEngine>> store = new HashMap<>();
@Override
public void save(
final CheckpointSimulationDriver.CachedSimulationEngine cachedSimulationEngine,
final CachedSimulationEngine cachedSimulationEngine,
final SimulationEngineConfiguration configuration) {
store.computeIfAbsent(configuration, conf -> new ArrayList<>());
store.get(configuration).add(cachedSimulationEngine);
}

@Override
public List<CheckpointSimulationDriver.CachedSimulationEngine> getCachedEngines(final SimulationEngineConfiguration configuration) {
public List<CachedSimulationEngine> getCachedEngines(final SimulationEngineConfiguration configuration) {
return store.get(configuration);
}

Expand Down Expand Up @@ -99,8 +100,7 @@ void testCompareCheckpointOnEmptyPlan() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);
assertResultsEqual(expected, results);
}

Expand Down Expand Up @@ -128,8 +128,7 @@ void testFooNonEmptyPlan() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);
assertResultsEqual(expected, results);

assertEquals(Duration.of(5, MINUTES), store.getCachedEngines(mockConfiguration()).getFirst().endsAt());
Expand Down Expand Up @@ -170,8 +169,7 @@ void testFooNonEmptyPlanMultipleResumes() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);
assertResultsEqual(expected, results);

assertEquals(Duration.of(5, MINUTES), store.getCachedEngines(mockConfiguration()).getFirst().endsAt());
Expand Down Expand Up @@ -223,8 +221,7 @@ void testFooNonEmptyPlanMultipleCheckpointsMultipleResumes() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);
assertResultsEqual(expected, results);

assertEquals(Duration.of(5, MINUTES), store.getCachedEngines(mockConfiguration()).getFirst().endsAt());
Expand Down Expand Up @@ -285,8 +282,7 @@ void testFooNonEmptyPlanMultipleCheckpointsMultipleResumesWithEdits() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);

final SimulationResults expected2 = SimulationDriver.simulate(
missionModel,
Expand All @@ -295,8 +291,7 @@ void testFooNonEmptyPlanMultipleCheckpointsMultipleResumesWithEdits() {
Duration.HOUR,
Instant.EPOCH,
Duration.HOUR,
() -> false,
$ -> {});
() -> false);

assertResultsEqual(expected1, results);

Expand Down Expand Up @@ -374,7 +369,7 @@ static void assertResultsEqual(SimulationResults expected, SimulationResults act

static SimulationResults simulateWithCheckpoints(
final MissionModel<?> missionModel,
final CheckpointSimulationDriver.CachedSimulationEngine cachedSimulationEngine,
final CachedSimulationEngine cachedSimulationEngine,
final List<Duration> desiredCheckpoints,
final Map<ActivityDirectiveId, ActivityDirective> schedule,
final CachedEngineStore cachedEngineStore,
Expand Down Expand Up @@ -413,7 +408,7 @@ static SimulationResults simulateWithCheckpoints(
Duration.HOUR,
$ -> {},
() -> false,
CheckpointSimulationDriver.CachedSimulationEngine.empty(missionModel),
CachedSimulationEngine.empty(missionModel, Instant.EPOCH),
CheckpointSimulationDriver.desiredCheckpoints(desiredCheckpoints),
CheckpointSimulationDriver.noCondition(),
cachedEngineStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ void simulateWithMapSchedule() {

simulationResults.realProfiles.forEach((name, samples) -> {
System.out.println(name + ":");
samples.getRight().forEach(point -> System.out.format("\t%s\t%s\n", point.extent(), point.dynamics()));
samples.segments().forEach(point -> System.out.format("\t%s\t%s\n", point.extent(), point.dynamics()));
});

simulationResults.discreteProfiles.forEach((name, samples) -> {
System.out.println(name + ":");
samples.getRight().forEach(point -> System.out.format("\t%s\t%s\n", point.extent(), point.dynamics()));
samples.segments().forEach(point -> System.out.format("\t%s\t%s\n", point.extent(), point.dynamics()));
});

simulationResults.simulatedActivities.forEach((name, activity) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import java.util.List;

public interface CachedEngineStore {
void save(final CheckpointSimulationDriver.CachedSimulationEngine cachedSimulationEngine,
void save(final CachedSimulationEngine cachedSimulationEngine,
final SimulationEngineConfiguration configuration);
List<CheckpointSimulationDriver.CachedSimulationEngine> getCachedEngines(
List<CachedSimulationEngine> getCachedEngines(
final SimulationEngineConfiguration configuration);

int capacity();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gov.nasa.jpl.aerie.merlin.driver;

import gov.nasa.jpl.aerie.merlin.driver.engine.SimulationEngine;
import gov.nasa.jpl.aerie.merlin.driver.engine.SpanException;
import gov.nasa.jpl.aerie.merlin.driver.resources.InMemorySimulationResourceManager;
import gov.nasa.jpl.aerie.merlin.protocol.driver.Topic;
import gov.nasa.jpl.aerie.merlin.protocol.types.Duration;

import java.time.Instant;
import java.util.Map;

public record CachedSimulationEngine(
Duration endsAt,
Map<ActivityDirectiveId, ActivityDirective> activityDirectives,
SimulationEngine simulationEngine,
Topic<ActivityDirectiveId> activityTopic,
MissionModel<?> missionModel,
InMemorySimulationResourceManager resourceManager
) {
public void freeze() {
simulationEngine.close();
}

public static CachedSimulationEngine empty(final MissionModel<?> missionModel, final Instant simulationStartTime) {
final SimulationEngine engine = new SimulationEngine(missionModel.getInitialCells());

// Specify a topic on which tasks can log the activity they're associated with.
final var activityTopic = new Topic<ActivityDirectiveId>();
try {
engine.init(missionModel.getResources(), missionModel.getDaemon());

return new CachedSimulationEngine(
Duration.MIN_VALUE,
Map.of(),
engine,
new Topic<>(),
missionModel,
new InMemorySimulationResourceManager()
);
} catch (SpanException ex) {
// Swallowing the spanException as the internal `spanId` is not user meaningful info.
final var topics = missionModel.getTopics();
final var directiveId = engine.getDirectiveIdFromSpan(activityTopic, topics, ex.spanId);
if (directiveId.isPresent()) {
throw new SimulationException(Duration.ZERO, simulationStartTime, directiveId.get(), ex.cause);
}
throw new SimulationException(Duration.ZERO, simulationStartTime, ex.cause);
} catch (Throwable ex) {
throw new SimulationException(Duration.ZERO, simulationStartTime, ex);
}
}
}
Loading

0 comments on commit 8d29fd9

Please sign in to comment.