-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1149 from NASA-AMMOS/1112-add-connectto-operation…
…-between-spans Implement connectTo
- Loading branch information
Showing
7 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
constraints/src/main/java/gov/nasa/jpl/aerie/constraints/tree/SpansConnectTo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package gov.nasa.jpl.aerie.constraints.tree; | ||
|
||
import gov.nasa.jpl.aerie.constraints.model.EvaluationEnvironment; | ||
import gov.nasa.jpl.aerie.constraints.model.SimulationResults; | ||
import gov.nasa.jpl.aerie.constraints.time.Interval; | ||
import gov.nasa.jpl.aerie.constraints.time.Spans; | ||
import gov.nasa.jpl.aerie.merlin.protocol.types.Duration; | ||
|
||
import java.util.Set; | ||
import java.util.stream.StreamSupport; | ||
|
||
public record SpansConnectTo(Expression<Spans> from, Expression<Spans> to) implements Expression<Spans> { | ||
|
||
@Override | ||
public Spans evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) { | ||
final var from = this.from.evaluate(results, bounds, environment); | ||
final var sortedFrom = StreamSupport.stream(from.spliterator(), true).sorted((l, r) -> l.interval().compareEnds(r.interval())).toList(); | ||
final var to = this.to.evaluate(results, bounds, environment); | ||
final var sortedTo = StreamSupport.stream(to.spliterator(), true).sorted((l, r) -> l.interval().compareStarts(r.interval())).toList(); | ||
final var result = new Spans(); | ||
var toIndex = 0; | ||
for (final var span: sortedFrom) { | ||
final var startTime = span.interval().end; | ||
while (toIndex < sortedTo.size() && span.interval().compareEndToStart(sortedTo.get(toIndex).interval()) == 1) { | ||
toIndex++; | ||
} | ||
final Duration endTime; | ||
final Interval.Inclusivity endInlusivity; | ||
if (toIndex == sortedTo.size()) { | ||
endTime = bounds.end; | ||
endInlusivity = bounds.endInclusivity; | ||
} | ||
else { | ||
endTime = sortedTo.get(toIndex).interval().start; | ||
endInlusivity = Interval.Inclusivity.Inclusive; | ||
} | ||
result.add( | ||
Interval.between( | ||
startTime, | ||
Interval.Inclusivity.Inclusive, | ||
endTime, | ||
endInlusivity | ||
), | ||
span.value() | ||
); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void extractResources(final Set<String> names) { | ||
this.from.extractResources(names); | ||
this.to.extractResources(names); | ||
} | ||
|
||
@Override | ||
public String prettyPrint(final String prefix) { | ||
return String.format( | ||
"\n%s(connect from %s to %s)", | ||
prefix, | ||
this.from.prettyPrint(), | ||
this.to.prettyPrint() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters