diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriterion.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriterion.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriterion.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriterion.java new file mode 100644 index 00000000..e15e4e41 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriterion.java | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStore; | ||
10 | import tools.refinery.store.model.ModelStoreBuilder; | ||
11 | import tools.refinery.store.query.ModelQueryAdapter; | ||
12 | import tools.refinery.store.query.ModelQueryBuilder; | ||
13 | import tools.refinery.store.query.ModelQueryStoreAdapter; | ||
14 | import tools.refinery.store.query.dnf.AnyQuery; | ||
15 | import tools.refinery.store.query.literal.Reduction; | ||
16 | |||
17 | public class QueryCriterion implements Criterion { | ||
18 | protected final boolean satisfiedIfHasMatch; | ||
19 | protected final AnyQuery query; | ||
20 | |||
21 | /** | ||
22 | * Criteria based on the existence of matches evaluated on the model. | ||
23 | * | ||
24 | * @param query The query evaluated on the model. | ||
25 | * @param satisfiedIfHasMatch If true, the criteria satisfied if the query has any match on the model. Otherwise, | ||
26 | * the criteria satisfied if the query has no match on the model. | ||
27 | */ | ||
28 | public QueryCriterion(AnyQuery query, boolean satisfiedIfHasMatch) { | ||
29 | this.query = query; | ||
30 | this.satisfiedIfHasMatch = satisfiedIfHasMatch; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public Reduction getReduction(ModelStore store) { | ||
35 | var queryStore = store.getAdapter(ModelQueryStoreAdapter.class); | ||
36 | var canonicalQuery = queryStore.getCanonicalQuery(query); | ||
37 | var reduction = canonicalQuery.getDnf().getReduction(); | ||
38 | if (satisfiedIfHasMatch) { | ||
39 | return reduction; | ||
40 | } | ||
41 | return reduction.negate(); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public CriterionCalculator createCalculator(Model model) { | ||
46 | var resultSet = model.getAdapter(ModelQueryAdapter.class).getResultSet(query); | ||
47 | if (satisfiedIfHasMatch) { | ||
48 | return () -> resultSet.size() > 0; | ||
49 | } else { | ||
50 | return () -> resultSet.size() == 0; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public void configure(ModelStoreBuilder storeBuilder) { | ||
56 | Criterion.super.configure(storeBuilder); | ||
57 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(query); | ||
58 | } | ||
59 | } | ||