diff options
author | 2023-02-17 17:35:48 +0100 | |
---|---|---|
committer | 2023-02-17 17:35:48 +0100 | |
commit | 8183b588cf5bc665c0f96abc6d11b8dc70eecaa3 (patch) | |
tree | bd0001d59cf1d794b35f44161831905e8da62f13 /subprojects/store-query-viatra/src/main | |
parent | refactor: EDSL for DNF literals (diff) | |
download | refinery-8183b588cf5bc665c0f96abc6d11b8dc70eecaa3.tar.gz refinery-8183b588cf5bc665c0f96abc6d11b8dc70eecaa3.tar.zst refinery-8183b588cf5bc665c0f96abc6d11b8dc70eecaa3.zip |
feat: PartialInterpretation representations
Diffstat (limited to 'subprojects/store-query-viatra/src/main')
-rw-r--r-- | subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/Dnf2PQuery.java | 32 |
1 files changed, 7 insertions, 25 deletions
diff --git a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/Dnf2PQuery.java b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/Dnf2PQuery.java index 02e26bde..201e0ed0 100644 --- a/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/Dnf2PQuery.java +++ b/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/pquery/Dnf2PQuery.java | |||
@@ -23,17 +23,16 @@ import tools.refinery.store.query.DnfUtils; | |||
23 | import tools.refinery.store.query.Variable; | 23 | import tools.refinery.store.query.Variable; |
24 | import tools.refinery.store.query.literal.*; | 24 | import tools.refinery.store.query.literal.*; |
25 | import tools.refinery.store.query.view.AnyRelationView; | 25 | import tools.refinery.store.query.view.AnyRelationView; |
26 | import tools.refinery.store.util.CycleDetectingMapper; | ||
26 | 27 | ||
27 | import java.util.*; | 28 | import java.util.*; |
28 | import java.util.function.Function; | 29 | import java.util.function.Function; |
29 | import java.util.stream.Collectors; | ||
30 | 30 | ||
31 | public class Dnf2PQuery { | 31 | public class Dnf2PQuery { |
32 | private static final Object P_CONSTRAINT_LOCK = new Object(); | 32 | private static final Object P_CONSTRAINT_LOCK = new Object(); |
33 | 33 | ||
34 | private final Set<Dnf> translating = new LinkedHashSet<>(); | 34 | private final CycleDetectingMapper<Dnf, RawPQuery> mapper = new CycleDetectingMapper<>(Dnf::name, |
35 | 35 | this::doTranslate); | |
36 | private final Map<Dnf, RawPQuery> dnf2PQueryMap = new HashMap<>(); | ||
37 | 36 | ||
38 | private final Map<AnyRelationView, RelationViewWrapper> view2WrapperMap = new LinkedHashMap<>(); | 37 | private final Map<AnyRelationView, RelationViewWrapper> view2WrapperMap = new LinkedHashMap<>(); |
39 | 38 | ||
@@ -47,24 +46,7 @@ public class Dnf2PQuery { | |||
47 | } | 46 | } |
48 | 47 | ||
49 | public RawPQuery translate(Dnf dnfQuery) { | 48 | public RawPQuery translate(Dnf dnfQuery) { |
50 | if (translating.contains(dnfQuery)) { | 49 | return mapper.map(dnfQuery); |
51 | var path = translating.stream().map(Dnf::name).collect(Collectors.joining(" -> ")); | ||
52 | throw new IllegalStateException("Circular reference %s -> %s detected".formatted(path, | ||
53 | dnfQuery.name())); | ||
54 | } | ||
55 | // We can't use computeIfAbsent here, because translating referenced queries calls this method in a reentrant | ||
56 | // way, which would cause a ConcurrentModificationException with computeIfAbsent. | ||
57 | var pQuery = dnf2PQueryMap.get(dnfQuery); | ||
58 | if (pQuery == null) { | ||
59 | translating.add(dnfQuery); | ||
60 | try { | ||
61 | pQuery = doTranslate(dnfQuery); | ||
62 | dnf2PQueryMap.put(dnfQuery, pQuery); | ||
63 | } finally { | ||
64 | translating.remove(dnfQuery); | ||
65 | } | ||
66 | } | ||
67 | return pQuery; | ||
68 | } | 50 | } |
69 | 51 | ||
70 | public Map<AnyRelationView, IInputKey> getRelationViews() { | 52 | public Map<AnyRelationView, IInputKey> getRelationViews() { |
@@ -72,7 +54,7 @@ public class Dnf2PQuery { | |||
72 | } | 54 | } |
73 | 55 | ||
74 | public RawPQuery getAlreadyTranslated(Dnf dnfQuery) { | 56 | public RawPQuery getAlreadyTranslated(Dnf dnfQuery) { |
75 | return dnf2PQueryMap.get(dnfQuery); | 57 | return mapper.getAlreadyMapped(dnfQuery); |
76 | } | 58 | } |
77 | 59 | ||
78 | private RawPQuery doTranslate(Dnf dnfQuery) { | 60 | private RawPQuery doTranslate(Dnf dnfQuery) { |
@@ -148,7 +130,7 @@ public class Dnf2PQuery { | |||
148 | } | 130 | } |
149 | 131 | ||
150 | private void translateRelationViewLiteral(RelationViewLiteral relationViewLiteral, PBody body) { | 132 | private void translateRelationViewLiteral(RelationViewLiteral relationViewLiteral, PBody body) { |
151 | var substitution = translateSubstitution(relationViewLiteral.getSubstitution(), body); | 133 | var substitution = translateSubstitution(relationViewLiteral.getArguments(), body); |
152 | var polarity = relationViewLiteral.getPolarity(); | 134 | var polarity = relationViewLiteral.getPolarity(); |
153 | var relationView = relationViewLiteral.getTarget(); | 135 | var relationView = relationViewLiteral.getTarget(); |
154 | if (polarity == CallPolarity.POSITIVE) { | 136 | if (polarity == CallPolarity.POSITIVE) { |
@@ -205,7 +187,7 @@ public class Dnf2PQuery { | |||
205 | } | 187 | } |
206 | 188 | ||
207 | private void translateDnfCallLiteral(DnfCallLiteral dnfCallLiteral, PBody body) { | 189 | private void translateDnfCallLiteral(DnfCallLiteral dnfCallLiteral, PBody body) { |
208 | var variablesTuple = translateSubstitution(dnfCallLiteral.getSubstitution(), body); | 190 | var variablesTuple = translateSubstitution(dnfCallLiteral.getArguments(), body); |
209 | var translatedReferred = translate(dnfCallLiteral.getTarget()); | 191 | var translatedReferred = translate(dnfCallLiteral.getTarget()); |
210 | var polarity = dnfCallLiteral.getPolarity(); | 192 | var polarity = dnfCallLiteral.getPolarity(); |
211 | switch (polarity) { | 193 | switch (polarity) { |