diff options
Diffstat (limited to 'subprojects/logic/src/main/java/tools/refinery/logic/equality/DeepDnfEqualityChecker.java')
-rw-r--r-- | subprojects/logic/src/main/java/tools/refinery/logic/equality/DeepDnfEqualityChecker.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/subprojects/logic/src/main/java/tools/refinery/logic/equality/DeepDnfEqualityChecker.java b/subprojects/logic/src/main/java/tools/refinery/logic/equality/DeepDnfEqualityChecker.java new file mode 100644 index 00000000..a49ef080 --- /dev/null +++ b/subprojects/logic/src/main/java/tools/refinery/logic/equality/DeepDnfEqualityChecker.java | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.logic.equality; | ||
7 | |||
8 | import tools.refinery.logic.dnf.Dnf; | ||
9 | import tools.refinery.logic.dnf.DnfClause; | ||
10 | import tools.refinery.logic.dnf.SymbolicParameter; | ||
11 | import tools.refinery.logic.literal.Literal; | ||
12 | import tools.refinery.logic.util.CycleDetectingMapper; | ||
13 | |||
14 | import java.util.List; | ||
15 | |||
16 | public class DeepDnfEqualityChecker implements DnfEqualityChecker { | ||
17 | private final CycleDetectingMapper<Pair, Boolean> mapper = new CycleDetectingMapper<>(this::doCheckEqual); | ||
18 | |||
19 | @Override | ||
20 | public boolean dnfEqual(Dnf left, Dnf right) { | ||
21 | return mapper.map(new Pair(left, right)); | ||
22 | } | ||
23 | |||
24 | public boolean dnfEqualRaw(List<SymbolicParameter> symbolicParameters, | ||
25 | List<? extends List<? extends Literal>> clauses, Dnf other) { | ||
26 | int arity = symbolicParameters.size(); | ||
27 | if (arity != other.arity()) { | ||
28 | return false; | ||
29 | } | ||
30 | for (int i = 0; i < arity; i++) { | ||
31 | if (!symbolicParameters.get(i).getDirection().equals(other.getSymbolicParameters().get(i).getDirection())) { | ||
32 | return false; | ||
33 | } | ||
34 | } | ||
35 | int numClauses = clauses.size(); | ||
36 | if (numClauses != other.getClauses().size()) { | ||
37 | return false; | ||
38 | } | ||
39 | for (int i = 0; i < numClauses; i++) { | ||
40 | var literalEqualityHelper = new SubstitutingLiteralEqualityHelper(this, symbolicParameters, | ||
41 | other.getSymbolicParameters()); | ||
42 | if (!equalsWithSubstitutionRaw(literalEqualityHelper, clauses.get(i), other.getClauses().get(i))) { | ||
43 | return false; | ||
44 | } | ||
45 | } | ||
46 | return true; | ||
47 | } | ||
48 | |||
49 | private boolean equalsWithSubstitutionRaw(LiteralEqualityHelper helper, List<? extends Literal> literals, | ||
50 | DnfClause other) { | ||
51 | int size = literals.size(); | ||
52 | if (size != other.literals().size()) { | ||
53 | return false; | ||
54 | } | ||
55 | for (int i = 0; i < size; i++) { | ||
56 | if (!literals.get(i).equalsWithSubstitution(helper, other.literals().get(i))) { | ||
57 | return false; | ||
58 | } | ||
59 | } | ||
60 | return true; | ||
61 | } | ||
62 | |||
63 | protected boolean doCheckEqual(Pair pair) { | ||
64 | return pair.left.equalsWithSubstitution(this, pair.right); | ||
65 | } | ||
66 | |||
67 | protected List<Pair> getInProgress() { | ||
68 | return mapper.getInProgress(); | ||
69 | } | ||
70 | |||
71 | protected record Pair(Dnf left, Dnf right) { | ||
72 | @Override | ||
73 | public String toString() { | ||
74 | return "(%s, %s)".formatted(left.name(), right.name()); | ||
75 | } | ||
76 | } | ||
77 | } | ||