Skip to content

Commit

Permalink
Remove 'Mutable' from resource constructor names
Browse files Browse the repository at this point in the history
  • Loading branch information
David Legg committed Dec 12, 2023
1 parent 395950b commit b0055ad
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteEffects.toggle;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteEffects.using;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteResources.assertThat;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteResources.discreteMutableResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteResources.discreteResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.monads.DiscreteResourceMonad.map;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialEffects.consume;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.asPolynomial;
Expand All @@ -42,7 +42,7 @@
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.integrate;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.lessThan;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.lessThan$;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialMutableResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.unitAware;
import static gov.nasa.jpl.aerie.contrib.streamline.unit_aware.Quantities.quantity;
import static gov.nasa.jpl.aerie.contrib.streamline.unit_aware.StandardUnits.*;
Expand All @@ -54,15 +54,15 @@ public final class Demo {
// Unit-naive version of a model, to demonstrate some core concepts:

// Consumable, continuous:
MutableResource<Polynomial> fuel_kg = PolynomialResources.polynomialMutableResource(20.0);
MutableResource<Polynomial> fuel_kg = PolynomialResources.polynomialResource(20.0);
// Non-consumable, discrete:
MutableResource<Discrete<Double>> power_w = discreteMutableResource(120.0);
MutableResource<Discrete<Double>> power_w = discreteResource(120.0);
// Atomic non-consumable:
MutableResource<Discrete<Integer>> rwaControl = DiscreteResources.discreteMutableResource(1);
MutableResource<Discrete<Integer>> rwaControl = DiscreteResources.discreteResource(1);
// Settable / enum state:
MutableResource<Discrete<OnOff>> enumSwitch = DiscreteResources.discreteMutableResource(OnOff.ON);
MutableResource<Discrete<OnOff>> enumSwitch = DiscreteResources.discreteResource(OnOff.ON);
// Toggle / flag:
MutableResource<Discrete<Boolean>> boolSwitch = DiscreteResources.discreteMutableResource(true);
MutableResource<Discrete<Boolean>> boolSwitch = DiscreteResources.discreteResource(true);

// Derived states:
Resource<Discrete<OnOff>> derivedEnumSwitch = map(boolSwitch, b -> b ? OnOff.ON : OnOff.OFF);
Expand Down Expand Up @@ -97,10 +97,10 @@ public final class Demo {
// Consumable, continuous:
// CellResource<Polynomial> fuel_kg = polynomialCellResource(20.0);
UnitAware<MutableResource<Polynomial>> fuel = unitAware(
PolynomialResources.polynomialMutableResource(20.0), KILOGRAM);
PolynomialResources.polynomialResource(20.0), KILOGRAM);
// Non-consumable, discrete:
UnitAware<MutableResource<Discrete<Double>>> power = DiscreteResources.unitAware(
discreteMutableResource(120.0), WATT);
discreteResource(120.0), WATT);

UnitAware<Resource<Polynomial>> batterySOC = integrate(asUnitAwarePolynomial(power), quantity(100, JOULE));
UnitAware<Resource<Discrete<Double>>> clampedPower = DiscreteResources.unitAware(map(power.value(WATT), p -> p < 0 ? 0 : p), WATT);
Expand All @@ -124,8 +124,8 @@ public final class Demo {

// Example of using unstructured resources + approximation to represent functions that aren't
// easily represented by analytic derivations
Resource<Polynomial> p = PolynomialResources.polynomialMutableResource(1, 2, 3);
Resource<Polynomial> q = PolynomialResources.polynomialMutableResource(6, 5, 4);
Resource<Polynomial> p = PolynomialResources.polynomialResource(1, 2, 3);
Resource<Polynomial> q = PolynomialResources.polynomialResource(6, 5, 4);
Resource<Unstructured<Double>> quotient = UnstructuredResourceApplicative.map(asUnstructured(p), asUnstructured(q), (p$, q$) -> p$ / q$);
Resource<Linear> approxQuotient = approximate(quotient, secantApproximation(IntervalFunctions.<Unstructured<Double>>byBoundingError(
1e-6, Duration.SECOND, Duration.HOUR.times(24), errorByOptimization())));
Expand Down Expand Up @@ -161,8 +161,8 @@ public final class Demo {

// Example of a locking state:

MutableResource<Discrete<Integer>> importantHardware = DiscreteResources.discreteMutableResource(42);
MutableResource<Discrete<Optional<Integer>>> importantHardwareLock = DiscreteResources.discreteMutableResource(Optional.empty());
MutableResource<Discrete<Integer>> importantHardware = DiscreteResources.discreteResource(42);
MutableResource<Discrete<Optional<Integer>>> importantHardwareLock = DiscreteResources.discreteResource(Optional.empty());
Resource<Discrete<Boolean>> importantHardwareLockAssertion = assertThat(
"Important hardware does not change state while locked",
map(importantHardwareLock, importantHardware, (lock, state) -> lock.map(state::equals).orElse(true)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public static <T> Resource<Discrete<T>> constant(T value) {
}

// General discrete cell resource constructor
public static <T> MutableResource<Discrete<T>> discreteMutableResource(T initialValue) {
public static <T> MutableResource<Discrete<T>> discreteResource(T initialValue) {
return resource(discrete(initialValue));
}

// Annoyingly, we need to repeat the specialization for integer resources, so that
// discreteMutableResource(42) doesn't become a double resource, due to the next overload
public static MutableResource<Discrete<Integer>> discreteMutableResource(int initialValue) {
public static MutableResource<Discrete<Integer>> discreteResource(int initialValue) {
return resource(discrete(initialValue));
}

// specialized constructor for doubles, because they require a toleranced equality comparison
public static MutableResource<Discrete<Double>> discreteMutableResource(double initialValue) {
public static MutableResource<Discrete<Double>> discreteResource(double initialValue) {
return resource(discrete(initialValue), autoEffects(testing(
(CommutativityTestInput<Discrete<Double>> input) -> DoubleUtils.areEqualResults(
input.original().extract(),
Expand Down Expand Up @@ -111,7 +111,7 @@ public static <V> Resource<Discrete<V>> cache(Resource<Discrete<V>> resource, Bi
* Sample valueSupplier once every samplePeriod.
*/
public static <V, T extends Dynamics<Duration, T>> Resource<Discrete<V>> sampled(Supplier<V> valueSupplier, Resource<T> samplePeriod) {
var result = discreteMutableResource(valueSupplier.get());
var result = discreteResource(valueSupplier.get());
every(() -> currentValue(samplePeriod, Duration.MAX_VALUE),
() -> set(result, valueSupplier.get()));
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public static UnitAware<Resource<Polynomial>> constant(UnitAware<Double> quantit
return result;
}

public static MutableResource<Polynomial> polynomialMutableResource(double... initialCoefficients) {
return polynomialMutableResource(polynomial(initialCoefficients));
public static MutableResource<Polynomial> polynomialResource(double... initialCoefficients) {
return polynomialResource(polynomial(initialCoefficients));
}

public static MutableResource<Polynomial> polynomialMutableResource(Polynomial initialDynamics) {
public static MutableResource<Polynomial> polynomialResource(Polynomial initialDynamics) {
return resource(initialDynamics, autoEffects(testing(
(CommutativityTestInput<Polynomial> input) -> {
Polynomial original = input.original();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class ApproximationModel {
public ApproximationModel(final Registrar registrar, final Configuration config) {
final double tolerance = config.approximationTolerance;

polynomial = polynomialMutableResource(1);
divisor = polynomialMutableResource(1);
polynomial = polynomialResource(1);
divisor = polynomialResource(1);

assumedLinear = assumeLinear(polynomial);
defaultApproximation = approximateAsLinear(polynomial, tolerance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.greaterThanOrEquals;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.lessThanOrEquals;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.max;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialMutableResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.subtract;

public class DataModel {
public MutableResource<Polynomial> desiredRateA = PolynomialResources.polynomialMutableResource(0);
public MutableResource<Polynomial> desiredRateB = PolynomialResources.polynomialMutableResource(0);
public MutableResource<Polynomial> desiredRateC = PolynomialResources.polynomialMutableResource(0);
public MutableResource<Polynomial> upperBoundOnTotalVolume = PolynomialResources.polynomialMutableResource(10);
public MutableResource<Polynomial> desiredRateA = PolynomialResources.polynomialResource(0);
public MutableResource<Polynomial> desiredRateB = PolynomialResources.polynomialResource(0);
public MutableResource<Polynomial> desiredRateC = PolynomialResources.polynomialResource(0);
public MutableResource<Polynomial> upperBoundOnTotalVolume = PolynomialResources.polynomialResource(10);

public Resource<Polynomial> actualRateA, actualRateB, actualRateC;
public MutableResource<Polynomial> volumeA, volumeB, volumeC;
Expand All @@ -51,9 +51,9 @@ public DataModel(final Registrar registrar, final Configuration config) {
this.actualRateC = rateC.resource();

// Use a simple feedback loop on volumes to do the integration and clamping.
this.volumeA = PolynomialResources.polynomialMutableResource(0);
this.volumeB = PolynomialResources.polynomialMutableResource(0);
this.volumeC = PolynomialResources.polynomialMutableResource(0);
this.volumeA = PolynomialResources.polynomialResource(0);
this.volumeB = PolynomialResources.polynomialResource(0);
this.volumeC = PolynomialResources.polynomialResource(0);
var clampedVolumeA = clamp(this.volumeA, constant(0), upperBoundOnTotalVolume);
var volumeB_ub = subtract(upperBoundOnTotalVolume, clampedVolumeA);
var clampedVolumeB = clamp(this.volumeB, constant(0), volumeB_ub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
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.discreteMutableResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.DiscreteResources.discreteResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.discrete.monads.DiscreteResourceMonad.map;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.asPolynomial;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.clamp;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.constant;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.multiply;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialMutableResource;
import static gov.nasa.jpl.aerie.contrib.streamline.modeling.polynomial.PolynomialResources.polynomialResource;

public class ErrorTestingModel {
public MutableResource<Discrete<Boolean>> bool = DiscreteResources.discreteMutableResource(true);
public MutableResource<Discrete<Integer>> counter = DiscreteResources.discreteMutableResource(5);
public MutableResource<Polynomial> continuous = PolynomialResources.polynomialMutableResource(1);
public MutableResource<Discrete<Boolean>> bool = DiscreteResources.discreteResource(true);
public MutableResource<Discrete<Integer>> counter = DiscreteResources.discreteResource(5);
public MutableResource<Polynomial> continuous = PolynomialResources.polynomialResource(1);
public Resource<Polynomial> derived = multiply(
continuous,
asPolynomial(map(counter, c -> (double) c)),
asPolynomial(map(bool, $ -> $ ? 1.0 : -1.0)));

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

public ErrorTestingModel(final Registrar registrar, final Configuration config) {
Expand Down

0 comments on commit b0055ad

Please sign in to comment.