| /***************************************************************************** |
| * 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; |
| } |
| |
| } |