Skip to content

Commit

Permalink
Merge MissionModelService and PlanService
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienmaillard committed Sep 21, 2023
1 parent 5c76d41 commit 60e923f
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void main(final String[] args) {
final var schedulerService = new CachedSchedulerService(stores.results());
final var scheduleAction = new ScheduleAction(specificationService, schedulerService);

final var generateSchedulingLibAction = new GenerateSchedulingLibAction(merlinService, merlinService);
final var generateSchedulingLibAction = new GenerateSchedulingLibAction(merlinService);

//establish bindings to the service layers
final var bindings = new SchedulerBindings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import gov.nasa.jpl.aerie.json.SchemaCache;
import gov.nasa.jpl.aerie.scheduler.server.models.ActivityType;
import gov.nasa.jpl.aerie.scheduler.server.models.SchedulingDSL;
import gov.nasa.jpl.aerie.scheduler.server.services.MissionModelService;
import gov.nasa.jpl.aerie.scheduler.server.services.MerlinService;

import static gov.nasa.jpl.aerie.constraints.json.ConstraintParsers.profileExpressionP;
import static gov.nasa.jpl.aerie.constraints.json.ConstraintParsers.structExpressionF;
Expand All @@ -20,7 +20,7 @@ public class ActivityTemplateJsonParser implements JsonParser<SchedulingDSL.Acti

private final Map<String, ActivityType> activityTypesByName = new HashMap<>();

public ActivityTemplateJsonParser(MissionModelService.MissionModelTypes activityTypes){
public ActivityTemplateJsonParser(MerlinService.MissionModelTypes activityTypes){
activityTypes.activityTypes().forEach((actType)-> activityTypesByName.put(actType.name(), actType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import gov.nasa.jpl.aerie.scheduler.TimeUtility;
import gov.nasa.jpl.aerie.scheduler.constraints.timeexpressions.TimeAnchor;
import gov.nasa.jpl.aerie.scheduler.server.http.ActivityTemplateJsonParser;
import gov.nasa.jpl.aerie.scheduler.server.services.MissionModelService;
import gov.nasa.jpl.aerie.scheduler.server.services.MerlinService;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
Expand Down Expand Up @@ -57,7 +57,7 @@ public class SchedulingDSL {
$ -> tuple($.duration(), $.occurrence()));

private static JsonObjectParser<GoalSpecifier.RecurrenceGoalDefinition> recurrenceGoalDefinitionP(
MissionModelService.MissionModelTypes activityTypes)
MerlinService.MissionModelTypes activityTypes)
{
return productP
.field("activityTemplate", new ActivityTemplateJsonParser(activityTypes))
Expand Down Expand Up @@ -112,7 +112,7 @@ private static JsonObjectParser<GoalSpecifier.RecurrenceGoalDefinition> recurren
(TimingConstraint.ActivityTimingConstraintFlexibleRange $) -> tuple($.lowerBound(), $.upperBound(), $.singleton()));

private static final JsonObjectParser<GoalSpecifier.CoexistenceGoalDefinition> coexistenceGoalDefinitionP(
MissionModelService.MissionModelTypes activityTypes)
MerlinService.MissionModelTypes activityTypes)
{
return
productP
Expand Down Expand Up @@ -146,7 +146,7 @@ private static final JsonObjectParser<GoalSpecifier.CoexistenceGoalDefinition> c
}

private static final JsonObjectParser<GoalSpecifier.CardinalityGoalDefinition> cardinalityGoalDefinitionP(
MissionModelService.MissionModelTypes activityTypes) {
MerlinService.MissionModelTypes activityTypes) {
return
productP
.field("activityTemplate", new ActivityTemplateJsonParser(activityTypes))
Expand Down Expand Up @@ -195,7 +195,7 @@ private static JsonObjectParser<GoalSpecifier.GoalApplyWhen> goalApplyWhenF(fina
}


private static JsonParser<GoalSpecifier> goalSpecifierF(MissionModelService.MissionModelTypes missionModelTypes) {
private static JsonParser<GoalSpecifier> goalSpecifierF(MerlinService.MissionModelTypes missionModelTypes) {
return recursiveP(self -> SumParsers.sumP("kind", GoalSpecifier.class, List.of(
SumParsers.variant(
"ActivityRecurrenceGoal",
Expand Down Expand Up @@ -243,7 +243,7 @@ private static JsonObjectParser<ConditionSpecifier.AndCondition> conditionAndF(f
globalSchedulingConditionP)
)));

public static final JsonParser<GoalSpecifier> schedulingJsonP(MissionModelService.MissionModelTypes missionModelTypes){
public static final JsonParser<GoalSpecifier> schedulingJsonP(MerlinService.MissionModelTypes missionModelTypes){
return goalSpecifierF(missionModelTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class ConstraintsTypescriptCodeGenerationHelper
{
private ConstraintsTypescriptCodeGenerationHelper() { }

public static Map<String, TypescriptCodeGenerationService.ActivityType> activityTypes(final MissionModelService.MissionModelTypes missionModelTypes) {
public static Map<String, TypescriptCodeGenerationService.ActivityType> activityTypes(final MerlinService.MissionModelTypes missionModelTypes) {
return missionModelTypes
.activityTypes()
.stream()
Expand All @@ -27,7 +27,7 @@ public static Map<String, TypescriptCodeGenerationService.ActivityType> activity
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public static Map<String, ValueSchema> resources(final MissionModelService.MissionModelTypes missionModelTypes) {
public static Map<String, ValueSchema> resources(final MerlinService.MissionModelTypes missionModelTypes) {
return missionModelTypes
.resourceTypes()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import gov.nasa.jpl.aerie.scheduler.server.models.PlanId;

public record GenerateSchedulingLibAction(
MissionModelService missionModelService,
PlanService.ReaderRole planService
MerlinService.ReaderRole merlinService
) {
public GenerateSchedulingLibAction {
Objects.requireNonNull(planService);
Objects.requireNonNull(merlinService);
}

/**
Expand Down Expand Up @@ -46,10 +45,10 @@ public Response run(final MissionModelId missionModelId, final Optional<PlanId>
final var temporalPolyfillTypes = getTypescriptResource("constraints/TemporalPolyfillTypes.ts");


var missionModelTypes = missionModelService.getMissionModelTypes(missionModelId);
var missionModelTypes = merlinService.getMissionModelTypes(missionModelId);
if(planId.isPresent()) {
final var allResourceTypes = planService.getResourceTypes(planId.get());
missionModelTypes = new MissionModelService.MissionModelTypes(missionModelTypes.activityTypes(), allResourceTypes);
final var allResourceTypes = merlinService.getResourceTypes(planId.get());
missionModelTypes = new MerlinService.MissionModelTypes(missionModelTypes.activityTypes(), allResourceTypes);
}

final var generatedSchedulerCode = TypescriptCodeGenerationService.generateTypescriptTypesFromMissionModel(missionModelTypes);
Expand All @@ -66,7 +65,7 @@ public Response run(final MissionModelId missionModelId, final Optional<PlanId>
"file:///mission-model-generated-code.ts", generatedConstraintsCode,
"file:///%s".formatted(temporalPolyfillTypes.basename), temporalPolyfillTypes.source
));
} catch (final IOException | MissionModelService.MissionModelServiceException | PlanServiceException |
} catch (final IOException | PlanServiceException |
NoSuchPlanException | NoSuchMissionModelException e) {
return new Response.Failure(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@
*
* @param merlinGraphqlURI endpoint of the merlin graphql service that should be used to access all plan data
*/
public record GraphQLMerlinService(URI merlinGraphqlURI, String hasuraGraphQlAdminSecret) implements PlanService.OwnerRole,
MissionModelService
{
public record GraphQLMerlinService(URI merlinGraphqlURI, String hasuraGraphQlAdminSecret) implements MerlinService.OwnerRole {

/**
* timeout for http graphql requests issued to aerie
Expand Down Expand Up @@ -605,8 +603,8 @@ mutation createAllPlanActivityDirectives($activities: [activity_directive_insert
}

@Override
public MissionModelTypes getMissionModelTypes(final PlanId planId)
throws IOException, MissionModelServiceException
public MerlinService.MissionModelTypes getMissionModelTypes(final PlanId planId)
throws IOException, PlanServiceException
{
final var request = """
query GetActivityTypesForPlan {
Expand All @@ -626,11 +624,8 @@ public MissionModelTypes getMissionModelTypes(final PlanId planId)
}
""".formatted(planId.id());
final JsonObject response;
try {
response = postRequest(request).get();
} catch (PlanServiceException e) {
throw new MissionModelServiceException("Failed to get mission model types for plan id %s".formatted(planId), e);
}
response = postRequest(request).get();

final var activityTypesJsonArray =
response.getJsonObject("data")
.getJsonObject("plan_by_pk")
Expand All @@ -643,7 +638,7 @@ public MissionModelTypes getMissionModelTypes(final PlanId planId)
.getJsonObject("mission_model")
.getInt("id"));

return new MissionModelTypes(activityTypes, getResourceTypes(missionModelId));
return new MerlinService.MissionModelTypes(activityTypes, getResourceTypes(missionModelId));
}

private static List<ActivityType> parseActivityTypes(final JsonArray activityTypesJsonArray) {
Expand Down Expand Up @@ -684,8 +679,8 @@ private static List<ActivityType> parseActivityTypes(final JsonArray activityTyp
}

@Override
public MissionModelTypes getMissionModelTypes(final MissionModelId missionModelId)
throws IOException, MissionModelServiceException, NoSuchMissionModelException
public MerlinService.MissionModelTypes getMissionModelTypes(final MissionModelId missionModelId)
throws IOException, NoSuchMissionModelException, PlanServiceException
{
final var request = """
query GetActivityTypesFromMissionModel{
Expand All @@ -702,23 +697,19 @@ public MissionModelTypes getMissionModelTypes(final MissionModelId missionModelI
}
""".formatted(missionModelId.id());
final JsonObject response;
try {
response = postRequest(request).get();
} catch (PlanServiceException e) {
throw new MissionModelServiceException("Failed to get mission model types for model id %s".formatted(missionModelId), e);
}
response = postRequest(request).get();
final var data = response.getJsonObject("data");
if (data.get("mission_model_by_pk").getValueType().equals(JsonValue.ValueType.NULL)) throw new NoSuchMissionModelException(missionModelId);
final var activityTypesJsonArray = data
.getJsonObject("mission_model_by_pk")
.getJsonArray("activity_types");
final var activityTypes = parseActivityTypes(activityTypesJsonArray);

return new MissionModelTypes(activityTypes, getResourceTypes(missionModelId));
return new MerlinService.MissionModelTypes(activityTypes, getResourceTypes(missionModelId));
}

public Collection<ResourceType> getResourceTypes(final MissionModelId missionModelId)
throws IOException, MissionModelServiceException
throws IOException, PlanServiceException
{
final var request = """
query GetResourceTypes {
Expand All @@ -729,11 +720,7 @@ public Collection<ResourceType> getResourceTypes(final MissionModelId missionMod
}
""".formatted(missionModelId.id());
final JsonObject response;
try {
response = postRequest(request).get();
} catch (PlanServiceException e) {
throw new MissionModelServiceException("Failed to get mission model types for model id %s".formatted(missionModelId), e);
}
response = postRequest(request).get();
final var data = response.getJsonObject("data");
final var resourceTypesJsonArray = data.getJsonArray("resourceTypes");

Expand Down Expand Up @@ -761,7 +748,7 @@ public Collection<ResourceType> getResourceTypes(final MissionModelId missionMod
*/
@Override
public Collection<ResourceType> getResourceTypes(final PlanId planId)
throws IOException, MissionModelServiceException, PlanServiceException, NoSuchPlanException
throws IOException, PlanServiceException, NoSuchPlanException
{
final var missionModelId = this.getPlanMetadata(planId).modelId();
final var missionModelResourceTypes = getResourceTypes(new MissionModelId(missionModelId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import gov.nasa.jpl.aerie.scheduler.model.SchedulingActivityDirective;
import gov.nasa.jpl.aerie.scheduler.model.SchedulingActivityDirectiveId;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchActivityInstanceException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchMissionModelException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchPlanException;
import gov.nasa.jpl.aerie.scheduler.server.http.InvalidJsonException;
import gov.nasa.jpl.aerie.scheduler.server.models.ActivityType;
import gov.nasa.jpl.aerie.scheduler.server.models.DatasetId;
import gov.nasa.jpl.aerie.scheduler.server.models.ExternalProfiles;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.MerlinPlan;
import gov.nasa.jpl.aerie.scheduler.server.models.MissionModelId;
import gov.nasa.jpl.aerie.scheduler.server.models.PlanId;
import gov.nasa.jpl.aerie.scheduler.server.models.PlanMetadata;
import gov.nasa.jpl.aerie.scheduler.server.models.ResourceType;
Expand All @@ -26,8 +29,17 @@
import java.util.Map;
import java.util.Optional;

public interface PlanService {
public interface MerlinService {
record MissionModelTypes(Collection<ActivityType> activityTypes, Collection<ResourceType> resourceTypes) {}

interface ReaderRole {

MerlinService.MissionModelTypes getMissionModelTypes(final PlanId planId)
throws IOException, PlanServiceException;
MerlinService.MissionModelTypes getMissionModelTypes(final MissionModelId missionModelId)
throws IOException, PlanServiceException,
NoSuchMissionModelException;

/**
* fetch current revision number of the target plan stored in aerie
*
Expand Down Expand Up @@ -91,12 +103,11 @@ ExternalProfiles getExternalProfiles(final PlanId planId)
* Gets resource types associated to a plan, those coming from the mission model as well as those coming from external dataset resources
* @param planId the plan id
* @throws IOException
* @throws MissionModelService.MissionModelServiceException
* @throws PlanServiceException
* @throws NoSuchPlanException
*/
Collection<ResourceType> getResourceTypes(final PlanId planId)
throws IOException, MissionModelService.MissionModelServiceException, PlanServiceException, NoSuchPlanException;
throws IOException, PlanServiceException, NoSuchPlanException;
}

interface WriterRole {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public final class TypescriptCodeGenerationService {
private TypescriptCodeGenerationService() { }

public static String generateTypescriptTypesFromMissionModel(final MissionModelService.MissionModelTypes missionModelTypes) {
public static String generateTypescriptTypesFromMissionModel(final MerlinService.MissionModelTypes missionModelTypes) {
final var activityTypeCodes = new ArrayList<ActivityTypeCode>();
for (final var activityType : missionModelTypes.activityTypes()) {
activityTypeCodes.add(getActivityTypeInformation(activityType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

public final class TypescriptCodeGenerationServiceTestFixtures {

public static final MissionModelService.MissionModelTypes MISSION_MODEL_TYPES =
new MissionModelService.MissionModelTypes(
public static final MerlinService.MissionModelTypes MISSION_MODEL_TYPES =
new MerlinService.MissionModelTypes(
List.of(
new ActivityType(
"SampleActivity1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public static void main(String[] args) throws Exception {

final var specificationService = new LocalSpecificationService(stores.specifications());
final var scheduleAgent = new SynchronousSchedulerAgent(specificationService,
merlinService,
merlinService,
config.merlinFileStore(),
config.missionRuleJarPath(),
Expand Down
Loading

0 comments on commit 60e923f

Please sign in to comment.