aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/plancompiler/CompilerHelper.java
blob: da2fb4323769053034adbcecaab29e6cf74db88f (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*******************************************************************************
 * 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.construction.plancompiler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import tools.refinery.viatra.runtime.matchers.backend.QueryEvaluationHint;
import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.context.IPosetComparator;
import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext;
import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.planning.helpers.TypeHelper;
import tools.refinery.viatra.runtime.matchers.psystem.EnumerablePConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PParameter;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.rete.matcher.TimelyConfiguration;
import tools.refinery.viatra.runtime.rete.recipes.EqualityFilterRecipe;
import tools.refinery.viatra.runtime.rete.recipes.IndexerBasedAggregatorRecipe;
import tools.refinery.viatra.runtime.rete.recipes.IndexerRecipe;
import tools.refinery.viatra.runtime.rete.recipes.JoinRecipe;
import tools.refinery.viatra.runtime.rete.recipes.Mask;
import tools.refinery.viatra.runtime.rete.recipes.MonotonicityInfo;
import tools.refinery.viatra.runtime.rete.recipes.ProductionRecipe;
import tools.refinery.viatra.runtime.rete.recipes.ProjectionIndexerRecipe;
import tools.refinery.viatra.runtime.rete.recipes.RecipesFactory;
import tools.refinery.viatra.runtime.rete.recipes.ReteNodeRecipe;
import tools.refinery.viatra.runtime.rete.recipes.SingleColumnAggregatorRecipe;
import tools.refinery.viatra.runtime.rete.recipes.TrimmerRecipe;
import tools.refinery.viatra.runtime.rete.recipes.helper.RecipesHelper;
import tools.refinery.viatra.runtime.rete.traceability.CompiledQuery;
import tools.refinery.viatra.runtime.rete.traceability.CompiledSubPlan;
import tools.refinery.viatra.runtime.rete.traceability.PlanningTrace;
import tools.refinery.viatra.runtime.rete.traceability.RecipeTraceInfo;
import tools.refinery.viatra.runtime.rete.util.ReteHintOptions;

/**
 * @author Bergmann Gabor
 *
 */
public class CompilerHelper {

    private CompilerHelper() {/*Utility class constructor*/}
    
    static final RecipesFactory FACTORY = RecipesFactory.eINSTANCE;

    /**
     * Makes sure that all variables in the tuple are different so that it can be used as {@link CompiledSubPlan}. If a
     * variable occurs multiple times, equality checks are applied and then the results are trimmed so that duplicates
     * are hidden. If no manipulation is necessary, the original trace is returned.
     * 
     * <p>
     * to be used whenever a constraint introduces new variables.
     */
    public static PlanningTrace checkAndTrimEqualVariables(SubPlan plan, final PlanningTrace coreTrace) {
        // are variables in the constraint all different?
        final List<PVariable> coreVariablesTuple = coreTrace.getVariablesTuple();
        final int constraintArity = coreVariablesTuple.size();
        final int distinctVariables = coreTrace.getPosMapping().size();
        if (constraintArity == distinctVariables) {
            // all variables occur exactly once in tuple
            return coreTrace;
        } else { // apply equality checks and trim

            // find the positions in the tuple for each variable
            Map<PVariable, SortedSet<Integer>> posMultimap = new HashMap<PVariable, SortedSet<Integer>>();
            List<PVariable> trimmedVariablesTuple = new ArrayList<PVariable>(distinctVariables);
            int[] trimIndices = new int[distinctVariables];
            for (int i = 0; i < constraintArity; ++i) {
                final PVariable variable = coreVariablesTuple.get(i);
                SortedSet<Integer> indexSet = posMultimap.get(variable);
                if (indexSet == null) { // first occurrence of variable
                    indexSet = new TreeSet<Integer>();
                    posMultimap.put(variable, indexSet);

                    // this is the first occurrence, set up trimming
                    trimIndices[trimmedVariablesTuple.size()] = i;
                    trimmedVariablesTuple.add(variable);
                }
                indexSet.add(i);
            }

            // construct equality checks for each variable occurring multiple times
            PlanningTrace lastTrace = coreTrace;
            for (Entry<PVariable, SortedSet<Integer>> entry : posMultimap.entrySet()) {
                if (entry.getValue().size() > 1) {
                    EqualityFilterRecipe equalityFilterRecipe = FACTORY.createEqualityFilterRecipe();
                    equalityFilterRecipe.setParent(lastTrace.getRecipe());
                    equalityFilterRecipe.getIndices().addAll(entry.getValue());
                    lastTrace = new PlanningTrace(plan, coreVariablesTuple, equalityFilterRecipe, lastTrace);
                }
            }

            // trim so that each variable occurs only once
            TrimmerRecipe trimmerRecipe = FACTORY.createTrimmerRecipe();
            trimmerRecipe.setParent(lastTrace.getRecipe());
            trimmerRecipe.setMask(tools.refinery.viatra.runtime.rete.recipes.helper.RecipesHelper
                    .mask(constraintArity, trimIndices));
            return new PlanningTrace(plan, trimmedVariablesTuple, trimmerRecipe, lastTrace);
        }
    }

    /**
     * Extracts the variable list representation of the variables tuple.
     */
    public static List<PVariable> convertVariablesTuple(EnumerablePConstraint constraint) {
        return convertVariablesTuple(constraint.getVariablesTuple());
    }

    /**
     * Extracts the variable list representation of the variables tuple.
     */
    public static List<PVariable> convertVariablesTuple(Tuple variablesTuple) {
        List<PVariable> result = new ArrayList<PVariable>();
        for (Object o : variablesTuple.getElements())
            result.add((PVariable) o);
        return result;
    }

    /**
     * Returns a compiled indexer trace according to a mask
     */
    public static RecipeTraceInfo makeIndexerTrace(SubPlan planToCompile, PlanningTrace parentTrace, TupleMask mask) {
        final ReteNodeRecipe parentRecipe = parentTrace.getRecipe();
        if (parentRecipe instanceof IndexerBasedAggregatorRecipe
                || parentRecipe instanceof SingleColumnAggregatorRecipe)
            throw new IllegalArgumentException(
                    "Cannot take projection indexer of aggregator node at plan " + planToCompile);
        IndexerRecipe recipe = RecipesHelper.projectionIndexerRecipe(parentRecipe, toRecipeMask(mask));
        // final List<PVariable> maskedVariables = mask.transform(parentTrace.getVariablesTuple());
        return new PlanningTrace(planToCompile, /* maskedVariables */ parentTrace.getVariablesTuple(), recipe,
                parentTrace);
        // TODO add specialized indexer trace info?
    }

    /**
     * Creates a trimmer that keeps selected variables only.
     */
    protected static TrimmerRecipe makeTrimmerRecipe(final PlanningTrace compiledParent,
            List<PVariable> projectedVariables) {
        final Mask projectionMask = makeProjectionMask(compiledParent, projectedVariables);
        final TrimmerRecipe trimmerRecipe = ReteRecipeCompiler.FACTORY.createTrimmerRecipe();
        trimmerRecipe.setParent(compiledParent.getRecipe());
        trimmerRecipe.setMask(projectionMask);
        return trimmerRecipe;
    }

    public static Mask makeProjectionMask(final PlanningTrace compiledParent, Iterable<PVariable> projectedVariables) {
        List<Integer> projectionSourceIndices = new ArrayList<Integer>();
        for (PVariable pVariable : projectedVariables) {
            projectionSourceIndices.add(compiledParent.getPosMapping().get(pVariable));
        }
        final Mask projectionMask = RecipesHelper.mask(compiledParent.getRecipe().getArity(), projectionSourceIndices);
        return projectionMask;
    }

    /**
     * @since 1.6
     */
    public static final class PosetTriplet {
        public Mask coreMask;
        public Mask posetMask;
        public IPosetComparator comparator;
    }

    /**
     * @since 1.6
     */
    public static PosetTriplet computePosetInfo(List<PVariable> variables, PBody body, IQueryMetaContext context) {
        Map<PVariable, Set<IInputKey>> typeMap = TypeHelper.inferUnaryTypesFor(variables, body.getConstraints(),
                context);
        List<Set<IInputKey>> keys = new LinkedList<Set<IInputKey>>();

        for (int i = 0; i < variables.size(); i++) {
            keys.add(typeMap.get(variables.get(i)));
        }

        return computePosetInfo(keys, context);
    }

    /**
     * @since 1.6
     */
    public static PosetTriplet computePosetInfo(List<PParameter> parameters, IQueryMetaContext context) {
        List<Set<IInputKey>> keys = new LinkedList<Set<IInputKey>>();
        for (int i = 0; i < parameters.size(); i++) {
            IInputKey key = parameters.get(i).getDeclaredUnaryType();
            if (key == null) {
                keys.add(Collections.emptySet());
            } else {
                keys.add(Collections.singleton(parameters.get(i).getDeclaredUnaryType()));
            }
        }
        return computePosetInfo(keys, context);
    }
    
    

    /**
     * @since 1.6
     */
    public static PosetTriplet computePosetInfo(Iterable<Set<IInputKey>> keys, IQueryMetaContext context) {
        PosetTriplet result = new PosetTriplet();
        List<Integer> coreIndices = new ArrayList<Integer>();
        List<Integer> posetIndices = new ArrayList<Integer>();
        List<IInputKey> filtered = new ArrayList<IInputKey>();
        boolean posetKey = false;
        int index = -1;

        for (Set<IInputKey> _keys : keys) {
            ++index;
            posetKey = false;

            for (IInputKey key : _keys) {
                if (key != null && context.isPosetKey(key)) {
                    posetKey = true;
                    filtered.add(key);
                    break;
                }
            }

            if (posetKey) {
                posetIndices.add(index);
            } else {
                coreIndices.add(index);
            }
        }

        result.comparator = context.getPosetComparator(filtered);
        result.coreMask = RecipesHelper.mask(index + 1, coreIndices);
        result.posetMask = RecipesHelper.mask(index + 1, posetIndices);

        return result;
    }

    /**
     * Creates a recipe for a production node and the corresponding trace.
     * <p> PRE: in case this is a recursion cutoff point (see {@link RecursionCutoffPoint}) 
     *  and bodyFinalTraces will be filled later, 
     *  the object yielded now by bodyFinalTraces.values() must return up-to-date results later 
     * @since 2.4
     */
    public static CompiledQuery makeQueryTrace(PQuery query, Map<PBody, RecipeTraceInfo> bodyFinalTraces,
            Collection<ReteNodeRecipe> bodyFinalRecipes, QueryEvaluationHint hint, IQueryMetaContext context, 
            boolean deleteAndRederiveEvaluation, TimelyConfiguration timelyEvaluation) {
        ProductionRecipe recipe = ReteRecipeCompiler.FACTORY.createProductionRecipe();

        // temporary solution to support the deprecated option for now
        boolean deleteAndRederiveEvaluationDep = deleteAndRederiveEvaluation || ReteHintOptions.deleteRederiveEvaluation.getValueOrDefault(hint);
        
        recipe.setDeleteRederiveEvaluation(deleteAndRederiveEvaluationDep);
        
        if (deleteAndRederiveEvaluationDep || (timelyEvaluation != null)) {
            PosetTriplet triplet = computePosetInfo(query.getParameters(), context);
            if (triplet.comparator != null) {
                MonotonicityInfo info = FACTORY.createMonotonicityInfo();
                info.setCoreMask(triplet.coreMask);
                info.setPosetMask(triplet.posetMask);
                info.setPosetComparator(triplet.comparator);
                recipe.setOptionalMonotonicityInfo(info);
            }
        }

        recipe.setPattern(query);
        recipe.setPatternFQN(query.getFullyQualifiedName());
        recipe.setTraceInfo(recipe.getPatternFQN());
        recipe.getParents().addAll(bodyFinalRecipes);
        for (int i = 0; i < query.getParameterNames().size(); ++i) {
            recipe.getMappedIndices().put(query.getParameterNames().get(i), i);
        }

        return new CompiledQuery(recipe, bodyFinalTraces, query);
    }

    /**
     * Calculated index mappings for a join, based on the common variables of the two parent subplans.
     * 
     * @author Gabor Bergmann
     * 
     */
    public static class JoinHelper {
        private TupleMask primaryMask;
        private TupleMask secondaryMask;
        private TupleMask complementerMask;
        private RecipeTraceInfo primaryIndexer;
        private RecipeTraceInfo secondaryIndexer;
        private JoinRecipe naturalJoinRecipe;
        private List<PVariable> naturalJoinVariablesTuple;

        /**
         * @pre enforceVariableCoincidences() has been called on both sides.
         */
        public JoinHelper(SubPlan planToCompile, PlanningTrace primaryCompiled, PlanningTrace callTrace) {
            super();

            Set<PVariable> primaryVariables = new LinkedHashSet<PVariable>(primaryCompiled.getVariablesTuple());
            Set<PVariable> secondaryVariables = new LinkedHashSet<PVariable>(callTrace.getVariablesTuple());
            int oldNodes = 0;
            Set<Integer> introducingSecondaryIndices = new TreeSet<Integer>();
            for (PVariable var : secondaryVariables) {
                if (primaryVariables.contains(var))
                    oldNodes++;
                else
                    introducingSecondaryIndices.add(callTrace.getPosMapping().get(var));
            }
            List<Integer> primaryIndices = new ArrayList<Integer>(oldNodes);
            List<Integer> secondaryIndices = new ArrayList<Integer>(oldNodes);
            for (PVariable var : secondaryVariables) {
                if (primaryVariables.contains(var)) {
                    primaryIndices.add(primaryCompiled.getPosMapping().get(var));
                    secondaryIndices.add(callTrace.getPosMapping().get(var));
                }
            }
            Collection<Integer> complementerIndices = introducingSecondaryIndices;

            primaryMask = TupleMask.fromSelectedIndices(primaryCompiled.getVariablesTuple().size(), primaryIndices);
            secondaryMask = TupleMask.fromSelectedIndices(callTrace.getVariablesTuple().size(), secondaryIndices);
            complementerMask = TupleMask.fromSelectedIndices(callTrace.getVariablesTuple().size(), complementerIndices);

            primaryIndexer = makeIndexerTrace(planToCompile, primaryCompiled, primaryMask);
            secondaryIndexer = makeIndexerTrace(planToCompile, callTrace, secondaryMask);

            naturalJoinRecipe = FACTORY.createJoinRecipe();
            naturalJoinRecipe.setLeftParent((ProjectionIndexerRecipe) primaryIndexer.getRecipe());
            naturalJoinRecipe.setRightParent((IndexerRecipe) secondaryIndexer.getRecipe());
            naturalJoinRecipe.setRightParentComplementaryMask(CompilerHelper.toRecipeMask(complementerMask));

            naturalJoinVariablesTuple = new ArrayList<PVariable>(primaryCompiled.getVariablesTuple());
            for (int complementerIndex : complementerMask.indices)
                naturalJoinVariablesTuple.add(callTrace.getVariablesTuple().get(complementerIndex));
        }

        public TupleMask getPrimaryMask() {
            return primaryMask;
        }

        public TupleMask getSecondaryMask() {
            return secondaryMask;
        }

        public TupleMask getComplementerMask() {
            return complementerMask;
        }

        public RecipeTraceInfo getPrimaryIndexer() {
            return primaryIndexer;
        }

        public RecipeTraceInfo getSecondaryIndexer() {
            return secondaryIndexer;
        }

        public JoinRecipe getNaturalJoinRecipe() {
            return naturalJoinRecipe;
        }

        public List<PVariable> getNaturalJoinVariablesTuple() {
            return naturalJoinVariablesTuple;
        }

    }

    /**
     * @since 1.4
     */
    public static Mask toRecipeMask(TupleMask mask) {
        return RecipesHelper.mask(mask.sourceWidth, mask.indices);
    }

}