diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators')
13 files changed, 788 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/AverageAccumulator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/AverageAccumulator.java new file mode 100644 index 00000000..2c09ede1 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/AverageAccumulator.java | |||
@@ -0,0 +1,24 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | /** | ||
12 | * @since 2.0 | ||
13 | */ | ||
14 | class AverageAccumulator<Domain> { | ||
15 | Domain value; | ||
16 | long count; | ||
17 | |||
18 | public AverageAccumulator(Domain value, long count) { | ||
19 | super(); | ||
20 | this.value = value; | ||
21 | this.count = count; | ||
22 | } | ||
23 | |||
24 | } \ No newline at end of file | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleAverageOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleAverageOperator.java new file mode 100644 index 00000000..e8a26afd --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleAverageOperator.java | |||
@@ -0,0 +1,82 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.OptionalDouble; | ||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
15 | |||
16 | /** | ||
17 | * @author Zoltan Ujhelyi | ||
18 | * @since 2.0 | ||
19 | */ | ||
20 | public class DoubleAverageOperator implements IMultisetAggregationOperator<Double, AverageAccumulator<Double>, Double> { | ||
21 | |||
22 | public static final DoubleAverageOperator INSTANCE = new DoubleAverageOperator(); | ||
23 | |||
24 | private DoubleAverageOperator() { | ||
25 | // Singleton, do not call. | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public String getShortDescription() { | ||
30 | return "avg<Integer> incrementally computes the average of java.lang.Integer values"; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public String getName() { | ||
35 | return "avg<Integer>"; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public AverageAccumulator<Double> createNeutral() { | ||
40 | return new AverageAccumulator<Double>(0d, 0l); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean isNeutral(AverageAccumulator<Double> result) { | ||
45 | return result.count == 0l; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public AverageAccumulator<Double> update(AverageAccumulator<Double> oldResult, Double updateValue, | ||
50 | boolean isInsertion) { | ||
51 | if (isInsertion) { | ||
52 | oldResult.value += updateValue; | ||
53 | oldResult.count++; | ||
54 | } else { | ||
55 | oldResult.value -= updateValue; | ||
56 | oldResult.count--; | ||
57 | } | ||
58 | return oldResult; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public Double getAggregate(AverageAccumulator<Double> result) { | ||
63 | return (result.count == 0) | ||
64 | ? null | ||
65 | : result.value/result.count; | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public Double aggregateStream(Stream<Double> stream) { | ||
70 | final OptionalDouble averageOpt = stream.mapToDouble(Double::doubleValue).average(); | ||
71 | return averageOpt.isPresent() ? averageOpt.getAsDouble() : null; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * @since 2.4 | ||
76 | */ | ||
77 | @Override | ||
78 | public AverageAccumulator<Double> clone(AverageAccumulator<Double> original) { | ||
79 | return new AverageAccumulator<Double>(original.value, original.count); | ||
80 | } | ||
81 | |||
82 | } \ No newline at end of file | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleSumOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleSumOperator.java new file mode 100644 index 00000000..744b0cd1 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/DoubleSumOperator.java | |||
@@ -0,0 +1,62 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.stream.Stream; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AbstractMemorylessAggregationOperator; | ||
14 | |||
15 | /** | ||
16 | * Incrementally computes the sum of java.lang.Double values | ||
17 | * @author Gabor Bergmann | ||
18 | * @since 1.4 | ||
19 | */ | ||
20 | public class DoubleSumOperator extends AbstractMemorylessAggregationOperator<Double, Double> { | ||
21 | public static final DoubleSumOperator INSTANCE = new DoubleSumOperator(); | ||
22 | |||
23 | private DoubleSumOperator() { | ||
24 | // Singleton, do not call. | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public String getShortDescription() { | ||
29 | return "sum<Double> incrementally computes the sum of java.lang.Double values"; | ||
30 | } | ||
31 | @Override | ||
32 | public String getName() { | ||
33 | return "sum<Double>"; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public Double createNeutral() { | ||
38 | return 0d; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public boolean isNeutral(Double result) { | ||
43 | return createNeutral().equals(result); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public Double update(Double oldResult, Double updateValue, boolean isInsertion) { | ||
48 | return isInsertion ? | ||
49 | oldResult + updateValue : | ||
50 | oldResult - updateValue; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * @since 2.0 | ||
55 | */ | ||
56 | @Override | ||
57 | public Double aggregateStream(Stream<Double> stream) { | ||
58 | return stream.mapToDouble(Double::doubleValue).sum(); | ||
59 | } | ||
60 | |||
61 | |||
62 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/ExtremumOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/ExtremumOperator.java new file mode 100644 index 00000000..ee4ceeb8 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/ExtremumOperator.java | |||
@@ -0,0 +1,135 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | import java.util.SortedMap; | ||
13 | import java.util.TreeMap; | ||
14 | import java.util.stream.Stream; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
17 | |||
18 | /** | ||
19 | * Incrementally computes the minimum or maximum of java.lang.Comparable values, using the default comparison | ||
20 | * | ||
21 | * @author Gabor Bergmann | ||
22 | * @since 1.4 | ||
23 | */ | ||
24 | public class ExtremumOperator<T extends Comparable<T>> | ||
25 | implements IMultisetAggregationOperator<T, SortedMap<T, Integer>, T> { | ||
26 | |||
27 | public enum Extreme { | ||
28 | MIN, MAX; | ||
29 | |||
30 | /** | ||
31 | * @since 2.0 | ||
32 | */ | ||
33 | public <T> T pickFrom(SortedMap<T, Integer> nonEmptyMultiSet) { | ||
34 | switch(this) { | ||
35 | case MIN: | ||
36 | return nonEmptyMultiSet.firstKey(); | ||
37 | case MAX: | ||
38 | return nonEmptyMultiSet.lastKey(); | ||
39 | default: | ||
40 | return null; | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | private static final ExtremumOperator MIN_OP = new ExtremumOperator<>(Extreme.MIN); | ||
46 | private static final ExtremumOperator MAX_OP = new ExtremumOperator<>(Extreme.MAX); | ||
47 | |||
48 | public static <T extends Comparable<T>> ExtremumOperator<T> getMin() { | ||
49 | return MIN_OP; | ||
50 | } | ||
51 | public static <T extends Comparable<T>> ExtremumOperator<T> getMax() { | ||
52 | return MAX_OP; | ||
53 | } | ||
54 | |||
55 | Extreme extreme; | ||
56 | private ExtremumOperator(Extreme extreme) { | ||
57 | super(); | ||
58 | this.extreme = extreme; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public String getShortDescription() { | ||
63 | String opName = getName(); | ||
64 | return String.format( | ||
65 | "%s incrementally computes the %simum of java.lang.Comparable values, using the default comparison", | ||
66 | opName, opName); | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public String getName() { | ||
71 | return extreme.name().toLowerCase(); | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * @since 2.0 | ||
76 | */ | ||
77 | @Override | ||
78 | public SortedMap<T, Integer> createNeutral() { | ||
79 | return new TreeMap<>(); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * @since 2.0 | ||
84 | */ | ||
85 | @Override | ||
86 | public boolean isNeutral(SortedMap<T, Integer> result) { | ||
87 | return result.isEmpty(); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * @since 2.0 | ||
92 | */ | ||
93 | @Override | ||
94 | public SortedMap<T, Integer> update(SortedMap<T, Integer> oldResult, T updateValue, boolean isInsertion) { | ||
95 | oldResult.compute(updateValue, (value, c) -> { | ||
96 | int count = (c == null) ? 0 : c; | ||
97 | int result = (isInsertion) ? count+1 : count-1; | ||
98 | return (result == 0) ? null : result; | ||
99 | }); | ||
100 | return oldResult; | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * @since 2.0 | ||
105 | */ | ||
106 | @Override | ||
107 | public T getAggregate(SortedMap<T, Integer> result) { | ||
108 | return result.isEmpty() ? null : | ||
109 | extreme.pickFrom(result); | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * @since 2.0 | ||
114 | */ | ||
115 | @Override | ||
116 | public T aggregateStream(Stream<T> stream) { | ||
117 | switch (extreme) { | ||
118 | case MIN: | ||
119 | return stream.min(Comparator.naturalOrder()).orElse(null); | ||
120 | case MAX: | ||
121 | return stream.max(Comparator.naturalOrder()).orElse(null); | ||
122 | default: | ||
123 | return null; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * @since 2.4 | ||
129 | */ | ||
130 | @Override | ||
131 | public SortedMap<T, Integer> clone(SortedMap<T, Integer> original) { | ||
132 | return new TreeMap<T, Integer>(original); | ||
133 | } | ||
134 | |||
135 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerAverageOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerAverageOperator.java new file mode 100644 index 00000000..bf422476 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerAverageOperator.java | |||
@@ -0,0 +1,82 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.OptionalDouble; | ||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
15 | |||
16 | /** | ||
17 | * @author Zoltan Ujhelyi | ||
18 | * @since 2.0 | ||
19 | */ | ||
20 | public class IntegerAverageOperator implements IMultisetAggregationOperator<Integer, AverageAccumulator<Integer>, Double> { | ||
21 | |||
22 | public static final IntegerAverageOperator INSTANCE = new IntegerAverageOperator(); | ||
23 | |||
24 | private IntegerAverageOperator() { | ||
25 | // Singleton, do not call. | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public String getShortDescription() { | ||
30 | return "avg<Integer> incrementally computes the average of java.lang.Integer values"; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public String getName() { | ||
35 | return "avg<Integer>"; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public AverageAccumulator<Integer> createNeutral() { | ||
40 | return new AverageAccumulator<Integer>(0, 0l); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean isNeutral(AverageAccumulator<Integer> result) { | ||
45 | return result.count == 0l; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public AverageAccumulator<Integer> update(AverageAccumulator<Integer> oldResult, Integer updateValue, | ||
50 | boolean isInsertion) { | ||
51 | if (isInsertion) { | ||
52 | oldResult.value += updateValue; | ||
53 | oldResult.count++; | ||
54 | } else { | ||
55 | oldResult.value -= updateValue; | ||
56 | oldResult.count--; | ||
57 | } | ||
58 | return oldResult; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public Double getAggregate(AverageAccumulator<Integer> result) { | ||
63 | return (result.count == 0) | ||
64 | ? null | ||
65 | : ((double)result.value)/result.count; | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public Double aggregateStream(Stream<Integer> stream) { | ||
70 | final OptionalDouble averageOpt = stream.mapToInt(Integer::intValue).average(); | ||
71 | return averageOpt.isPresent() ? averageOpt.getAsDouble() : null; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * @since 2.4 | ||
76 | */ | ||
77 | @Override | ||
78 | public AverageAccumulator<Integer> clone(AverageAccumulator<Integer> original) { | ||
79 | return new AverageAccumulator<Integer>(original.value, original.count); | ||
80 | } | ||
81 | |||
82 | } \ No newline at end of file | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerSumOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerSumOperator.java new file mode 100644 index 00000000..18584256 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/IntegerSumOperator.java | |||
@@ -0,0 +1,61 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.stream.Stream; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AbstractMemorylessAggregationOperator; | ||
14 | |||
15 | /** | ||
16 | * Incrementally computes the sum of java.lang.Integer values | ||
17 | * @author Gabor Bergmann | ||
18 | * @since 1.4 | ||
19 | */ | ||
20 | public class IntegerSumOperator extends AbstractMemorylessAggregationOperator<Integer, Integer> { | ||
21 | public static final IntegerSumOperator INSTANCE = new IntegerSumOperator(); | ||
22 | |||
23 | private IntegerSumOperator() { | ||
24 | // Singleton, do not call. | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public String getShortDescription() { | ||
29 | return "sum<Integer> incrementally computes the sum of java.lang.Integer values"; | ||
30 | } | ||
31 | @Override | ||
32 | public String getName() { | ||
33 | return "sum<Integer>"; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public Integer createNeutral() { | ||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public boolean isNeutral(Integer result) { | ||
43 | return createNeutral().equals(result); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public Integer update(Integer oldResult, Integer updateValue, boolean isInsertion) { | ||
48 | return isInsertion ? | ||
49 | oldResult + updateValue : | ||
50 | oldResult - updateValue; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * @since 2.0 | ||
55 | */ | ||
56 | @Override | ||
57 | public Integer aggregateStream(Stream<Integer> stream) { | ||
58 | return stream.mapToInt(Integer::intValue).sum(); | ||
59 | } | ||
60 | |||
61 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java new file mode 100644 index 00000000..d56c9507 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java | |||
@@ -0,0 +1,82 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.OptionalDouble; | ||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
15 | |||
16 | /** | ||
17 | * @author Zoltan Ujhelyi | ||
18 | * @since 2.0 | ||
19 | */ | ||
20 | public class LongAverageOperator implements IMultisetAggregationOperator<Long, AverageAccumulator<Long>, Double> { | ||
21 | |||
22 | public static final LongAverageOperator INSTANCE = new LongAverageOperator(); | ||
23 | |||
24 | private LongAverageOperator() { | ||
25 | // Singleton, do not call. | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public String getShortDescription() { | ||
30 | return "avg<Integer> incrementally computes the average of java.lang.Integer values"; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public String getName() { | ||
35 | return "avg<Integer>"; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public AverageAccumulator<Long> createNeutral() { | ||
40 | return new AverageAccumulator<Long>(0l, 0l); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean isNeutral(AverageAccumulator<Long> result) { | ||
45 | return result.count == 0l; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public AverageAccumulator<Long> update(AverageAccumulator<Long> oldResult, Long updateValue, | ||
50 | boolean isInsertion) { | ||
51 | if (isInsertion) { | ||
52 | oldResult.value += updateValue; | ||
53 | oldResult.count++; | ||
54 | } else { | ||
55 | oldResult.value -= updateValue; | ||
56 | oldResult.count--; | ||
57 | } | ||
58 | return oldResult; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public Double getAggregate(AverageAccumulator<Long> result) { | ||
63 | return (result.count == 0) | ||
64 | ? null | ||
65 | : ((double)result.value)/result.count; | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public Double aggregateStream(Stream<Long> stream) { | ||
70 | final OptionalDouble averageOpt = stream.mapToLong(Long::longValue).average(); | ||
71 | return averageOpt.isPresent() ? averageOpt.getAsDouble() : null; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * @since 2.4 | ||
76 | */ | ||
77 | @Override | ||
78 | public AverageAccumulator<Long> clone(AverageAccumulator<Long> original) { | ||
79 | return new AverageAccumulator<Long>(original.value, original.count); | ||
80 | } | ||
81 | |||
82 | } \ No newline at end of file | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java new file mode 100644 index 00000000..29ded090 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java | |||
@@ -0,0 +1,61 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.stream.Stream; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AbstractMemorylessAggregationOperator; | ||
14 | |||
15 | /** | ||
16 | * Incrementally computes the sum of java.lang.Long values | ||
17 | * @author Gabor Bergmann | ||
18 | * @since 1.4 | ||
19 | */ | ||
20 | public class LongSumOperator extends AbstractMemorylessAggregationOperator<Long, Long> { | ||
21 | public static final LongSumOperator INSTANCE = new LongSumOperator(); | ||
22 | |||
23 | private LongSumOperator() { | ||
24 | // Singleton, do not call. | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public String getShortDescription() { | ||
29 | return "sum<Long> incrementally computes the sum of java.lang.Long values"; | ||
30 | } | ||
31 | @Override | ||
32 | public String getName() { | ||
33 | return "sum<Long>"; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public Long createNeutral() { | ||
38 | return 0L; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public boolean isNeutral(Long result) { | ||
43 | return createNeutral().equals(result); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public Long update(Long oldResult, Long updateValue, boolean isInsertion) { | ||
48 | return isInsertion ? | ||
49 | oldResult + updateValue : | ||
50 | oldResult - updateValue; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * @since 2.0 | ||
55 | */ | ||
56 | @Override | ||
57 | public Long aggregateStream(Stream<Long> stream) { | ||
58 | return stream.mapToLong(Long::longValue).sum(); | ||
59 | } | ||
60 | |||
61 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/avg.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/avg.java new file mode 100644 index 00000000..c25678aa --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/avg.java | |||
@@ -0,0 +1,39 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType; | ||
12 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory; | ||
14 | |||
15 | /** | ||
16 | * This aggregator calculates the average of the values of a selected aggregate parameter of a called pattern. The aggregate | ||
17 | * parameter is selected with the '#' symbol; the aggregate parameter must not be used outside the aggregator call. The | ||
18 | * other parameters of the call might be bound or unbound; bound parameters limit the matches to consider for the | ||
19 | * summation. | ||
20 | * | ||
21 | * @since 2.0 | ||
22 | * | ||
23 | */ | ||
24 | @AggregatorType( | ||
25 | parameterTypes = {Integer.class, Double.class, Long.class}, | ||
26 | returnTypes = {Double.class, Double.class, Double.class}) | ||
27 | public final class avg implements IAggregatorFactory { | ||
28 | |||
29 | @Override | ||
30 | public BoundAggregator getAggregatorLogic(Class<?> domainClass) { | ||
31 | if (Integer.class.equals(domainClass)) | ||
32 | return new BoundAggregator(IntegerAverageOperator.INSTANCE, Integer.class, Double.class); | ||
33 | if (Double.class.equals(domainClass)) | ||
34 | return new BoundAggregator(DoubleAverageOperator.INSTANCE, Double.class, Double.class); | ||
35 | if (Long.class.equals(domainClass)) | ||
36 | return new BoundAggregator(LongAverageOperator.INSTANCE, Long.class, Double.class); | ||
37 | else throw new IllegalArgumentException(); | ||
38 | } | ||
39 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/count.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/count.java new file mode 100644 index 00000000..8310a0ce --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/count.java | |||
@@ -0,0 +1,33 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType; | ||
12 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory; | ||
14 | |||
15 | /** | ||
16 | * An aggregator to count the number of matches a pattern has. The return of the aggregator is an non-negative integer | ||
17 | * number. | ||
18 | * | ||
19 | * @since 1.4 | ||
20 | * | ||
21 | */ | ||
22 | @AggregatorType(parameterTypes = {Void.class}, returnTypes = {Integer.class}) | ||
23 | public final class count implements IAggregatorFactory { | ||
24 | |||
25 | @Override | ||
26 | public BoundAggregator getAggregatorLogic(Class<?> domainClass) { | ||
27 | if (Void.class.equals(domainClass)) | ||
28 | return new BoundAggregator(null, Void.class, Integer.class); | ||
29 | else throw new IllegalArgumentException(); | ||
30 | } | ||
31 | |||
32 | |||
33 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/max.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/max.java new file mode 100644 index 00000000..e0236223 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/max.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.math.BigDecimal; | ||
12 | import java.math.BigInteger; | ||
13 | import java.util.Calendar; | ||
14 | import java.util.Date; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType; | ||
17 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory; | ||
19 | |||
20 | /** | ||
21 | * This aggregator calculates the maximum value of a selected aggregate parameter of a called pattern. The aggregate | ||
22 | * parameter is selected with the '#' symbol; the aggregate parameter must not be used outside the aggregator call. The | ||
23 | * other parameters of the call might be bound or unbound; bound parameters limit the matches to consider for the | ||
24 | * minimum calculation. | ||
25 | * | ||
26 | * @since 1.4 | ||
27 | * @author Gabor Bergmann | ||
28 | */ | ||
29 | @AggregatorType( | ||
30 | // TODO T extends Comparable? | ||
31 | parameterTypes = {BigDecimal.class, BigInteger.class, Boolean.class, Byte.class, Calendar.class, Character.class, | ||
32 | Date.class, Double.class, Enum.class, Float.class, Integer.class, Long.class, Short.class, String.class}, | ||
33 | returnTypes = {BigDecimal.class, BigInteger.class, Boolean.class, Byte.class, Calendar.class, Character.class, | ||
34 | Date.class, Double.class, Enum.class, Float.class, Integer.class, Long.class, Short.class, String.class}) | ||
35 | public final class max implements IAggregatorFactory { | ||
36 | |||
37 | @Override | ||
38 | public BoundAggregator getAggregatorLogic(Class<?> domainClass) { | ||
39 | if (Comparable.class.isAssignableFrom(domainClass)) | ||
40 | return new BoundAggregator(ExtremumOperator.getMax(), domainClass, domainClass); | ||
41 | else throw new IllegalArgumentException(); | ||
42 | } | ||
43 | |||
44 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/min.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/min.java new file mode 100644 index 00000000..6408c57b --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/min.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.math.BigDecimal; | ||
12 | import java.math.BigInteger; | ||
13 | import java.util.Calendar; | ||
14 | import java.util.Date; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType; | ||
17 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory; | ||
19 | |||
20 | /** | ||
21 | * This aggregator calculates the minimum value of a selected aggregate parameter of a called pattern. The aggregate | ||
22 | * parameter is selected with the '#' symbol; the aggregate parameter must not be used outside the aggregator call. The | ||
23 | * other parameters of the call might be bound or unbound; bound parameters limit the matches to consider for the | ||
24 | * minimum calculation. | ||
25 | * | ||
26 | * @since 1.4 | ||
27 | * | ||
28 | */ | ||
29 | @AggregatorType( | ||
30 | // TODO T extends Comparable? | ||
31 | parameterTypes = {BigDecimal.class, BigInteger.class, Boolean.class, Byte.class, Calendar.class, Character.class, | ||
32 | Date.class, Double.class, Enum.class, Float.class, Integer.class, Long.class, Short.class, String.class}, | ||
33 | returnTypes = {BigDecimal.class, BigInteger.class, Boolean.class, Byte.class, Calendar.class, Character.class, | ||
34 | Date.class, Double.class, Enum.class, Float.class, Integer.class, Long.class, Short.class, String.class}) | ||
35 | public final class min implements IAggregatorFactory { | ||
36 | |||
37 | @Override | ||
38 | public BoundAggregator getAggregatorLogic(Class<?> domainClass) { | ||
39 | if (Comparable.class.isAssignableFrom(domainClass)) | ||
40 | return new BoundAggregator(ExtremumOperator.getMin(), domainClass, domainClass); | ||
41 | else throw new IllegalArgumentException(); | ||
42 | } | ||
43 | |||
44 | } | ||
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/sum.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/sum.java new file mode 100644 index 00000000..69ff2e75 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/sum.java | |||
@@ -0,0 +1,39 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType; | ||
12 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator; | ||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory; | ||
14 | |||
15 | /** | ||
16 | * This aggregator calculates the sum of the values of a selected aggregate parameter of a called pattern. The aggregate | ||
17 | * parameter is selected with the '#' symbol; the aggregate parameter must not be used outside the aggregator call. The | ||
18 | * other parameters of the call might be bound or unbound; bound parameters limit the matches to consider for the | ||
19 | * summation. | ||
20 | * | ||
21 | * @since 1.4 | ||
22 | * | ||
23 | */ | ||
24 | @AggregatorType( | ||
25 | parameterTypes = {Integer.class, Double.class, Long.class}, | ||
26 | returnTypes = {Integer.class, Double.class, Long.class}) | ||
27 | public final class sum implements IAggregatorFactory { | ||
28 | |||
29 | @Override | ||
30 | public BoundAggregator getAggregatorLogic(Class<?> domainClass) { | ||
31 | if (Integer.class.equals(domainClass)) | ||
32 | return new BoundAggregator(IntegerSumOperator.INSTANCE, Integer.class, Integer.class); | ||
33 | if (Double.class.equals(domainClass)) | ||
34 | return new BoundAggregator(DoubleSumOperator.INSTANCE, Double.class, Double.class); | ||
35 | if (Long.class.equals(domainClass)) | ||
36 | return new BoundAggregator(LongSumOperator.INSTANCE, Long.class, Long.class); | ||
37 | else throw new IllegalArgumentException(); | ||
38 | } | ||
39 | } | ||