Skip to content

Commit

Permalink
Move args to goal invocationr record and represent them as Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdailis committed Sep 6, 2024
1 parent cbbafd0 commit 9bb0286
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static gov.nasa.jpl.aerie.scheduler.plan.InMemoryEditablePlan.toSchedulingActivity;

public class Procedure extends Goal {
private final Path jarPath;
private final SerializedValue args;
private final Map<String, SerializedValue> args;

public Procedure(final PlanningHorizon planningHorizon, Path jarPath, SerializedValue args, boolean simulateAfter) {
public Procedure(final PlanningHorizon planningHorizon, Path jarPath, Map<String, SerializedValue> args, boolean simulateAfter) {
this.simulateAfter = simulateAfter;
this.planHorizon = planningHorizon;
this.jarPath = jarPath;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void run(Evaluation eval, Plan plan, MissionModel<?> missionModel, Functi
lookupActivityType::apply
);

procedureMapper.deserialize(this.args).run(editablePlan);
procedureMapper.deserialize(SerializedValue.of(this.args)).run(editablePlan);

if (!editablePlan.getUncommittedChanges().isEmpty()) {
throw new IllegalStateException("procedural goal %s had changes that were not committed or rolled back".formatted(jarPath.getFileName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import gov.nasa.jpl.aerie.types.MissionModelId;
import gov.nasa.jpl.aerie.types.Timestamp;

import javax.json.Json;
import javax.json.stream.JsonParsingException;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;

import static gov.nasa.jpl.aerie.json.BasicParsers.anyP;
Expand Down Expand Up @@ -121,4 +125,16 @@ private static <I extends HasuraAction.Input> JsonParser<HasuraAction<I>> hasura
.map(
untuple(HasuraAction.HasuraSchedulingGoalEvent::new),
$ -> tuple($.goalId(), $.revision()));

public static <T> T parseJson(final String jsonStr, final JsonParser<T> parser)
throws InvalidJsonException, InvalidEntityException
{
try (final var reader = Json.createReader(new StringReader(jsonStr))) {
final var requestJson = reader.readValue();
final var result = parser.parse(requestJson);
return result.getSuccessOrThrow(reason -> new InvalidEntityException(List.of(reason)));
} catch (JsonParsingException e) {
throw new InvalidJsonException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gov.nasa.jpl.aerie.scheduler.server.models;

import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;

import java.util.Map;

public record GoalInvocationRecord(
GoalId id,
String name,
GoalType type,
Map<String, SerializedValue> args,
boolean simulateAfter) {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gov.nasa.jpl.aerie.scheduler.server.models;

import javax.json.JsonObject;
import java.nio.file.Path;

public sealed interface GoalType {
record EDSL(GoalSource source) implements GoalType {}
record JAR(Path path, String args) implements GoalType {}
record JAR(Path path) implements GoalType {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ record GoalApplyWhen(
) implements GoalSpecifier {}
record Procedure(
Path jarPath,
SerializedValue arguments
Map<String, SerializedValue> arguments
) implements GoalSpecifier {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public record Specification(
Timestamp horizonEndTimestamp,
Map<String, SerializedValue> simulationArguments,
boolean analysisOnly,
List<GoalRecord> goalsByPriority,
List<GoalInvocationRecord> goalsByPriority,
List<SchedulingConditionRecord> schedulingConditions
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSpecificationException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.SpecificationLoadException;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import gov.nasa.jpl.aerie.scheduler.server.models.Specification;
import gov.nasa.jpl.aerie.scheduler.server.models.SpecificationId;
import gov.nasa.jpl.aerie.scheduler.server.remotes.postgres.SpecificationRevisionData;
Expand All @@ -15,6 +15,6 @@ public interface SpecificationRepository {
Specification getSpecification(SpecificationId specificationId)
throws NoSuchSpecificationException, SpecificationLoadException;
SpecificationRevisionData getSpecificationRevisionData(SpecificationId specificationId) throws NoSuchSpecificationException;
GoalRecord getGoal(GoalId goalId) throws NoSuchSchedulingGoalException;
GoalType getGoal(GoalId goalId) throws NoSuchSchedulingGoalException;
void updateGoalParameterSchema(GoalId goalId, ValueSchema schema);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gov.nasa.jpl.aerie.scheduler.server.remotes.postgres;

import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalSource;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import org.intellij.lang.annotations.Language;
Expand All @@ -14,7 +13,7 @@

/*package-local*/ final class GetSchedulingGoalAction implements AutoCloseable {
private final @Language("SQL") String sql = """
select gd.goal_id, gd.revision, gm.name, gd.definition, gd.type, encode(f.path, 'escape') as path
select gd.definition, gd.type, encode(f.path, 'escape') as path
from scheduler.scheduling_goal_definition gd
left join scheduler.scheduling_goal_metadata gm on gd.goal_id = gm.id
left join merlin.uploaded_file f on gd.uploaded_jar_id = f.id
Expand All @@ -27,24 +26,18 @@ public GetSchedulingGoalAction(final Connection connection) throws SQLException
this.statement = connection.prepareStatement(sql);
}

public Optional<GoalRecord> get(final GoalId goalId) throws SQLException {
public Optional<GoalType> get(final GoalId goalId) throws SQLException {
this.statement.setLong(1, goalId.id());
this.statement.setLong(2, goalId.revision());
final var resultSet = this.statement.executeQuery();

if (!resultSet.next()) return Optional.empty();

final var name = resultSet.getString("name");
final var definition = resultSet.getString("definition");
final var type = resultSet.getString("type");
final var path = resultSet.getString("path");

return Optional.of(new GoalRecord(
goalId,
name,
type.equals("JAR") ? new GoalType.JAR(Path.of(path), "" /* TODO this is a property of the specification, not the goal */) : new GoalType.EDSL(new GoalSource(definition)),
true // TODO this is not a property of the goal, but rather of the specification
));
return Optional.of(type.equals("JAR") ? new GoalType.JAR(Path.of(path)) : new GoalType.EDSL(new GoalSource(definition)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gov.nasa.jpl.aerie.scheduler.server.remotes.postgres;

import gov.nasa.jpl.aerie.merlin.driver.json.SerializedValueJsonParser;
import gov.nasa.jpl.aerie.scheduler.server.http.InvalidEntityException;
import gov.nasa.jpl.aerie.scheduler.server.http.InvalidJsonException;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalInvocationRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalSource;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import org.intellij.lang.annotations.Language;
Expand All @@ -14,6 +17,8 @@
import java.util.List;
import java.util.Optional;

import static gov.nasa.jpl.aerie.scheduler.server.http.SchedulerParsers.parseJson;

/*package-local*/ final class GetSpecificationGoalsAction implements AutoCloseable {
private final @Language("SQL") String sql = """
select s.goal_id, gd.revision, gm.name, gd.definition, s.goal_invocation_id, s.simulate_after, gd.type, encode(f.path, 'escape') as path, s.arguments
Expand All @@ -37,30 +42,36 @@ public GetSpecificationGoalsAction(final Connection connection) throws SQLExcept
this.statement = connection.prepareStatement(sql);
}

public List<GoalRecord> get(final long specificationId) throws SQLException {
public List<GoalInvocationRecord> get(final long specificationId) throws SQLException {
this.statement.setLong(1, specificationId);
final var resultSet = this.statement.executeQuery();

final var goals = new ArrayList<GoalRecord>();
while (resultSet.next()) {
final var id = resultSet.getLong("goal_id");
final var goalInvocationId = resultSet.getLong("goal_invocation_id");
final var revision = resultSet.getLong("revision");
final var name = resultSet.getString("name");
final var definition = resultSet.getString("definition");
final var simulateAfter = resultSet.getBoolean("simulate_after");
final var type = resultSet.getString("type");
final var path = resultSet.getString("path");
final var args = resultSet.getString("arguments");
goals.add(new GoalRecord(
try {

final var goals = new ArrayList<GoalInvocationRecord>();
while (resultSet.next()) {
final var id = resultSet.getLong("goal_id");
final var goalInvocationId = resultSet.getLong("goal_invocation_id");
final var revision = resultSet.getLong("revision");
final var name = resultSet.getString("name");
final var definition = resultSet.getString("definition");
final var simulateAfter = resultSet.getBoolean("simulate_after");
final var type = resultSet.getString("type");
final var path = resultSet.getString("path");
final var args = parseJson(resultSet.getString("arguments"), new SerializedValueJsonParser());

goals.add(new GoalInvocationRecord(
new GoalId(id, revision, Optional.of(goalInvocationId)),
name,
type.equals("JAR") ? new GoalType.JAR(Path.of(path), args) : new GoalType.EDSL(new GoalSource(definition)),
type.equals("JAR") ? new GoalType.JAR(Path.of(path)) : new GoalType.EDSL(new GoalSource(definition)),
args.asMap().get(),
simulateAfter
));
));
}
return goals;
} catch (InvalidJsonException | InvalidEntityException e) {
throw new RuntimeException(e);
}

return goals;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSchedulingGoalException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSpecificationException;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import gov.nasa.jpl.aerie.scheduler.server.models.SchedulingConditionRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalInvocationRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.PlanId;
import gov.nasa.jpl.aerie.scheduler.server.models.Specification;
import gov.nasa.jpl.aerie.scheduler.server.models.SpecificationId;
Expand All @@ -28,7 +29,7 @@ public Specification getSpecification(final SpecificationId specificationId)
{
final SpecificationRecord specificationRecord;
final PlanId planId;
final List<GoalRecord> goals;
final List<GoalInvocationRecord> goals;
final List<SchedulingConditionRecord> schedulingConditions;
try (final var connection = this.dataSource.getConnection();
final var getSpecificationAction = new GetSpecificationAction(connection);
Expand Down Expand Up @@ -77,7 +78,7 @@ public SpecificationRevisionData getSpecificationRevisionData(final Specificatio
}

@Override
public GoalRecord getGoal(final GoalId goalId) throws NoSuchSchedulingGoalException {
public GoalType getGoal(final GoalId goalId) throws NoSuchSchedulingGoalException {
try (final var connection = this.dataSource.getConnection()) {
try (final var getGoalAction = new GetSchedulingGoalAction(connection)) {
return getGoalAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
import gov.nasa.jpl.aerie.merlin.driver.json.ValueSchemaJsonParser;
import gov.nasa.jpl.aerie.merlin.protocol.types.ValueSchema;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import org.intellij.lang.annotations.Language;

import java.nio.file.Path;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Optional;

/*package-local*/ final class UpdateSchedulingGoalParameterSchemaAction implements AutoCloseable {
private final @Language("SQL") String sql = """
update scheduler.scheduling_goal_definition gd
set parameter_schema=?::jsonb
set parameter_schema=?::jsonb
where gd.goal_id = ? and gd.revision = ?;
""";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package gov.nasa.jpl.aerie.scheduler.server.services;

import gov.nasa.jpl.aerie.merlin.driver.DirectiveTypeRegistry;
import gov.nasa.jpl.aerie.merlin.driver.MissionModelLoader;
import gov.nasa.jpl.aerie.merlin.protocol.model.ModelType;
import gov.nasa.ammos.aerie.procedural.scheduling.ProcedureMapper;
import gov.nasa.jpl.aerie.scheduler.ProcedureLoader;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSchedulingGoalException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSpecificationException;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.SpecificationLoadException;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalId;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalRecord;
import gov.nasa.jpl.aerie.scheduler.server.models.GoalType;
import gov.nasa.jpl.aerie.scheduler.server.models.Specification;
import gov.nasa.jpl.aerie.scheduler.server.models.SpecificationId;
import gov.nasa.jpl.aerie.scheduler.server.remotes.SpecificationRepository;
import gov.nasa.jpl.aerie.scheduler.server.remotes.postgres.SpecificationRevisionData;

import java.nio.file.Path;
import java.util.HashMap;

public record SpecificationService(SpecificationRepository specificationRepository) {
// Queries
Expand All @@ -34,13 +29,13 @@ public SpecificationRevisionData getSpecificationRevisionData(final Specificatio
}

public void refreshSchedulingProcedureParameterTypes(long goalId, long revision) {
final GoalRecord goal;
final GoalType goal;
try {
goal = specificationRepository.getGoal(new GoalId(goalId, revision));
} catch (NoSuchSchedulingGoalException e) {
throw new RuntimeException(e);
}
switch (goal.type()) {
switch (goal) {
case GoalType.EDSL edsl -> {
// Do nothing
}
Expand Down
Loading

0 comments on commit 9bb0286

Please sign in to comment.