aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java')
-rw-r--r--subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java
new file mode 100644
index 00000000..2be92464
--- /dev/null
+++ b/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/LowerTypeScopePropagator.java
@@ -0,0 +1,86 @@
1/*
2 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.reasoning.scope;
7
8import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder;
9import tools.refinery.store.dse.transition.objectives.Criteria;
10import tools.refinery.store.dse.transition.objectives.Objectives;
11import tools.refinery.store.model.ModelStoreBuilder;
12import tools.refinery.store.query.dnf.AnyQuery;
13import tools.refinery.store.query.dnf.Query;
14import tools.refinery.store.query.dnf.RelationalQuery;
15import tools.refinery.store.query.term.Variable;
16import tools.refinery.store.reasoning.ReasoningBuilder;
17import tools.refinery.store.reasoning.literal.CountCandidateLowerBoundLiteral;
18import tools.refinery.store.reasoning.representation.PartialRelation;
19
20import java.util.Collection;
21import java.util.List;
22
23import static tools.refinery.store.query.literal.Literals.check;
24import static tools.refinery.store.query.term.int_.IntTerms.*;
25import static tools.refinery.store.reasoning.literal.PartialLiterals.may;
26import static tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator.MULTI_VIEW;
27
28class LowerTypeScopePropagator extends TypeScopePropagator {
29 private final int lowerBound;
30
31 private LowerTypeScopePropagator(BoundScopePropagator adapter, int lowerBound, RelationalQuery allQuery,
32 RelationalQuery multiQuery) {
33 super(adapter, allQuery, multiQuery);
34 this.lowerBound = lowerBound;
35 }
36
37 @Override
38 protected void doUpdateBounds() {
39 constraint.setLb((lowerBound - getSingleCount()));
40 }
41
42 public static class Factory extends TypeScopePropagator.Factory {
43 private final PartialRelation type;
44 private final int lowerBound;
45 private final RelationalQuery allMay;
46 private final RelationalQuery multiMay;
47
48 public Factory(PartialRelation type, int lowerBound) {
49 this.type = type;
50 this.lowerBound = lowerBound;
51 allMay = Query.of(type.name() + "#may", (builder, instance) -> builder.clause(
52 may(type.call(instance))
53 ));
54 multiMay = Query.of(type.name() + "#multiMay", (builder, instance) -> builder.clause(
55 may(type.call(instance)),
56 MULTI_VIEW.call(instance)
57 ));
58 }
59
60 @Override
61 public TypeScopePropagator createPropagator(BoundScopePropagator adapter) {
62 return new LowerTypeScopePropagator(adapter, lowerBound, allMay, multiMay);
63 }
64
65 @Override
66 protected Collection<AnyQuery> getQueries() {
67 return List.of(allMay, multiMay);
68 }
69
70 @Override
71 public void configure(ModelStoreBuilder storeBuilder) {
72 super.configure(storeBuilder);
73
74 var requiredObjects = Query.of(type.name() + "#required", Integer.class, (builder, output) -> builder
75 .clause(Integer.class, candidateLowerBound -> List.of(
76 new CountCandidateLowerBoundLiteral(candidateLowerBound, type, List.of(Variable.of())),
77 output.assign(sub(constant(lowerBound), candidateLowerBound)),
78 check(greater(output, constant(0)))
79 )));
80
81 storeBuilder.getAdapter(ReasoningBuilder.class).objective(Objectives.value(requiredObjects));
82 storeBuilder.tryGetAdapter(DesignSpaceExplorationBuilder.class).ifPresent(dseBuilder ->
83 dseBuilder.accept(Criteria.whenNoMatch(requiredObjects)));
84 }
85 }
86}