Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 42da3b6ce9881301eaedf642bbb66dda32da0fc0 (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
/*******************************************************************************
 * Copyright (c) 2010 THALES GLOBAL SERVICES.
 * 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.diagram.sequence.business.internal.operation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.DNode;
import org.eclipse.sirius.business.api.logger.RuntimeLoggerInterpreter;
import org.eclipse.sirius.business.api.logger.RuntimeLoggerManager;
import org.eclipse.sirius.description.DiagramDescription;
import org.eclipse.sirius.diagram.business.internal.operation.AbstractModelChangeOperation;
import org.eclipse.sirius.diagram.sequence.SequenceDDiagram;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.InstanceRole;
import org.eclipse.sirius.diagram.sequence.business.internal.ordering.RefreshOrderingHelper;
import org.eclipse.sirius.diagram.sequence.description.DescriptionPackage;
import org.eclipse.sirius.diagram.sequence.description.SequenceDiagramDescription;
import org.eclipse.sirius.diagram.sequence.ordering.EventEnd;
import org.eclipse.sirius.diagram.sequence.ordering.EventEndsOrdering;
import org.eclipse.sirius.diagram.sequence.ordering.InstanceRolesOrdering;
import org.eclipse.sirius.tools.api.interpreter.InterpreterUtil;

/**
 * An operation to re-compute the global semantic orderings of events and
 * instance roles in a sequence diagram according to the user-specified
 * criteria.
 * 
 * <pre>
 * Semantic Model + User-specified Ordering Expression ---> SemanticMessageOrdering
 * </pre>
 * 
 * @author pcdavid, smonnier
 */
public class RefreshSemanticOrderingsOperation extends AbstractModelChangeOperation<Void> {
    /**
     * The name of the variable used to pass event ends to sort to
     * user-specified expressions.
     */
    private static final String EVENT_ENDS_TO_SORT_VARIABLE = "eventEnds";

    private final SequenceDDiagram sequenceDDiagram;

    /**
     * Constructor.
     * 
     * @param diagram
     *            the diagram whose semantic ordering should be refreshed.
     */
    public RefreshSemanticOrderingsOperation(SequenceDDiagram diagram) {
        super("Refresh semantic ordering");
        this.sequenceDDiagram = Preconditions.checkNotNull(diagram);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Void execute() {
        EventEndsOrdering semanticOrdering = sequenceDDiagram.getSemanticOrdering();
        if (semanticOrdering != null) {
            refreshGlobalOrdering(semanticOrdering);
        }

        InstanceRolesOrdering irSemanticOrdering = sequenceDDiagram.getInstanceRoleSemanticOrdering();
        if (semanticOrdering != null) {
            refreshGlobalOrdering(irSemanticOrdering);
        }
        return null;
    }

    /**
     * Refreshes the semantic ordering of all the events in the diagram.
     * <p>
     * {@inheritDoc}
     */
    private void refreshGlobalOrdering(EventEndsOrdering semanticOrdering) {
        Iterable<? extends EventEnd> allEnds = RefreshOrderingHelper.getAllEventEnds(sequenceDDiagram);
        RefreshOrderingHelper.updateIfNeeded(semanticOrdering.getEventEnds(), computeEventEndsOrdering(semanticOrdering, allEnds));
    }

    private List<EventEnd> computeEventEndsOrdering(EventEndsOrdering semanticOrdering, Iterable<? extends EventEnd> allEnds) {
        Map<EObject, EventEnd> index = Maps.newHashMap();
        for (EventEnd eventEnd : allEnds) {
            index.put(eventEnd.getSemanticEnd(), eventEnd);
        }

        Iterable<EObject> semanticEnds = Iterables.transform(allEnds, new Function<EventEnd, EObject>() {
            public EObject apply(EventEnd from) {
                return from.getSemanticEnd();
            }
        });
        List<EObject> orderedSemanticEnds = computeOrdering(semanticEnds, DescriptionPackage.eINSTANCE.getSequenceDiagramDescription_EndsOrdering(), true);

        List<EventEnd> result = new ArrayList<EventEnd>();
        for (EObject semanticEnd : orderedSemanticEnds) {
            EventEnd ee = index.get(semanticEnd);
            if (ee != null) {
                result.add(ee);
            }
        }

        return result;
    }

    /**
     * Refreshes the semantic ordering of all the instance roles in the diagram.
     */
    private void refreshGlobalOrdering(InstanceRolesOrdering semanticOrdering) {
        Iterable<? extends DNode> allInstanceRoles = Iterables.filter(sequenceDDiagram.getNodes(), InstanceRole.viewpointElementPredicate());
        RefreshOrderingHelper.updateIfNeeded(semanticOrdering.getSemanticInstanceRoles(), computeInstanceRolesOrdering(semanticOrdering, allInstanceRoles));
    }

    private List<EObject> computeInstanceRolesOrdering(InstanceRolesOrdering semanticOrdering, Iterable<? extends DNode> allInstanceRoles) {
        List<EObject> semanticInstanceRoles = Lists.newArrayList();
        for (DNode node : allInstanceRoles) {
            semanticInstanceRoles.add(node.getTarget());
        }

        List<EObject> orderedSemanticEnds = computeOrdering(semanticInstanceRoles, DescriptionPackage.eINSTANCE.getSequenceDiagramDescription_InstanceRolesOrdering(), false);
        return orderedSemanticEnds;
    }

    private List<EObject> computeOrdering(Iterable<EObject> semanticEnds, EAttribute expressionAttribute, boolean declareEventEndsVariable) {
        if (StringUtil.isEmpty((String) getSequenceDescription().eGet(expressionAttribute))) {
            return Lists.newArrayList(semanticEnds);
        }

        final IInterpreter interpreter = InterpreterUtil.getInterpreter(sequenceDDiagram);
        if (declareEventEndsVariable) {
            interpreter.setVariable(EVENT_ENDS_TO_SORT_VARIABLE, Lists.newArrayList(semanticEnds));
        }

        try {
            RuntimeLoggerInterpreter loggerInterpreter = RuntimeLoggerManager.INSTANCE.decorate(interpreter);
            Collection<EObject> result = loggerInterpreter.evaluateCollection(sequenceDDiagram.getTarget(), getSequenceDescription(), expressionAttribute);
            return Lists.newArrayList(result);
        } finally {
            if (declareEventEndsVariable) {
                interpreter.unSetVariable(EVENT_ENDS_TO_SORT_VARIABLE);
            }
        }
    }

    private SequenceDiagramDescription getSequenceDescription() {
        if (sequenceDDiagram != null) {
            DiagramDescription desc = sequenceDDiagram.getDescription();
            return SequenceDiagramDescription.class.cast(desc);
        } else {
            return null;
        }
    }
}

Back to the top