diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/ISearchContext.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/ISearchContext.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/ISearchContext.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/ISearchContext.java new file mode 100644 index 00000000..71aa4aac --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/ISearchContext.java | |||
@@ -0,0 +1,120 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, 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; | ||
10 | |||
11 | import org.apache.log4j.Logger; | ||
12 | import tools.refinery.viatra.runtime.localsearch.matcher.integration.IAdornmentProvider; | ||
13 | import tools.refinery.viatra.runtime.matchers.ViatraQueryRuntimeException; | ||
14 | import tools.refinery.viatra.runtime.matchers.backend.IQueryResultProvider; | ||
15 | import tools.refinery.viatra.runtime.matchers.backend.ResultProviderRequestor; | ||
16 | import tools.refinery.viatra.runtime.matchers.context.IQueryBackendContext; | ||
17 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
18 | import tools.refinery.viatra.runtime.matchers.util.ICache; | ||
19 | import tools.refinery.viatra.runtime.matchers.util.IProvider; | ||
20 | |||
21 | import java.util.Collections; | ||
22 | |||
23 | /** | ||
24 | * The {@link ISearchContext} interface allows search operations to reuse platform services such as the indexer. | ||
25 | * | ||
26 | * @author Zoltan Ujhelyi | ||
27 | * @noreference This interface is not intended to be referenced by clients. | ||
28 | * @noimplement This interface is not intended to be implemented by clients. | ||
29 | * @noextend This interface is not intended to be extended by clients. | ||
30 | * | ||
31 | */ | ||
32 | public interface ISearchContext { | ||
33 | |||
34 | /** | ||
35 | * Provides access to the generic query runtime context of the current engine | ||
36 | * @since 1.7 | ||
37 | */ | ||
38 | IQueryRuntimeContext getRuntimeContext(); | ||
39 | |||
40 | /** | ||
41 | * Returns a matcher for a selected query specification. | ||
42 | * | ||
43 | * @throws ViatraQueryRuntimeException | ||
44 | * @since 1.5 | ||
45 | */ | ||
46 | IQueryResultProvider getMatcher(CallWithAdornment dependency); | ||
47 | |||
48 | /** | ||
49 | * Allows search operations to cache values through the entire lifecycle of the local search backend. The values are | ||
50 | * calculated if not cached before using the given provider, or returned from the cache accordingly. | ||
51 | * | ||
52 | * @since 1.7 | ||
53 | */ | ||
54 | <T> T accessBackendLevelCache(Object key, Class<? extends T> clazz, IProvider<T> valueProvider); | ||
55 | |||
56 | /** | ||
57 | * Returns the engine-specific logger | ||
58 | * | ||
59 | * @since 2.0 | ||
60 | */ | ||
61 | Logger getLogger(); | ||
62 | |||
63 | /** | ||
64 | * @noreference This class is not intended to be referenced by clients. | ||
65 | * @noimplement This interface is not intended to be implemented by clients. | ||
66 | * @noextend This interface is not intended to be extended by clients. | ||
67 | */ | ||
68 | public class SearchContext implements ISearchContext { | ||
69 | |||
70 | private final IQueryRuntimeContext runtimeContext; | ||
71 | |||
72 | private final ICache backendLevelCache; | ||
73 | private final Logger logger; | ||
74 | private final ResultProviderRequestor resultProviderRequestor; | ||
75 | |||
76 | /** | ||
77 | * Initializes a search context using an arbitrary backend context | ||
78 | */ | ||
79 | public SearchContext(IQueryBackendContext backendContext, ICache backendLevelCache, | ||
80 | ResultProviderRequestor resultProviderRequestor) { | ||
81 | this.resultProviderRequestor = resultProviderRequestor; | ||
82 | this.runtimeContext = backendContext.getRuntimeContext(); | ||
83 | this.logger = backendContext.getLogger(); | ||
84 | |||
85 | this.backendLevelCache = backendLevelCache; | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * @throws ViatraQueryRuntimeException | ||
90 | * @since 2.1 | ||
91 | */ | ||
92 | @Override | ||
93 | public IQueryResultProvider getMatcher(CallWithAdornment dependency) { | ||
94 | // Inject adornment for referenced pattern | ||
95 | IAdornmentProvider adornmentProvider = query -> { | ||
96 | if (query.equals(dependency.getReferredQuery())){ | ||
97 | return Collections.singleton(dependency.getAdornment()); | ||
98 | } | ||
99 | return Collections.emptySet(); | ||
100 | }; | ||
101 | return resultProviderRequestor.requestResultProvider(dependency.getCall(), | ||
102 | IAdornmentProvider.toHint(adornmentProvider)); | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public <T> T accessBackendLevelCache(Object key, Class<? extends T> clazz, IProvider<T> valueProvider) { | ||
107 | return backendLevelCache.getValue(key, clazz, valueProvider); | ||
108 | } | ||
109 | |||
110 | public IQueryRuntimeContext getRuntimeContext() { | ||
111 | return runtimeContext; | ||
112 | } | ||
113 | |||
114 | @Override | ||
115 | public Logger getLogger() { | ||
116 | return logger; | ||
117 | } | ||
118 | |||
119 | } | ||
120 | } | ||