diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/BasePQuery.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/BasePQuery.java | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/BasePQuery.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/BasePQuery.java new file mode 100644 index 00000000..2c03a894 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/BasePQuery.java | |||
@@ -0,0 +1,231 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Bergmann Gabor, 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.matchers.psystem.queries; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collections; | ||
13 | import java.util.HashSet; | ||
14 | import java.util.List; | ||
15 | import java.util.Objects; | ||
16 | import java.util.Optional; | ||
17 | import java.util.Set; | ||
18 | import java.util.stream.Collectors; | ||
19 | import java.util.stream.Stream; | ||
20 | |||
21 | import tools.refinery.viatra.runtime.matchers.ViatraQueryRuntimeException; | ||
22 | import tools.refinery.viatra.runtime.matchers.backend.IQueryBackendFactory; | ||
23 | import tools.refinery.viatra.runtime.matchers.backend.QueryEvaluationHint; | ||
24 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
25 | import tools.refinery.viatra.runtime.matchers.psystem.PBody; | ||
26 | import tools.refinery.viatra.runtime.matchers.psystem.TypeJudgement; | ||
27 | import tools.refinery.viatra.runtime.matchers.psystem.annotations.PAnnotation; | ||
28 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
29 | import tools.refinery.viatra.runtime.matchers.util.Preconditions; | ||
30 | |||
31 | /** | ||
32 | * Default implementation of PQuery. | ||
33 | * | ||
34 | * @author Bergmann Gabor | ||
35 | */ | ||
36 | public abstract class BasePQuery implements PQuery { | ||
37 | |||
38 | protected PQueryStatus status = PQueryStatus.UNINITIALIZED; | ||
39 | /** | ||
40 | * @since 2.0 | ||
41 | */ | ||
42 | protected final PVisibility visibility; | ||
43 | protected List<PProblem> pProblems = new ArrayList<PProblem>(); | ||
44 | private List<PAnnotation> annotations = new ArrayList<PAnnotation>(); | ||
45 | private QueryEvaluationHint evaluationHints = new QueryEvaluationHint(null, (IQueryBackendFactory)null); | ||
46 | PDisjunction canonicalDisjunction; | ||
47 | private List<String> parameterNames = null; // Lazy initialization | ||
48 | |||
49 | /** For traceability only. */ | ||
50 | private List<Object> wrappingQuerySpecifications = new ArrayList<Object>(1); | ||
51 | |||
52 | @Override | ||
53 | public Integer getPositionOfParameter(String parameterName) { | ||
54 | ensureInitialized(); | ||
55 | int index = getParameterNames().indexOf(parameterName); | ||
56 | return index != -1 ? index : null; | ||
57 | } | ||
58 | |||
59 | protected void setStatus(PQueryStatus newStatus) { | ||
60 | this.status = newStatus; | ||
61 | } | ||
62 | |||
63 | protected void addError(PProblem problem) { | ||
64 | status = PQueryStatus.ERROR; | ||
65 | pProblems.add(problem); | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public PQueryStatus getStatus() { | ||
70 | return status; | ||
71 | } | ||
72 | |||
73 | @Override | ||
74 | public List<PProblem> getPProblems() { | ||
75 | return Collections.unmodifiableList(pProblems); | ||
76 | } | ||
77 | |||
78 | @Override | ||
79 | public boolean isMutable() { | ||
80 | return status.equals(PQueryStatus.UNINITIALIZED) || status.equals(PQueryStatus.INITIALIZING); | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public void checkMutability() { | ||
85 | Preconditions.checkState(isMutable(), "Cannot edit query definition %s", getFullyQualifiedName()); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * @since 1.5 | ||
90 | */ | ||
91 | public void setEvaluationHints(QueryEvaluationHint hints) { | ||
92 | checkMutability(); | ||
93 | this.evaluationHints = hints; | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public QueryEvaluationHint getEvaluationHints() { | ||
98 | ensureInitialized(); | ||
99 | return evaluationHints; | ||
100 | // TODO instead of field, compute something from annotations? | ||
101 | } | ||
102 | |||
103 | protected void addAnnotation(PAnnotation annotation) { | ||
104 | checkMutability(); | ||
105 | annotations.add(annotation); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public List<PAnnotation> getAllAnnotations() { | ||
110 | ensureInitialized(); | ||
111 | return new ArrayList<>(annotations); | ||
112 | } | ||
113 | |||
114 | private Stream<PAnnotation> getAnnotationStreamByName(final String name) { | ||
115 | ensureInitialized(); | ||
116 | return annotations.stream().filter(Objects::nonNull).filter(annotation -> Objects.equals(name, annotation.getName())); | ||
117 | } | ||
118 | |||
119 | @Override | ||
120 | public List<PAnnotation> getAnnotationsByName(final String annotationName) { | ||
121 | return getAnnotationStreamByName(annotationName).collect(Collectors.toList()); | ||
122 | } | ||
123 | |||
124 | @Override | ||
125 | public Optional<PAnnotation> getFirstAnnotationByName(String annotationName) { | ||
126 | return getAnnotationStreamByName(annotationName).findFirst(); | ||
127 | } | ||
128 | |||
129 | @Override | ||
130 | public List<String> getParameterNames() { | ||
131 | ensureInitialized(); | ||
132 | if (parameterNames == null) { | ||
133 | parameterNames = getParameters().stream().map(PParameter::getName).collect(Collectors.toList()); | ||
134 | } | ||
135 | return parameterNames; | ||
136 | } | ||
137 | |||
138 | @Override | ||
139 | public Set<PQuery> getDirectReferredQueries() { | ||
140 | ensureInitialized(); | ||
141 | return canonicalDisjunction.getDirectReferredQueries(); | ||
142 | } | ||
143 | |||
144 | @Override | ||
145 | public Set<PQuery> getAllReferredQueries() { | ||
146 | ensureInitialized(); | ||
147 | return canonicalDisjunction.getAllReferredQueries(); | ||
148 | } | ||
149 | |||
150 | |||
151 | @Override | ||
152 | public List<Object> publishedAs() { | ||
153 | return wrappingQuerySpecifications; | ||
154 | } | ||
155 | |||
156 | @Override | ||
157 | public Set<TypeJudgement> getTypeGuarantees() { | ||
158 | ensureInitialized(); | ||
159 | Set<TypeJudgement> result = new HashSet<TypeJudgement>(); | ||
160 | |||
161 | List<PParameter> parameters = getParameters(); | ||
162 | for (int i=0; i<parameters.size(); ++i) { | ||
163 | PParameter parameter = parameters.get(i); | ||
164 | IInputKey declaredUnaryType = parameter.getDeclaredUnaryType(); | ||
165 | if (declaredUnaryType != null) { | ||
166 | result.add(new TypeJudgement(declaredUnaryType, Tuples.staticArityFlatTupleOf(i))); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | return result; | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * @since 2.0 | ||
175 | */ | ||
176 | public BasePQuery(PVisibility visibility) { | ||
177 | super(); | ||
178 | this.visibility = visibility; | ||
179 | } | ||
180 | |||
181 | @Override | ||
182 | public PDisjunction getDisjunctBodies() { | ||
183 | ensureInitialized(); | ||
184 | Preconditions.checkState(!status.equals(PQueryStatus.ERROR), "Query %s contains errors.", getFullyQualifiedName()); | ||
185 | return canonicalDisjunction; | ||
186 | } | ||
187 | |||
188 | @Override | ||
189 | public final void ensureInitialized() { | ||
190 | try { | ||
191 | if (status.equals(PQueryStatus.UNINITIALIZED)) { | ||
192 | setStatus(PQueryStatus.INITIALIZING); | ||
193 | setBodies(doGetContainedBodies()); | ||
194 | setStatus(PQueryStatus.OK); | ||
195 | } | ||
196 | } catch (QueryInitializationException e) { | ||
197 | addError(new PProblem(e, e.getShortMessage())); | ||
198 | throw e; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | protected final void setBodies(Set<PBody> bodies) { | ||
203 | canonicalDisjunction = new PDisjunction(this, bodies); | ||
204 | for (PBody body : canonicalDisjunction.getBodies()) { | ||
205 | body.setStatus(null); | ||
206 | } | ||
207 | setStatus(PQueryStatus.OK); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Creates and returns the bodies of the query. If recalled again, a new instance is created. | ||
212 | * | ||
213 | * @return | ||
214 | * @throws ViatraQueryRuntimeException | ||
215 | */ | ||
216 | protected abstract Set<PBody> doGetContainedBodies(); | ||
217 | |||
218 | @Override | ||
219 | public String toString() { | ||
220 | return String.format("PQuery<%s>=%s", getFullyQualifiedName(), super.toString()); | ||
221 | } | ||
222 | |||
223 | /** | ||
224 | * @since 2.0 | ||
225 | */ | ||
226 | @Override | ||
227 | public PVisibility getVisibility() { | ||
228 | return visibility; | ||
229 | } | ||
230 | |||
231 | } \ No newline at end of file | ||