diff options
Diffstat (limited to 'subprojects/store-query-viatra/src')
3 files changed, 0 insertions, 250 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperator.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperator.java deleted file mode 100644 index bfd4c049..00000000 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperator.java +++ /dev/null | |||
@@ -1,102 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.viatra.internal.cardinality; | ||
7 | |||
8 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
9 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
10 | import tools.refinery.store.representation.cardinality.FiniteUpperCardinality; | ||
11 | import tools.refinery.store.representation.cardinality.UnboundedUpperCardinality; | ||
12 | import tools.refinery.store.representation.cardinality.UpperCardinalities; | ||
13 | import tools.refinery.store.representation.cardinality.UpperCardinality; | ||
14 | |||
15 | import java.util.stream.Stream; | ||
16 | |||
17 | public class UpperCardinalitySumAggregationOperator implements IMultisetAggregationOperator<UpperCardinality, | ||
18 | UpperCardinalitySumAggregationOperator.Accumulator, UpperCardinality> { | ||
19 | public static final UpperCardinalitySumAggregationOperator INSTANCE = new UpperCardinalitySumAggregationOperator(); | ||
20 | |||
21 | public static final BoundAggregator BOUND_AGGREGATOR = new BoundAggregator(INSTANCE, UpperCardinality.class, | ||
22 | UpperCardinality.class); | ||
23 | |||
24 | private UpperCardinalitySumAggregationOperator() { | ||
25 | // Singleton constructor. | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public String getName() { | ||
30 | return "sum<UpperCardinality>"; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public String getShortDescription() { | ||
35 | return "%s computes the sum of finite or unbounded upper cardinalities".formatted(getName()); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public Accumulator createNeutral() { | ||
40 | return new Accumulator(); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean isNeutral(Accumulator result) { | ||
45 | return result.sumFiniteUpperBounds == 0 && result.countUnbounded == 0; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public Accumulator update(Accumulator oldResult, UpperCardinality updateValue, boolean isInsertion) { | ||
50 | if (updateValue instanceof FiniteUpperCardinality finiteUpperCardinality) { | ||
51 | int finiteUpperBound = finiteUpperCardinality.finiteUpperBound(); | ||
52 | if (isInsertion) { | ||
53 | oldResult.sumFiniteUpperBounds += finiteUpperBound; | ||
54 | } else { | ||
55 | oldResult.sumFiniteUpperBounds -= finiteUpperBound; | ||
56 | } | ||
57 | } else if (updateValue instanceof UnboundedUpperCardinality) { | ||
58 | if (isInsertion) { | ||
59 | oldResult.countUnbounded += 1; | ||
60 | } else { | ||
61 | oldResult.countUnbounded -= 1; | ||
62 | } | ||
63 | } else { | ||
64 | throw new IllegalArgumentException("Unknown UpperCardinality: " + updateValue); | ||
65 | } | ||
66 | return oldResult; | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public UpperCardinality getAggregate(Accumulator result) { | ||
71 | return result.countUnbounded > 0 ? UpperCardinalities.UNBOUNDED : | ||
72 | UpperCardinalities.valueOf(result.sumFiniteUpperBounds); | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public UpperCardinality aggregateStream(Stream<UpperCardinality> stream) { | ||
77 | var result = stream.collect(this::createNeutral, (accumulator, value) -> update(accumulator, value, true), | ||
78 | (left, right) -> new Accumulator(left.sumFiniteUpperBounds + right.sumFiniteUpperBounds, | ||
79 | left.countUnbounded + right.countUnbounded)); | ||
80 | return getAggregate(result); | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public Accumulator clone(Accumulator original) { | ||
85 | return new Accumulator(original.sumFiniteUpperBounds, original.countUnbounded); | ||
86 | } | ||
87 | |||
88 | public static class Accumulator { | ||
89 | private int sumFiniteUpperBounds; | ||
90 | |||
91 | private int countUnbounded; | ||
92 | |||
93 | private Accumulator(int sumFiniteUpperBounds, int countUnbounded) { | ||
94 | this.sumFiniteUpperBounds = sumFiniteUpperBounds; | ||
95 | this.countUnbounded = countUnbounded; | ||
96 | } | ||
97 | |||
98 | private Accumulator() { | ||
99 | this(0, 0); | ||
100 | } | ||
101 | } | ||
102 | } | ||
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorStreamTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorStreamTest.java deleted file mode 100644 index 53d03cbd..00000000 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorStreamTest.java +++ /dev/null | |||
@@ -1,56 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.viatra.internal.cardinality; | ||
7 | |||
8 | import org.junit.jupiter.params.ParameterizedTest; | ||
9 | import org.junit.jupiter.params.provider.Arguments; | ||
10 | import org.junit.jupiter.params.provider.MethodSource; | ||
11 | import tools.refinery.store.representation.cardinality.UpperCardinalities; | ||
12 | import tools.refinery.store.representation.cardinality.UpperCardinality; | ||
13 | |||
14 | import java.util.List; | ||
15 | import java.util.stream.Stream; | ||
16 | |||
17 | import static org.hamcrest.MatcherAssert.assertThat; | ||
18 | import static org.hamcrest.Matchers.equalTo; | ||
19 | |||
20 | class UpperCardinalitySumAggregationOperatorStreamTest { | ||
21 | @ParameterizedTest | ||
22 | @MethodSource | ||
23 | void testStream(List<UpperCardinality> list, UpperCardinality expected) { | ||
24 | var result = UpperCardinalitySumAggregationOperator.INSTANCE.aggregateStream(list.stream()); | ||
25 | assertThat(result, equalTo(expected)); | ||
26 | } | ||
27 | |||
28 | static Stream<Arguments> testStream() { | ||
29 | return Stream.of( | ||
30 | Arguments.of(List.of(), UpperCardinalities.ZERO), | ||
31 | Arguments.of(List.of(UpperCardinality.of(3)), UpperCardinality.of(3)), | ||
32 | Arguments.of( | ||
33 | List.of( | ||
34 | UpperCardinality.of(2), | ||
35 | UpperCardinality.of(3) | ||
36 | ), | ||
37 | UpperCardinality.of(5) | ||
38 | ), | ||
39 | Arguments.of(List.of(UpperCardinalities.UNBOUNDED), UpperCardinalities.UNBOUNDED), | ||
40 | Arguments.of( | ||
41 | List.of( | ||
42 | UpperCardinalities.UNBOUNDED, | ||
43 | UpperCardinalities.UNBOUNDED | ||
44 | ), | ||
45 | UpperCardinalities.UNBOUNDED | ||
46 | ), | ||
47 | Arguments.of( | ||
48 | List.of( | ||
49 | UpperCardinalities.UNBOUNDED, | ||
50 | UpperCardinality.of(3) | ||
51 | ), | ||
52 | UpperCardinalities.UNBOUNDED | ||
53 | ) | ||
54 | ); | ||
55 | } | ||
56 | } | ||
diff --git a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorTest.java b/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorTest.java deleted file mode 100644 index 677eeeac..00000000 --- a/subprojects/store-query-viatra/src/test/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperatorTest.java +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.viatra.internal.cardinality; | ||
7 | |||
8 | import org.junit.jupiter.api.BeforeEach; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | import tools.refinery.store.representation.cardinality.UpperCardinalities; | ||
11 | import tools.refinery.store.representation.cardinality.UpperCardinality; | ||
12 | |||
13 | import static org.hamcrest.MatcherAssert.assertThat; | ||
14 | import static org.hamcrest.Matchers.equalTo; | ||
15 | |||
16 | class UpperCardinalitySumAggregationOperatorTest { | ||
17 | private UpperCardinalitySumAggregationOperator.Accumulator accumulator; | ||
18 | |||
19 | @BeforeEach | ||
20 | void beforeEach() { | ||
21 | accumulator = UpperCardinalitySumAggregationOperator.INSTANCE.createNeutral(); | ||
22 | } | ||
23 | |||
24 | @Test | ||
25 | void emptyAggregationTest() { | ||
26 | assertResult(UpperCardinality.of(0)); | ||
27 | } | ||
28 | |||
29 | @Test | ||
30 | void singleBoundedTest() { | ||
31 | insert(UpperCardinality.of(3)); | ||
32 | assertResult(UpperCardinality.of(3)); | ||
33 | } | ||
34 | |||
35 | @Test | ||
36 | void multipleBoundedTest() { | ||
37 | insert(UpperCardinality.of(2)); | ||
38 | insert(UpperCardinality.of(3)); | ||
39 | assertResult(UpperCardinality.of(5)); | ||
40 | } | ||
41 | |||
42 | @Test | ||
43 | void singleUnboundedTest() { | ||
44 | insert(UpperCardinalities.UNBOUNDED); | ||
45 | assertResult(UpperCardinalities.UNBOUNDED); | ||
46 | } | ||
47 | |||
48 | @Test | ||
49 | void multipleUnboundedTest() { | ||
50 | insert(UpperCardinalities.UNBOUNDED); | ||
51 | insert(UpperCardinalities.UNBOUNDED); | ||
52 | assertResult(UpperCardinalities.UNBOUNDED); | ||
53 | } | ||
54 | |||
55 | @Test | ||
56 | void removeBoundedTest() { | ||
57 | insert(UpperCardinality.of(2)); | ||
58 | insert(UpperCardinality.of(3)); | ||
59 | remove(UpperCardinality.of(2)); | ||
60 | assertResult(UpperCardinality.of(3)); | ||
61 | } | ||
62 | |||
63 | @Test | ||
64 | void removeAllUnboundedTest() { | ||
65 | insert(UpperCardinalities.UNBOUNDED); | ||
66 | insert(UpperCardinality.of(3)); | ||
67 | remove(UpperCardinalities.UNBOUNDED); | ||
68 | assertResult(UpperCardinality.of(3)); | ||
69 | } | ||
70 | |||
71 | @Test | ||
72 | void removeSomeUnboundedTest() { | ||
73 | insert(UpperCardinalities.UNBOUNDED); | ||
74 | insert(UpperCardinalities.UNBOUNDED); | ||
75 | insert(UpperCardinality.of(3)); | ||
76 | remove(UpperCardinalities.UNBOUNDED); | ||
77 | assertResult(UpperCardinalities.UNBOUNDED); | ||
78 | } | ||
79 | |||
80 | private void insert(UpperCardinality value) { | ||
81 | accumulator = UpperCardinalitySumAggregationOperator.INSTANCE.update(accumulator, value, true); | ||
82 | } | ||
83 | |||
84 | private void remove(UpperCardinality value) { | ||
85 | accumulator = UpperCardinalitySumAggregationOperator.INSTANCE.update(accumulator, value, false); | ||
86 | } | ||
87 | |||
88 | private void assertResult(UpperCardinality expected) { | ||
89 | var result = UpperCardinalitySumAggregationOperator.INSTANCE.getAggregate(accumulator); | ||
90 | assertThat(result, equalTo(expected)); | ||
91 | } | ||
92 | } | ||