aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/traceability/PlanningTrace.java
blob: c1cc3a691c89e7e3f8a23682a4917dc067338f4f (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
/*******************************************************************************
 * Copyright (c) 2010-2014, Bergmann Gabor, Istvan Rath and Daniel Varro
 * 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.viatra.runtime.rete.traceability;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.rete.recipes.ReteNodeRecipe;

/**
 * A trace marker associating a Rete recipe with a query SubPlan. 
 * 
 * <p> The recipe may be an auxiliary node; 
 *   see {@link CompiledSubPlan} if it represents the entire SubPlan instead.
 */
public class PlanningTrace extends RecipeTraceInfo implements PatternTraceInfo {

    protected SubPlan subPlan;
    protected List<PVariable> variablesTuple;
    protected Map<PVariable, Integer> posMapping;

    public PlanningTrace(SubPlan subPlan, List<PVariable> variablesTuple, 
            ReteNodeRecipe recipe,
            Collection<? extends RecipeTraceInfo> parentRecipeTraces) {
        super(recipe, parentRecipeTraces);
        this.subPlan = subPlan;
        this.variablesTuple = variablesTuple;
        
        this.posMapping = new HashMap<PVariable, Integer>();
        for (int i = 0; i < variablesTuple.size(); ++i)
            posMapping.put(variablesTuple.get(i), i);
    }

    public PlanningTrace(SubPlan subPlan, List<PVariable> variablesTuple, 
            ReteNodeRecipe recipe,
            RecipeTraceInfo... parentRecipeTraces) {
        this(subPlan, variablesTuple, recipe, Arrays.asList(parentRecipeTraces));
    }

    public SubPlan getSubPlan() {
        return subPlan;
    }

    @Override
    public String getPatternName() {
        return subPlan.getBody().getPattern().getFullyQualifiedName();
    }

    public List<PVariable> getVariablesTuple() {
        return variablesTuple;
    }

    public Map<PVariable, Integer> getPosMapping() {
        return posMapping;
    }

    /**
     * Returns a new clone that reinterprets the same compiled form
     *  as the compiled form of a (potentially different) subPlan.
     * Useful e.g. if child plan turns out to be a no-op, or when promoting a {@link PlanningTrace} to {@link CompiledSubPlan}. 
     */
    public CompiledSubPlan cloneFor(SubPlan newSubPlan) {
        return new CompiledSubPlan(newSubPlan, 
                getVariablesTuple(), 
                getRecipe(), 
                getParentRecipeTracesForCloning());
    }

}