-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build during expression by updating ForEach and Spans
- Loading branch information
1 parent
10da48b
commit 7ec6844
Showing
14 changed files
with
681 additions
and
224 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
50 changes: 50 additions & 0 deletions
50
constraints/src/main/java/gov/nasa/jpl/aerie/constraints/tree/ActivitySpan.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,50 @@ | ||
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.Segment; | ||
import gov.nasa.jpl.aerie.constraints.time.Spans; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public final class ActivitySpan implements Expression<Spans> { | ||
public final String activityAlias; | ||
|
||
public ActivitySpan(final String activityAlias) { | ||
this.activityAlias = activityAlias; | ||
} | ||
|
||
@Override | ||
public Spans evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) { | ||
final var activity = environment.activityInstances().get(this.activityAlias); | ||
return new Spans(Segment.of(activity.interval, Optional.of(new Spans.Metadata(activity.id)))); | ||
} | ||
|
||
@Override | ||
public void extractResources(final Set<String> names) { } | ||
|
||
@Override | ||
public String prettyPrint(final String prefix) { | ||
return String.format( | ||
"\n%s(during %s)", | ||
prefix, | ||
this.activityAlias | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof ActivitySpan)) return false; | ||
final var o = (ActivitySpan)obj; | ||
|
||
return Objects.equals(this.activityAlias, o.activityAlias); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.activityAlias); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
constraints/src/main/java/gov/nasa/jpl/aerie/constraints/tree/ForEachActivitySpans.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,76 @@ | ||
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 java.util.HashMap; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public final class ForEachActivitySpans implements Expression<Spans> { | ||
public final String activityType; | ||
public final String alias; | ||
public final Expression<Spans> expression; | ||
|
||
public ForEachActivitySpans( | ||
final String activityType, | ||
final String alias, | ||
final Expression<Spans> expression | ||
) { | ||
this.activityType = activityType; | ||
this.alias = alias; | ||
this.expression = expression; | ||
} | ||
|
||
@Override | ||
public Spans evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) { | ||
final var spans = new Spans(); | ||
for (final var activity : results.activities) { | ||
if (activity.type.equals(this.activityType)) { | ||
final var newEnvironment = new EvaluationEnvironment( | ||
new HashMap<>(environment.activityInstances()), | ||
environment.realExternalProfiles(), | ||
environment.discreteExternalProfiles() | ||
); | ||
newEnvironment.activityInstances().put(this.alias, activity); | ||
|
||
final var expressionSpans = this.expression.evaluate(results, bounds, newEnvironment); | ||
spans.addAll(expressionSpans); | ||
} | ||
} | ||
return spans; | ||
} | ||
|
||
@Override | ||
public void extractResources(final Set<String> names) { | ||
this.expression.extractResources(names); | ||
} | ||
|
||
@Override | ||
public String prettyPrint(final String prefix) { | ||
return String.format( | ||
"\n%s(for-each-activity %s %s %s)", | ||
prefix, | ||
this.activityType, | ||
this.alias, | ||
this.expression.prettyPrint(prefix + " ") | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof ForEachActivitySpans)) return false; | ||
final var o = (ForEachActivitySpans)obj; | ||
|
||
return Objects.equals(this.activityType, o.activityType) && | ||
Objects.equals(this.alias, o.alias) && | ||
Objects.equals(this.expression, o.expression); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.activityType, this.alias, this.expression); | ||
} | ||
} |
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
Oops, something went wrong.