diff options
author | 2019-05-09 00:49:19 -0400 | |
---|---|---|
committer | 2019-05-09 00:49:19 -0400 | |
commit | 1999ab4733071c6a4c9989c137eb44ec62b09847 (patch) | |
tree | fb4c06a88689e80d4a021bf5ad995a377ed765a6 | |
parent | Implement interval arithmetic without exponentiation (diff) | |
download | VIATRA-Generator-1999ab4733071c6a4c9989c137eb44ec62b09847.tar.gz VIATRA-Generator-1999ab4733071c6a4c9989c137eb44ec62b09847.tar.zst VIATRA-Generator-1999ab4733071c6a4c9989c137eb44ec62b09847.zip |
Interval comparison
2 files changed, 217 insertions, 4 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 cf22315b..93749767 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 | |||
@@ -13,7 +13,47 @@ abstract class Interval { | |||
13 | private new() { | 13 | private new() { |
14 | } | 14 | } |
15 | 15 | ||
16 | abstract def boolean isZero() | 16 | abstract def boolean mustEqual(Interval other) |
17 | |||
18 | abstract def boolean mayEqual(Interval other) | ||
19 | |||
20 | def mustNotEqual(Interval other) { | ||
21 | !mayEqual(other) | ||
22 | } | ||
23 | |||
24 | def mayNotEqual(Interval other) { | ||
25 | !mustEqual(other) | ||
26 | } | ||
27 | |||
28 | abstract def boolean mustBeLessThan(Interval other) | ||
29 | |||
30 | abstract def boolean mayBeLessThan(Interval other) | ||
31 | |||
32 | def mustBeLessThanOrEqual(Interval other) { | ||
33 | !mayBeGreaterThan(other) | ||
34 | } | ||
35 | |||
36 | def mayBeLessThanOrEqual(Interval other) { | ||
37 | !mustBeGreaterThan(other) | ||
38 | } | ||
39 | |||
40 | def mustBeGreaterThan(Interval other) { | ||
41 | other.mustBeLessThan(this) | ||
42 | } | ||
43 | |||
44 | def mayBeGreaterThan(Interval other) { | ||
45 | other.mayBeLessThan(this) | ||
46 | } | ||
47 | |||
48 | def mustBeGreaterThanOrEqual(Interval other) { | ||
49 | other.mustBeLessThanOrEqual(this) | ||
50 | } | ||
51 | |||
52 | def mayBeGreaterThanOrEqual(Interval other) { | ||
53 | other.mayBeLessThanOrEqual(this) | ||
54 | } | ||
55 | |||
56 | abstract def Interval join(Interval other) | ||
17 | 57 | ||
18 | def operator_plus() { | 58 | def operator_plus() { |
19 | this | 59 | this |
@@ -30,9 +70,25 @@ abstract class Interval { | |||
30 | abstract def Interval operator_divide(Interval other) | 70 | abstract def Interval operator_divide(Interval other) |
31 | 71 | ||
32 | public static val EMPTY = new Interval { | 72 | public static val EMPTY = new Interval { |
33 | override isZero() { | 73 | override mustEqual(Interval other) { |
74 | true | ||
75 | } | ||
76 | |||
77 | override mayEqual(Interval other) { | ||
34 | false | 78 | false |
35 | } | 79 | } |
80 | |||
81 | override mustBeLessThan(Interval other) { | ||
82 | true | ||
83 | } | ||
84 | |||
85 | override mayBeLessThan(Interval other) { | ||
86 | false | ||
87 | } | ||
88 | |||
89 | override join(Interval other) { | ||
90 | other | ||
91 | } | ||
36 | 92 | ||
37 | override operator_minus() { | 93 | override operator_minus() { |
38 | EMPTY | 94 | EMPTY |
@@ -98,8 +154,45 @@ abstract class Interval { | |||
98 | this.upper = upper | 154 | this.upper = upper |
99 | } | 155 | } |
100 | 156 | ||
101 | override isZero() { | 157 | override mustEqual(Interval other) { |
102 | upper == BigDecimal.ZERO && lower == BigDecimal.ZERO | 158 | switch (other) { |
159 | case EMPTY: true | ||
160 | NonEmpty: lower == upper && lower == other.lower && lower == other.upper | ||
161 | default: throw new IllegalArgumentException("") | ||
162 | } | ||
163 | } | ||
164 | |||
165 | override mayEqual(Interval other) { | ||
166 | if (other instanceof NonEmpty) { | ||
167 | (lower === null || other.upper === null || lower <= other.upper) && | ||
168 | (other.lower === null || upper === null || other.lower <= upper) | ||
169 | } else { | ||
170 | false | ||
171 | } | ||
172 | } | ||
173 | |||
174 | override mustBeLessThan(Interval other) { | ||
175 | switch (other) { | ||
176 | case EMPTY: true | ||
177 | NonEmpty: upper !== null && other.lower !== null && upper < other.lower | ||
178 | default: throw new IllegalArgumentException("") | ||
179 | } | ||
180 | } | ||
181 | |||
182 | override mayBeLessThan(Interval other) { | ||
183 | if (other instanceof NonEmpty) { | ||
184 | lower === null || other.upper === null || lower < other.upper | ||
185 | } else { | ||
186 | false | ||
187 | } | ||
188 | } | ||
189 | |||
190 | override join(Interval other) { | ||
191 | switch (other) { | ||
192 | case EMPTY: this | ||
193 | NonEmpty: new NonEmpty(lower.tryMin(other.lower), upper.tryMin(other.upper)) | ||
194 | default: throw new IllegalArgumentException("") | ||
195 | } | ||
103 | } | 196 | } |
104 | 197 | ||
105 | override operator_minus() { | 198 | override operator_minus() { |
diff --git a/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/RelationTest.xtend b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/RelationTest.xtend new file mode 100644 index 00000000..23fc69ea --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/RelationTest.xtend | |||
@@ -0,0 +1,120 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests.interval | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.Interval | ||
4 | import java.util.Collection | ||
5 | import org.junit.Assert | ||
6 | import org.junit.Test | ||
7 | import org.junit.runner.RunWith | ||
8 | import org.junit.runners.Parameterized | ||
9 | import org.junit.runners.Parameterized.Parameter | ||
10 | import org.junit.runners.Parameterized.Parameters | ||
11 | |||
12 | import static hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.Interval.* | ||
13 | |||
14 | @RunWith(Parameterized) | ||
15 | class RelationTest { | ||
16 | @Parameters(name = "{index}: {0} <> {1}") | ||
17 | static def Collection<Object[]> data() { | ||
18 | #[ | ||
19 | #[EMPTY, EMPTY, true, false, true, false], | ||
20 | #[EMPTY, between(1, 2), true, false, true, false], | ||
21 | #[between(1, 2), EMPTY, true, false, true, false], | ||
22 | #[upTo(1), upTo(0), false, true, false, true], | ||
23 | #[upTo(1), upTo(1), false, true, false, true], | ||
24 | #[upTo(1), upTo(2), false, true, false, true], | ||
25 | #[upTo(1), above(0), false, true, false, true], | ||
26 | #[upTo(1), above(1), false, true, false, true], | ||
27 | #[upTo(1), above(2), false, false, true, true], | ||
28 | #[upTo(1), between(-1, -1), false, true, false, true], | ||
29 | #[upTo(1), between(-1, 0), false, true, false, true], | ||
30 | #[upTo(1), between(-1, 1), false, true, false, true], | ||
31 | #[upTo(1), between(-1, 2), false, true, false, true], | ||
32 | #[upTo(1), between(1, 1), false, true, false, true], | ||
33 | #[upTo(1), between(1, 2), false, true, false, true], | ||
34 | #[upTo(1), between(2, 2), false, false, true, true], | ||
35 | #[upTo(1), between(2, 3), false, false, true, true], | ||
36 | #[above(1), upTo(0), false, false, false, false], | ||
37 | #[above(1), upTo(1), false, true, false, false], | ||
38 | #[above(1), upTo(2), false, true, false, true], | ||
39 | #[above(1), above(0), false, true, false, true], | ||
40 | #[above(1), above(1), false, true, false, true], | ||
41 | #[above(1), above(2), false, true, false, true], | ||
42 | #[above(1), between(-1, -1), false, false, false, false], | ||
43 | #[above(1), between(-1, 0), false, false, false, false], | ||
44 | #[above(1), between(-1, 1), false, true, false, false], | ||
45 | #[above(1), between(-1, 2), false, true, false, true], | ||
46 | #[above(1), between(1, 1), false, true, false, false], | ||
47 | #[above(1), between(1, 2), false, true, false, true], | ||
48 | #[above(1), between(2, 2), false, true, false, true], | ||
49 | #[above(1), between(2, 3), false, true, false, true], | ||
50 | #[between(1, 1), upTo(0), false, false, false, false], | ||
51 | #[between(1, 1), upTo(1), false, true, false, false], | ||
52 | #[between(1, 1), upTo(2), false, true, false, true], | ||
53 | #[between(1, 1), above(0), false, true, false, true], | ||
54 | #[between(1, 1), above(1), false, true, false, true], | ||
55 | #[between(1, 1), above(2), false, false, true, true], | ||
56 | #[between(1, 1), between(-1, -1), false, false, false, false], | ||
57 | #[between(1, 1), between(-1, 0), false, false, false, false], | ||
58 | #[between(1, 1), between(-1, 1), false, true, false, false], | ||
59 | #[between(1, 1), between(-1, 2), false, true, false, true], | ||
60 | #[between(1, 1), between(1, 1), true, true, false, false], | ||
61 | #[between(1, 1), between(1, 2), false, true, false, true], | ||
62 | #[between(1, 1), between(2, 2), false, false, true, true], | ||
63 | #[between(1, 1), between(2, 3), false, false, true, true], | ||
64 | #[between(-1, 1), upTo(-2), false, false, false, false], | ||
65 | #[between(-1, 1), upTo(-1), false, true, false, false], | ||
66 | #[between(-1, 1), upTo(0), false, true, false, true], | ||
67 | #[between(-1, 1), upTo(1), false, true, false, true], | ||
68 | #[between(-1, 1), upTo(2), false, true, false, true], | ||
69 | #[between(-1, 1), above(-2), false, true, false, true], | ||
70 | #[between(-1, 1), above(-1), false, true, false, true], | ||
71 | #[between(-1, 1), above(0), false, true, false, true], | ||
72 | #[between(-1, 1), above(1), false, true, false, true], | ||
73 | #[between(-1, 1), above(2), false, false, true, true], | ||
74 | #[between(-1, 1), between(-3, -2), false, false, false, false], | ||
75 | #[between(-1, 1), between(-2, -2), false, false, false, false], | ||
76 | #[between(-1, 1), between(-2, -1), false, true, false, false], | ||
77 | #[between(-1, 1), between(-2, 0), false, true, false, true], | ||
78 | #[between(-1, 1), between(-2, 1), false, true, false, true], | ||
79 | #[between(-1, 1), between(-2, 2), false, true, false, true], | ||
80 | #[between(-1, 1), between(-1, -1), false, true, false, false], | ||
81 | #[between(-1, 1), between(-1, 0), false, true, false, true], | ||
82 | #[between(-1, 1), between(-1, 1), false, true, false, true], | ||
83 | #[between(-1, 1), between(-1, 2), false, true, false, true], | ||
84 | #[between(-1, 1), between(0, 0), false, true, false, true], | ||
85 | #[between(-1, 1), between(0, 1), false, true, false, true], | ||
86 | #[between(-1, 1), between(0, 2), false, true, false, true], | ||
87 | #[between(-1, 1), between(1, 1), false, true, false, true], | ||
88 | #[between(-1, 1), between(1, 2), false, true, false, true], | ||
89 | #[between(-1, 1), between(2, 2), false, false, true, true], | ||
90 | #[between(-1, 1), between(2, 3), false, false, true, true] | ||
91 | ] | ||
92 | } | ||
93 | |||
94 | @Parameter(0) public var Interval a | ||
95 | @Parameter(1) public var Interval b | ||
96 | @Parameter(2) public var boolean mustEqual | ||
97 | @Parameter(3) public var boolean mayEqual | ||
98 | @Parameter(4) public var boolean mustBeLessThan | ||
99 | @Parameter(5) public var boolean mayBeLessThan | ||
100 | |||
101 | @Test | ||
102 | def void mustEqualTest() { | ||
103 | Assert.assertEquals(mustEqual, a.mustEqual(b)) | ||
104 | } | ||
105 | |||
106 | @Test | ||
107 | def void mayEqualTest() { | ||
108 | Assert.assertEquals(mayEqual, a.mayEqual(b)) | ||
109 | } | ||
110 | |||
111 | @Test | ||
112 | def void mustBeLessThanTest() { | ||
113 | Assert.assertEquals(mustBeLessThan, a.mustBeLessThan(b)) | ||
114 | } | ||
115 | |||
116 | @Test | ||
117 | def void mayBeLessThanTest() { | ||
118 | Assert.assertEquals(mayBeLessThan, a.mayBeLessThan(b)) | ||
119 | } | ||
120 | } | ||