diff options
author | 2019-05-18 14:25:00 -0400 | |
---|---|---|
committer | 2019-05-18 14:25:00 -0400 | |
commit | 1f60bda44172f1dedaf30785c88163ba6c36a0b9 (patch) | |
tree | c76b0529f2ec3a8fec66677dd9d9d86ab222d754 | |
parent | Model generator for CPS case study (diff) | |
download | VIATRA-Generator-1f60bda44172f1dedaf30785c88163ba6c36a0b9.tar.gz VIATRA-Generator-1f60bda44172f1dedaf30785c88163ba6c36a0b9.tar.zst VIATRA-Generator-1f60bda44172f1dedaf30785c88163ba6c36a0b9.zip |
Interval hull aggregation operator
4 files changed, 166 insertions, 5 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.xtend index 4f0f594f..691c8783 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.xtend | |||
@@ -7,8 +7,8 @@ import org.eclipse.xtend.lib.annotations.Data | |||
7 | 7 | ||
8 | abstract class Interval implements Comparable<Interval> { | 8 | abstract class Interval implements Comparable<Interval> { |
9 | static val PRECISION = 32 | 9 | static val PRECISION = 32 |
10 | static val ROUND_DOWN = new MathContext(PRECISION, RoundingMode.FLOOR) | 10 | package static val ROUND_DOWN = new MathContext(PRECISION, RoundingMode.FLOOR) |
11 | static val ROUND_UP = new MathContext(PRECISION, RoundingMode.CEILING) | 11 | package static val ROUND_UP = new MathContext(PRECISION, RoundingMode.CEILING) |
12 | 12 | ||
13 | private new() { | 13 | private new() { |
14 | } | 14 | } |
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 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval | ||
2 | |||
3 | import java.math.BigDecimal | ||
4 | import java.math.MathContext | ||
5 | import java.util.SortedMap | ||
6 | import java.util.TreeMap | ||
7 | import java.util.stream.Stream | ||
8 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator | ||
9 | |||
10 | abstract 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 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/IntervalAggregatorFactory.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/IntervalAggregatorFactory.xtend index 2b6059da..dee31f67 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/IntervalAggregatorFactory.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/IntervalAggregatorFactory.xtend | |||
@@ -8,14 +8,14 @@ import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.BoundAggre | |||
8 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IAggregatorFactory | 8 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IAggregatorFactory |
9 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor | 9 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor |
10 | 10 | ||
11 | @AggregatorType(parameterTypes = #[Interval], returnTypes = #[Interval]) | 11 | @AggregatorType(parameterTypes=#[Interval], returnTypes=#[Interval]) |
12 | abstract class IntervalAggregatorFactory implements IAggregatorFactory { | 12 | abstract class IntervalAggregatorFactory implements IAggregatorFactory { |
13 | val IntervalAggregationMode mode | 13 | val IntervalAggregationMode mode |
14 | 14 | ||
15 | @FinalFieldsConstructor | 15 | @FinalFieldsConstructor |
16 | protected new() { | 16 | protected new() { |
17 | } | 17 | } |
18 | 18 | ||
19 | override getAggregatorLogic(Class<?> domainClass) { | 19 | override getAggregatorLogic(Class<?> domainClass) { |
20 | if (domainClass == Interval) { | 20 | if (domainClass == Interval) { |
21 | new BoundAggregator(new IntervalAggregationOperator(mode), Interval, Interval) | 21 | new BoundAggregator(new IntervalAggregationOperator(mode), Interval, Interval) |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/intervalHull.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/intervalHull.xtend new file mode 100644 index 00000000..72605f57 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/aggregators/intervalHull.xtend | |||
@@ -0,0 +1,74 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.aggregators | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.Interval | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.IntervalHullAggregatorOperator | ||
5 | import java.math.BigDecimal | ||
6 | import java.math.BigInteger | ||
7 | import java.math.MathContext | ||
8 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.AggregatorType | ||
9 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.BoundAggregator | ||
10 | import org.eclipse.viatra.query.runtime.matchers.psystem.aggregations.IAggregatorFactory | ||
11 | |||
12 | @AggregatorType(parameterTypes=#[BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short], returnTypes=#[ | ||
13 | Interval, Interval, Interval, Interval, Interval, Interval, Interval, Interval]) | ||
14 | class intervalHull implements IAggregatorFactory { | ||
15 | |||
16 | override getAggregatorLogic(Class<?> domainClass) { | ||
17 | new BoundAggregator(getAggregationOperator(domainClass), domainClass, Interval) | ||
18 | } | ||
19 | |||
20 | private def getAggregationOperator(Class<?> domainClass) { | ||
21 | switch (domainClass) { | ||
22 | case BigDecimal: | ||
23 | new IntervalHullAggregatorOperator<BigDecimal>() { | ||
24 | override protected toBigDecimal(BigDecimal value, MathContext mc) { | ||
25 | value.round(mc) | ||
26 | } | ||
27 | } | ||
28 | case BigInteger: | ||
29 | new IntervalHullAggregatorOperator<BigInteger>() { | ||
30 | override protected toBigDecimal(BigInteger value, MathContext mc) { | ||
31 | new BigDecimal(value, mc) | ||
32 | } | ||
33 | } | ||
34 | case Byte: | ||
35 | new IntervalHullAggregatorOperator<Byte>() { | ||
36 | override protected toBigDecimal(Byte value, MathContext mc) { | ||
37 | new BigDecimal(value, mc) | ||
38 | } | ||
39 | } | ||
40 | case Double: | ||
41 | new IntervalHullAggregatorOperator<Double>() { | ||
42 | override protected toBigDecimal(Double value, MathContext mc) { | ||
43 | new BigDecimal(value, mc) | ||
44 | } | ||
45 | } | ||
46 | case Float: | ||
47 | new IntervalHullAggregatorOperator<Float>() { | ||
48 | override protected toBigDecimal(Float value, MathContext mc) { | ||
49 | new BigDecimal(value, mc) | ||
50 | } | ||
51 | } | ||
52 | case Integer: | ||
53 | new IntervalHullAggregatorOperator<Integer>() { | ||
54 | override protected toBigDecimal(Integer value, MathContext mc) { | ||
55 | new BigDecimal(value, mc) | ||
56 | } | ||
57 | } | ||
58 | case Long: | ||
59 | new IntervalHullAggregatorOperator<Long>() { | ||
60 | override protected toBigDecimal(Long value, MathContext mc) { | ||
61 | new BigDecimal(value, mc) | ||
62 | } | ||
63 | } | ||
64 | case Short: | ||
65 | new IntervalHullAggregatorOperator<Short>() { | ||
66 | override protected toBigDecimal(Short value, MathContext mc) { | ||
67 | new BigDecimal(value, mc) | ||
68 | } | ||
69 | } | ||
70 | default: | ||
71 | throw new IllegalArgumentException("Unknown domain class: " + domainClass) | ||
72 | } | ||
73 | } | ||
74 | } | ||