aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query-viatra/src/main/java/tools/refinery/store/query/viatra/internal/rete/RefineryReteRecipeCompiler.java
blob: fd1b48d8601c5863c5600a638f89333fcef47df2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2010-2014, Bergmann Gabor, Istvan Rath and Daniel Varro
 * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.store.query.viatra.internal.rete;

import org.apache.log4j.Logger;
import org.eclipse.viatra.query.runtime.matchers.backend.IQueryBackendHintProvider;
import org.eclipse.viatra.query.runtime.matchers.context.IQueryCacheContext;
import org.eclipse.viatra.query.runtime.matchers.context.IQueryMetaContext;
import org.eclipse.viatra.query.runtime.matchers.planning.IQueryPlannerStrategy;
import org.eclipse.viatra.query.runtime.matchers.planning.SubPlan;
import org.eclipse.viatra.query.runtime.matchers.planning.operations.PApply;
import org.eclipse.viatra.query.runtime.matchers.planning.operations.PEnumerate;
import org.eclipse.viatra.query.runtime.matchers.psystem.EnumerablePConstraint;
import org.eclipse.viatra.query.runtime.matchers.psystem.analysis.QueryAnalyzer;
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery;
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.PDisjunctionRewriterCacher;
import org.eclipse.viatra.query.runtime.matchers.tuple.Tuple;
import org.eclipse.viatra.query.runtime.rete.construction.plancompiler.CompilerHelper;
import org.eclipse.viatra.query.runtime.rete.construction.plancompiler.ReteRecipeCompiler;
import org.eclipse.viatra.query.runtime.rete.matcher.TimelyConfiguration;
import org.eclipse.viatra.query.runtime.rete.recipes.ReteNodeRecipe;
import org.eclipse.viatra.query.runtime.rete.traceability.CompiledSubPlan;
import org.eclipse.viatra.query.runtime.rete.traceability.PlanningTrace;
import org.eclipse.viatra.query.runtime.rete.util.ReteHintOptions;
import org.jetbrains.annotations.Nullable;
import tools.refinery.store.query.viatra.internal.pquery.RepresentativeElectionConstraint;
import tools.refinery.store.query.viatra.internal.rete.recipe.RefineryRecipesFactory;
import tools.refinery.store.query.viatra.internal.pquery.rewriter.RefineryPBodyNormalizer;
import tools.refinery.store.query.viatra.internal.pquery.rewriter.RefinerySurrogateQueryRewriter;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.util.Map;

// Since we don't modify VIATRA code, this is our last resort.
@SuppressWarnings("squid:S3011")
public class RefineryReteRecipeCompiler extends ReteRecipeCompiler {
	private static final MethodHandle GET_SUB_PLAN_COMPILER_CACHE;
	private static final MethodHandle GET_COMPILER_BACK_TRACE;
	private static final Field NORMALIZER_FIELD;
	private static final MethodHandle DO_COMPILE_DISPATCH;
	private static final MethodHandle COMPILE_TO_NATURAL_JOIN;
	private static final MethodHandle REFER_QUERY;

	static {
		MethodHandles.Lookup lookup;
		try {
			lookup = MethodHandles.privateLookupIn(ReteRecipeCompiler.class, MethodHandles.lookup());
		} catch (IllegalAccessException e) {
			throw new IllegalStateException("Failed to create lookup", e);
		}
		try {
			GET_SUB_PLAN_COMPILER_CACHE = lookup.findGetter(ReteRecipeCompiler.class, "subPlanCompilerCache",
					Map.class);
			GET_COMPILER_BACK_TRACE = lookup.findGetter(ReteRecipeCompiler.class, "compilerBackTrace", Map.class);
		} catch (NoSuchFieldException | IllegalAccessException e) {
			throw new IllegalStateException("Failed to find getter", e);
		}

		try {
			NORMALIZER_FIELD = ReteRecipeCompiler.class.getDeclaredField("normalizer");
		} catch (NoSuchFieldException e) {
			throw new IllegalStateException("Failed to find field", e);
		}
		NORMALIZER_FIELD.setAccessible(true);

		try {
			DO_COMPILE_DISPATCH = lookup.findVirtual(ReteRecipeCompiler.class, "doCompileDispatch",
					MethodType.methodType(CompiledSubPlan.class, SubPlan.class));
			COMPILE_TO_NATURAL_JOIN = lookup.findVirtual(ReteRecipeCompiler.class, "compileToNaturalJoin",
					MethodType.methodType(CompiledSubPlan.class, SubPlan.class, PlanningTrace.class,
							PlanningTrace.class));
			REFER_QUERY = lookup.findVirtual(ReteRecipeCompiler.class, "referQuery",
					MethodType.methodType(PlanningTrace.class, PQuery.class, SubPlan.class, Tuple.class));
		} catch (NoSuchMethodException | IllegalAccessException e) {
			throw new IllegalStateException("Failed to find method", e);
		}
	}

	private final Map<SubPlan, CompiledSubPlan> subPlanCompilerCache;
	private final Map<ReteNodeRecipe, SubPlan> compilerBackTrace;

	public RefineryReteRecipeCompiler(IQueryPlannerStrategy plannerStrategy, Logger logger,
									  IQueryMetaContext metaContext, IQueryCacheContext queryCacheContext,
									  IQueryBackendHintProvider hintProvider, QueryAnalyzer queryAnalyzer,
									  boolean deleteAndReDeriveEvaluation, TimelyConfiguration timelyEvaluation) {
		super(plannerStrategy, logger, metaContext, queryCacheContext, hintProvider, queryAnalyzer,
				deleteAndReDeriveEvaluation, timelyEvaluation);

		var normalizer = new PDisjunctionRewriterCacher(new RefinerySurrogateQueryRewriter(),
				new RefineryPBodyNormalizer(metaContext) {

					@Override
					protected boolean shouldExpandWeakenedAlternatives(PQuery query) {
						var hint = hintProvider.getQueryEvaluationHint(query);
						return ReteHintOptions.expandWeakenedAlternativeConstraints.getValueOrDefault(hint);
					}

				});
		try {
			// https://docs.oracle.com/javase/specs/jls/se17/html/jls-17.html#jls-17.5.3
			// "The object should not be made visible to other threads, nor should the final fields be read,
			// until all updates to the final fields of the object are complete."
			// The {@code super} constructor only sets but doesn't read the {@code normalizer} field,
			// therefore this is fine.
			NORMALIZER_FIELD.set(this, normalizer);
		} catch (IllegalAccessException e) {
			throw new IllegalStateException("Failed to set private final field", e);
		}

		try {
			@SuppressWarnings("unchecked")
			var cache = (Map<SubPlan, CompiledSubPlan>) GET_SUB_PLAN_COMPILER_CACHE.invokeExact(
					(ReteRecipeCompiler) this);
			subPlanCompilerCache = cache;
			@SuppressWarnings("unchecked")
			var backTrace = (Map<ReteNodeRecipe, SubPlan>) GET_COMPILER_BACK_TRACE.invokeExact(
					(ReteRecipeCompiler) this);
			compilerBackTrace = backTrace;
		} catch (Error e) {
			// Fatal JVM errors should not be wrapped.
			throw e;
		} catch (Throwable e) {
			throw new IllegalStateException("Failed to access private fields", e);
		}
	}

	@Override
	public CompiledSubPlan getCompiledForm(SubPlan plan) {
		CompiledSubPlan compiled = subPlanCompilerCache.get(plan);
		if (compiled == null) {
			compiled = doCompileDispatchExtension(plan);
			if (compiled == null) {
				compiled = superDoCompileDispatch(plan);
			}
			subPlanCompilerCache.put(plan, compiled);
			compilerBackTrace.put(compiled.getRecipe(), plan);
		}
		return compiled;
	}

	@Nullable
	private CompiledSubPlan doCompileDispatchExtension(SubPlan plan) {
		var operation = plan.getOperation();
		if (operation instanceof PEnumerate enumerateOperation) {
			return doCompileEnumerateExtension(enumerateOperation.getEnumerablePConstraint(), plan);
		} else if (operation instanceof PApply applyOperation &&
				applyOperation.getPConstraint() instanceof EnumerablePConstraint constraint) {
			var secondaryParent = doEnumerateDispatchExtension(plan, constraint);
			if (secondaryParent != null) {
				var primaryParent = getCompiledForm(plan.getParentPlans().get(0));
				return superCompileToNaturalJoin(plan, primaryParent, secondaryParent);
			}
		}
		return null;
	}

	@Nullable
	private CompiledSubPlan doCompileEnumerateExtension(EnumerablePConstraint constraint, SubPlan plan) {
		var coreTrace = doEnumerateDispatchExtension(plan, constraint);
		if (coreTrace == null) {
			return null;
		}
		var trimmedTrace = CompilerHelper.checkAndTrimEqualVariables(plan, coreTrace);
		return trimmedTrace.cloneFor(plan);
	}

	@Nullable
	private PlanningTrace doEnumerateDispatchExtension(SubPlan plan, EnumerablePConstraint constraint) {
		if (constraint instanceof RepresentativeElectionConstraint representativeElectionConstraint) {
			return compileEnumerableExtension(plan, representativeElectionConstraint);
		}
		return null;
	}

	private PlanningTrace compileEnumerableExtension(SubPlan plan, RepresentativeElectionConstraint constraint) {
		var referredQuery = constraint.getSupplierKey();
		var callTrace = superReferQuery(referredQuery, plan, constraint.getVariablesTuple());
		var recipe = RefineryRecipesFactory.eINSTANCE.createRepresentativeElectionRecipe();
		recipe.setParent(callTrace.getRecipe());
		recipe.setConnectivity(constraint.getConnectivity());
		return new PlanningTrace(plan, CompilerHelper.convertVariablesTuple(constraint), recipe, callTrace);
	}

	private CompiledSubPlan superDoCompileDispatch(SubPlan plan) {
		try {
			return (CompiledSubPlan) DO_COMPILE_DISPATCH.invokeExact((ReteRecipeCompiler) this, plan);
		} catch (Error | RuntimeException e) {
			// Fatal JVM errors and runtime exceptions should not be wrapped.
			throw e;
		} catch (Throwable e) {
			throw new IllegalStateException("Failed to call doCompileDispatch", e);
		}
	}

	private CompiledSubPlan superCompileToNaturalJoin(SubPlan plan, PlanningTrace leftCompiled,
													  PlanningTrace rightCompiled) {
		try {
			return (CompiledSubPlan) COMPILE_TO_NATURAL_JOIN.invokeExact((ReteRecipeCompiler) this, plan,
					leftCompiled, rightCompiled);
		} catch (Error | RuntimeException e) {
			// Fatal JVM errors and runtime exceptions should not be wrapped.
			throw e;
		} catch (Throwable e) {
			throw new IllegalStateException("Failed to call compileToNaturalJoin", e);
		}
	}

	private PlanningTrace superReferQuery(PQuery query, SubPlan plan, Tuple actualParametersTuple) {
		try {
			return (PlanningTrace) REFER_QUERY.invokeExact((ReteRecipeCompiler) this, query, plan,
					actualParametersTuple);
		} catch (Error | RuntimeException e) {
			// Fatal JVM errors and runtime exceptions should not be wrapped.
			throw e;
		} catch (Throwable e) {
			throw new IllegalStateException("Failed to call referQuery", e);
		}
	}
}