From fb15cdaa3ce8dcf8ad46ecf772ac061a813a135a Mon Sep 17 00:00:00 2001 From: Matthew Dailis Date: Mon, 29 Jan 2024 10:35:06 -0800 Subject: [PATCH] Add javadocs and remove unused imports --- .../jpl/aerie/contrib/streamline/core/CellRefV2.java | 7 ++++++- .../jpl/aerie/contrib/streamline/core/Dynamics.java | 2 ++ .../jpl/aerie/contrib/streamline/core/Reactions.java | 1 - .../aerie/contrib/streamline/modeling/Registrar.java | 10 ++++++++-- .../modeling/black_box/UnstructuredResources.java | 3 +-- .../streamline/modeling/polynomial/Polynomial.java | 11 +++++++++++ .../jpl/aerie/streamline_demo/ErrorTestingModel.java | 4 ++-- 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/CellRefV2.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/CellRefV2.java index 064f575411..c747d0a689 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/CellRefV2.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/CellRefV2.java @@ -1,7 +1,6 @@ package gov.nasa.jpl.aerie.contrib.streamline.core; import gov.nasa.jpl.aerie.contrib.streamline.core.monads.ErrorCatchingMonad; -import gov.nasa.jpl.aerie.contrib.streamline.debugging.Naming; import gov.nasa.jpl.aerie.merlin.framework.CellRef; import gov.nasa.jpl.aerie.merlin.protocol.model.CellType; import gov.nasa.jpl.aerie.merlin.protocol.model.EffectTrait; @@ -15,6 +14,9 @@ import static gov.nasa.jpl.aerie.contrib.streamline.debugging.Naming.*; import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.ZERO; +/** + * Utility class for a simplified allocate method. + */ public final class CellRefV2 { private CellRefV2() {} @@ -89,6 +91,9 @@ public static > EffectTrait> autoEffe * correctly comparing expiry and error information in the process. */ public static Predicate>>> testing(Predicate> test) { + // If both expiring, compare expiry and data + // If both error, compare error contents + // If one is expiring and the other is error, return false return input -> input.leftResult.match( leftExpiring -> input.rightResult.match( rightExpiring -> leftExpiring.expiry().equals(rightExpiring.expiry()) && test.test(new CommutativityTestInput<>( diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Dynamics.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Dynamics.java index c70509f47b..1d4fd78376 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Dynamics.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Dynamics.java @@ -14,6 +14,8 @@ public interface Dynamics> { /** * Evolve for the given time. + * + * @apiNote This method should always return the same value when called on the same object with the same duration */ D step(Duration t); } diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Reactions.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Reactions.java index 5ae4597240..17125f3460 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Reactions.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/core/Reactions.java @@ -3,7 +3,6 @@ import gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.Discrete; import gov.nasa.jpl.aerie.merlin.framework.Condition; import gov.nasa.jpl.aerie.merlin.protocol.types.Duration; -import org.apache.commons.lang3.mutable.MutableObject; import java.util.function.Consumer; import java.util.function.Supplier; diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/Registrar.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/Registrar.java index a173fe683d..124eb56d5c 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/Registrar.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/Registrar.java @@ -70,6 +70,9 @@ public Registrar(final gov.nasa.jpl.aerie.merlin.framework.Registrar baseRegistr this.errorBehavior = errorBehavior; errors = resource(Discrete.discrete(Map.of())); var errorString = map(errors, errors$ -> errors$.entrySet().stream().map(entry -> formatError(entry.getKey(), entry.getValue())).collect(joining("\n\n"))); + + // Register the errors and number of errors resources for output + // TODO consider using serializable events, rather than resources, to log errors discrete("errors", errorString, new StringValueMapper()); discrete("numberOfErrors", map(errors, Map::size), new IntegerValueMapper()); } @@ -99,7 +102,7 @@ public void setProfile() { } public void clearProfile() { - profile = true; + profile = false; } public void discrete(final String name, final Resource> resource, final ValueMapper mapper) { @@ -145,7 +148,7 @@ private > void logErrors(String name, Resource resou }); } - // TODO: Consider pulling in a Guava MultiMap instead of doing this by hand below + // TODO: Consider using a MultiMap instead of doing this by hand below private Unit logError(String resourceName, Throwable e) { errors.emit(effect(s -> { var s$ = new HashMap<>(s); @@ -163,6 +166,9 @@ private Unit logError(String resourceName, Throwable e) { return Unit.UNIT; } + /** + * Include the resource name in the error to give context + */ private static gov.nasa.jpl.aerie.merlin.framework.Resource wrapErrors(String resourceName, gov.nasa.jpl.aerie.merlin.framework.Resource resource) { return () -> { try { diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/black_box/UnstructuredResources.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/black_box/UnstructuredResources.java index faba22ce1f..8b90f68fad 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/black_box/UnstructuredResources.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/black_box/UnstructuredResources.java @@ -1,7 +1,6 @@ package gov.nasa.jpl.aerie.contrib.streamline.modeling.black_box; import gov.nasa.jpl.aerie.contrib.streamline.core.Dynamics; -import gov.nasa.jpl.aerie.contrib.streamline.core.MutableResource; import gov.nasa.jpl.aerie.contrib.streamline.core.Resource; import gov.nasa.jpl.aerie.contrib.streamline.modeling.black_box.SecantApproximation.ErrorEstimates; import gov.nasa.jpl.aerie.contrib.streamline.modeling.black_box.monads.UnstructuredResourceApplicative; @@ -32,7 +31,7 @@ public static Resource> timeBased(Function f) { // Put this in a cell so it'll be stepped up appropriately return resource(Unstructured.timeBased(f)); } - + public static > Resource> asUnstructured(Resource resource) { return map(resource, Unstructured::unstructured); } diff --git a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/Polynomial.java b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/Polynomial.java index ab8d3cb541..7e00dd83a9 100644 --- a/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/Polynomial.java +++ b/contrib/src/main/java/gov/nasa/jpl/aerie/contrib/streamline/modeling/polynomial/Polynomial.java @@ -25,9 +25,20 @@ import static gov.nasa.jpl.aerie.merlin.protocol.types.Duration.ZERO; import static org.apache.commons.math3.analysis.polynomials.PolynomialsUtils.shift; +/** + * An implementation of Polynomial Dynamics + * @param coefficients an array of polynomial coefficients, where an entry's index in the array corresponds to the degree of that coefficient + * + * @apiNote The units of `t` are seconds + */ public record Polynomial(double[] coefficients) implements Dynamics { // TODO: Add Duration parameter for unit of formal parameter? + /** + * + * @param coefficients the polynomial coefficients, from least to most significant + * @return a Polynomial with the given coefficients + */ public static Polynomial polynomial(double... coefficients) { int n = coefficients.length; if (n == 0) { diff --git a/examples/streamline-demo/src/main/java/gov/nasa/jpl/aerie/streamline_demo/ErrorTestingModel.java b/examples/streamline-demo/src/main/java/gov/nasa/jpl/aerie/streamline_demo/ErrorTestingModel.java index ce2693751f..8c4de6e23a 100644 --- a/examples/streamline-demo/src/main/java/gov/nasa/jpl/aerie/streamline_demo/ErrorTestingModel.java +++ b/examples/streamline-demo/src/main/java/gov/nasa/jpl/aerie/streamline_demo/ErrorTestingModel.java @@ -10,7 +10,7 @@ import gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.Polynomial; import gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources; -import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteResources.discreteResource; +import static gov.nasa.jpl.aerie.contrib.streamline.debugging.Naming.name; import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.monads.DiscreteResourceMonad.map; import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.*; @@ -23,7 +23,7 @@ public class ErrorTestingModel { asPolynomial(map(counter, c -> (double) c)), asPolynomial(map(bool, $ -> $ ? 1.0 : -1.0))); - public MutableResource upperBound = PolynomialResources.polynomialResource(5); + public MutableResource upperBound = name(PolynomialResources.polynomialResource(5), "upperBound"); public MutableResource lowerBound = PolynomialResources.polynomialResource(-5); public Resource clamped = clamp(constant(10), lowerBound, upperBound);