aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java
blob: 0700c6960fc563708c6473cb240bc76807cda297 (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
87
88
89
90
91
92
93
94
95
96
97
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.scope;

import tools.refinery.logic.dnf.AnyQuery;
import tools.refinery.logic.dnf.Query;
import tools.refinery.logic.dnf.RelationalQuery;
import tools.refinery.logic.term.Variable;
import tools.refinery.logic.term.uppercardinality.UpperCardinality;
import tools.refinery.logic.term.uppercardinality.UpperCardinalityTerms;
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.reasoning.ReasoningBuilder;
import tools.refinery.store.reasoning.literal.CountCandidateLowerBoundLiteral;
import tools.refinery.store.reasoning.literal.CountUpperBoundLiteral;
import tools.refinery.store.reasoning.representation.PartialRelation;

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

import static tools.refinery.logic.literal.Literals.check;
import static tools.refinery.logic.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
	protected void doUpdateBounds() {
		constraint.setLb((lowerBound - getSingleCount()));
	}

	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)))
					)));
			var tooFewObjects = Query.of(type.name() + "#tooFew", builder -> builder
					.clause(UpperCardinality.class, upperBound -> List.of(
							new CountUpperBoundLiteral(upperBound, type, List.of(Variable.of())),
							check(UpperCardinalityTerms.less(upperBound,
									UpperCardinalityTerms.constant(UpperCardinality.of(lowerBound))))
					)));

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