aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/rewriters/VariableMappingExpressionEvaluatorWrapper.java
blob: 10337979f2b899966961b6de484c1be20eed9008 (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
/*******************************************************************************
 * Copyright (c) 2010-2016, Grill Balázs, IncQuery Labs Ltd.
 * 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.rewriters;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import tools.refinery.viatra.runtime.matchers.psystem.IExpressionEvaluator;
import tools.refinery.viatra.runtime.matchers.psystem.IValueProvider;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.util.Preconditions;

/**
 * A wrapper for {@link IExpressionEvaluator} which is capable of correctly mapping variable names used by the
 * expression.
 * 
 * @author Grill Balázs
 *
 */
class VariableMappingExpressionEvaluatorWrapper implements IExpressionEvaluator {

    private final IExpressionEvaluator wrapped;
    private final Map<String, String> variableMapping;
    
    public VariableMappingExpressionEvaluatorWrapper(IExpressionEvaluator wrapped,
            Map<PVariable, PVariable> variableMapping) {
        
        // Support to rewrap an already wrapped expression.
        boolean rewrap = wrapped instanceof VariableMappingExpressionEvaluatorWrapper;
        this.wrapped = rewrap ? ((VariableMappingExpressionEvaluatorWrapper)wrapped).wrapped : wrapped;

        // Instead of just saving the reference of the mapping, save the actual (trimmed) state of the mapping as it
        // may change during copying (especially during flattening). A LinkedHashMap is used to retain ordering of
        // original parameter names iterator.
        this.variableMapping = new LinkedHashMap<>();

        // Index map by variable names
        Map<String, PVariable> names = new HashMap<>();
        for (PVariable originalVar : variableMapping.keySet()) {
            names.put(originalVar.getName(), originalVar);
        }
        
        // In case of rewrapping, current names are contained by the previous mapping
        Map<String, String> previousMapping = null;
        if (rewrap){
            previousMapping = ((VariableMappingExpressionEvaluatorWrapper)wrapped).variableMapping;
        }

        // Populate mapping
        for (String inputParameterName : this.wrapped.getInputParameterNames()) {
            String parameterName = rewrap ? previousMapping.get(inputParameterName) : inputParameterName;
            Preconditions.checkArgument(parameterName != null);
            PVariable original = names.get(parameterName);
            Preconditions.checkArgument(original != null);
            PVariable mapped = variableMapping.get(original);
            if (mapped != null){
                this.variableMapping.put(inputParameterName, mapped.getName());
            }
        }
    }

    @Override
    public String getShortDescription() {
        return wrapped.getShortDescription();
    }

    @Override
    public Iterable<String> getInputParameterNames() {
        return variableMapping.values();
    }

    @Override
    public Object evaluateExpression(final IValueProvider provider) throws Exception {
        return wrapped.evaluateExpression(variableName -> {
            String mappedVariableName = variableMapping.get(variableName);
            Preconditions.checkArgument(mappedVariableName != null, "Could not find variable %s", variableName);
            return provider.getValue(mappedVariableName);
        });
    }

}