diff options
author | 2023-04-23 23:39:39 +0200 | |
---|---|---|
committer | 2023-04-23 23:42:53 +0200 | |
commit | 565de1cb116e5e4f116544aaa035be58336656ec (patch) | |
tree | 5d4cff8e77e280552f13b9c7f8aafa79a39a0090 /subprojects/store-query-viatra/src/main | |
parent | refactor: simplify ModelAdapter (diff) | |
download | refinery-565de1cb116e5e4f116544aaa035be58336656ec.tar.gz refinery-565de1cb116e5e4f116544aaa035be58336656ec.tar.zst refinery-565de1cb116e5e4f116544aaa035be58336656ec.zip |
refactor: query terms
* Separate different operators into different classes to make it easier to add
functionality (e.g., simplification) later.
* Add UpperCardinality terms.
* Move UpperCardinality aggregator and tests into refinery-store-query.
Diffstat (limited to 'subprojects/store-query-viatra/src/main')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/cardinality/UpperCardinalitySumAggregationOperator.java | 102 |
1 files changed, 0 insertions, 102 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 | } | ||