diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchBackend.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchBackend.java | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchBackend.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchBackend.java new file mode 100644 index 00000000..ae51e2b0 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchBackend.java | |||
@@ -0,0 +1,259 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Marton Bur, Zoltan Ujhelyi, Istvan Rath and Daniel Varro | ||
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.localsearch.matcher.integration; | ||
10 | |||
11 | import java.lang.reflect.InvocationTargetException; | ||
12 | import java.util.ArrayList; | ||
13 | import java.util.Arrays; | ||
14 | import java.util.Collection; | ||
15 | import java.util.Collections; | ||
16 | import java.util.HashSet; | ||
17 | import java.util.List; | ||
18 | import java.util.Map; | ||
19 | import java.util.Set; | ||
20 | import java.util.stream.Stream; | ||
21 | |||
22 | import tools.refinery.viatra.runtime.localsearch.exceptions.LocalSearchException; | ||
23 | import tools.refinery.viatra.runtime.localsearch.matcher.ILocalSearchAdapter; | ||
24 | import tools.refinery.viatra.runtime.localsearch.plan.IPlanDescriptor; | ||
25 | import tools.refinery.viatra.runtime.localsearch.plan.IPlanProvider; | ||
26 | import tools.refinery.viatra.runtime.localsearch.plan.SimplePlanProvider; | ||
27 | import tools.refinery.viatra.runtime.matchers.ViatraQueryRuntimeException; | ||
28 | import tools.refinery.viatra.runtime.matchers.backend.IMatcherCapability; | ||
29 | import tools.refinery.viatra.runtime.matchers.backend.IQueryBackend; | ||
30 | import tools.refinery.viatra.runtime.matchers.backend.IQueryBackendHintProvider; | ||
31 | import tools.refinery.viatra.runtime.matchers.backend.IQueryResultProvider; | ||
32 | import tools.refinery.viatra.runtime.matchers.backend.QueryEvaluationHint; | ||
33 | import tools.refinery.viatra.runtime.matchers.backend.ResultProviderRequestor; | ||
34 | import tools.refinery.viatra.runtime.matchers.context.IQueryBackendContext; | ||
35 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
36 | import tools.refinery.viatra.runtime.matchers.psystem.analysis.QueryAnalyzer; | ||
37 | import tools.refinery.viatra.runtime.matchers.psystem.queries.PParameter; | ||
38 | import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery; | ||
39 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
40 | import tools.refinery.viatra.runtime.matchers.util.ICache; | ||
41 | import tools.refinery.viatra.runtime.matchers.util.PurgableCache; | ||
42 | |||
43 | /** | ||
44 | * @author Marton Bur, Zoltan Ujhelyi | ||
45 | * @noextend This class is not intended to be subclassed by clients. | ||
46 | */ | ||
47 | public abstract class LocalSearchBackend implements IQueryBackend { | ||
48 | |||
49 | IQueryBackendContext context; | ||
50 | IPlanProvider planProvider; | ||
51 | private final Set<ILocalSearchAdapter> adapters = new HashSet<>(); | ||
52 | |||
53 | private final PurgableCache generalCache; | ||
54 | |||
55 | private final Map<PQuery, List<AbstractLocalSearchResultProvider>> resultProviderCache = CollectionsFactory.createMap(); | ||
56 | |||
57 | |||
58 | /** | ||
59 | * @since 1.5 | ||
60 | */ | ||
61 | public LocalSearchBackend(IQueryBackendContext context) { | ||
62 | super(); | ||
63 | this.context = context; | ||
64 | this.generalCache = new PurgableCache(); | ||
65 | this.planProvider = new SimplePlanProvider(context.getLogger()); | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public void flushUpdates() { | ||
70 | |||
71 | } | ||
72 | |||
73 | @Override | ||
74 | public IQueryResultProvider getResultProvider(PQuery query) { | ||
75 | return getResultProvider(query, null); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * @since 1.4 | ||
80 | */ | ||
81 | @Override | ||
82 | public IQueryResultProvider getResultProvider(PQuery query, QueryEvaluationHint hints) { | ||
83 | |||
84 | final QueryEvaluationHint callHints = getHintProvider().getQueryEvaluationHint(query).overrideBy(hints); | ||
85 | IMatcherCapability requestedCapability = context.getRequiredMatcherCapability(query, callHints); | ||
86 | for(AbstractLocalSearchResultProvider existingResultProvider : resultProviderCache.getOrDefault(query, Collections.emptyList())){ | ||
87 | if (requestedCapability.canBeSubstitute(existingResultProvider.getCapabilites())){ | ||
88 | return existingResultProvider; | ||
89 | } | ||
90 | } | ||
91 | |||
92 | AbstractLocalSearchResultProvider resultProvider = initializeResultProvider(query, hints); | ||
93 | resultProviderCache.computeIfAbsent(query, k->new ArrayList<>()).add(resultProvider); | ||
94 | resultProvider.prepare(); | ||
95 | return resultProvider; | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * Returns a requestor that this backend uses while processing pattern calls <i>from</i> this query. | ||
100 | * @noreference This method is not intended to be referenced by clients. | ||
101 | * @since 2.1 | ||
102 | */ | ||
103 | public ResultProviderRequestor getResultProviderRequestor(PQuery query, QueryEvaluationHint userHints) { | ||
104 | QueryEvaluationHint hintOnQuery = | ||
105 | context.getHintProvider().getQueryEvaluationHint(query).overrideBy(userHints); | ||
106 | LocalSearchHints defaultsApplied = LocalSearchHints.getDefaultOverriddenBy(hintOnQuery); | ||
107 | |||
108 | return new ResultProviderRequestor(this, | ||
109 | context.getResultProviderAccess(), | ||
110 | context.getHintProvider(), | ||
111 | defaultsApplied.getCallDelegationStrategy(), | ||
112 | userHints, | ||
113 | /* no global overrides */ null); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * @throws ViatraQueryRuntimeException | ||
118 | * @since 1.7 | ||
119 | */ | ||
120 | protected abstract AbstractLocalSearchResultProvider initializeResultProvider(PQuery query, QueryEvaluationHint hints); | ||
121 | |||
122 | @Override | ||
123 | public void dispose() { | ||
124 | resultProviderCache.clear(); | ||
125 | generalCache.purge(); | ||
126 | } | ||
127 | |||
128 | @Override | ||
129 | public boolean isCaching() { | ||
130 | return false; | ||
131 | } | ||
132 | |||
133 | /** | ||
134 | * @since 2.0 | ||
135 | */ | ||
136 | @Override | ||
137 | public AbstractLocalSearchResultProvider peekExistingResultProvider(PQuery query) { | ||
138 | return resultProviderCache.getOrDefault(query, Collections.emptyList()).stream().findAny().orElse(null); | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * @since 1.4 | ||
143 | */ | ||
144 | public IQueryRuntimeContext getRuntimeContext() { | ||
145 | return context.getRuntimeContext(); | ||
146 | } | ||
147 | |||
148 | |||
149 | /** | ||
150 | * @since 1.5 | ||
151 | */ | ||
152 | public QueryAnalyzer getQueryAnalyzer() { | ||
153 | return context.getQueryAnalyzer(); | ||
154 | } | ||
155 | |||
156 | |||
157 | /** | ||
158 | * @since 1.4 | ||
159 | */ | ||
160 | public IQueryBackendHintProvider getHintProvider() { | ||
161 | return context.getHintProvider(); | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * @since 1.5 | ||
166 | */ | ||
167 | public void addAdapter(ILocalSearchAdapter adapter){ | ||
168 | adapters.add(adapter); | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * @since 1.5 | ||
173 | */ | ||
174 | public void removeAdapter(ILocalSearchAdapter adapter){ | ||
175 | adapters.remove(adapter); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Return a copy of the current adapters | ||
180 | * @since 1.7 | ||
181 | */ | ||
182 | public List<ILocalSearchAdapter> getAdapters() { | ||
183 | return new ArrayList<>(adapters); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * @since 1.5 | ||
188 | */ | ||
189 | public IQueryBackendContext getBackendContext() { | ||
190 | return context; | ||
191 | } | ||
192 | |||
193 | /** | ||
194 | * Returns the internal cache of the backend | ||
195 | * @since 1.7 | ||
196 | * @noreference This method is not intended to be referenced by clients. | ||
197 | */ | ||
198 | public ICache getCache() { | ||
199 | return generalCache; | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * Updates the previously stored search plans for one or more given queries, computing a new set of plans if | ||
204 | * necessary. The new plans created are the same that would be created by executing prepare on the given query | ||
205 | * definitions. | ||
206 | * | ||
207 | * @since 2.0 | ||
208 | */ | ||
209 | public void recomputePlans(PQuery... queries) { | ||
210 | recomputePlans(Arrays.stream(queries).flatMap(query -> resultProviderCache.getOrDefault(query, Collections.emptyList()).stream())); | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * Updates the previously stored search plans for one or more given queries, computing a new set of plans if | ||
215 | * necessary The new plans created are the same that would be created by executing prepare on the given query | ||
216 | * definitions. | ||
217 | * | ||
218 | * @since 2.0 | ||
219 | */ | ||
220 | public void recomputePlans(Collection<PQuery> queries) { | ||
221 | recomputePlans(queries.stream().flatMap(query -> resultProviderCache.getOrDefault(query, Collections.emptyList()).stream())); | ||
222 | } | ||
223 | |||
224 | /** | ||
225 | * Updates the previously stored search plans for one or more given queries, computing a new set of plans if | ||
226 | * necessary The new plans created are the same that would be created by executing prepare on the given query | ||
227 | * definitions. | ||
228 | * | ||
229 | * @since 2.0 | ||
230 | */ | ||
231 | public void recomputePlans() { | ||
232 | recomputePlans(resultProviderCache.values().stream().flatMap(List::stream)); | ||
233 | } | ||
234 | |||
235 | private void recomputePlans(Stream<AbstractLocalSearchResultProvider> resultProviders) { | ||
236 | try { | ||
237 | context.getRuntimeContext().coalesceTraversals(() -> { | ||
238 | resultProviders.forEach(resultProvider -> { | ||
239 | resultProvider.forgetAllPlans(); | ||
240 | resultProvider.prepare(); | ||
241 | }); | ||
242 | return null; | ||
243 | }); | ||
244 | } catch (InvocationTargetException e) { | ||
245 | throw new LocalSearchException("Error while rebuilding plans: " + e.getMessage(), e); | ||
246 | } | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * Returns a search plan for a given query and adornment if such plan is already calculated. | ||
251 | * | ||
252 | * @return a previously calculated search plan for the given query and adornment, or null if no such plan exists | ||
253 | * @since 2.0 | ||
254 | */ | ||
255 | public IPlanDescriptor getSearchPlan(PQuery query, Set<PParameter> adornment) { | ||
256 | final AbstractLocalSearchResultProvider resultProvider = peekExistingResultProvider(query); | ||
257 | return (resultProvider == null) ? null : resultProvider.getSearchPlan(adornment); | ||
258 | } | ||
259 | } | ||