Skip to content

Commit

Permalink
add source support WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-super committed Oct 23, 2024
1 parent 9510d24 commit c2a5b48
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gov.nasa.jpl.aerie.e2e.procedural.scheduling.procedures;

import gov.nasa.ammos.aerie.procedural.scheduling.Goal;
import gov.nasa.ammos.aerie.procedural.scheduling.annotations.SchedulingProcedure;
import gov.nasa.ammos.aerie.procedural.scheduling.plan.EditablePlan;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalSource;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart;
import gov.nasa.ammos.aerie.procedural.timeline.plan.EventQuery;
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

@SchedulingProcedure
public record ExternalEventsSourceQueryGoal() implements Goal {
@Override
public void run(@NotNull final EditablePlan plan) {

// demonstrate more complicated query functionality
EventQuery eventQuery = new EventQuery(
null,
null,
List.of(new ExternalSource("NewTest.json", "TestGroup_2"))
);

for (final var e: plan.events(eventQuery)) {
plan.create("BiteBanana", new DirectiveStart.Absolute(e.getInterval().start), Map.of("biteSize", SerializedValue.of(1)));
}
plan.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Map;

@SchedulingProcedure
public record ExternalEventsQueryGoal() implements Goal {
public record ExternalEventsTypeQueryGoal() implements Goal {
@Override
public void run(@NotNull final EditablePlan plan) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ void testExternalEventSimple() throws IOException {
}

@Test
void testExternalEventQuery() throws IOException {
void testExternalEventTypeQuery() throws IOException {
// first, run the goal
try (final var gateway = new GatewayRequests(playwright)) {
int procedureJarId = gateway.uploadJarFile("build/libs/ExternalEventsQueryGoal.jar");
int procedureJarId = gateway.uploadJarFile("build/libs/ExternalEventsTypeQueryGoal.jar");
// Add Scheduling Procedure
procedureId = hasura.createSchedulingSpecProcedure(
"Test Scheduling Procedure",
Expand Down Expand Up @@ -205,4 +205,35 @@ void testExternalEventQuery() throws IOException {
assertEquals(activityStartTime.toString(), expected.get(i).start_time());
}
}

@Test
void testExternalEventSourceQuery() throws IOException {
// first, run the goal
try (final var gateway = new GatewayRequests(playwright)) {
int procedureJarId = gateway.uploadJarFile("build/libs/ExternalEventsSourceQueryGoal.jar");
// Add Scheduling Procedure
procedureId = hasura.createSchedulingSpecProcedure(
"Test Scheduling Procedure",
procedureJarId,
specId,
0
);
}
hasura.awaitScheduling(specId);
final var plan = hasura.getPlan(planId);
final var activities = plan.activityDirectives();

// ensure the orderings line up
activities.sort(Comparator.comparing(Plan.ActivityDirective::startOffset));

// compare arrays
assertEquals(additionalExternalEvents.size(), activities.size());
for (int i = 0; i < activities.size(); i++) {
Instant activityStartTime = Duration.addToInstant(
Instant.parse(planStartTimestamp),
Duration.fromString(activities.get(i).startOffset())
);
assertEquals(activityStartTime.toString(), additionalExternalEvents.get(i).start_time());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.Instance
import gov.nasa.ammos.aerie.procedural.timeline.ops.*
import gov.nasa.ammos.aerie.procedural.timeline.ops.coalesce.CoalesceNoOp
import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalEvent
import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalSource
import gov.nasa.ammos.aerie.procedural.timeline.util.preprocessList

/**
Expand All @@ -23,8 +24,8 @@ data class ExternalEvents(private val timeline: Timeline<ExternalEvent, External
fun filterByType(vararg types: String) = filter { it.type in types }

/** Filter by one or more event sources. */
fun filterBySource(vararg sources: String) = filter { it.source in sources }
fun filterBySource(vararg sources: ExternalSource) = filter { it.source in sources }

/** Filter by one or more derivation groups. */
fun filterByDerivationGroup(vararg groups: String) = filter { it.derivationGroup in groups }
fun filterByDerivationGroup(vararg groups: String) = filter { it.source.derivationGroup in groups }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ data class ExternalEvent(
/** The type of the event. */
val type: String,
/** The source this event comes from. */
val source: String,
/** The derivation group that this event comes from. */
val derivationGroup: String,
val source: ExternalSource,
override val interval: Interval,
): IntervalLike<ExternalEvent> {
override fun withNewInterval(i: Interval) = ExternalEvent(key, type, source, derivationGroup, i)
override fun withNewInterval(i: Interval) = ExternalEvent(key, type, source, i)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gov.nasa.ammos.aerie.procedural.timeline.payloads

import gov.nasa.ammos.aerie.procedural.timeline.Interval
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue

/**
* An external source instance. Used for querying purposes - see EventQuery.kt.
* The included fields represent the primary key used to identify External Sources.
*/
data class ExternalSource(
/** The string name of this source. */
val key: String,
/** The derivation group that this source is a member of. */
val derivationGroup: String,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gov.nasa.ammos.aerie.procedural.timeline.plan

import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalSource

/** Fields for filtering events as they are queried. */
data class EventQuery(
/**
Expand All @@ -17,13 +19,14 @@ data class EventQuery(
val eventTypes: List<String>?,

/**
* A nullable list of sources; the event must belong to one of them if present.
* A nullable list of sources (described as a tuple of the source's (key, derivation group name)); the event must
* belong to one of them if present.
*
* If null, all sources are allowed.
*/
val sources: List<String>?,
val sources: List<ExternalSource>?,
) {
constructor(derivationGroup: String?, eventType: String?, source: String?): this(
constructor(derivationGroup: String?, eventType: String?, source: ExternalSource?): this(
derivationGroup?.let { listOf(it) },
eventType?.let { listOf(it) },
source?.let { listOf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.MICROSECOND;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.ZERO;
import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.min;

/**
* prototype scheduling algorithm that schedules activities for a plan
Expand Down Expand Up @@ -618,7 +617,7 @@ private Optional<MissingActivityNetworkConflict> makeActivityNetworkConflict(
curPreviouslyInstantiated = null;
}
taskNetwork.startsAfterStart(
allActivitiesInNetwork.getLast(),
allActivitiesInNetwork.get(allActivitiesInNetwork.size()),
activityName,
missingRecurrenceConflict.minMaxConstraints.start,
missingRecurrenceConflict.minMaxConstraints.end);
Expand All @@ -635,12 +634,12 @@ private Optional<MissingActivityNetworkConflict> makeActivityNetworkConflict(
//add constraints between last task and end boundary
if (missingRecurrenceConflict.afterBoundIsActivity) {
taskNetwork.addStartInterval(
allActivitiesInNetwork.getLast(),
allActivitiesInNetwork.get(allActivitiesInNetwork.size()),
missingRecurrenceConflict.nextStart.minus(missingRecurrenceConflict.minMaxConstraints.end),
missingRecurrenceConflict.nextStart.minus(missingRecurrenceConflict.minMaxConstraints.start));
} else {
taskNetwork.addStartInterval(
allActivitiesInNetwork.getLast(),
allActivitiesInNetwork.get(allActivitiesInNetwork.size()),
missingRecurrenceConflict.nextStart.minus(missingRecurrenceConflict.minMaxConstraints.end) ,
missingRecurrenceConflict.nextStart);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ data class SchedulerToProcedurePlanAdapter(
}
else eventsByDerivationGroup.values.flatten()
if (query.eventTypes != null) result = result.filter { it.type in query.eventTypes!! }
if (query.sources != null) result = result.filter { it.source in query.sources!! }
if (query.sources != null) result = result.filter { event ->
query.sources!!.map { it.key }.contains(event.source.key) && query.sources!!.map { it.derivationGroup }.contains(event.source.key)
}
return ExternalEvents(result)
}
override fun <V : Any, TL : SerialSegmentOps<V, TL>> resource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gov.nasa.ammos.aerie.procedural.timeline.Interval;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalEvent;
import gov.nasa.ammos.aerie.procedural.timeline.payloads.ExternalSource;
import gov.nasa.jpl.aerie.constraints.model.DiscreteProfile;
import gov.nasa.jpl.aerie.constraints.model.LinearProfile;
import gov.nasa.jpl.aerie.json.BasicParsers;
Expand Down Expand Up @@ -1185,8 +1186,10 @@ private List<ExternalEvent> parseExternalEvents(final JsonArray eventsJson, fina
result.add(new ExternalEvent(
e.getString("event_key"),
e.getString("event_type_name"),
e.getString("source_key"),
e.getString("derivation_group_name"),
new ExternalSource(
e.getString("source_key"),
e.getString("derivation_group_name")
),
Interval.between(start, end)
));
}
Expand Down

0 comments on commit c2a5b48

Please sign in to comment.