aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete-recipes/src/main/java/tools/refinery/viatra/runtime/rete/recipes/helper/RecipesHelper.java
blob: 3070fcf5addf732b1b5c671e262bb6f182fb61d1 (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
/**
 * Copyright (c) 2004-2014 Gabor Bergmann 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.recipes.helper;

import org.eclipse.emf.common.util.EList;
import tools.refinery.viatra.runtime.rete.recipes.*;

import java.util.Collection;

/**
 * Static helper class for easy construction of recipes.
 *
 * @author Bergmann Gabor
 */
public class RecipesHelper {
    private static final RecipesFactory FACTORY = RecipesFactory.eINSTANCE;

    private RecipesHelper() {/* Utility class constructor */}

    /**
     * @since 2.0
     */
    public static Mask mask(final int sourceArity, final Collection<Integer> sourceIndices) {
        Mask mask = RecipesHelper.FACTORY.createMask();
        mask.setSourceArity(sourceArity);
        mask.getSourceIndices().addAll(sourceIndices);
        return mask;
    }

    public static Mask mask(final int sourceArity, final int... sourceIndices) {
        Mask mask = RecipesHelper.FACTORY.createMask();
        mask.setSourceArity(sourceArity);
        final EList<Integer> maskIndeces = mask.getSourceIndices();
        for (int index : sourceIndices) {
            maskIndeces.add(index);
        }
        return mask;
    }

    public static ProjectionIndexerRecipe projectionIndexerRecipe(final ReteNodeRecipe parent, final Mask mask) {
        ProjectionIndexerRecipe recipe = RecipesHelper.FACTORY.createProjectionIndexerRecipe();
        recipe.setParent(parent);
        recipe.setMask(mask);
        return recipe;
    }

    public static ExpressionDefinition expressionDefinition(final Object evaluator) {
        ExpressionDefinition definition = RecipesHelper.FACTORY.createExpressionDefinition();
        definition.setEvaluator(evaluator);
        return definition;
    }

    public static InputRecipe inputRecipe(final Object inputKey, final String inputKeyID, final int arity) {
        InputRecipe recipe = RecipesHelper.FACTORY.createInputRecipe();
        recipe.setInputKey(inputKey);
        recipe.setKeyArity(arity);
        recipe.setKeyID(inputKeyID);
        recipe.setTraceInfo(inputKeyID);
        return recipe;
    }

    /**
     * Mask can be null in case no tuple reordering or trimming is needed
     */
    public static InputFilterRecipe inputFilterRecipe(final ReteNodeRecipe parent, final Object inputKey,
            final String inputKeyID, final Mask mask) {
        InputFilterRecipe it = RecipesHelper.FACTORY.createInputFilterRecipe();
        it.setParent(parent);
        it.setInputKey(inputKey);
        it.setKeyID(inputKeyID);
        it.setTraceInfo(inputKeyID);
        it.setMask(mask);
        return it;
    }
}