aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/AbstractPolyhedronSaturationOperator.xtend
blob: 94f97e94b0645dfd7e2a9f3fba1bc41869349071 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality

import com.google.common.collect.ImmutableList
import org.eclipse.xtend.lib.annotations.Accessors

abstract class AbstractPolyhedronSaturationOperator implements PolyhedronSaturationOperator {
	@Accessors val Polyhedron polyhedron

	new(Polyhedron polyhedron) {
		if (polyhedron.dimensions.empty) {
			throw new IllegalArgumentException("Polyhedron must have at least one dimension.")
		}
		this.polyhedron = polyhedron
	}

	override saturate() {
		if (polyhedron.expressionsToSaturate.empty) {
			return PolyhedronSaturationResult.SATURATED
		}
		for (constraint : polyhedron.constraints) {
			if (constraint.zero) {
				if (constraint.lowerBound !== null && constraint.lowerBound > 0) {
					return PolyhedronSaturationResult.EMPTY
				}
				if (constraint.upperBound !== null && constraint.upperBound < 0) {
					return PolyhedronSaturationResult.EMPTY
				}
			} else {
				if (constraint.lowerBound !== null && constraint.upperBound !== null &&
					constraint.upperBound < constraint.lowerBound) {
					return PolyhedronSaturationResult.EMPTY
				}
			}
		}
		doSaturate()
	}

	protected def PolyhedronSaturationResult doSaturate()

	protected def getNonTrivialConstraints() {
		ImmutableList.copyOf(polyhedron.constraints.filter [ constraint |
			(constraint.lowerBound !== null || constraint.upperBound !== null) && !constraint.zero
		])
	}

	private static def isZero(LinearConstraint constraint) {
		constraint.coefficients.values.forall[it == 0]
	}

	override close() throws Exception {
		// Nothing to close by default.
	}
}