Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ed16b9d110fb9dc727d7dc24b1f70f2d8122470 (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
/*******************************************************************************
 * Copyright (c) 2016 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.internal.expressions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.eef.common.api.utils.Util;
import org.eclipse.eef.core.api.EEFExpressionUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.sirius.business.api.dialect.description.AbstractInterpretedExpressionQuery;
import org.eclipse.sirius.business.api.dialect.description.IInterpretedExpressionQuery;
import org.eclipse.sirius.business.api.dialect.description.IInterpretedExpressionTargetSwitch;
import org.eclipse.sirius.business.api.dialect.description.MultiLanguagesValidator;
import org.eclipse.sirius.business.api.query.EObjectQuery;
import org.eclipse.sirius.business.internal.dialect.description.ToolInterpretedExpressionTargetSwitch;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterContext;
import org.eclipse.sirius.common.tools.api.interpreter.TypeName;
import org.eclipse.sirius.common.tools.api.interpreter.ValidationResult;
import org.eclipse.sirius.common.tools.api.interpreter.VariableType;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.base.Options;
import org.eclipse.sirius.properties.DynamicMappingForDescription;
import org.eclipse.sirius.properties.GroupDescription;
import org.eclipse.sirius.properties.PageDescription;
import org.eclipse.sirius.properties.PropertiesPackage;
import org.eclipse.sirius.properties.ViewExtensionDescription;
import org.eclipse.sirius.properties.core.api.SiriusInputDescriptor;
import org.eclipse.sirius.properties.core.internal.SiriusToolServices;
import org.eclipse.sirius.tools.api.interpreter.context.SiriusInterpreterContextFactory;
import org.eclipse.sirius.viewpoint.ViewpointPackage;
import org.eclipse.sirius.viewpoint.description.DescriptionPackage;
import org.eclipse.sirius.viewpoint.description.Extension;
import org.eclipse.sirius.viewpoint.description.Group;
import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
import org.eclipse.sirius.viewpoint.description.tool.InitialOperation;
import org.eclipse.sirius.viewpoint.description.tool.ToolPackage;
import org.eclipse.sirius.viewpoint.description.validation.ValidationPackage;

/**
 * An {@code IInterpretedExpressionQuery} for expressions occuring inside properties view descriptions.
 *
 * @author pcdavid
 */
@SuppressWarnings("restriction")
public final class PropertiesInterpretedExpressionQuery extends AbstractInterpretedExpressionQuery implements IInterpretedExpressionQuery {
    private Collection<EPackage> packagesToImport;

    /**
     * Constructor.
     *
     * @param target
     *            the VSM element on which the expression appears.
     * @param expressionAttribute
     *            the attribute of the VSM element which defines the expression (assumed to be an InterpredExpression).
     */
    public PropertiesInterpretedExpressionQuery(EObject target, EStructuralFeature expressionAttribute) {
        super(target, expressionAttribute);
    }

    @Override
    protected void initializeTargetSwitch() {
        this.targetSwitch = new PropertiesExpressionsGlobalTargetSwitch(feature);
    }

    @Override
    public Collection<EPackage> getPackagesToImport() {
        /*
         * We can't rely on the default implementation here, as it assumes we are inside a RepresentationDescription,
         * which is not the case for properties definitions.
         */
        if (packagesToImport == null) {
            packagesToImport = PropertiesInterpretedExpressionQuery.getEPackagesInScope(target);
        }
        return packagesToImport;
    }

    @Override
    public Collection<String> getDependencies() {
        /*
         * We can't rely on the default implementation here, as it assumes we are inside a Viewpoint, which is not the
         * case for properties definitions.
         */
        if (dependencies == null) {
            Collection<String> result = new ArrayList<>();
            result.addAll(VSMNavigation.getJavaExtensionsInVSM(target));
            // Make sure the implicitly registered SiriusToolServices class is
            // also visible.
            result.add(SiriusToolServices.class.getName());
            dependencies = result;
        }
        return dependencies;
    }

    private static Collection<EPackage> getEPackagesInScope(EObject target) {
        Collection<EPackage> result = new LinkedHashSet<>();

        boolean needsGlobalPackages = false;
        for (RepresentationDescription desc : VSMNavigation.getRepresentationDescriptionsInVSM(target)) {
            EList<EPackage> configured = desc.getMetamodel();
            result.addAll(configured);
            if (configured.isEmpty()) {
                /*
                 * If at least one of the possible source representations has no explicitly configured metamodel, we
                 * must include the globally registered packages.
                 */
                needsGlobalPackages = true;
            }
        }

        if (needsGlobalPackages) {
            result.addAll(PropertiesInterpretedExpressionQuery.getAllRegisteredEPackages(EPackage.Registry.INSTANCE));
        }

        // Also add metamodels explicitly added to the ViewExtensionDescription,
        // if any.
        Option<EObject> viewDescriptionOpt = new EObjectQuery(target).getFirstAncestorOfType(PropertiesPackage.Literals.VIEW_EXTENSION_DESCRIPTION);
        if (viewDescriptionOpt.some()) {
            ViewExtensionDescription ved = (ViewExtensionDescription) viewDescriptionOpt.get();
            result.addAll(ved.getMetamodels());
        }

        // In all cases, we make sure the core Sirius metamodels are present...
        result.add(EcorePackage.eINSTANCE);
        result.add(ViewpointPackage.eINSTANCE);
        result.add(DescriptionPackage.eINSTANCE);
        result.add(ToolPackage.eINSTANCE);
        result.add(ValidationPackage.eINSTANCE);
        // ... and the properties view one too.
        result.add(PropertiesPackage.eINSTANCE);

        return result;
    }

    private static Collection<EPackage> getAllRegisteredEPackages(Registry source) {
        Collection<EPackage> result = new LinkedHashSet<>();
        Set<String> nsURIs = new LinkedHashSet<>();
        nsURIs.addAll(source.keySet());
        for (String nsURI : nsURIs) {
            try {
                EPackage ePackage = source.getEPackage(nsURI);
                if (ePackage != null) {
                    result.add(ePackage);
                } 
                // CHECKSTYLE:OFF
            } catch (Throwable e) {
                /*
                 * anything might happen here depending on the other Eclipse tools, and we've seen many time tools
                 * breaking all the others.
                 */
                // CHECKSTYLE:ON
            }
        }
        return result;
    }

    @Override
    public Map<String, VariableType> getAvailableVariables() {
        if (availableVariables == null) {
            availableVariables = new LinkedHashMap<>();
        }

        // going through eContainer() to declare any For variable (dynamic
        // mappings)
        EObject cur = target;
        while (!(cur instanceof Group) && cur != null) {
            EObject parent = cur.eContainer();
            if (parent instanceof DynamicMappingForDescription) {
                String iteratorName = ((DynamicMappingForDescription) parent).getIterator();
                if (!Util.isBlank(iteratorName)) {
                    VariableType iteratorType = getResultType(parent, PropertiesPackage.Literals.ABSTRACT_DYNAMIC_MAPPING_FOR_DESCRIPTION__ITERABLE_EXPRESSION);
                    availableVariables.put(iteratorName, iteratorType);
                }
            }
            cur = parent;
        }

        if (isFromOrInheritsPropertiesEPackage(this.target)) {
            // "input" is always available.
            availableVariables.put(EEFExpressionUtils.INPUT, VariableType.fromJavaClass(SiriusInputDescriptor.class));

            if (feature == PropertiesPackage.Literals.ABSTRACT_SELECT_DESCRIPTION__CANDIDATE_DISPLAY_EXPRESSION) {
                VariableType candidatesExpressionType = getResultType(target, PropertiesPackage.Literals.ABSTRACT_SELECT_DESCRIPTION__CANDIDATES_EXPRESSION);
                availableVariables.put(EEFExpressionUtils.EEFSelect.CANDIDATE, candidatesExpressionType);
            } else if (feature == PropertiesPackage.Literals.ABSTRACT_RADIO_DESCRIPTION__CANDIDATE_DISPLAY_EXPRESSION) {
                VariableType candidatesExpressionType = getResultType(target, PropertiesPackage.Literals.ABSTRACT_RADIO_DESCRIPTION__CANDIDATES_EXPRESSION);
                availableVariables.put(EEFExpressionUtils.EEFSelect.CANDIDATE, candidatesExpressionType);
            } else if (feature == PropertiesPackage.Literals.ABSTRACT_LABEL_DESCRIPTION__DISPLAY_EXPRESSION) {
                VariableType candidatesExpressionType = getResultType(target, PropertiesPackage.Literals.ABSTRACT_LABEL_DESCRIPTION__VALUE_EXPRESSION);
                availableVariables.put(EEFExpressionUtils.EEFReference.VALUE, candidatesExpressionType);
            } else if (feature == PropertiesPackage.Literals.ABSTRACT_LIST_DESCRIPTION__DISPLAY_EXPRESSION) {
                VariableType candidatesExpressionType = getResultType(target, PropertiesPackage.Literals.ABSTRACT_LIST_DESCRIPTION__VALUE_EXPRESSION);
                availableVariables.put(EEFExpressionUtils.EEFList.VALUE, candidatesExpressionType);
            } else if (feature == PropertiesPackage.Literals.ABSTRACT_HYPERLINK_DESCRIPTION__DISPLAY_EXPRESSION) {
                VariableType candidatesExpressionType = getResultType(target, PropertiesPackage.Literals.ABSTRACT_HYPERLINK_DESCRIPTION__VALUE_EXPRESSION);
                availableVariables.put(EEFExpressionUtils.EEFReference.VALUE, candidatesExpressionType);
            } else if (feature == PropertiesPackage.Literals.ABSTRACT_DYNAMIC_MAPPING_IF_DESCRIPTION__PREDICATE_EXPRESSION) {
                if (target.eContainer() instanceof DynamicMappingForDescription) {
                    DynamicMappingForDescription forDefinition = (DynamicMappingForDescription) target.eContainer();
                    String iteratorName = forDefinition.getIterator();
                    if (!Util.isBlank(iteratorName)) {
                        VariableType iteratorType = getResultType(forDefinition, PropertiesPackage.Literals.ABSTRACT_DYNAMIC_MAPPING_FOR_DESCRIPTION__ITERABLE_EXPRESSION);
                        availableVariables.put(iteratorName, iteratorType);
                    }
                }
            }
            return availableVariables;
        } else {
            return super.getAvailableVariables();
        }
    }

    /**
     * return true if the given EObject is from PropertiesPackage or inherits from one of the EClasses of
     * PropertiesPackage.
     * 
     * @param object
     *            any EObject
     * @return true if the given EObject is from PropertiesPackage or inherits from one of the EClasses of
     *         PropertiesPackage
     */
    private boolean isFromOrInheritsPropertiesEPackage(EObject object) {
        Stream<EClass> eClasses = Stream.concat(Stream.of(object.eClass()), object.eClass().getEAllSuperTypes().stream());

        return eClasses.map(EClass::getEPackage).anyMatch(ePackage -> ePackage == PropertiesPackage.eINSTANCE);
    }

    @Override
    protected Option<EObject> getToolContext() {
        Option<EObject> result = super.getToolContext();
        if (!result.some()) {
            if (target instanceof PageDescription || target instanceof GroupDescription) {
                result = Options.newSome(target);
            } else {
                result = new EObjectQuery(target).getFirstAncestorOfType(ToolPackage.Literals.INITIAL_OPERATION);
            }
        }
        return result;
    }

    @Override
    protected void addVariablesFromToolContext(EObject toolContext) {
        super.addVariablesFromToolContext(toolContext);
        availableVariables.put(EEFExpressionUtils.INPUT, VariableType.fromJavaClass(SiriusInputDescriptor.class));
        if (toolContext instanceof InitialOperation) {
            EReference callbackFeature = toolContext.eContainmentFeature();
            VariableType stringType = VariableType.fromString("java.lang.String"); //$NON-NLS-1$
            VariableType booleanType = VariableType.fromString("java.lang.Boolean"); //$NON-NLS-1$
            VariableType unkownType = VariableType.fromString(TypeName.EOBJECT_TYPENAME.getCompleteName());
            if (callbackFeature == PropertiesPackage.Literals.ABSTRACT_TEXT_DESCRIPTION__INITIAL_OPERATION || callbackFeature == PropertiesPackage.Literals.ABSTRACT_TEXT_AREA_DESCRIPTION__INITIAL_OPERATION) {
                availableVariables.put(EEFExpressionUtils.EEFText.NEW_VALUE, stringType);
            } else if (callbackFeature == PropertiesPackage.Literals.ABSTRACT_CHECKBOX_DESCRIPTION__INITIAL_OPERATION) {
                availableVariables.put(EEFExpressionUtils.EEFCheckbox.NEW_VALUE, booleanType);
            } else if (callbackFeature == PropertiesPackage.Literals.ABSTRACT_HYPERLINK_DESCRIPTION__INITIAL_OPERATION) {
                Option<Collection<String>> domainClass = VSMNavigation.getDomainClassFromContainingGroup(toolContext);
                if (domainClass.some()) {
                    availableVariables.put(EEFExpressionUtils.EEFHyperlink.SELECTION, VariableType.fromStrings(domainClass.get()));
                } else {
                    availableVariables.put(EEFExpressionUtils.EEFHyperlink.SELECTION, unkownType);
                }
            } else if (callbackFeature == PropertiesPackage.Literals.ABSTRACT_RADIO_DESCRIPTION__INITIAL_OPERATION) {
                /*
                 * in the case of the radio button the type of newValue is the return type of the candidate expression.
                 */
                availableVariables.put(EEFExpressionUtils.EEFText.NEW_VALUE, getResultType(toolContext.eContainer(), PropertiesPackage.Literals.ABSTRACT_RADIO_DESCRIPTION__CANDIDATES_EXPRESSION));
            } else if (callbackFeature == PropertiesPackage.Literals.WIDGET_ACTION__INITIAL_OPERATION) {
                Option<Collection<String>> domainClass = VSMNavigation.getDomainClassFromContainingGroup(toolContext);
                if (!domainClass.some()) {
                    availableVariables.put(EEFExpressionUtils.EEFHyperlink.SELECTION, VariableType.fromStrings(domainClass.get()));
                } else {
                    availableVariables.put(EEFExpressionUtils.EEFHyperlink.SELECTION, unkownType);
                }
            } else if (callbackFeature == PropertiesPackage.Literals.ABSTRACT_SELECT_DESCRIPTION__INITIAL_OPERATION) {
                availableVariables.put(EEFExpressionUtils.EEFText.NEW_VALUE, getResultType(toolContext.eContainer(), PropertiesPackage.Literals.ABSTRACT_SELECT_DESCRIPTION__CANDIDATES_EXPRESSION));
            }
        }
    }

    private VariableType getResultType(EObject owner, EAttribute attr) {
        IInterpreterContext context = SiriusInterpreterContextFactory.createInterpreterContext(owner, attr);
        ValidationResult res = MultiLanguagesValidator.getInstance().validateExpression(context, (String) owner.eGet(attr));
        return res.getReturnTypes();
    }

    /**
     * The switch used to compute domainClasses for expressions used in properties definitions.
     *
     * @author pcdavid
     */
    private static class PropertiesExpressionsGlobalTargetSwitch implements IInterpretedExpressionTargetSwitch {
        /**
         * The switch for properties-specific expressions.
         */
        private final DomainClassSwitch propertiesSwitch;

        /**
         * The switch we delegate to for Model Operations.
         */
        private final ToolInterpretedExpressionTargetSwitch delegateSwitch;

        /**
         * By default ToolInterpretedExpressionTargetSwitch assumes operations will appear inside representations or
         * mappings, so we override getFirstContextChangingContainer() to locate the parent GroupDescription instead.
         */
        private static class CustomToolInterpretedExpressionTargetSwitch extends ToolInterpretedExpressionTargetSwitch {
            CustomToolInterpretedExpressionTargetSwitch(EStructuralFeature feature, IInterpretedExpressionTargetSwitch defaultSwitch) {
                super(feature, defaultSwitch);
            }

            @Override
            protected EObject getFirstContextChangingContainer(EObject element) {
                EObject defaultResult = super.getFirstContextChangingContainer(element);
                if (defaultResult instanceof Extension) {
                    /*
                     * The generic algorithm in the super-class does not know anything about the properties metamodel
                     * but will stop at the top-level ViewExtensionDescription as it is an Extension.
                     */
                    return VSMNavigation.findClosestGroupDescription(element);
                } else {
                    return defaultResult;
                }
            }
        }

        PropertiesExpressionsGlobalTargetSwitch(EStructuralFeature feature) {
            this.propertiesSwitch = new DomainClassSwitch(feature);
            this.delegateSwitch = new CustomToolInterpretedExpressionTargetSwitch(feature, this);
        }

        @Override
        public Option<Collection<String>> doSwitch(EObject target, boolean considerFeature) {
            Collection<String> targetTypes = new LinkedHashSet<>();
            Option<Collection<String>> expressionTarget = Options.newSome(targetTypes);
            if (target != null) {
                if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
                    propertiesSwitch.setConsiderFeature(considerFeature);
                    expressionTarget = propertiesSwitch.doSwitch(target);
                }
                if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
                    delegateSwitch.setConsiderFeature(considerFeature);
                    expressionTarget = delegateSwitch.doSwitch(target);
                }
            }
            return expressionTarget;
        }

        @Override
        public EObject getFirstRelevantContainer(EObject obj) {
            if (obj != null) {
                EObject container = obj.eContainer();
                while (container != null && !isRelevant(container)) {
                    container = container.eContainer();
                }
                return container;
            } else {
                return null;
            }
        }

        /**
         * In this context, relevant containers are top-level pages and groups only. The root ViewExtensionDescriptions
         * do not define anything relevant for domain class computation.
         */
        private boolean isRelevant(EObject container) {
            return container instanceof PageDescription || container instanceof GroupDescription;
        }
    }
}

Back to the top