Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a0233525c628ee43d156b617d6398de859875b0a (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
/*****************************************************************************
 * Copyright (c) 2018 CEA LIST and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.cdo.benchmarks.tests;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ExtensionServicesRegistry;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServiceMultiException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.uml.diagram.wizards.command.NewPapyrusModelCommand;
import org.eclipse.papyrus.uml.extensionpoints.profile.IRegisteredProfile;
import org.eclipse.papyrus.uml.extensionpoints.profile.RegisteredProfile;
import org.eclipse.papyrus.uml.tools.commands.ApplyProfileCommand;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * @author VL222926
 *
 */
public abstract class AbstractModelCreation implements IPapyrusModelCreation {


	protected ServicesRegistry servicesRegistry;

	protected ModelSet modelSet;


	public ModelSet getModelSet() {
		return this.modelSet;
	}

	private URI diURi = null;

	private Resource umlResource;

	private Model rootModel;

	private List<String> profilesToApply;

	protected void createModelSet(final String folderName, final String modelName, final List<String> profilesToApply) throws IOException, ServiceException {
		if (null == profilesToApply) {
			this.profilesToApply = Collections.emptyList();
		} else {
			this.profilesToApply = profilesToApply;
		}
		servicesRegistry = createServicesRegistry();
		Assert.isNotNull(servicesRegistry);


		this.modelSet = servicesRegistry.getService(ModelSet.class);
		Assert.isNotNull(this.modelSet);



		diURi = createBaseURI();
		if (null == diURi) {
			diURi = URI.createPlatformResourceURI(folderName, true);
		} else {
			diURi = diURi.appendSegment(folderName);
		}
		diURi = diURi.appendSegment(modelName);
		diURi = diURi.appendFileExtension(DiModel.MODEL_FILE_EXTENSION);

		RecordingCommand command = new NewPapyrusModelCommand(this.modelSet, diURi);

		modelSet.getTransactionalEditingDomain().getCommandStack().execute(command);
		modelSet.save(new NullProgressMonitor());
		final URI umlURi = diURi.trimFileExtension().appendFileExtension("uml");
		umlResource = modelSet.getResource(umlURi, true);


		// create the architecture context
		ArchitectureDescriptionUtils helper = new ArchitectureDescriptionUtils(modelSet);


		final String contextId;
		final List<String> viewpointsIds = new ArrayList<String>();
		if (profilesToApply.contains("SysML 1.4")) {
			contextId = "org.eclipse.papyrus.sysml.architecture.SysML14";
			viewpointsIds.add("org.eclipse.papyrus.sysml14.standard.modeling");
		} else {
			// we assume we are in pure UML
			contextId = "org.eclipse.papyrus.infra.services.edit.TypeContext";
			viewpointsIds.add("org.eclipse.papyrus.uml.analysis");
			viewpointsIds.add("org.eclipse.papyrus.uml.design");
		}
		// CompoundCommand cc = new CompoundCommand();
		// final Command cmd1 = helper.switchArchitectureContextId(contextId);
		// cc.append(cmd1);
		String[] stringsViewPointIds = new String[viewpointsIds.size()];
		stringsViewPointIds = viewpointsIds.toArray(stringsViewPointIds);
		// final Command cmd2 = helper.switchArchitectureViewpointIds(stringsViewPointIds);
		// cc.append(cmd2);

		modelSet.getTransactionalEditingDomain().getCommandStack().execute(helper.createNewModel(contextId, stringsViewPointIds));
		modelSet.save(new NullProgressMonitor());


		Assert.isNotNull(umlResource);
		final Model model = (Model) umlResource.getContents().get(0);
		// UMLFactory.eINSTANCE.createModel();
		// model.setName(modelName);

		RecordingCommand rc = new RecordingCommand(modelSet.getTransactionalEditingDomain()) {

			@Override
			protected void doExecute() {
				model.setName(modelName);
				// umlResource.getContents().add(model);
			}
		};

		modelSet.getTransactionalEditingDomain().getCommandStack().execute(rc);
		rootModel = model;
		initServicesRegistry(servicesRegistry);// we need to have a root in the model to init it!?!?


		modelSet.save(new NullProgressMonitor());
		if (this.profilesToApply.size() > 0) {
			applyRequiredProfilesFromName(getProfilesToApply());
			modelSet.save(new NullProgressMonitor());
		}


	}

	protected void createModelSet(final String folderName, final String modelName) throws IOException, ServiceException {
		createModelSet(folderName, modelName, Collections.<String> emptyList());

	}

	protected void applyRequiredProfilesFromName(final List<String> profileNames) {
		if (profileNames.size() >= 0) {
			final List<Profile> profilesToApply = new ArrayList<Profile>();
			for (final String current : profileNames) {
				IRegisteredProfile profile = RegisteredProfile.getRegisteredProfile(current);
				org.junit.Assert.assertNotNull(profile);
				URI uri = profile.getUri();
				Resource resource = modelSet.getResource(uri, true);
				Profile profileToApply = (Profile) resource.getContents().get(0);
				org.junit.Assert.assertNotNull(profileToApply);
				profilesToApply.add(profileToApply);
			}
			applyRequiredProfiles(profilesToApply);
		}
	}

	protected void applyRequiredProfiles(final List<Profile> profiles) {
		if (profiles.size() > 0) {
			final CompoundCommand cc = new CompoundCommand("Apply Profiles");
			for (Profile current : profiles) {
				cc.append(new ApplyProfileCommand(rootModel, current, modelSet.getTransactionalEditingDomain()));
			}
			modelSet.getTransactionalEditingDomain().getCommandStack().execute(cc);
			org.junit.Assert.assertTrue(profiles.size() <= rootModel.getAppliedProfiles().size());
		}
	}

	protected List<String> getProfilesToApply() {
		return this.profilesToApply;
	}

	/**
	 * @throws ServiceMultiException
	 * @see org.eclipse.papyrus.cdo.benchmarks.tests.IPapyrusModelCreation#disposeAll()
	 *
	 */
	@Override
	public void disposeAll() throws ServiceMultiException {
		this.modelSet.unload();
		this.modelSet = null;
		this.servicesRegistry.disposeRegistry();
		this.servicesRegistry = null;
	}

	/**
	 * @return
	 */
	protected abstract URI createBaseURI();

	/**
	 * 
	 * 
	 * TODO : duplicated code from CreateModelWizard
	 * 
	 * 
	 * @return
	 */
	protected ServicesRegistry createServicesRegistry() {
		ServicesRegistry result = null;

		try {
			result = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
		} catch (ServiceException e) {
			// couldn't create the registry? Fatal problem
			// Activator.log.error(e); //TODO
		}

		try {
			// have to create the model set and populate it with the DI model
			// before initializing other services that actually need the DI
			// model, such as the SashModel Manager service
			result.startServicesByClassKeys(ModelSet.class);
		} catch (ServiceException ex) {
			// Ignore this exception: some services may not have been loaded,
			// which is probably normal at this point
		}

		return result;
	}

	/**
	 * 
	 * 
	 * TODO : duplicated code from CreateModelWizard
	 * 
	 * 
	 * @return
	 */
	protected void initServicesRegistry(ServicesRegistry registry) throws ServiceException {
		try {
			registry.startRegistry();
		} catch (ServiceException ex) {
			// Ignore this exception: some services may not have been loaded,
			// which is probably normal at this point
		}

		registry.getService(IPageManager.class);
	}

	public ServicesRegistry getServicesRegistry() {
		return this.servicesRegistry;
	}

	/**
	 * @see org.eclipse.papyrus.cdo.benchmarks.tests.IPapyrusModelCreation#getModelRoot()
	 *
	 * @return
	 */
	@Override
	public Model getModelRoot() {
		return this.rootModel;
	}

}

Back to the top