aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/AndCriterion.java
blob: 0ad2b7a44112f0bc4085243e990f66e2b1161bcc (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.transition.objectives;

import tools.refinery.store.model.Model;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.query.literal.Reduction;

import java.util.ArrayList;
import java.util.Collection;

public final class AndCriterion extends CompositeCriterion {
	AndCriterion(Collection<? extends Criterion> criteria) {
		super(criteria);
	}

	@Override
	public Reduction getReduction(ModelStore store) {
		for (var criterion : getCriteria()) {
			var reduction = criterion.getReduction(store);
			if (reduction == Reduction.ALWAYS_FALSE) {
				return Reduction.ALWAYS_FALSE;
			} else if (reduction == Reduction.NOT_REDUCIBLE) {
				return Reduction.NOT_REDUCIBLE;
			}
		}
		return Reduction.ALWAYS_TRUE;
	}

	@Override
	public CriterionCalculator createCalculator(Model model) {
		var calculators = new ArrayList<CriterionCalculator>();
		for (var criterion : getCriteria()) {
			var reduction = criterion.getReduction(model.getStore());
			if (reduction == Reduction.ALWAYS_FALSE) {
				return () -> false;
			} else if (reduction == Reduction.NOT_REDUCIBLE) {
				calculators.add(criterion.createCalculator(model));
			}
		}
		return () -> {
			for (var calculator : calculators) {
				if (!calculator.isSatisfied()) {
					return false;
				}
			}
			return true;
		};
	}
}