aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend')
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend87
1 files changed, 87 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend
new file mode 100644
index 00000000..ce48eca1
--- /dev/null
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/IntervalHullAggregatorOperator.xtend
@@ -0,0 +1,87 @@
1package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval
2
3import java.math.BigDecimal
4import java.math.MathContext
5import java.util.SortedMap
6import java.util.TreeMap
7import java.util.stream.Stream
8import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator
9
10abstract class IntervalHullAggregatorOperator<T extends Comparable<T>> implements IMultisetAggregationOperator<T, SortedMap<T, Integer>, Interval> {
11 protected new() {
12 }
13
14 override getName() {
15 "intervalHull"
16 }
17
18 override getShortDescription() {
19 "Calculates the interval hull of a set of numbers"
20 }
21
22 override createNeutral() {
23 new TreeMap
24 }
25
26 override getAggregate(SortedMap<T, Integer> result) {
27 if (result.neutral) {
28 Interval.EMPTY
29 } else {
30 toInterval(result.firstKey, result.lastKey)
31 }
32 }
33
34 protected abstract def BigDecimal toBigDecimal(T value, MathContext mc)
35
36 private def toInterval(T min, T max) {
37 Interval.of(min.toBigDecimal(Interval.ROUND_DOWN), max.toBigDecimal(Interval.ROUND_UP))
38 }
39
40 override isNeutral(SortedMap<T, Integer> result) {
41 result.empty
42 }
43
44 override update(SortedMap<T, Integer> oldResult, T updateValue, boolean isInsertion) {
45 if (isInsertion) {
46 oldResult.compute(updateValue) [ key, value |
47 if (value === null) {
48 1
49 } else if (value > 0) {
50 value + 1
51 } else {
52 throw new IllegalStateException("Invalid count: " + value)
53 }
54 ]
55 } else {
56 oldResult.compute(updateValue) [ key, value |
57 if (value === 1) {
58 null
59 } else if (value > 1) {
60 value - 1
61 } else {
62 throw new IllegalStateException("Invalid count: " + value)
63 }
64 ]
65 }
66 oldResult
67 }
68
69 override aggregateStream(Stream<T> stream) {
70 val iterator = stream.iterator
71 if (!iterator.hasNext) {
72 return Interval.EMPTY
73 }
74 var min = iterator.next
75 var max = min
76 while (iterator.hasNext) {
77 val element = iterator.next
78 if (element.compareTo(min) < 0) {
79 min = element
80 }
81 if (element.compareTo(max) > 0) {
82 max = element
83 }
84 }
85 toInterval(min, max)
86 }
87}