aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java
blob: 5d903f417ad9742c035afc9444b67d35d3c40941 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.scope;

import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder;
import tools.refinery.store.dse.transition.objectives.Criteria;
import tools.refinery.store.dse.transition.objectives.Objectives;
import tools.refinery.store.model.ModelStoreBuilder;
import tools.refinery.store.query.dnf.AnyQuery;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.reasoning.ReasoningBuilder;
import tools.refinery.store.reasoning.literal.CountCandidateLowerBoundLiteral;
import tools.refinery.store.reasoning.representation.PartialRelation;

import java.util.Collection;
import java.util.List;

import static tools.refinery.store.query.literal.Literals.check;
import static tools.refinery.store.query.term.int_.IntTerms.*;
import static tools.refinery.store.reasoning.literal.PartialLiterals.may;
import static tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator.MULTI_VIEW;

class LowerTypeScopePropagator extends TypeScopePropagator {
	private final int lowerBound;

	private LowerTypeScopePropagator(BoundScopePropagator adapter, int lowerBound, RelationalQuery allQuery,
									 RelationalQuery multiQuery) {
		super(adapter, allQuery, multiQuery);
		this.lowerBound = lowerBound;
	}

	@Override
	public void updateBounds() {
		constraint.setLb((lowerBound - getSingleCount()));
	}

	public static class Factory extends TypeScopePropagator.Factory {
		private final PartialRelation type;
		private final int lowerBound;
		private final RelationalQuery allMay;
		private final RelationalQuery multiMay;

		public Factory(PartialRelation type, int lowerBound) {
			this.type = type;
			this.lowerBound = lowerBound;
			allMay = Query.of(type.name() + "#may", (builder, instance) -> builder.clause(
					may(type.call(instance))
			));
			multiMay = Query.of(type.name() + "#multiMay", (builder, instance) -> builder.clause(
					may(type.call(instance)),
					MULTI_VIEW.call(instance)
			));
		}

		@Override
		public TypeScopePropagator createPropagator(BoundScopePropagator adapter) {
			return new LowerTypeScopePropagator(adapter, lowerBound, allMay, multiMay);
		}

		@Override
		protected Collection<AnyQuery> getQueries() {
			return List.of(allMay, multiMay);
		}

		@Override
		public void configure(ModelStoreBuilder storeBuilder) {
			super.configure(storeBuilder);

			var requiredObjects = Query.of(type.name() + "#required", Integer.class, (builder, output) -> builder
					.clause(Integer.class, candidateLowerBound -> List.of(
							new CountCandidateLowerBoundLiteral(candidateLowerBound, type, List.of(Variable.of())),
							output.assign(sub(constant(lowerBound), candidateLowerBound)),
							check(greater(output, constant(0)))
					)));

			storeBuilder.getAdapter(ReasoningBuilder.class).objective(Objectives.value(requiredObjects));
			storeBuilder.tryGetAdapter(DesignSpaceExplorationBuilder.class).ifPresent(dseBuilder ->
					dseBuilder.accept(Criteria.whenNoMatch(requiredObjects)));
		}
	}
}