Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db122ffe79f1b32452db529b90bf82b739bb8172 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 CEA LIST, Christian W. Damus, and others.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus - bug 436666
 *****************************************************************************/
package org.eclipse.papyrus.migration.rsa.transformation;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.migration.rsa.Activator;
import org.eclipse.papyrus.uml.modelrepair.internal.stereotypes.IRepairAction;
import org.eclipse.papyrus.uml.modelrepair.internal.stereotypes.ZombieStereotypesDescriptor;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.UMLPackage;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * Repairs stereotype applications in the specified model set, using the default action for each
 *
 * The given URI Map is used when the proper profile can't be found automatically
 *
 * @author Camille Letavernier
 *
 */
public class RepairStereotypes {

	private final ModelSet modelSet;

	private final Collection<Resource> resourcesToRepair;

	private final Map<URI, URI> profileMappings;

	public RepairStereotypes(ModelSet modelSet, Collection<Resource> resourcesToRepair, Map<URI, URI> profileMappings) {
		this.modelSet = modelSet;
		this.resourcesToRepair = resourcesToRepair;
		this.profileMappings = profileMappings;
	}

	/**
	 * Finds all zombie stereotypes, and repair them with the default action.
	 *
	 * If the profile can't be found automatically, the profileMappings map is used
	 *
	 * @throws InterruptedException
	 * @throws RollbackException
	 */
	public void execute() throws InterruptedException, RollbackException {
		ZombieStereotypesDescriptor result = null;

		LabelProviderService labelProvider = new LabelProviderServiceImpl();
		try {
			labelProvider.startService();
		} catch (Exception ex) {
			Activator.log.error(ex);
			return;
		}

		Function<? super EPackage, Profile> profileSupplier = getProfileSupplier();

		for (Resource resource : resourcesToRepair) {
			Element rootElement = (Element) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.ELEMENT);

			if (rootElement == null || rootElement.getNearestPackage() == null) {
				continue;
			}

			Package rootPackage = rootElement.getNearestPackage();

			Collection<ProfileApplication> profileApplications = Lists.newArrayList();
			for (TreeIterator<EObject> iter = EcoreUtil.getAllProperContents(Collections.singleton(rootPackage), false); iter.hasNext();) {
				EObject next = iter.next();
				if (next instanceof ProfileApplication) {
					profileApplications.add((ProfileApplication) next);
					iter.prune();
				} else if (!(next instanceof Package) && !(next instanceof Component)) {
					// No sense looking for packages except in the things that can contain packages
					iter.prune();
				}
			}

			Set<EPackage> appliedDefinitions = getAppliedDefinitions(profileApplications);

			ZombieStereotypesDescriptor zombies = new ZombieStereotypesDescriptor(resource, rootPackage, appliedDefinitions, profileSupplier, labelProvider);

			for (EObject next : resource.getContents()) {
				if (!(next instanceof Element)) {
					zombies.analyze(next);
				}
			}

			if (zombies.hasZombies()) {
				result = zombies;
			}

			if (result == null) {
				continue;
			}

			final ZombieStereotypesDescriptor descriptor = result;

			final TransactionalEditingDomain domain = modelSet.getTransactionalEditingDomain();


			InternalTransactionalEditingDomain internalDomain = (InternalTransactionalEditingDomain) domain;

			Map<String, Object> options = new HashMap<String, Object>();
			options.put(Transaction.OPTION_NO_UNDO, true);
			options.put(Transaction.OPTION_NO_VALIDATION, true);
			options.put(Transaction.OPTION_NO_TRIGGERS, true);
			options.put(Transaction.OPTION_UNPROTECTED, true);

			// We're in a batch environment, with no undo/redo support. Run a vanilla transaction to improve performances
			Transaction fastTransaction = internalDomain.startTransaction(false, options);
			try {
				final BasicDiagnostic diagnostics = new BasicDiagnostic(Activator.PLUGIN_ID, 0, "Problems in repairing stereotypes", null);

				for (IAdaptable packageToRepair : descriptor.getZombieSchemas()) {
					IRepairAction action = descriptor.getRepairAction(packageToRepair, IRepairAction.Kind.APPLY_LATEST_PROFILE_DEFINITION);
					descriptor.repair(packageToRepair, action, diagnostics, new NullProgressMonitor());
				}
			} finally {
				fastTransaction.commit();
			}

		}

		try {
			labelProvider.disposeService();
		} catch (ServiceException ex) {
			Activator.log.error(ex);
		}
	}

	/**
	 * Maps a zombie EPackage with an existing Profile. Called only if the Profile can't be resolved automatically
	 *
	 * @return
	 */
	protected Function<? super EPackage, Profile> getProfileSupplier() {
		return new Function<EPackage, Profile>() {

			// If we're here, we didn't find a perfect match for the profile. We need to make some guesses
			@Override
			public Profile apply(EPackage input) {
				if (input.eResource() == null || input.eResource().getURI() == null) {
					return null;
				}

				URI packageResourceURI = input.eResource().getURI();
				URI targetResourceURI = profileMappings.get(packageResourceURI);

				Profile result = null;
				URI selectedProfileURI = targetResourceURI;
				if (selectedProfileURI != null) {
					result = EMFHelper.load(modelSet, selectedProfileURI, Profile.class);

					// If there is a single Profile, there is no ambiguity. Just return it
					if ((result != null) && !hasNestedProfiles(result)) {
						return result;
					}

					// TODO: handle the case with nested profiles (We already opened a popup dialog earlier; wouldn't be a good idea to pop another dialog here)
				}

				return null;
			}

			boolean hasNestedProfiles(Profile profile) {
				return Iterators.any(profile.eAllContents(), Predicates.instanceOf(Profile.class));
			}
		};
	}

	protected Set<EPackage> getAppliedDefinitions(Iterable<? extends ProfileApplication> profileApplications) {
		Set<EPackage> result = Sets.newHashSet();

		for (ProfileApplication next : profileApplications) {
			EPackage definition = next.getAppliedDefinition();
			if ((definition != null) && !definition.eIsProxy()) {
				result.add(definition);
			}
		}

		return result;
	}
}

Back to the top