diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/helpers/StatisticsHelper.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/helpers/StatisticsHelper.java | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/helpers/StatisticsHelper.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/helpers/StatisticsHelper.java new file mode 100644 index 00000000..b4f848a7 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/helpers/StatisticsHelper.java | |||
@@ -0,0 +1,62 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Gabor Bergmann, 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.planning.helpers; | ||
10 | |||
11 | import java.util.Optional; | ||
12 | import java.util.function.BiFunction; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
15 | import tools.refinery.viatra.runtime.matchers.util.Accuracy; | ||
16 | |||
17 | /** | ||
18 | * Helpers dealing with optionally present statistics information | ||
19 | * | ||
20 | * @author Gabor Bergmann | ||
21 | * @since 2.1 | ||
22 | * | ||
23 | */ | ||
24 | public class StatisticsHelper { | ||
25 | |||
26 | private StatisticsHelper() { | ||
27 | // Hidden utility class constructor | ||
28 | } | ||
29 | |||
30 | public static Optional<Double> estimateAverageBucketSize(TupleMask groupMask, Accuracy requiredAccuracy, | ||
31 | BiFunction<TupleMask, Accuracy, Optional<Long>> estimateCardinality) | ||
32 | { | ||
33 | if (groupMask.isIdentity()) return Optional.of(1.0); | ||
34 | |||
35 | Accuracy numeratorAccuracy = requiredAccuracy; | ||
36 | Accuracy denominatorAccuracy = requiredAccuracy.reciprocal(); | ||
37 | TupleMask identityMask = TupleMask.identity(groupMask.sourceWidth); | ||
38 | |||
39 | Optional<Long> totalCountEstimate = estimateCardinality.apply(identityMask, numeratorAccuracy); | ||
40 | Optional<Long> bucketCountEstimate = estimateCardinality.apply(groupMask, denominatorAccuracy); | ||
41 | |||
42 | return totalCountEstimate.flatMap(matchCount -> | ||
43 | bucketCountEstimate.map(bucketCount -> | ||
44 | bucketCount == 0L ? 0L : ((double) matchCount) / bucketCount | ||
45 | )); | ||
46 | } | ||
47 | |||
48 | public static Optional<Double> min(Optional<Double> a, Optional<Double> b) { | ||
49 | if (b.isPresent()) { | ||
50 | if (a.isPresent()) { | ||
51 | return Optional.of(Math.min(a.get(), b.get())); | ||
52 | } else return b; | ||
53 | } else return a; | ||
54 | } | ||
55 | public static Optional<Double> min(Optional<Double> a, double b) { | ||
56 | if (a.isPresent()) { | ||
57 | return Optional.of(Math.min(a.get(), b)); | ||
58 | } else return Optional.of(b); | ||
59 | } | ||
60 | |||
61 | |||
62 | } | ||