diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java index 966e080a..2b0e0f08 100644 --- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/lifting/DnfLifter.java | |||
@@ -1,18 +1,18 @@ | |||
1 | package tools.refinery.store.reasoning.lifting; | 1 | package tools.refinery.store.reasoning.lifting; |
2 | 2 | ||
3 | import org.jetbrains.annotations.Nullable; | 3 | import org.jetbrains.annotations.Nullable; |
4 | import tools.refinery.store.reasoning.literal.ModalDnfCallLiteral; | 4 | import tools.refinery.store.query.dnf.Dnf; |
5 | import tools.refinery.store.reasoning.Reasoning; | 5 | import tools.refinery.store.query.dnf.DnfBuilder; |
6 | import tools.refinery.store.reasoning.literal.ModalRelationLiteral; | 6 | import tools.refinery.store.query.dnf.DnfClause; |
7 | import tools.refinery.store.reasoning.literal.PartialRelationLiteral; | 7 | import tools.refinery.store.query.literal.CallLiteral; |
8 | import tools.refinery.store.query.Dnf; | ||
9 | import tools.refinery.store.query.DnfBuilder; | ||
10 | import tools.refinery.store.query.DnfClause; | ||
11 | import tools.refinery.store.query.Variable; | ||
12 | import tools.refinery.store.query.literal.CallPolarity; | 8 | import tools.refinery.store.query.literal.CallPolarity; |
13 | import tools.refinery.store.query.literal.DnfCallLiteral; | ||
14 | import tools.refinery.store.query.literal.Literal; | 9 | import tools.refinery.store.query.literal.Literal; |
10 | import tools.refinery.store.query.term.DataVariable; | ||
11 | import tools.refinery.store.query.term.Variable; | ||
12 | import tools.refinery.store.reasoning.Reasoning; | ||
13 | import tools.refinery.store.reasoning.literal.ModalConstraint; | ||
15 | import tools.refinery.store.reasoning.literal.Modality; | 14 | import tools.refinery.store.reasoning.literal.Modality; |
15 | import tools.refinery.store.reasoning.literal.PartialLiterals; | ||
16 | import tools.refinery.store.util.CycleDetectingMapper; | 16 | import tools.refinery.store.util.CycleDetectingMapper; |
17 | 17 | ||
18 | import java.util.ArrayList; | 18 | import java.util.ArrayList; |
@@ -46,7 +46,10 @@ public class DnfLifter { | |||
46 | 46 | ||
47 | private boolean liftClause(Modality modality, DnfClause clause, DnfBuilder builder) { | 47 | private boolean liftClause(Modality modality, DnfClause clause, DnfBuilder builder) { |
48 | boolean changed = false; | 48 | boolean changed = false; |
49 | var quantifiedVariables = new HashSet<>(clause.quantifiedVariables()); | 49 | var quantifiedVariables = new HashSet<>(clause.boundVariables() |
50 | .stream() | ||
51 | .filter(DataVariable.class::isInstance) | ||
52 | .toList()); | ||
50 | var literals = clause.literals(); | 53 | var literals = clause.literals(); |
51 | var liftedLiterals = new ArrayList<Literal>(literals.size()); | 54 | var liftedLiterals = new ArrayList<Literal>(literals.size()); |
52 | for (var literal : literals) { | 55 | for (var literal : literals) { |
@@ -65,8 +68,8 @@ public class DnfLifter { | |||
65 | } | 68 | } |
66 | } | 69 | } |
67 | for (var quantifiedVariable : quantifiedVariables) { | 70 | for (var quantifiedVariable : quantifiedVariables) { |
68 | // Quantify over variables that are not already quantified with the expected modality. | 71 | // Quantify over data variables that are not already quantified with the expected modality. |
69 | liftedLiterals.add(Reasoning.EXISTS.call(CallPolarity.POSITIVE, modality, | 72 | liftedLiterals.add(new CallLiteral(CallPolarity.POSITIVE, new ModalConstraint(modality, Reasoning.EXISTS), |
70 | List.of(quantifiedVariable))); | 73 | List.of(quantifiedVariable))); |
71 | } | 74 | } |
72 | builder.clause(liftedLiterals); | 75 | builder.clause(liftedLiterals); |
@@ -75,33 +78,39 @@ public class DnfLifter { | |||
75 | 78 | ||
76 | @Nullable | 79 | @Nullable |
77 | private Variable isExistsLiteralForVariable(Modality modality, Literal literal) { | 80 | private Variable isExistsLiteralForVariable(Modality modality, Literal literal) { |
78 | if (literal instanceof ModalRelationLiteral modalRelationLiteral && | 81 | if (literal instanceof CallLiteral callLiteral && |
79 | modalRelationLiteral.getPolarity() == CallPolarity.POSITIVE && | 82 | callLiteral.getPolarity() == CallPolarity.POSITIVE && |
80 | modalRelationLiteral.getModality() == modality && | 83 | callLiteral.getTarget() instanceof ModalConstraint modalConstraint && |
81 | modalRelationLiteral.getTarget().equals(Reasoning.EXISTS)) { | 84 | modalConstraint.modality() == modality && |
82 | return modalRelationLiteral.getArguments().get(0); | 85 | modalConstraint.constraint().equals(Reasoning.EXISTS)) { |
86 | return callLiteral.getArguments().get(0); | ||
83 | } | 87 | } |
84 | return null; | 88 | return null; |
85 | } | 89 | } |
86 | 90 | ||
87 | @Nullable | 91 | @Nullable |
88 | private Literal liftLiteral(Modality modality, Literal literal) { | 92 | private Literal liftLiteral(Modality modality, Literal literal) { |
89 | if (literal instanceof PartialRelationLiteral partialRelationLiteral) { | 93 | if (!(literal instanceof CallLiteral callLiteral)) { |
90 | return new ModalRelationLiteral(modality, partialRelationLiteral); | 94 | return null; |
91 | } else if (literal instanceof DnfCallLiteral dnfCallLiteral) { | 95 | } |
92 | var polarity = dnfCallLiteral.getPolarity(); | 96 | var target = callLiteral.getTarget(); |
93 | var target = dnfCallLiteral.getTarget(); | 97 | if (target instanceof ModalConstraint modalTarget) { |
94 | var liftedTarget = lift(modality.commute(polarity), target); | 98 | var actualTarget = modalTarget.constraint(); |
95 | if (target.equals(liftedTarget)) { | 99 | if (actualTarget instanceof Dnf dnf) { |
96 | return null; | 100 | var targetModality = modalTarget.modality(); |
101 | var liftedTarget = lift(targetModality, dnf); | ||
102 | return new CallLiteral(callLiteral.getPolarity(), liftedTarget, callLiteral.getArguments()); | ||
97 | } | 103 | } |
98 | return new DnfCallLiteral(polarity, liftedTarget, dnfCallLiteral.getArguments()); | 104 | // No more lifting to be done, pass any modal call to a partial symbol through. |
99 | } else if (literal instanceof ModalDnfCallLiteral modalDnfCallLiteral) { | ||
100 | var liftedTarget = lift(modalDnfCallLiteral.getModality(), modalDnfCallLiteral.getTarget()); | ||
101 | return new DnfCallLiteral(modalDnfCallLiteral.getPolarity(), liftedTarget, | ||
102 | modalDnfCallLiteral.getArguments()); | ||
103 | } else { | ||
104 | return null; | 105 | return null; |
106 | } else if (target instanceof Dnf dnf) { | ||
107 | var polarity = callLiteral.getPolarity(); | ||
108 | var liftedTarget = lift(modality.commute(polarity), dnf); | ||
109 | // Use == instead of equals(), because lift will return the same object by reference is there are no | ||
110 | // changes made during lifting. | ||
111 | return liftedTarget == target ? null : new CallLiteral(polarity, liftedTarget, callLiteral.getArguments()); | ||
112 | } else { | ||
113 | return PartialLiterals.addModality(callLiteral, modality); | ||
105 | } | 114 | } |
106 | } | 115 | } |
107 | } | 116 | } |