diff options
Diffstat (limited to 'subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/analysis/QueryAnalyzer.java')
-rw-r--r-- | subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/analysis/QueryAnalyzer.java | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/analysis/QueryAnalyzer.java b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/analysis/QueryAnalyzer.java new file mode 100644 index 00000000..e3f28cff --- /dev/null +++ b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/analysis/QueryAnalyzer.java | |||
@@ -0,0 +1,194 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.psystem.analysis; | ||
10 | |||
11 | import java.util.HashMap; | ||
12 | import java.util.HashSet; | ||
13 | import java.util.Map; | ||
14 | import java.util.Map.Entry; | ||
15 | import java.util.Set; | ||
16 | import java.util.stream.Collectors; | ||
17 | |||
18 | import tools.refinery.viatra.runtime.matchers.context.IQueryBackendContext; | ||
19 | import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext; | ||
20 | import tools.refinery.viatra.runtime.matchers.planning.helpers.FunctionalDependencyHelper; | ||
21 | import tools.refinery.viatra.runtime.matchers.psystem.PBody; | ||
22 | import tools.refinery.viatra.runtime.matchers.psystem.PConstraint; | ||
23 | import tools.refinery.viatra.runtime.matchers.psystem.PVariable; | ||
24 | import tools.refinery.viatra.runtime.matchers.psystem.annotations.PAnnotation; | ||
25 | import tools.refinery.viatra.runtime.matchers.psystem.annotations.ParameterReference; | ||
26 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.ExportedParameter; | ||
27 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.PositivePatternCall; | ||
28 | import tools.refinery.viatra.runtime.matchers.psystem.queries.PParameter; | ||
29 | import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery; | ||
30 | |||
31 | /** | ||
32 | * Object responsible for computing and caching static query analysis results. | ||
33 | * <p> Any client can instantiate this to statically analyze queries. | ||
34 | * Query backends should share an instance obtained via {@link IQueryBackendContext} to save resources. | ||
35 | * <p> Precondition: all involved queries must be initialized. | ||
36 | * @noinstantiate Considered unstable API; subject to change in future versions. | ||
37 | * Either use the analyzer provided by {@link IQueryBackendContext}, or anticipate | ||
38 | * potential future breakage when instantiating your own analyzer. | ||
39 | * @author Gabor Bergmann | ||
40 | * @since 1.5 | ||
41 | */ | ||
42 | public final class QueryAnalyzer { | ||
43 | |||
44 | private IQueryMetaContext metaContext; | ||
45 | |||
46 | public QueryAnalyzer(IQueryMetaContext metaContext) { | ||
47 | this.metaContext = metaContext; | ||
48 | } | ||
49 | |||
50 | // Functional dependencies | ||
51 | |||
52 | /** | ||
53 | * Maps query and strictness to functional dependencies | ||
54 | */ | ||
55 | private Map<PQuery, Map<Set<Integer>, Set<Integer>>> strictFunctionalDependencyGuarantees = | ||
56 | new HashMap<>(); | ||
57 | private Map<PQuery, Map<Set<Integer>, Set<Integer>>> softFunctionalDependencyGuarantees = | ||
58 | new HashMap<>(); | ||
59 | |||
60 | /** | ||
61 | * Functional dependency information, expressed on query parameters, that the match set of the query is guaranteed to respect. | ||
62 | * <p> The type dependencies shall be expressed on the <i>parameter index</i> integers, NOT the {@link PParameter} object. | ||
63 | * @return a non-null map of functional dependencies on parameters that can be processed by {@link FunctionalDependencyHelper} | ||
64 | * @param strict if true, only "hard" dependencies are taken into account that are strictly enforced by the model representation; | ||
65 | * if false, user-provided soft dependencies (@FunctionalDependency) are included as well, that are anticipated but not guaranteed by the storage mechanism; | ||
66 | * use true if superfluous dependencies may taint the correctness of a computation, false if they would merely impact performance | ||
67 | * @since 1.5 | ||
68 | */ | ||
69 | public Map<Set<Integer>, Set<Integer>> getProjectedFunctionalDependencies(PQuery query, boolean strict) { | ||
70 | Map<PQuery, Map<Set<Integer>, Set<Integer>>> guaranteeStore = strict ? strictFunctionalDependencyGuarantees : softFunctionalDependencyGuarantees; | ||
71 | Map<Set<Integer>, Set<Integer>> dependencies = guaranteeStore.get(query); | ||
72 | // Why not computeIfAbsent? See Bug 532507 | ||
73 | // Invoked method #computeFunctionalDependencies may trigger functional dependency computation for called queries; | ||
74 | // and may thus recurs back into #getProjectedFunctionalDependencies, causing a ConcurrentModificationException | ||
75 | // if the called query has not been previously analyzed. | ||
76 | // | ||
77 | // Note: if patterns are recursive, the empty accumulator will be found in the store | ||
78 | // (this yields a safe lower estimate and guarantees termination for #getProjectedFunctionalDependencies) | ||
79 | // But this case probably will not occur due to recursive queries having a disjunction at some point, | ||
80 | // and thus ignored by #computeFunctionalDependencies | ||
81 | if (dependencies == null) { | ||
82 | dependencies = new HashMap<>(); // accumulator | ||
83 | guaranteeStore.put(query, dependencies); | ||
84 | computeFunctionalDependencies(dependencies, query, strict); | ||
85 | } | ||
86 | return dependencies; | ||
87 | } | ||
88 | |||
89 | private void computeFunctionalDependencies(Map<Set<Integer>, Set<Integer>> accumulator, PQuery query, boolean strict) { | ||
90 | Set<PBody> bodies = query.getDisjunctBodies().getBodies(); | ||
91 | if (bodies.size() == 1) { // no support for recursion or disjunction | ||
92 | |||
93 | PBody body = bodies.iterator().next(); | ||
94 | |||
95 | // collect parameter variables | ||
96 | Map<PVariable, Integer> parameters = body.getSymbolicParameters().stream() | ||
97 | .collect(Collectors.toMap(ExportedParameter::getParameterVariable, | ||
98 | param -> query.getParameters().indexOf(param.getPatternParameter()))); | ||
99 | |||
100 | // collect all internal dependencies | ||
101 | Map<Set<PVariable>, Set<PVariable>> internalDependencies = | ||
102 | getFunctionalDependencies(body.getConstraints(), strict); | ||
103 | |||
104 | // project onto parameter variables | ||
105 | Map<Set<PVariable>, Set<PVariable>> projectedDeps = | ||
106 | FunctionalDependencyHelper.projectDependencies(internalDependencies, parameters.keySet()); | ||
107 | |||
108 | // translate into indices | ||
109 | for (Entry<Set<PVariable>, Set<PVariable>> entry : projectedDeps.entrySet()) { | ||
110 | Set<Integer> left = new HashSet<Integer>(); | ||
111 | Set<Integer> right = new HashSet<Integer>(); | ||
112 | for (PVariable pVariable : entry.getKey()) { | ||
113 | left.add(parameters.get(pVariable)); | ||
114 | } | ||
115 | for (PVariable pVariable : entry.getValue()) { | ||
116 | right.add(parameters.get(pVariable)); | ||
117 | } | ||
118 | accumulator.put(left, right); | ||
119 | } | ||
120 | |||
121 | } else { | ||
122 | // Disjunctive case, no dependencies are inferred | ||
123 | // TODO: we can still salvage the intersection of dependencies IF | ||
124 | // - all bodies have disjoint match sets | ||
125 | // - and we avoid recursion | ||
126 | } | ||
127 | |||
128 | // add annotation-based soft dependencies (regardless of number of bodies) | ||
129 | if (!strict) { | ||
130 | outer: | ||
131 | for (PAnnotation annotation : query.getAnnotationsByName("FunctionalDependency")) { | ||
132 | Set<Integer> lefts = new HashSet<Integer>(); | ||
133 | Set<Integer> rights = new HashSet<Integer>(); | ||
134 | |||
135 | for (Object object : annotation.getAllValues("forEach")) { | ||
136 | ParameterReference parameter = (ParameterReference) object; | ||
137 | Integer position = query.getPositionOfParameter(parameter.getName()); | ||
138 | if (position == null) continue outer; | ||
139 | lefts.add(position); | ||
140 | } | ||
141 | for (Object object : annotation.getAllValues("unique")) { | ||
142 | ParameterReference parameter = (ParameterReference) object; | ||
143 | Integer position = query.getPositionOfParameter(parameter.getName()); | ||
144 | if (position == null) continue outer; | ||
145 | rights.add(position); | ||
146 | } | ||
147 | |||
148 | FunctionalDependencyHelper.includeDependency(accumulator, lefts, rights); | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Functional dependency information, expressed on PVariables within a body, that the selected constraints imply. | ||
155 | * @return a non-null map of functional dependencies on PVariables that can be processed by {@link FunctionalDependencyHelper} | ||
156 | * @param constraints the set of constraints whose consequences will be analyzed | ||
157 | * @param strict if true, only "hard" dependencies are taken into account that are strictly enforced by the model representation; | ||
158 | * if false, user-provided soft dependencies (@FunctionalDependency) are included as well, that are anticipated but not guaranteed by the storage mechanism; | ||
159 | * use true if superfluous dependencies may taint the correctness of a computation, false if they would merely impact performance | ||
160 | * @since 1.5 | ||
161 | */ | ||
162 | public Map<Set<PVariable>, Set<PVariable>> getFunctionalDependencies(Set<? extends PConstraint> constraints, boolean strict) { | ||
163 | Map<Set<PVariable>, Set<PVariable>> accumulator = new HashMap<Set<PVariable>, Set<PVariable>>(); | ||
164 | for (PConstraint pConstraint : constraints){ | ||
165 | if (pConstraint instanceof PositivePatternCall) { | ||
166 | // use query analysis results instead | ||
167 | PositivePatternCall call = (PositivePatternCall) pConstraint; | ||
168 | PQuery query = call.getSupplierKey(); | ||
169 | Map<Set<Integer>, Set<Integer>> paramDependencies = getProjectedFunctionalDependencies(query, strict); | ||
170 | for (Entry<Set<Integer>, Set<Integer>> entry : paramDependencies.entrySet()) { | ||
171 | Set<PVariable> lefts = new HashSet<PVariable>(); | ||
172 | Set<PVariable> rights = new HashSet<PVariable>(); | ||
173 | |||
174 | for (Integer index : entry.getKey()) { | ||
175 | lefts.add(call.getVariableInTuple(index)); | ||
176 | } | ||
177 | for (Integer index : entry.getValue()) { | ||
178 | rights.add(call.getVariableInTuple(index)); | ||
179 | } | ||
180 | |||
181 | FunctionalDependencyHelper.includeDependency(accumulator, | ||
182 | lefts, rights); | ||
183 | } | ||
184 | } else { | ||
185 | // delegate to PConstraint | ||
186 | FunctionalDependencyHelper.includeDependencies(accumulator, | ||
187 | pConstraint.getFunctionalDependencies(metaContext)); | ||
188 | } | ||
189 | } | ||
190 | return accumulator; | ||
191 | } | ||
192 | |||
193 | |||
194 | } | ||