aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/basicdeferred/ExpressionEvaluation.java
blob: 06688c360d7697a4196933c63735d756d1fef64d (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-2013, Zoltan Ujhelyi, 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.matchers.psystem.basicdeferred;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext;
import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.tuple.Tuples;

/**
 * @author Zoltan Ujhelyi
 *
 */
public class ExpressionEvaluation extends BaseTypeSafeConstraint {

    private IExpressionEvaluator evaluator;
    private boolean isUnwinding;

    public ExpressionEvaluation(PBody pBody, IExpressionEvaluator evaluator, PVariable outputVariable) {
        this(pBody, evaluator, outputVariable, false);
    }

    /**
     * @since 2.4
     */
    public ExpressionEvaluation(PBody pBody, IExpressionEvaluator evaluator, PVariable outputVariable,
            boolean isUnwinding) {
        super(pBody, getPVariablesOfExpression(pBody, evaluator), outputVariable);
        this.evaluator = evaluator;
        this.isUnwinding = isUnwinding;
    }

    /**
     * @since 2.4
     */
    public boolean isUnwinding() {
        return isUnwinding;
    }

    public IExpressionEvaluator getEvaluator() {
        return evaluator;
    }

    @Override
    protected String toStringRest() {
        return Tuples.flatTupleOf(new ArrayList<PVariable>(inputVariables).toArray()).toString() + "|="
                + evaluator.getShortDescription();
    }

    @Override
    public Map<Set<PVariable>, Set<PVariable>> getFunctionalDependencies(IQueryMetaContext context) {
        if (outputVariable == null)
            return Collections.emptyMap();
        else
            return Collections.singletonMap(inputVariables, Collections.singleton(outputVariable));
    }

    private static Set<PVariable> getPVariablesOfExpression(PBody pBody, IExpressionEvaluator evaluator) {
        // use a linked set, so that the variables will come in the order of the parameters
        Set<PVariable> result = new LinkedHashSet<PVariable>();
        for (String name : evaluator.getInputParameterNames()) {
            PVariable variable = pBody.getOrCreateVariableByName(name);
            result.add(variable);
        }
        return result;
    }
}