diff options
author | 2023-09-14 19:29:36 +0200 | |
---|---|---|
committer | 2023-09-14 19:29:36 +0200 | |
commit | 98ed3b6db5f4e51961a161050cc31c66015116e8 (patch) | |
tree | 8bfd6d9bc8d6ed23b9eb0f889dd40b6c24fe8f92 /subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java | |
parent | Merge pull request #38 from nagilooh/design-space-exploration (diff) | |
parent | Merge remote-tracking branch 'upstream/main' into partial-interpretation (diff) | |
download | refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.gz refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.zst refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.zip |
Merge pull request #39 from kris7t/partial-interpretation
Implement partial interpretation based model generation
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java new file mode 100644 index 00000000..0f402b49 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/ViatraQueryEngine.java | |||
@@ -0,0 +1,153 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro | ||
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | ||
8 | * SPDX-License-Identifier: EPL-2.0 | ||
9 | *******************************************************************************/ | ||
10 | |||
11 | package tools.refinery.viatra.runtime.api; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.api.scope.IBaseIndex; | ||
14 | import tools.refinery.viatra.runtime.api.scope.QueryScope; | ||
15 | import tools.refinery.viatra.runtime.matchers.ViatraQueryRuntimeException; | ||
16 | |||
17 | import java.util.Set; | ||
18 | import java.util.function.Supplier; | ||
19 | import java.util.stream.Collectors; | ||
20 | |||
21 | /** | ||
22 | * A Viatra Query (incremental) evaluation engine, attached to a model such as an EMF resource. The engine hosts pattern matchers, and | ||
23 | * will listen on model update notifications stemming from the given model in order to maintain live results. | ||
24 | * | ||
25 | * <p> | ||
26 | * By default, ViatraQueryEngines do not need to be separately disposed; they will be garbage collected along with the model. | ||
27 | * Advanced users: see {@link AdvancedViatraQueryEngine} if you want fine control over the lifecycle of an engine. | ||
28 | * | ||
29 | * <p> | ||
30 | * Pattern matchers within this engine may be instantiated in the following ways: | ||
31 | * <ul> | ||
32 | * <li>Recommended: instantiate the specific matcher class generated for the pattern by e.g. MyPatternMatcher.on(engine). | ||
33 | * <li>Use {@link #getMatcher(IQuerySpecification)} if the pattern-specific generated matcher API is not available. | ||
34 | * <li>Advanced: use the query specification associated with the generated matcher class to achieve the same. | ||
35 | * </ul> | ||
36 | * Additionally, a group of patterns (see {@link IQueryGroup}) can be initialized together before usage; this may improve | ||
37 | * the performance of pattern matcher construction by trying to gather all necessary information from the model in one go. | ||
38 | * Note that no such improvement is to be expected if the engine is specifically constructed in wildcard mode, | ||
39 | * an option available in some scope implementations | ||
40 | * (see {@link EMFScope#EMFScope(Notifier, BaseIndexOptions)} and {@link BaseIndexOptions#withWildcardMode(boolean)}). | ||
41 | * | ||
42 | * | ||
43 | * @author Bergmann Gábor | ||
44 | * @noextend This class is not intended to be subclassed by clients. | ||
45 | */ | ||
46 | public abstract class ViatraQueryEngine { | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Obtain a (managed) {@link ViatraQueryEngine} to evaluate queries over a given scope specified by an {@link QueryScope}. | ||
51 | * | ||
52 | * <p> For a given matcher scope, the same engine will be returned to any client. | ||
53 | * This facilitates the reuse of internal caches of the engine, greatly improving performance. | ||
54 | * | ||
55 | * <p> The lifecycle of this engine is centrally managed, and will not be disposed as long as the model is retained in memory. | ||
56 | * The engine will be garbage collected along with the model. | ||
57 | * | ||
58 | * <p> | ||
59 | * Advanced users: see {@link AdvancedViatraQueryEngine#createUnmanagedEngine(QueryScope)} to obtain a private, | ||
60 | * unmanaged engine that is not shared with other clients and allows tight control over its lifecycle. | ||
61 | * | ||
62 | * @param scope | ||
63 | * the scope of query evaluation; the definition of the set of model elements that this engine is operates on. | ||
64 | * Provide e.g. a {@link EMFScope} for evaluating queries on an EMF model. | ||
65 | * @return a (managed) {@link ViatraQueryEngine} instance | ||
66 | */ | ||
67 | public static ViatraQueryEngine on(QueryScope scope) { | ||
68 | return ViatraQueryEngineManager.getInstance().getQueryEngine(scope); | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Obtain a (managed) {@link ViatraQueryEngine} to evaluate queries over a given scope specified by an {@link QueryScope}. | ||
73 | * | ||
74 | * <p> For a given matcher scope, the same engine will be returned to any client. | ||
75 | * This facilitates the reuse of internal caches of the engine, greatly improving performance. | ||
76 | * | ||
77 | * <p> The lifecycle of this engine is centrally managed, and will not be disposed as long as the model is retained in memory. | ||
78 | * The engine will be garbage collected along with the model. | ||
79 | * | ||
80 | * <p> | ||
81 | * Advanced users: see {@link AdvancedViatraQueryEngine#createUnmanagedEngine(QueryScope)} to obtain a private, | ||
82 | * unmanaged engine that is not shared with other clients and allows tight control over its lifecycle. | ||
83 | * | ||
84 | * @param scope | ||
85 | * the scope of query evaluation; the definition of the set of model elements that this engine is operates on. | ||
86 | * Provide e.g. a {@link EMFScope} for evaluating queries on an EMF model. | ||
87 | * @return a (managed) {@link ViatraQueryEngine} instance | ||
88 | * @since 1.4 | ||
89 | */ | ||
90 | public static ViatraQueryEngine on(QueryScope scope, ViatraQueryEngineOptions options) { | ||
91 | return ViatraQueryEngineManager.getInstance().getQueryEngine(scope, options); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Provides access to the internal base index component of the engine, responsible for keeping track of basic | ||
96 | * contents of the model. | ||
97 | * | ||
98 | * <p>If using an {@link EMFScope}, | ||
99 | * consider {@link EMFScope#extractUnderlyingEMFIndex(ViatraQueryEngine)} instead to access EMF-specific details. | ||
100 | * | ||
101 | * @return the baseIndex the NavigationHelper maintaining the base index | ||
102 | * @throws ViatraQueryRuntimeException | ||
103 | * if the base index could not be constructed | ||
104 | */ | ||
105 | public abstract IBaseIndex getBaseIndex(); | ||
106 | |||
107 | /** | ||
108 | * Access a pattern matcher based on a {@link IQuerySpecification}. | ||
109 | * Multiple calls will return the same matcher. | ||
110 | * @param querySpecification a {@link IQuerySpecification} that describes a VIATRA query specification | ||
111 | * @return a pattern matcher corresponding to the specification | ||
112 | * @throws ViatraQueryRuntimeException if the matcher could not be initialized | ||
113 | */ | ||
114 | public abstract <Matcher extends ViatraQueryMatcher<? extends IPatternMatch>> Matcher getMatcher(IQuerySpecification<Matcher> querySpecification); | ||
115 | |||
116 | /** | ||
117 | * Access a pattern matcher for the graph pattern with the given fully qualified name. | ||
118 | * Will succeed only if a query specification for this fully qualified name has been generated and registered. | ||
119 | * Multiple calls will return the same matcher unless the registered specification changes. | ||
120 | * | ||
121 | * @param patternFQN the fully qualified name of a VIATRA query specification | ||
122 | * @return a pattern matcher corresponding to the specification | ||
123 | * @throws ViatraQueryRuntimeException if the matcher could not be initialized | ||
124 | */ | ||
125 | public abstract ViatraQueryMatcher<? extends IPatternMatch> getMatcher(String patternFQN); | ||
126 | |||
127 | /** | ||
128 | * Access an existing pattern matcher based on a {@link IQuerySpecification}. | ||
129 | * @param querySpecification a {@link IQuerySpecification} that describes a VIATRA query specification | ||
130 | * @return a pattern matcher corresponding to the specification, <code>null</code> if a matcher does not exist yet. | ||
131 | */ | ||
132 | public abstract <Matcher extends ViatraQueryMatcher<? extends IPatternMatch>> Matcher getExistingMatcher(IQuerySpecification<Matcher> querySpecification); | ||
133 | |||
134 | |||
135 | /** | ||
136 | * Access a copy of available {@link ViatraQueryMatcher} pattern matchers. | ||
137 | * @return a copy of the set of currently available pattern matchers registered on this engine instance | ||
138 | */ | ||
139 | public abstract Set<? extends ViatraQueryMatcher<? extends IPatternMatch>> getCurrentMatchers(); | ||
140 | |||
141 | public Set<IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> getRegisteredQuerySpecifications() { | ||
142 | return getCurrentMatchers().stream().map(ViatraQueryMatcher::getSpecification).collect(Collectors.toSet()); | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * @return the scope of query evaluation; the definition of the set of model elements that this engine is operates on. | ||
147 | */ | ||
148 | public abstract QueryScope getScope(); | ||
149 | |||
150 | public abstract void flushChanges(); | ||
151 | |||
152 | public abstract <T> T withFlushingChanges(Supplier<T> supplier); | ||
153 | } | ||