Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [ATL] code example for a wrapper class for programmatic launch of ATL
[ATL] code example for a wrapper class for programmatic launch of ATL [message #105721] Wed, 27 May 2009 11:16 Go to next message
Henrik Rentz-Reichert is currently offline Henrik Rentz-ReichertFriend
Messages: 261
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040707070503050904050103
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi all,

I've created a little wrapper class for programmatic launch of an ATL
transformation (no refinement) with one source and one target (meta-)model.

All parameters are passed using special Objects rather than the Map used
in the ATL code. The loader can be customized.

Usage example:

RunTransformationDlg rtd = new RunTransformationDlg(window.getShell());
if (rtd.open()!=Window.OK)
return null;

try {
ATLExecutor.execute(rtd.getAtlPath().replace(".atl", ".asm"),
inModelInfo, rtd.getOutModelInfo());
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

I've also attached the RunTransformationDlg code (it's in major parts
from the ATL code).

Hope this will be useful.

Regards,
Henrik

--------------040707070503050904050103
Content-Type: text/plain;
name="ATLExecutor.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ATLExecutor.java"

package de.protos.atl.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.m2m.atl.common.ATLExecutionException;
import org.eclipse.m2m.atl.core.ATLCoreException;
import org.eclipse.m2m.atl.core.IExtractor;
import org.eclipse.m2m.atl.core.IInjector;
import org.eclipse.m2m.atl.core.IModel;
import org.eclipse.m2m.atl.core.IReferenceModel;
import org.eclipse.m2m.atl.core.ModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFInjector;
import org.eclipse.m2m.atl.core.emf.EMFModel;
import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
import org.eclipse.m2m.atl.core.launch.ILauncher;
import org.eclipse.m2m.atl.core.service.CoreService;

public class ATLExecutor {

public interface ModelLoader {
Resource load(ResourceSet rs);
}

public static class MetaInfo {
static public final String TYPE_EMF = "EMF";

private String name;
private String path;
private String type;

public MetaInfo(String name, String path) {
super();
this.name = name;
this.path = path;
this.type = TYPE_EMF;
}

public String getName() {
return name;
}

public String getPath() {
return path;
}

public String getType() {
return type;
}
}

public static class ModelInfo {

private String name;
private String path;
private ModelLoader loader;
private MetaInfo meta;

public ModelInfo(String name, String path, MetaInfo meta) {
super();
this.name = name;
this.path = path;
this.loader = null;
this.meta = meta;
}

public ModelInfo(String name, String path, ModelLoader loader, MetaInfo meta) {
super();
this.name = name;
this.path = path;
this.loader = loader;
this.meta = meta;
}

public String getName() {
return name;
}

public String getPath() {
return path;
}

public void setMeta(MetaInfo meta) {
this.meta = meta;
}

public MetaInfo getMeta() {
return meta;
}

public ModelLoader getLoader() {
return loader;
}

public void setLoader(ModelLoader loader) {
this.loader = loader;
}
}

// prevent instantiation
private ATLExecutor() {
}

public static void execute (
String asmPath,
ModelInfo in,
ModelInfo out
) throws ATLCoreException, IOException
{
Map<String, String> modelHandlers = new HashMap<String, String>();
modelHandlers.put(in.getMeta().getName(), in.getMeta().getType());
modelHandlers.put(out.getMeta().getName(), out.getMeta().getType());

URL asmURL = new URL(asmPath);

Map<String, Object> options = new HashMap<String, Object>();
options.put("modelHandlers", modelHandlers); //$NON-NLS-1$

InputStream asmInputStream = asmURL.openStream();
InputStream[] modules = new InputStream[1];
modules[0] = asmInputStream;

ILauncher launcher = CoreService.getLauncher("EMF-specific VM");
launcher.initialize(options);
ModelFactory defaultFactory = CoreService.createModelFactory(launcher.getDefaultModelFacto ryName());
IExtractor extractor = CoreService.getExtractor(defaultFactory.getDefaultExtractorN ame());
IInjector injector = CoreService.getInjector(defaultFactory.getDefaultInjectorNam e());

IModel model = getModel(in, false, launcher, defaultFactory, injector);
launcher.addInModel(model, in.getName(), in.getMeta().getName());

model = getModel(out, true, launcher, defaultFactory, injector);
launcher.addOutModel(model, out.getName(), out.getMeta().getName());

// LAUNCH
try {
launcher.launch(
ILauncher.RUN_MODE,
new NullProgressMonitor(),
options,
(Object[])modules);
}
catch (ATLExecutionException e) {
e.printStackTrace();
throw new ATLCoreException(asmPath, e);
}

// OUTPUT MODELS EXTRACTION
extractor.extract(launcher.getModel(out.getName()), out.getPath());
}

private static IModel getModel(ModelInfo mdlInfo, boolean newModel, ILauncher launcher, ModelFactory modelFactory, IInjector injector ) throws ATLCoreException {
IReferenceModel referenceModel = (IReferenceModel)launcher.getModel(mdlInfo.getMeta().getName ());

if (referenceModel == null) {
Map<String, Object> referenceModelOptions = new HashMap<String, Object>();
referenceModelOptions.put("modelHandlerName", mdlInfo.getMeta().getType()); //$NON-NLS-1$
referenceModelOptions.put("modelName", mdlInfo.getMeta().getName()); //$NON-NLS-1$
referenceModelOptions.put("path", mdlInfo.getMeta().getPath()); //$NON-NLS-1$
referenceModel = modelFactory.newReferenceModel(referenceModelOptions);
injector.inject(referenceModel, mdlInfo.getMeta().getPath());
}

Map<String, Object> modelOptions = new HashMap<String, Object>();
modelOptions.put("modelName", mdlInfo.getName()); //$NON-NLS-1$
modelOptions.put("path", mdlInfo.getPath()); //$NON-NLS-1$
modelOptions.put("newModel", newModel); //$NON-NLS-1$

IModel model = modelFactory.newModel(referenceModel, modelOptions);
if (!newModel) {
if (mdlInfo.getLoader()!=null && injector instanceof EMFInjector && model instanceof EMFModel && model.getModelFactory() instanceof EMFModelFactory) {
ResourceSet resourceSet = ((EMFModelFactory)model.getModelFactory()).getResourceSet();
Resource mainResource = mdlInfo.getLoader().load(resourceSet);
((EMFInjector)injector).inject((EMFModel)model, mainResource);
}
else {
injector.inject(model, mdlInfo.getPath());
}
}
return model;
}
}

--------------040707070503050904050103
Content-Type: text/plain;
name="RunTransformationDlg.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="RunTransformationDlg.java"

package de.protos.tests.ui.dialogs;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.m2m.atl.common.ATLLaunchConstants;
import org.eclipse.m2m.atl.common.ATLLogger;
import org.eclipse.m2m.atl.core.ui.launch.DialogUriSelection;
import org.eclipse.m2m.atl.engine.parser.AtlSourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.dialogs.SaveAsDialog;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import de.protos.atl.util.ATLExecutor;
import de.protos.tests.ui.Activator;

public class RunTransformationDlg extends TrayDialog {

private static final String OUT_MODEL = "out_model";

private static final String IN_MODEL = "in_model";

private static final String OUT_META_MODEL = "out_meta_model";

private static final String IN_META_MODEL = "in_meta_model";

private static final String ATL_SCRIPT = "atl_script";

/** Metamodel type. */
public static final int IS_METAMODEL = 1 << 1;

/** Model type. */
public static final int IS_MODEL = 1 << 2;

/** Source type. */
public static final int IS_SOURCE = 1 << 3;

/** Target type. */
public static final int IS_TARGET = 1 << 5;

/** Library type. */
public static final int IS_LIBRARY = 1 << 6;

/** Module type. */
public static final int IS_MODULE = 1 << 7;

private Text atlPathText;

private Text inMetaModelText;

private Text outMetaModelText;

private Text inModelText;

private Text outModelText;

private String atlPath;

private String inMetaModelPath;

private String outMetaModelPath;

private String inModelPath;

private String outModelPath;

private Label inMetamodelLabel;

private Label outMetamodelLabel;

private Label inModelLabel;

private Label outModelLabel;

private String inMetamodelName;

private String outMetamodelName;

private String inModelName;

private String outModelName;

public RunTransformationDlg(Shell shell) {
super(shell);
setShellStyle(getShellStyle()| SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE);
}

@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Run ATL Transformation");

shell.setImage(Activator.getImageDescriptor("icons/atllogo_icon.gif ").createImage());
}

@Override
protected Control createDialogArea(Composite parent) {
Composite rootContainer = new Composite(parent, SWT.NULL);
rootContainer.setLayout(new GridLayout());
rootContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Group moduleGroup = new Group(rootContainer, SWT.NULL);
moduleGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
moduleGroup.setLayout(new GridLayout(3, false));
moduleGroup.setText("ATL Module"); //$NON-NLS-1$

atlPathText = new Text(moduleGroup, SWT.SINGLE | SWT.BORDER);
atlPathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

final Button browseWorkspace = new Button(moduleGroup, SWT.RIGHT);
browseWorkspace.setText("Browse Workspace"); //$NON-NLS-1$

browseWorkspace.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
String path = editPath(IS_MODULE);
if (path.trim().length() > 0) {
atlPathText.setText(path);
}
getModelsFromATLFiles();
}
});

Group metamodelsGroup = new Group(rootContainer, SWT.NULL);
metamodelsGroup.setText("Metamodels"); //$NON-NLS-1$
metamodelsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
metamodelsGroup.setLayout(new GridLayout(4, false));

inMetamodelLabel = new Label(metamodelsGroup, SWT.NULL);
inMetaModelText = buildMetamodelControls(metamodelsGroup, inMetamodelLabel, IS_SOURCE | IS_METAMODEL);
outMetamodelLabel = new Label(metamodelsGroup, SWT.NULL);
outMetaModelText = buildMetamodelControls(metamodelsGroup, outMetamodelLabel, IS_TARGET | IS_METAMODEL);

Group sourceModelsGroup = new Group(rootContainer, SWT.NULL);
sourceModelsGroup.setText("Source and Target Model"); //$NON-NLS-1$
sourceModelsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
sourceModelsGroup.setLayout(new GridLayout(3, false));

inModelLabel = new Label(sourceModelsGroup, SWT.NULL);
inModelText = buildModelControls(sourceModelsGroup, inModelLabel, IS_SOURCE | IS_MODEL);
outModelLabel = new Label(sourceModelsGroup, SWT.NULL);
outModelText = buildModelControls(sourceModelsGroup, outModelLabel, IS_TARGET | IS_MODEL);

IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
atlPathText.setText(ps.getString(ATL_SCRIPT));
inMetaModelText.setText(ps.getString(IN_META_MODEL));
outMetaModelText.setText(ps.getString(OUT_META_MODEL));
inModelText.setText(ps.getString(IN_MODEL));
outModelText.setText(ps.getString(OUT_MODEL));

getModelsFromATLFiles();

return rootContainer;
}

private Text buildMetamodelControls(final Group parent, final Label metamodelLabel, final int type) {
metamodelLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));

final Text metamodelLocation = new Text(parent, SWT.BORDER);
metamodelLocation.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));

final Button browseWorkspace = new Button(parent, SWT.NULL);
browseWorkspace.setText("Browse Workspace"); //$NON-NLS-1$

final Button browseFilesystem = new Button(parent, SWT.NULL);
browseFilesystem.setText("Browse Filesystem"); //$NON-NLS-1$

final Button browseEMFRegistry = new Button(parent, SWT.NULL);
browseEMFRegistry.setText("Browse EMF Registry"); //$NON-NLS-1$

browseWorkspace.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
String path = editPath(type);
if (path.trim().length() > 0) {
metamodelLocation.setText(path);
}
}
});

browseFilesystem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
String path = editExternalPath(type);
if (path.trim().length() > 0) {
metamodelLocation.setText(path);
}
}
});

browseEMFRegistry.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
DialogUriSelection launcher = new DialogUriSelection(parent.getShell());
launcher.create();
if (launcher.open() == Dialog.OK) {
metamodelLocation.setText("uri:" + launcher.getUriSelected()); //$NON-NLS-1$
}
}
});

return metamodelLocation;
}

private void getModelsFromATLFiles() {
IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
String path = atlPathText.getText();
IResource res = wsRoot.findMember(path);
if (res instanceof IFile) {
getModelsFromATLFile((IFile)res);
}
}

private void getModelsFromATLFile(IFile file) {
AtlSourceManager sourceManager = new AtlSourceManager();
try {
sourceManager.updateDataSource(file.getContents());
} catch (CoreException e) {
return;
} catch (IOException e) {
return;
}

switch (sourceManager.getATLFileType()) {
case AtlSourceManager.ATL_FILE_TYPE_MODULE:
// boolean isRefining = sourceManager.isRefining();
for (Iterator<?> iterator = sourceManager.getInputModels().entrySet().iterator(); iterator
.hasNext();) {
Entry<?, ?> entry = (Entry<?, ?>)iterator.next();
String modelName = (String)entry.getKey();
inModelLabel.setText(modelName + ":"); //$NON-NLS-1$
String metamodelName = (String)entry.getValue();
inMetamodelLabel.setText(metamodelName + ":"); //$NON-NLS-1$

}
for (Iterator<?> iterator = sourceManager.getOutputModels().entrySet().iterator(); iterator
.hasNext();) {
Entry<?, ?> entry = (Entry<?, ?>)iterator.next();
String modelName = (String)entry.getKey();
outModelLabel.setText(modelName + ":"); //$NON-NLS-1$
String metamodelName = (String)entry.getValue();
outMetamodelLabel.setText(metamodelName + ":"); //$NON-NLS-1$
}

// for (Iterator<?> iterator = sourceManager.getLibrariesImports().iterator(); iterator
// .hasNext();) {
// String library = (String)iterator.next();
// }
break;
case AtlSourceManager.ATL_FILE_TYPE_QUERY:
for (Iterator<?> iterator = sourceManager.getInputModels().entrySet().iterator(); iterator
.hasNext();) {
// Entry<?, ?> entry = (Entry<?, ?>)iterator.next();
// String modelName = (String)entry.getKey();
// String metamodelName = (String)entry.getValue();
}
// for (Iterator<?> iterator = sourceManager.getLibrariesImports().iterator(); iterator
// .hasNext();) {
// String library = (String)iterator.next();
// }
break;
default:
break;
}
}

@Override
protected void okPressed() {
atlPath = atlPathText.getText();
inMetaModelPath = inMetaModelText.getText();
outMetaModelPath = outMetaModelText.getText();
inModelPath = inModelText.getText();
outModelPath = outModelText.getText();
IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
ps.setValue(ATL_SCRIPT, atlPath);
ps.setValue(IN_META_MODEL, inMetaModelPath);
ps.setValue(OUT_META_MODEL, outMetaModelPath);
ps.setValue(IN_MODEL, inModelPath);
ps.setValue(OUT_MODEL, outModelPath);

inMetamodelName = inMetamodelLabel.getText().substring(0, inMetamodelLabel.getText().length()-1);
inModelName = inModelLabel.getText().substring(0, inModelLabel.getText().length()-1);
outMetamodelName = outMetamodelLabel.getText().substring(0, outMetamodelLabel.getText().length()-1);
outModelName = outModelLabel.getText().substring(0, outModelLabel.getText().length()-1);

super.okPressed();
}

public String getAtlPath() {
return convertPath(atlPath);
}

public String getInMetaModelPath() {
return convertPath(inMetaModelPath);
}

public String getOutMetaModelPath() {
return convertPath(outMetaModelPath);
}

public String getInModelPath() {
return convertPath(inModelPath);
}

public String getOutModelPath() {
return convertPath(outModelPath);
}

public String getInMetamodelName() {
return inMetamodelName;
}

public String getOutMetamodelName() {
return outMetamodelName;
}

public String getInModelName() {
return inModelName;
}

public String getOutModelName() {
return outModelName;
}

public ATLExecutor.ModelInfo getInModelInfo () {
ATLExecutor.MetaInfo inMeta = new ATLExecutor.MetaInfo(
inMetamodelName,
convertPath(inMetaModelPath));
ATLExecutor.ModelInfo in = new ATLExecutor.ModelInfo(
inModelName,
convertPath(inModelPath),
inMeta);
return in;
}

public ATLExecutor.ModelInfo getOutModelInfo () {
ATLExecutor.MetaInfo outMeta = new ATLExecutor.MetaInfo(
outMetamodelName,
convertPath(outMetaModelPath));
ATLExecutor.ModelInfo out = new ATLExecutor.ModelInfo(
outModelName,
convertPath(outModelPath),
outMeta);
return out;
}

/**
* Convert "launch configuration style" paths to EMF uris:
* <ul>
* <li>ext:<i>path</i> => file:<i>path</i> (file system resource)</li>
* <li>uri:<i>uri</i> => <i>uri</i> (EMF uri)</li>
* <li><i>path</i> => platform:/resource/<i>path</i> (workspace resource)</li>
* </ul>
* Unchanged paths:
* <ul>
* <li>platform:/plugin/<i>path</i> (plugin resource)</li>
* <li>pathmap:<i>path</i> (pathmap resource, e.g. UML2 profile)</li>
* </ul>
*
* @param path
* the path as created by the launchConfiguration
* @return the converted path
*/
public static String convertPath(String path) {
if (path.startsWith("ext:")) { //$NON-NLS-1$
return path.replaceFirst("ext:", "file:/"); //$NON-NLS-1$ //$NON-NLS-2$
} else if (path.startsWith("uri:")) { //$NON-NLS-1$
return path.substring(4);
} else if (path.startsWith("#") || path.startsWith("platform:") || path.startsWith("pathmap")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return path;
}
return "platform:/resource" + path; //$NON-NLS-1$
}

private Text buildModelControls(Group parent, final Label modelLabel, final int type) {
final Text location = new Text(parent, SWT.BORDER);
location.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));

final Button browseWorkspace = new Button(parent, SWT.NULL);
browseWorkspace.setText("Browse Workspace"); //$NON-NLS-1$

final Button browseFilesystem = new Button(parent, SWT.NULL);
browseFilesystem.setText("Browse Filesystem"); //$NON-NLS-1$
browseFilesystem.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));

browseWorkspace.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
String path = editPath(type);
if (path.trim().length() > 0) {
location.setText(path);
}
}
});

browseFilesystem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent evt) {
String path = editExternalPath(type);
if (path.trim().length() > 0) {
location.setText(path);
}
}
});

return location;
}

/**
* This method edit the path of a model. The path can be external at the workspace
*
* @param type
* @param table
*/
private String editExternalPath(final int type) {
String ret = ""; //$NON-NLS-1$
FileDialog fileDialog;

if (type == (IS_TARGET | IS_MODEL)) {
fileDialog = new FileDialog(this.getShell(), SWT.SAVE);
} else {
fileDialog = new FileDialog(this.getShell(), SWT.OPEN);
}

if (type == IS_MODULE) {
String[] extensions = new String[ATLLaunchConstants.ATL_EXTENSIONS.length];
for (int i = 0; i < ATLLaunchConstants.ATL_EXTENSIONS.length; i++) {
extensions[i] = "*." + ATLLaunchConstants.ATL_EXTENSIONS[i]; //$NON-NLS-1$
}
fileDialog.setFilterExtensions(extensions);
} else {
fileDialog.setFilterExtensions(new String[] {"*"}); //$NON-NLS-1$
}
String fileName = fileDialog.open();
if (fileName != null) {
ret = "ext:" + fileName; //$NON-NLS-1$
}

return ret;
}

/**
* This method edit the path of the model selected. The path corresponding the a file in the workspace
*
* @param type
* @param table
*/
private String editPath(final int type) {
String ret = ""; //$NON-NLS-1$

if (type == (IS_TARGET | IS_MODEL)) {
SaveAsDialog sad = new SaveAsDialog(getShell());
sad.open();
IPath outputPath = sad.getResult();
if (outputPath != null) {
ret = outputPath.toString();
}
} else {
ElementTreeSelectionDialog elementTreeSelectionDialog = new ElementTreeSelectionDialog(
getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider());
elementTreeSelectionDialog.setInput(ResourcesPlugin.getWorks pace().getRoot());
elementTreeSelectionDialog.setMessage("Select File"); //$NON-NLS-1$
elementTreeSelectionDialog.setTitle("Select File"); //$NON-NLS-1$
elementTreeSelectionDialog.setAllowMultiple(false);
elementTreeSelectionDialog.setDoubleClickSelects(true);
elementTreeSelectionDialog.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
boolean ret = false;
try {
String[] extensions = null;
if (type == IS_MODULE) {
extensions = ATLLaunchConstants.ATL_EXTENSIONS;
} else if (type == IS_LIBRARY) {
extensions = new String[] {"asm"}; //$NON-NLS-1$
}
ret = isATLResource((IResource)element, extensions);
} catch (CoreException e) {
ATLLogger.warning(e.getMessage());
}
return ret;
}
});
elementTreeSelectionDialog.setValidator(new ISelectionStatusValidator() {
public IStatus validate(Object[] selection) {
IStatus ret = Status.CANCEL_STATUS;
if (selection.length == 1) {
if (selection[0] instanceof IFile) {
ret = Status.OK_STATUS;
}
}
return ret;
}
});
elementTreeSelectionDialog.open();
Object result = elementTreeSelectionDialog.getFirstResult();

if ((result != null) && (result instanceof IFile)) {
IFile currentFile = (IFile)result;
ret = currentFile.getFullPath().toString();
}
}
return ret;
}

private static boolean isATLResource(IResource resource, String[] extensions) throws CoreException {
if (resource instanceof IContainer) {
if (((IContainer)resource).isAccessible()) {
IResource[] members = ((IContainer)resource).members();
for (IResource member : members) {
if (isATLResource(member, extensions)) {
return true;
}
}
}
} else if (resource instanceof IFile) {
IFile currentFile = (IFile)resource;
if (extensions == null) {
return true;
} else if (currentFile.getFileExtension() != null) {
for (int i = 0; i < extensions.length; i++) {
String extension = extensions[i];
if (currentFile.getFileExtension().toUpperCase().equals(extensi on.toUpperCase())) {
return true;
}
}
}
}
return false;
}
}

--------------040707070503050904050103--
Re: [ATL] code example for a wrapper class for programmatic launch of ATL [message #107795 is a reply to message #105721] Fri, 03 July 2009 15:37 Go to previous message
Miguel Beca is currently offline Miguel BecaFriend
Messages: 11
Registered: July 2009
Junior Member
Hi Henrik,

Thank you for posting your code.

I am trying to use your example, however, there is an import for

import de.protos.tests.ui.Activator;


Is there a way you could provide this package as well?

Thanks,

Miguel Beca


Henrik Rentz-Reichert wrote:

> Hi all,

> I've created a little wrapper class for programmatic launch of an ATL
> transformation (no refinement) with one source and one target (meta-)model.

> All parameters are passed using special Objects rather than the Map used
> in the ATL code. The loader can be customized.

> Usage example:

> RunTransformationDlg rtd = new RunTransformationDlg(window.getShell());
> if (rtd.open()!=Window.OK)
> return null;

> try {
> ATLExecutor.execute(rtd.getAtlPath().replace(".atl", ".asm"),
> inModelInfo, rtd.getOutModelInfo());
> } catch (ATLCoreException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> }

> I've also attached the RunTransformationDlg code (it's in major parts
> from the ATL code).

> Hope this will be useful.

> Regards,
> Henrik
Previous Topic:[ATL] Best method for java integration
Next Topic:[ATL] Model with several metamodels
Goto Forum:
  


Current Time: Fri Apr 19 06:26:30 GMT 2024

Powered by FUDForum. Page generated in 0.01378 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top