From b6810555957511f4d6234b2b1854844c7236e959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20Marussy?= Date: Wed, 19 Jun 2024 18:28:54 +0200 Subject: [PATCH] refactor(logic): non-null aggregators --- .../java/tools/refinery/logic/term/Aggregator.java | 4 ++++ .../refinery/logic/term/ExtremeValueAggregator.java | 4 ++++ .../tools/refinery/logic/term/StatefulAggregate.java | 3 +++ .../refinery/logic/term/StatefulAggregator.java | 4 ++++ .../refinery/logic/term/StatelessAggregator.java | 3 +++ .../refinery/logic/term/int_/IntSumAggregator.java | 4 ++++ .../refinery/logic/term/real/RealSumAggregator.java | 5 +++++ .../UpperCardinalitySumAggregator.java | 12 +++++++----- .../query/interpreter/AggregatorBatchingTest.java | 2 ++ 9 files changed, 36 insertions(+), 5 deletions(-) diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/Aggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/Aggregator.java index 40189ebc5..d99904fd0 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/Aggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/Aggregator.java @@ -5,6 +5,8 @@ */ package tools.refinery.logic.term; +import org.jetbrains.annotations.NotNull; + import java.util.stream.Stream; public interface Aggregator { @@ -12,7 +14,9 @@ public interface Aggregator { Class getInputType(); + @NotNull R aggregateStream(Stream stream); + @NotNull R getEmptyResult(); } diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/ExtremeValueAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/ExtremeValueAggregator.java index 31c61b6a5..284418fda 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/ExtremeValueAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/ExtremeValueAggregator.java @@ -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; @@ -40,6 +42,7 @@ public StatefulAggregate createEmptyAggregate() { return new Aggregate(); } + @NotNull @Override public T getEmptyResult() { return emptyResult; @@ -85,6 +88,7 @@ public void remove(T value) { }); } + @NotNull @Override public T getResult() { return isEmpty() ? emptyResult : values.firstKey(); diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregate.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregate.java index 75e7ecff8..960e8c53a 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregate.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregate.java @@ -5,11 +5,14 @@ */ package tools.refinery.logic.term; +import org.jetbrains.annotations.NotNull; + public interface StatefulAggregate { void add(T value); void remove(T value); + @NotNull R getResult(); boolean isEmpty(); diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregator.java index 79d325358..2b9243b7f 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatefulAggregator.java @@ -5,11 +5,14 @@ */ package tools.refinery.logic.term; +import org.jetbrains.annotations.NotNull; + import java.util.stream.Stream; public interface StatefulAggregator extends Aggregator { StatefulAggregate createEmptyAggregate(); + @NotNull @Override default R aggregateStream(Stream stream) { var accumulator = createEmptyAggregate(); @@ -21,6 +24,7 @@ default R aggregateStream(Stream stream) { return accumulator.getResult(); } + @NotNull @Override default R getEmptyResult() { return createEmptyAggregate().getResult(); diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatelessAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatelessAggregator.java index 37db262ec..e489378bc 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/StatelessAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/StatelessAggregator.java @@ -5,6 +5,8 @@ */ package tools.refinery.logic.term; +import org.jetbrains.annotations.NotNull; + import java.util.stream.Stream; public interface StatelessAggregator extends Aggregator { @@ -12,6 +14,7 @@ public interface StatelessAggregator extends Aggregator { R remove(R current, T value); + @NotNull @Override default R aggregateStream(Stream stream) { var accumulator = getEmptyResult(); diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/int_/IntSumAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/int_/IntSumAggregator.java index b42038abb..d44625873 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/int_/IntSumAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/int_/IntSumAggregator.java @@ -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 { public static final IntSumAggregator INSTANCE = new IntSumAggregator(); @@ -23,6 +26,7 @@ public Class getInputType() { return Integer.class; } + @NotNull @Override public Integer getEmptyResult() { return 0; diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/real/RealSumAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/real/RealSumAggregator.java index 4b0901885..b6f79270e 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/real/RealSumAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/real/RealSumAggregator.java @@ -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 { public static final RealSumAggregator INSTANCE = new RealSumAggregator(); @@ -32,6 +35,7 @@ public StatefulAggregate createEmptyAggregate() { return new Aggregate(); } + @NotNull @Override public Double getEmptyResult() { return 0d; @@ -63,6 +67,7 @@ public void remove(Double value) { }); } + @NotNull @Override public Double getResult() { return values.entrySet() diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/term/uppercardinality/UpperCardinalitySumAggregator.java b/subprojects/logic/src/main/java/tools/refinery/logic/term/uppercardinality/UpperCardinalitySumAggregator.java index 7c45e6ef2..846e954b4 100644 --- a/subprojects/logic/src/main/java/tools/refinery/logic/term/uppercardinality/UpperCardinalitySumAggregator.java +++ b/subprojects/logic/src/main/java/tools/refinery/logic/term/uppercardinality/UpperCardinalitySumAggregator.java @@ -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 { public static final UpperCardinalitySumAggregator INSTANCE = new UpperCardinalitySumAggregator(); @@ -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 { @@ -57,8 +58,8 @@ 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 { @@ -66,6 +67,7 @@ public void remove(UpperCardinality value) { } } + @NotNull @Override public UpperCardinality getResult() { return countUnbounded > 0 ? UpperCardinalities.UNBOUNDED : UpperCardinalities.atMost(sumFiniteUpperBounds); diff --git a/subprojects/store-query-interpreter/src/test/java/tools/refinery/store/query/interpreter/AggregatorBatchingTest.java b/subprojects/store-query-interpreter/src/test/java/tools/refinery/store/query/interpreter/AggregatorBatchingTest.java index 209d7ac01..0bdd21489 100644 --- a/subprojects/store-query-interpreter/src/test/java/tools/refinery/store/query/interpreter/AggregatorBatchingTest.java +++ b/subprojects/store-query-interpreter/src/test/java/tools/refinery/store/query/interpreter/AggregatorBatchingTest.java @@ -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; @@ -167,6 +168,7 @@ public void remove(Integer value) { sum -= value; } + @NotNull @Override public Integer getResult() { extractCount++;