diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/PartialQueryRewriter.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/PartialQueryRewriter.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/PartialQueryRewriter.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/PartialQueryRewriter.java new file mode 100644 index 00000000..79cba263 --- /dev/null +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/PartialQueryRewriter.java | |||
@@ -0,0 +1,53 @@ | |||
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.reasoning.internal; | ||
7 | |||
8 | import tools.refinery.store.query.dnf.Dnf; | ||
9 | import tools.refinery.store.query.rewriter.AbstractRecursiveRewriter; | ||
10 | import tools.refinery.store.reasoning.interpretation.PartialRelationRewriter; | ||
11 | import tools.refinery.store.reasoning.lifting.DnfLifter; | ||
12 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
13 | |||
14 | import java.util.HashMap; | ||
15 | import java.util.Map; | ||
16 | |||
17 | class PartialQueryRewriter extends AbstractRecursiveRewriter { | ||
18 | private final DnfLifter lifter; | ||
19 | private final Map<PartialRelation, PartialRelationRewriter> relationRewriterMap = new HashMap<>(); | ||
20 | |||
21 | PartialQueryRewriter(DnfLifter lifter) { | ||
22 | this.lifter = lifter; | ||
23 | } | ||
24 | |||
25 | DnfLifter getLifter() { | ||
26 | return lifter; | ||
27 | } | ||
28 | |||
29 | PartialRelationRewriter getRelationRewriter(PartialRelation partialRelation) { | ||
30 | var rewriter = relationRewriterMap.get(partialRelation); | ||
31 | if (rewriter == null) { | ||
32 | throw new IllegalArgumentException("Do not know how to interpret partial relation: " + partialRelation); | ||
33 | } | ||
34 | return rewriter; | ||
35 | } | ||
36 | |||
37 | public void addRelationRewriter(PartialRelation partialRelation, PartialRelationRewriter interpreter) { | ||
38 | if (relationRewriterMap.put(partialRelation, interpreter) != null) { | ||
39 | throw new IllegalArgumentException("Duplicate partial relation: " + partialRelation); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | protected Dnf doRewrite(Dnf dnf) { | ||
45 | var builder = Dnf.builderFrom(dnf); | ||
46 | for (var clause : dnf.getClauses()) { | ||
47 | var clauseRewriter = new PartialClauseRewriter(this); | ||
48 | var rewrittenClauses = clauseRewriter.rewriteClause(clause); | ||
49 | builder.clause(rewrittenClauses); | ||
50 | } | ||
51 | return builder.build(); | ||
52 | } | ||
53 | } | ||