Skip to content

Commit

Permalink
Add javadocs and remove unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdailis committed Jan 29, 2024
1 parent 6bbb77f commit fb15cda
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {}

Expand Down Expand Up @@ -89,6 +91,9 @@ public static <D extends Dynamics<?, D>> EffectTrait<DynamicsEffect<D>> autoEffe
* correctly comparing expiry and error information in the process.
*/
public static <D> Predicate<CommutativityTestInput<ErrorCatching<Expiring<D>>>> testing(Predicate<CommutativityTestInput<D>> 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<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface Dynamics<V, D extends Dynamics<V, D>> {

/**
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -99,7 +102,7 @@ public void setProfile() {
}

public void clearProfile() {
profile = true;
profile = false;
}

public <Value> void discrete(final String name, final Resource<Discrete<Value>> resource, final ValueMapper<Value> mapper) {
Expand Down Expand Up @@ -145,7 +148,7 @@ private <D extends Dynamics<?, D>> void logErrors(String name, Resource<D> 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);
Expand All @@ -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 <D> gov.nasa.jpl.aerie.merlin.framework.Resource<D> wrapErrors(String resourceName, gov.nasa.jpl.aerie.merlin.framework.Resource<D> resource) {
return () -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,7 +31,7 @@ public static <A> Resource<Unstructured<A>> timeBased(Function<Duration, A> f) {
// Put this in a cell so it'll be stepped up appropriately
return resource(Unstructured.timeBased(f));
}

public static <A, D extends Dynamics<A, D>> Resource<Unstructured<A>> asUnstructured(Resource<D> resource) {
return map(resource, Unstructured::unstructured);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double, Polynomial> {

// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -23,7 +23,7 @@ public class ErrorTestingModel {
asPolynomial(map(counter, c -> (double) c)),
asPolynomial(map(bool, $ -> $ ? 1.0 : -1.0)));

public MutableResource<Polynomial> upperBound = PolynomialResources.polynomialResource(5);
public MutableResource<Polynomial> upperBound = name(PolynomialResources.polynomialResource(5), "upperBound");
public MutableResource<Polynomial> lowerBound = PolynomialResources.polynomialResource(-5);
public Resource<Polynomial> clamped = clamp(constant(10), lowerBound, upperBound);

Expand Down

0 comments on commit fb15cda

Please sign in to comment.