aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.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/Interval.xtend')
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/interval/Interval.xtend88
1 files changed, 72 insertions, 16 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 6ea96866..229656c0 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
@@ -5,7 +5,7 @@ import java.math.MathContext
5import java.math.RoundingMode 5import java.math.RoundingMode
6import org.eclipse.xtend.lib.annotations.Data 6import org.eclipse.xtend.lib.annotations.Data
7 7
8abstract class Interval { 8abstract 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 static val ROUND_DOWN = new MathContext(PRECISION, RoundingMode.FLOOR)
11 static val ROUND_UP = new MathContext(PRECISION, RoundingMode.CEILING) 11 static val ROUND_UP = new MathContext(PRECISION, RoundingMode.CEILING)
@@ -24,35 +24,35 @@ abstract class Interval {
24 def mayNotEqual(Interval other) { 24 def mayNotEqual(Interval other) {
25 !mustEqual(other) 25 !mustEqual(other)
26 } 26 }
27 27
28 abstract def boolean mustBeLessThan(Interval other) 28 abstract def boolean mustBeLessThan(Interval other)
29 29
30 abstract def boolean mayBeLessThan(Interval other) 30 abstract def boolean mayBeLessThan(Interval other)
31 31
32 def mustBeLessThanOrEqual(Interval other) { 32 def mustBeLessThanOrEqual(Interval other) {
33 !mayBeGreaterThan(other) 33 !mayBeGreaterThan(other)
34 } 34 }
35 35
36 def mayBeLessThanOrEqual(Interval other) { 36 def mayBeLessThanOrEqual(Interval other) {
37 !mustBeGreaterThan(other) 37 !mustBeGreaterThan(other)
38 } 38 }
39 39
40 def mustBeGreaterThan(Interval other) { 40 def mustBeGreaterThan(Interval other) {
41 other.mustBeLessThan(this) 41 other.mustBeLessThan(this)
42 } 42 }
43 43
44 def mayBeGreaterThan(Interval other) { 44 def mayBeGreaterThan(Interval other) {
45 other.mayBeLessThan(this) 45 other.mayBeLessThan(this)
46 } 46 }
47 47
48 def mustBeGreaterThanOrEqual(Interval other) { 48 def mustBeGreaterThanOrEqual(Interval other) {
49 other.mustBeLessThanOrEqual(this) 49 other.mustBeLessThanOrEqual(this)
50 } 50 }
51 51
52 def mayBeGreaterThanOrEqual(Interval other) { 52 def mayBeGreaterThanOrEqual(Interval other) {
53 other.mayBeLessThanOrEqual(this) 53 other.mayBeLessThanOrEqual(this)
54 } 54 }
55 55
56 abstract def Interval join(Interval other) 56 abstract def Interval join(Interval other)
57 57
58 def operator_plus() { 58 def operator_plus() {
@@ -65,6 +65,8 @@ abstract class Interval {
65 65
66 abstract def Interval operator_minus(Interval other) 66 abstract def Interval operator_minus(Interval other)
67 67
68 abstract def Interval operator_multiply(int count)
69
68 abstract def Interval operator_multiply(Interval other) 70 abstract def Interval operator_multiply(Interval other)
69 71
70 abstract def Interval operator_divide(Interval other) 72 abstract def Interval operator_divide(Interval other)
@@ -77,15 +79,15 @@ abstract class Interval {
77 override mayEqual(Interval other) { 79 override mayEqual(Interval other) {
78 false 80 false
79 } 81 }
80 82
81 override mustBeLessThan(Interval other) { 83 override mustBeLessThan(Interval other) {
82 true 84 true
83 } 85 }
84 86
85 override mayBeLessThan(Interval other) { 87 override mayBeLessThan(Interval other) {
86 false 88 false
87 } 89 }
88 90
89 override join(Interval other) { 91 override join(Interval other) {
90 other 92 other
91 } 93 }
@@ -102,6 +104,10 @@ abstract class Interval {
102 EMPTY 104 EMPTY
103 } 105 }
104 106
107 override operator_multiply(int count) {
108 EMPTY
109 }
110
105 override operator_multiply(Interval other) { 111 override operator_multiply(Interval other) {
106 EMPTY 112 EMPTY
107 } 113 }
@@ -113,6 +119,15 @@ abstract class Interval {
113 override toString() { 119 override toString() {
114 "∅" 120 "∅"
115 } 121 }
122
123 override compareTo(Interval o) {
124 if (o == EMPTY) {
125 0
126 } else {
127 -1
128 }
129 }
130
116 } 131 }
117 132
118 public static val Interval ZERO = new NonEmpty(BigDecimal.ZERO, BigDecimal.ZERO) 133 public static val Interval ZERO = new NonEmpty(BigDecimal.ZERO, BigDecimal.ZERO)
@@ -170,7 +185,7 @@ abstract class Interval {
170 false 185 false
171 } 186 }
172 } 187 }
173 188
174 override mustBeLessThan(Interval other) { 189 override mustBeLessThan(Interval other) {
175 switch (other) { 190 switch (other) {
176 case EMPTY: true 191 case EMPTY: true
@@ -178,7 +193,7 @@ abstract class Interval {
178 default: throw new IllegalArgumentException("") 193 default: throw new IllegalArgumentException("")
179 } 194 }
180 } 195 }
181 196
182 override mayBeLessThan(Interval other) { 197 override mayBeLessThan(Interval other) {
183 if (other instanceof NonEmpty) { 198 if (other instanceof NonEmpty) {
184 lower === null || other.upper === null || lower < other.upper 199 lower === null || other.upper === null || lower < other.upper
@@ -186,7 +201,7 @@ abstract class Interval {
186 false 201 false
187 } 202 }
188 } 203 }
189 204
190 override join(Interval other) { 205 override join(Interval other) {
191 switch (other) { 206 switch (other) {
192 case EMPTY: this 207 case EMPTY: this
@@ -245,6 +260,14 @@ abstract class Interval {
245 } 260 }
246 } 261 }
247 262
263 override operator_multiply(int count) {
264 val bigCount = new BigDecimal(count)
265 new NonEmpty(
266 lower.tryMultiply(bigCount, ROUND_DOWN),
267 upper.tryMultiply(bigCount, ROUND_UP)
268 )
269 }
270
248 override operator_multiply(Interval other) { 271 override operator_multiply(Interval other) {
249 switch (other) { 272 switch (other) {
250 case EMPTY: EMPTY 273 case EMPTY: EMPTY
@@ -431,5 +454,38 @@ abstract class Interval {
431 override toString() { 454 override toString() {
432 '''«IF lower === null»(-∞«ELSE»[«lower»«ENDIF», «IF upper === null»∞)«ELSE»«upper»]«ENDIF»''' 455 '''«IF lower === null»(-∞«ELSE»[«lower»«ENDIF», «IF upper === null»∞)«ELSE»«upper»]«ENDIF»'''
433 } 456 }
457
458 override compareTo(Interval o) {
459 switch (o) {
460 case EMPTY: 1
461 NonEmpty: compareTo(o)
462 default: throw new IllegalArgumentException("")
463 }
464 }
465
466 def compareTo(NonEmpty o) {
467 if (lower === null) {
468 if (o.lower !== null) {
469 return -1
470 }
471 } else if (o.lower === null) { // lower !== null
472 return 1
473 } else { // both lower and o.lower are finite
474 val lowerDifference = lower.compareTo(o.lower)
475 if (lowerDifference != 0) {
476 return lowerDifference
477 }
478 }
479 if (upper === null) {
480 if (o.upper === null) {
481 return 0
482 } else {
483 return 1
484 }
485 } else if (o.upper === null) { // upper !== null
486 return -1
487 }
488 upper.compareTo(o.upper)
489 }
434 } 490 }
435} 491}