aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java')
-rw-r--r--subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java
new file mode 100644
index 00000000..7d1e4d3a
--- /dev/null
+++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/RecursionCutoffPoint.java
@@ -0,0 +1,86 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2014, 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 *******************************************************************************/
9package tools.refinery.viatra.runtime.rete.construction.plancompiler;
10
11import java.util.Collections;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15import java.util.stream.Collectors;
16
17import tools.refinery.viatra.runtime.matchers.backend.QueryEvaluationHint;
18import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext;
19import tools.refinery.viatra.runtime.matchers.psystem.PBody;
20import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery;
21import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration;
22import tools.refinery.viatra.runtime.rete.recipes.ProductionRecipe;
23import tools.refinery.viatra.runtime.rete.recipes.ReteNodeRecipe;
24import tools.refinery.viatra.runtime.rete.traceability.CompiledQuery;
25import tools.refinery.viatra.runtime.rete.traceability.RecipeTraceInfo;
26
27/**
28 * In a recursive query structure, query composition references can be cut off so that the remaining structure is DAG.
29 * {@link RecursionCutoffPoint} represents one such cut off query composition.
30 * When the compilation of the recursive query finishes and the compiled form becomes available,
31 * the {@link RecursionCutoffPoint} has to be signaled to update parent traces and recipes of the recursive call.
32 *
33 * @author Bergmann Gabor
34 * @noreference This class is not intended to be referenced by clients
35 *
36 */
37public class RecursionCutoffPoint {
38 final Map<PBody, RecipeTraceInfo> futureTraceMap;
39 final CompiledQuery compiledQuery;
40 final ProductionRecipe recipe;
41 final QueryEvaluationHint hint;
42
43 public RecursionCutoffPoint(PQuery query, QueryEvaluationHint hint, IQueryMetaContext context, boolean deleteAndRederiveEvaluation, TimelyConfiguration timelyEvaluation) {
44 super();
45 this.hint = hint;
46 this.futureTraceMap = new HashMap<>(); // IMPORTANT: the identity of futureTraceMap.values() will not change
47 this.compiledQuery = CompilerHelper.makeQueryTrace(query, futureTraceMap, Collections.<ReteNodeRecipe>emptySet(), hint, context, deleteAndRederiveEvaluation, timelyEvaluation);
48 this.recipe = (ProductionRecipe)compiledQuery.getRecipe();
49 if (!compiledQuery.getParentRecipeTraces().isEmpty()) {
50 throw new IllegalArgumentException(String.format("Recursion cut-off point of query %s has trace parents: %s",
51 compiledQuery.getQuery(),
52 prettyPrintParentRecipeTraces(compiledQuery.getParentRecipeTraces())));
53 }
54 if (!recipe.getParents().isEmpty()) {
55 throw new IllegalArgumentException(String.format("Recursion cut-off point of query %s has recipe parents: %s",
56 compiledQuery.getQuery(),
57 prettyPrintParentRecipeTraces(compiledQuery.getParentRecipeTraces())));
58 }
59 }
60
61
62
63 private String prettyPrintParentRecipeTraces(List<RecipeTraceInfo> trace) {
64 return trace.stream().map(Object::toString).collect(Collectors.joining(", "));
65 }
66
67 /**
68 * Signals that compilation of the recursive query has terminated, culminating into the given compiled form.
69 * The query composition that has been cut off will be connected now.
70 */
71 public void mend(CompiledQuery finalCompiledForm) {
72 futureTraceMap.putAll(finalCompiledForm.getParentRecipeTracesPerBody());
73 recipe.getParents().addAll(((ProductionRecipe)finalCompiledForm.getRecipe()).getParents());
74 }
75
76 public CompiledQuery getCompiledQuery() {
77 return compiledQuery;
78 }
79
80 public ProductionRecipe getRecipe() {
81 return recipe;
82 }
83
84
85
86}