Commit c4e89b54 authored by Erwan Bousse's avatar Erwan Bousse
Browse files

Splitting bis

parent 00b0dd7f
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.plugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.SWTFactory;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsMessages;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.StringVariableSelectionDialog;
import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
import org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.AccessibleAdapter;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.views.navigator.ResourceComparator;
@SuppressWarnings("restriction")
public class CloningMaterialGenerationLaunchTab extends AbstractJavaMainTab {
// Local/shared UI widgets
private Button fEcoreFolderBrowse;
private Text fEcoreFolderText;
private Button fVariables;
private Button fWorkspaceBrowse;
private Text fMetamodelNameText;
private Button fEMFCheckButton;
private void createEMFCheckbox(Composite parent) {
fEMFCheckButton = SWTFactory.createCheckButton(parent, "Generate EMF model code with appropriate supertypes.",
null, false, 1);
fEMFCheckButton.addSelectionListener(getDefaultListener());
}
@Override
public void createControl(Composite parent) {
Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
((GridLayout) comp.getLayout()).verticalSpacing = 0;
createEcoreFolderComponent(comp);
createVerticalSpacer(comp, 1);
createProjectEditor(comp);
((Group) fProjText.getParent()).setText("Plug-in/EMF project with EMF model code of the metamodel.");
createVerticalSpacer(comp, 1);
createMetamodelNameComponent(comp);
createEMFCheckbox(comp);
setControl(comp);
PlatformUI.getWorkbench().getHelpSystem()
.setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
}
/**
* Modify listener that simply updates the owning launch configuration dialog.
*/
private ModifyListener fBasicModifyListener = new ModifyListener() {
public void modifyText(ModifyEvent evt) {
scheduleUpdateJob();
}
};
/**
* Constant representing the id of the {@link IDialogSettings} location for the {@link ContainerSelectionDialog}
* used on this tab
*
* @since 3.6
*/
private final String WORKSPACE_SELECTION_DIALOG = IDebugUIConstants.PLUGIN_ID + ".WORKSPACE_SELECTION_DIALOG"; //$NON-NLS-1$
/**
* Returns the {@link IDialogSettings} for the given id
*
* @param id
* the id of the dialog settings to get
* @return the {@link IDialogSettings} to pass into the {@link ContainerSelectionDialog}
* @since 3.6
*/
IDialogSettings getDialogBoundsSettings(String id) {
IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
IDialogSettings section = settings.getSection(id);
if (section == null) {
section = settings.addNewSection(id);
}
return section;
}
/**
* Creates the component set for the ecore folder
*
* @param parent
* the parent to add this component to
*/
private void createEcoreFolderComponent(Composite parent) {
Group group = SWTFactory.createGroup(parent, "Folder with ecore models defining the metamodel", 5, 2,
GridData.FILL_HORIZONTAL);
Composite comp = SWTFactory.createComposite(group, 5, 5, GridData.FILL_BOTH);
GridLayout ld = (GridLayout) comp.getLayout();
ld.marginWidth = 1;
ld.marginHeight = 1;
GridData gd = new GridData(SWT.BEGINNING, SWT.NORMAL, true, false);
gd.horizontalSpan = 5;
fEcoreFolderText = SWTFactory.createSingleText(comp, 4);
fEcoreFolderText.getAccessible().addAccessibleListener(new AccessibleAdapter() {
public void getName(AccessibleEvent e) {
e.result = LaunchConfigurationsMessages.CommonTab_6;
}
});
fEcoreFolderText.addModifyListener(fBasicModifyListener);
Composite bcomp = SWTFactory.createComposite(comp, 3, 5, GridData.HORIZONTAL_ALIGN_END);
ld = (GridLayout) bcomp.getLayout();
ld.marginHeight = 1;
ld.marginWidth = 0;
fWorkspaceBrowse = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_12, null);
fWorkspaceBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(),
new WorkbenchLabelProvider(), new WorkbenchContentProvider());
dialog.setTitle(LaunchConfigurationsMessages.CommonTab_13);
dialog.setMessage("Select a folder containing all the ecore models defining the metamodel.");
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
dialog.setDialogBoundsSettings(getDialogBoundsSettings(WORKSPACE_SELECTION_DIALOG),
Dialog.DIALOG_PERSISTSIZE);
if (dialog.open() == IDialogConstants.OK_ID) {
IResource resource = (IResource) dialog.getFirstResult();
if (resource != null) {
String arg = resource.getFullPath().toString();
String fileLoc = VariablesPlugin.getDefault().getStringVariableManager()
.generateVariableExpression("workspace_loc", arg); //$NON-NLS-1$
fEcoreFolderText.setText(fileLoc);
}
}
}
});
fEcoreFolderBrowse = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_7, null);
fEcoreFolderBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String filePath = fEcoreFolderText.getText();
FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
filePath = dialog.open();
if (filePath != null) {
fEcoreFolderText.setText(filePath);
}
}
});
fVariables = createPushButton(bcomp, LaunchConfigurationsMessages.CommonTab_9, null);
fVariables.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
dialog.open();
String variable = dialog.getVariableExpression();
if (variable != null) {
fEcoreFolderText.insert(variable);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
/**
* Creates the component set for the ecore folder
*
* @param parent
* the parent to add this component to
*/
private void createMetamodelNameComponent(Composite parent) {
Group group = SWTFactory.createGroup(parent, "Metamodel name", 5, 2, GridData.FILL_HORIZONTAL);
Composite comp = SWTFactory.createComposite(group, 5, 5, GridData.FILL_BOTH);
GridLayout ld = (GridLayout) comp.getLayout();
ld.marginWidth = 1;
ld.marginHeight = 1;
GridData gd = new GridData(SWT.BEGINNING, SWT.NORMAL, true, false);
gd.horizontalSpan = 5;
fMetamodelNameText = SWTFactory.createSingleText(comp, 4);
fMetamodelNameText.getAccessible().addAccessibleListener(new AccessibleAdapter() {
public void getName(AccessibleEvent e) {
e.result = LaunchConfigurationsMessages.CommonTab_6;
}
});
fMetamodelNameText.addModifyListener(fBasicModifyListener);
}
@Override
public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
// TODO Stub de la méthode généré automatiquement
}
@Override
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText().trim());
configuration
.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, fEcoreFolderText.getText());
configuration.setAttribute("mmName", fMetamodelNameText.getText());
configuration.setAttribute("emf", fEMFCheckButton.getSelection());
}
public void initializeFrom(ILaunchConfiguration configuration) {
super.initializeFrom(configuration);
try {
String ecoreFolder = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
"");
fEcoreFolderText.setText(ecoreFolder);
String mmName = configuration.getAttribute("mmName", "");
fMetamodelNameText.setText(mmName);
boolean emf = configuration.getAttribute("emf", false);
fEMFCheckButton.setSelection(emf);
} catch (CoreException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
}
}
@Override
public String getName() {
// TODO Stub de la méthode généré automatiquement
return "Configuration";
}
}
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.plugin;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTabGroup;
import org.eclipse.debug.ui.ILaunchConfigurationDialog;
import org.eclipse.debug.ui.ILaunchConfigurationTab;
public class CloningMaterialGenerationLaunchTabGroup extends AbstractLaunchConfigurationTabGroup {
private ILaunchConfigurationTab[] tabs;
@Override
public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
tabs = new ILaunchConfigurationTab[] {
// new CommonTab(), // working, but not required
new CloningMaterialGenerationLaunchTab() };
setTabs(tabs);
}
}
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.plugin;
import java.io.File;
import java.io.FileFilter;
public class EcoreFileFilter implements FileFilter {
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".ecore");
}
}
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.start;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.eclipse.gmt.modisco.java.Model;
import org.eclipse.gmt.modisco.java.generation.files.GenerateJavaExtended;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.modisco.infra.discovery.core.exception.DiscoveryException;
import org.eclipse.modisco.java.discoverer.DiscoverJavaModelFromJavaProject;
import fr.inria.diverse.cloning.materialgenerator.generators.EMFCodeGenerator;
import fr.inria.diverse.cloning.materialgenerator.generators.EMFCodeGenerator.EMFGenerationException;
import fr.inria.diverse.cloning.materialgenerator.generators.EMFImpl2PImpl;
import fr.inria.diverse.cloning.materialgenerator.generators.ManifestEditor;
import fr.inria.diverse.cloning.materialgenerator.tagger.Tagger;
import fr.inria.diverse.cloning.materialgenerator.tagger.impl.DeepCloningTagger;
import fr.inria.diverse.cloning.materialgenerator.tagger.impl.ShareFieldsOnlyTagger;
import fr.inria.diverse.cloning.materialgenerator.tagger.impl.ShareAllTagger;
import fr.inria.diverse.cloning.materialgenerator.tagger.impl.ShareObjOnlyTagger;
import fr.inria.diverse.cloning.runtime.util.Log;
public class Start {
public static void start(Set<File> ecoreFiles, String metamodelName, IJavaProject javaProject, boolean emf) {
Log.info("Starting Cloning material generator");
try {
ResourceSet rs;
if (emf)
rs = EMFCodeGenerator.generate(ecoreFiles, metamodelName, javaProject.getProject());
else {
// Loading some ecore files in a resource set (= considered metamodel)
EcoreResourceFactoryImpl fact = new EcoreResourceFactoryImpl();
if (!EPackage.Registry.INSTANCE.containsKey(EcorePackage.eNS_URI)) {
EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
}
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", fact);
rs = new ResourceSetImpl();
}
for (File f : ecoreFiles)
rs.getResource(URI.createFileURI(f.getAbsolutePath()), true);
// Tagging
Collection<Tagger> taggers = Arrays.asList(new DeepCloningTagger(rs, metamodelName),
new ShareFieldsOnlyTagger(rs, metamodelName), new ShareObjOnlyTagger(rs, metamodelName),
new ShareAllTagger(rs, metamodelName));
// Discovering the model from the project
DiscoverJavaModelFromJavaProject discoverer = new DiscoverJavaModelFromJavaProject();
discoverer.discoverElement(javaProject, new NullProgressMonitor());
// Accessing the obtained model
Resource result = discoverer.getTargetModel();
Model javaModel = (Model) result.getContents().get(0);
// Getting the absolute path of the folder
File emfProjectFolder = javaProject.getProject().getLocation().toFile();
// String srcOutputFolder = "/home/ebousse/Dev/modelCloning/emf/SomeMetamodel/src";
String srcOutputFolder = new File(emfProjectFolder, "src").getAbsolutePath();
// Calling the partial implementations transformer with one tagger
Model brandNew;
Model toProcess = javaModel;
for (Tagger t : taggers) {
brandNew = new EMFImpl2PImpl(toProcess, t, true).transform2PImpl(srcOutputFolder);
GenerateJavaExtended javaGenerator = new GenerateJavaExtended(toProcess, new File(srcOutputFolder),
new ArrayList<Object>());
javaGenerator.doGenerate(null);
toProcess = brandNew;
}
//new EMFImpl2PImpl(originalModel, tagger2, true).transform2PImpl(srcOutputFolder);
// // Generating the code from the obtained model
// GenerateJavaExtended javaGenerator = new GenerateJavaExtended(javaModel, new File(srcOutputFolder),
// new ArrayList<Object>());
// javaGenerator.doGenerate(null);
//
// // Generating the code from the obtained model
// GenerateJavaExtended javaGenerator2 = new GenerateJavaExtended(originalModel, new File(srcOutputFolder),
// new ArrayList<Object>());
// javaGenerator2.doGenerate(null);
// Adding a dependency in the manifest
ManifestEditor manEdit = new ManifestEditor(javaProject.getProject());
manEdit.addDependency("fr.inria.diverse.cloning.runtime");
manEdit.save();
javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
// Done
Log.info("Done !");
} catch (DiscoveryException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
} catch (IOException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
} catch (EMFGenerationException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
} catch (CoreException e) {
// TODO Bloc catch généré automatiquement
e.printStackTrace();
}
}
}
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.tagger;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.ResourceSet;
import fr.inria.diverse.cloning.runtime.common.ClassTag;
public interface Tagger {
public abstract void recomputeTags();
public abstract boolean mayTagClassesShareable();
public abstract boolean mayTagClassesPartShareable();
public abstract String getSuffix();
public abstract String getCloningName();
public ClassTag getTagOfEClass(EClass c);
public ResourceSet getMetamodel();
public boolean isPropertyShareable(EStructuralFeature property);
public String getMetamodelName();
}
/*******************************************************************************
* Copyright (c) 2014 Université de Rennes 1.
* 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:
* Erwan Bousse - initial API and implementation
******************************************************************************/
package fr.inria.diverse.cloning.materialgenerator.tagger.impl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import fr.inria.diverse.cloning.materialgenerator.tagger.Tagger;
import fr.inria.diverse.cloning.runtime.common.ClassTag;
import fr.inria.diverse.cloning.runtime.util.EcoreHelper;
import fr.inria.diverse.cloning.runtime.util.Log;
public abstract class AbstractTagger implements Tagger {
/**
* Final result of the algorithm, with a tag for each class concerning its "mutability".
*/
protected Map<EClass, ClassTag> classesTags;
protected Map<EStructuralFeature, Boolean> propertiesTags;
protected ResourceSet metamodel;
protected String metamodelname;
@Override
public String getMetamodelName() {
return metamodelname;
}