Skip to content

Commit

Permalink
Small rework for profile deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Sep 5, 2024
1 parent c9cf51b commit 352d31e
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void queryActivityDirectives() {

@Test
void queryResources() {
final var fruit = simulatedPlan.resource("/fruit", Real::deserialize).collect();
final var fruit = simulatedPlan.resource("/fruit", Real.deserialize()).collect();
assertIterableEquals(
List.of(
Segment.of(Interval.betweenClosedOpen(Duration.ZERO, Duration.HOUR), new LinearEquation(4.0)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ConstFruit implements Constraint {
@NotNull
@Override
public Violations run(SimulatedPlan plan, @NotNull CollectOptions options) {
final var fruit = plan.resource("/fruit", Real::deserialize);
final var fruit = plan.resource("/fruit", Real.deserialize());

return Violations.violateOn(
fruit.equalTo(4),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void run(EditablePlan plan) {
var simResults = plan.latestResults();
if (simResults == null) simResults = plan.simulate();

final var lowFruit = simResults.resource("/fruit", Real::deserialize).lessThan(3.5).isolateTrue();
final var lowFruit = simResults.resource("/fruit", Real.deserialize()).lessThan(3.5).isolateTrue();
final var bites = simResults.instances("BiteBanana");

final var connections = lowFruit.starts().shift(Duration.MINUTE.negate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void run(@NotNull final EditablePlan plan) {
// I'm using producer as a substitute for a mission phase variable.
// This goal will only apply during the Dole mission phase. :)
final var dolePhase = simResults
.resource("/producer", Strings::deserialize)
.resource("/producer", Strings.deserialize())
.highlightEqualTo("Dole")
.cache();

Expand Down Expand Up @@ -91,7 +91,7 @@ public void run(@NotNull final EditablePlan plan) {
// So we iteratively find the first time /fruit drops below zero
// and add a grow banana fix it. We then mock the effect of grow banana
// by adding one to /fruit, rather than resimulating, and do it again.
var fruit = simResults.resource("/fruit", Real::deserialize).cache();
var fruit = simResults.resource("/fruit", Real.deserialize()).cache();

var ranOutAt = fruit.lessThan(0).filterByWindows(dolePhase, true).risingEdges().highlightTrue().collect();
while (!ranOutAt.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ data class Booleans(private val timeline: Timeline<Segment<Boolean>, Booleans>):
* Converts a list of serialized value segments into a [Booleans] profile;
* for use with [gov.nasa.ammos.aerie.procedural.timeline.plan.Plan.resource].
*/
@JvmStatic fun deserialize(list: List<Segment<SerializedValue>>) = Booleans(list.map { it.withNewValue(it.value.asBoolean().get()) })
@JvmStatic fun deserialize() = { list: List<Segment<SerializedValue>> ->
Booleans(list.map { it.withNewValue(it.value.asBoolean().get()) })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ data class Numbers<N: Number>(private val timeline: Timeline<Segment<N>, Numbers
*
* Prefers converting to longs if possible, and falls back to doubles if not.
*/
@JvmStatic fun deserialize(list: List<Segment<SerializedValue>>) = Numbers(list.map { seg ->
@JvmStatic fun deserialize() = { list: List<Segment<SerializedValue>> -> Numbers(list.map { seg ->
val bigDecimal = seg.value.asNumeric().orElseThrow { Exception("value was not numeric: $seg") }
val number: Number = try {
bigDecimal.longValueExact()
} catch (e: ArithmeticException) {
bigDecimal.toDouble()
}
seg.withNewValue(number)
})
}) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ data class Real(private val timeline: Timeline<Segment<LinearEquation>, Real>):
* While plain numbers are acceptable and will be converted to a [LinearEquation] without warning, consider using [Numbers]
* to keep the precision.
*/
@JvmStatic fun deserialize(list: List<Segment<SerializedValue>>): Real {
@JvmStatic fun deserialize() = { list: List<Segment<SerializedValue>> ->
val converted: List<Segment<LinearEquation>> = list.map { s ->
s.value.asReal().getOrNull()?.let { return@map s.withNewValue(LinearEquation(it)) }
val map = s.value.asMap().orElseThrow { RealDeserializeException("value was not a map or plain number: $s") }
Expand All @@ -221,7 +221,7 @@ data class Real(private val timeline: Timeline<Segment<LinearEquation>, Real>):
.asReal().orElseThrow { RealDeserializeException("rate was not a double") }
s.withNewValue(LinearEquation(s.interval.start, initialValue, rate))
}
return Real(converted)
Real(converted)
}

/***/ class RealDeserializeException(message: String): Exception(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ data class Strings(private val timeline: Timeline<Segment<String>, Strings>):
* Converts a list of serialized value segments into a [Strings] profile;
* for use with [gov.nasa.ammos.aerie.procedural.timeline.plan.Plan.resource].
*/
@JvmStatic fun deserialize(list: List<Segment<SerializedValue>>) = Strings(list.map { seg ->
val string = seg.value.asString().orElseThrow { Exception("value was not a string: $seg") }
seg.withNewValue(string)
})
@JvmStatic fun deserialize() = { list: List<Segment<SerializedValue>> ->
Strings(list.map { seg ->
val string = seg.value.asString().orElseThrow { Exception("value was not a string: $seg") }
seg.withNewValue(string)
})
}
}
}

0 comments on commit 352d31e

Please sign in to comment.