Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ccd5dd89d97ef14a1bac6d40a71ae62f7289628 (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
/*******************************************************************************
 * Copyright (c) 2017 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.properties.core.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.StreamSupport;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
import org.eclipse.sirius.common.interpreter.api.IVariableManager;
import org.eclipse.sirius.properties.core.internal.converter.SiriusInitialOperationAdapter;
import org.eclipse.sirius.viewpoint.description.tool.InitialOperation;

/**
 * {@link IDescriptionPreprocessor} implementation that supports ignoring some features and copying the value of some
 * other features. This means that in the models resulting from the preprocessing, the ignored values are not copied or
 * inherited. Features that must be copied are copied using {@link org.eclipse.emf.ecore.util.EcoreUtil#copy(EObject)}.
 * 
 * @author flatombe
 * @author mbats
 *
 * @param <SIRIUS>
 *            the type of description supported by this preprocessor.
 */
public class DefaultDescriptionPreprocessorWithFiltering<SIRIUS extends EObject> extends DefaultDescriptionPreprocessor<SIRIUS> {

    /**
     * The features to ignore.
     */
    private Collection<EStructuralFeature> featuresToFilter = new ArrayList<EStructuralFeature>();

    /**
     * The features to copy.
     */
    private Collection<EStructuralFeature> featuresToCopy = new ArrayList<EStructuralFeature>();

    /**
     * The constructor.
     * 
     * @param descriptionClass
     *            the SIRIUS class
     * @param featuresToFilter
     *            the collection of features of {@code descriptionClass} that must be ignored during the preprocessing.
     * @param featuresToCopy
     *            the collection of features of {@code descriptionClass} that must be copied during the preprocessing
     */
    public DefaultDescriptionPreprocessorWithFiltering(Class<SIRIUS> descriptionClass, Collection<EStructuralFeature> featuresToFilter, Collection<EStructuralFeature> featuresToCopy) {
        super(descriptionClass);
        this.featuresToCopy.addAll(featuresToCopy);
        this.featuresToFilter.addAll(featuresToFilter);
    }

    /**
     * Processes the given feature if it is not to be ignored. If the feature is to be copied, then re-directs to the
     * methods that do the copying.
     * 
     * @param feature
     *            the {@link EStructuralFeature} to process
     * @param processedDescription
     *            the resulting description
     * @param currentDescription
     *            the original description or one of its ancestors, from which properties are inherited
     * @param cache
     *            the processing cache
     * @param interpreter
     *            the interpreter
     * @param variableManager
     *            the variable manager
     * @param overridesProvider
     *            Utility class used to provide the override descriptions
     */
    @Override
    protected void processDescriptionFeature(EStructuralFeature feature, SIRIUS processedDescription, SIRIUS currentDescription, TransformationCache cache, IInterpreter interpreter,
            IVariableManager variableManager, OverridesProvider overridesProvider) {
        if (!featuresToFilter.contains(feature)) {
            if (featuresToCopy.contains(feature)) {
                if (!feature.isMany()) {
                    processMonoValuedFeatureByCopying(feature, processedDescription, currentDescription, cache);
                } else {
                    processManyValuedFeatureByCopying(feature, processedDescription, currentDescription, cache, interpreter, variableManager, overridesProvider);
                }
            } else {
                super.processDescriptionFeature(feature, processedDescription, currentDescription, cache, interpreter, variableManager, overridesProvider);
            }
        }
    }

    /**
     * Processes the given {@link EStructuralFeature} by copying the value of the current description.
     * 
     * @param monoValuedfeature
     *            the mono-valued feature being processed.
     * @param processedDescription
     *            the resulting description.
     * @param currentDescription
     *            the original or an ancestor description.
     */
    private void processMonoValuedFeatureByCopying(EStructuralFeature monoValuedfeature, SIRIUS processedDescription, SIRIUS currentDescription, TransformationCache cache) {
        if (!processedDescription.eIsSet(monoValuedfeature)) {
            Object existingValue = currentDescription.eGet(monoValuedfeature);
            if (existingValue instanceof EObject) {
                EObject newValue = EcoreUtil.copy((EObject) existingValue);
                cache.put(existingValue, newValue);
                processedDescription.eSet(monoValuedfeature, newValue);

                if (existingValue instanceof InitialOperation) {
                    newValue.eAdapters().add(new SiriusInitialOperationAdapter(EcoreUtil.getURI((InitialOperation) existingValue)));
                }
            }
        }
    }

    /**
     * Processes the given {@link EStructuralFeature} by adding to the processed description's feature value a copy of
     * the values held by the current description.
     * 
     * @param manyValuedFeature
     *            the many-valued feature being processed.
     * @param processedDescription
     *            the resulting description.
     * @param currentDescription
     *            the original or an ancestor description.
     * @param cache
     *            the cache
     * @param interpreter
     *            The interpreter
     * @param variableManager
     *            The variable manager
     * @param overridesProvider
     *            Utility class used to provide the override descriptions
     */
    protected void processManyValuedFeatureByCopying(EStructuralFeature manyValuedFeature, SIRIUS processedDescription, SIRIUS currentDescription, TransformationCache cache, IInterpreter interpreter,
            IVariableManager variableManager, OverridesProvider overridesProvider) {
        Object processedValue = processedDescription.eGet(manyValuedFeature);
        Object currentValue = currentDescription.eGet(manyValuedFeature);
        if (currentValue instanceof Iterable<?> && processedValue instanceof Iterable<?>) {
            List<Object> newValue = new ArrayList<>();
            Iterable<?> currentIterable = (Iterable<?>) currentValue;
            Iterable<?> processedIterable = (Iterable<?>) processedValue;

            // For each current description create a copy and set it in the new
            // values
            StreamSupport.stream(currentIterable.spliterator(), false).filter(EObject.class::isInstance).map(EObject.class::cast).forEach(object -> {
                if (!this.isFiltered(manyValuedFeature, processedDescription, object, cache, interpreter, variableManager, overridesProvider)) {
                    EObject newEObject = EcoreUtil.copy(object);
                    cache.put(object, newEObject);
                    newValue.add(newEObject);

                    if (object instanceof InitialOperation) {
                        newEObject.eAdapters().add(new SiriusInitialOperationAdapter(EcoreUtil.getURI(object)));
                    }
                }
            });

            // Get all the already processed values
            processedIterable.forEach(newValue::add);

            // Set the new reference values
            processedDescription.eSet(manyValuedFeature, newValue);
        }
    }
}

Back to the top