Skip to content

Commit

Permalink
refactor(logic): non-null aggregators
Browse files Browse the repository at this point in the history
  • Loading branch information
kris7t committed Jun 19, 2024
1 parent ac5b719 commit b681055
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
*/
package tools.refinery.logic.term;

import org.jetbrains.annotations.NotNull;

import java.util.stream.Stream;

public interface Aggregator<R, T> {
Class<R> getResultType();

Class<T> getInputType();

@NotNull
R aggregateStream(Stream<T> stream);

@NotNull
R getEmptyResult();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package tools.refinery.logic.term;

import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.Objects;
import java.util.SortedMap;
Expand Down Expand Up @@ -40,6 +42,7 @@ public StatefulAggregate<T, T> createEmptyAggregate() {
return new Aggregate();
}

@NotNull
@Override
public T getEmptyResult() {
return emptyResult;
Expand Down Expand Up @@ -85,6 +88,7 @@ public void remove(T value) {
});
}

@NotNull
@Override
public T getResult() {
return isEmpty() ? emptyResult : values.firstKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
package tools.refinery.logic.term;

import org.jetbrains.annotations.NotNull;

public interface StatefulAggregate<R, T> {
void add(T value);

void remove(T value);

@NotNull
R getResult();

boolean isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
package tools.refinery.logic.term;

import org.jetbrains.annotations.NotNull;

import java.util.stream.Stream;

public interface StatefulAggregator<R, T> extends Aggregator<R, T> {
StatefulAggregate<R, T> createEmptyAggregate();

@NotNull
@Override
default R aggregateStream(Stream<T> stream) {
var accumulator = createEmptyAggregate();
Expand All @@ -21,6 +24,7 @@ default R aggregateStream(Stream<T> stream) {
return accumulator.getResult();
}

@NotNull
@Override
default R getEmptyResult() {
return createEmptyAggregate().getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
*/
package tools.refinery.logic.term;

import org.jetbrains.annotations.NotNull;

import java.util.stream.Stream;

public interface StatelessAggregator<R, T> extends Aggregator<R, T> {
R add(R current, T value);

R remove(R current, T value);

@NotNull
@Override
default R aggregateStream(Stream<T> stream) {
var accumulator = getEmptyResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
*/
package tools.refinery.logic.term.int_;

import org.jetbrains.annotations.NotNull;
import tools.refinery.logic.term.StatelessAggregator;

// Singleton implementation, since there is only one way to sum integers.
@SuppressWarnings("squid:S6548")
public final class IntSumAggregator implements StatelessAggregator<Integer, Integer> {
public static final IntSumAggregator INSTANCE = new IntSumAggregator();

Expand All @@ -23,6 +26,7 @@ public Class<Integer> getInputType() {
return Integer.class;
}

@NotNull
@Override
public Integer getEmptyResult() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
*/
package tools.refinery.logic.term.real;

import org.jetbrains.annotations.NotNull;
import tools.refinery.logic.term.StatefulAggregate;
import tools.refinery.logic.term.StatefulAggregator;

import java.util.Map;
import java.util.TreeMap;

// Singleton implementation, since there is only one way to sum reals.
@SuppressWarnings("squid:S6548")
public final class RealSumAggregator implements StatefulAggregator<Double, Double> {
public static final RealSumAggregator INSTANCE = new RealSumAggregator();

Expand All @@ -32,6 +35,7 @@ public StatefulAggregate<Double, Double> createEmptyAggregate() {
return new Aggregate();
}

@NotNull
@Override
public Double getEmptyResult() {
return 0d;
Expand Down Expand Up @@ -63,6 +67,7 @@ public void remove(Double value) {
});
}

@NotNull
@Override
public Double getResult() {
return values.entrySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
package tools.refinery.logic.term.uppercardinality;

import org.jetbrains.annotations.NotNull;
import tools.refinery.logic.term.StatefulAggregate;
import tools.refinery.logic.term.StatefulAggregator;

// Singleton implementation, since there is only one way to aggregate upper cardinalities.
// Singleton implementation, since there is only one way to sum upper cardinalities.
@SuppressWarnings("squid:S6548")
public class UpperCardinalitySumAggregator implements StatefulAggregator<UpperCardinality, UpperCardinality> {
public static final UpperCardinalitySumAggregator INSTANCE = new UpperCardinalitySumAggregator();
Expand Down Expand Up @@ -46,8 +47,8 @@ private Aggregate(int sumFiniteUpperBounds, int countUnbounded) {

@Override
public void add(UpperCardinality value) {
if (value instanceof FiniteUpperCardinality finiteUpperCardinality) {
sumFiniteUpperBounds += finiteUpperCardinality.finiteUpperBound();
if (value instanceof FiniteUpperCardinality(var finiteUpperBound)) {
sumFiniteUpperBounds += finiteUpperBound;
} else if (value instanceof UnboundedUpperCardinality) {
countUnbounded += 1;
} else {
Expand All @@ -57,15 +58,16 @@ public void add(UpperCardinality value) {

@Override
public void remove(UpperCardinality value) {
if (value instanceof FiniteUpperCardinality finiteUpperCardinality) {
sumFiniteUpperBounds -= finiteUpperCardinality.finiteUpperBound();
if (value instanceof FiniteUpperCardinality(var finiteUpperBound)) {
sumFiniteUpperBounds -= finiteUpperBound;
} else if (value instanceof UnboundedUpperCardinality) {
countUnbounded -= 1;
} else {
throw new IllegalArgumentException("Unknown UpperCardinality: " + value);
}
}

@NotNull
@Override
public UpperCardinality getResult() {
return countUnbounded > 0 ? UpperCardinalities.UNBOUNDED : UpperCardinalities.atMost(sumFiniteUpperBounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package tools.refinery.store.query.interpreter;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import tools.refinery.store.model.Model;
import tools.refinery.store.model.ModelStore;
Expand Down Expand Up @@ -167,6 +168,7 @@ public void remove(Integer value) {
sum -= value;
}

@NotNull
@Override
public Integer getResult() {
extractCount++;
Expand Down

0 comments on commit b681055

Please sign in to comment.