Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [ATL] How to inject XML?
[ATL] How to inject XML? [message #105157] Mon, 18 May 2009 09:16 Go to next message
Lucas is currently offline Lucas
Messages: 9
Registered: July 2009
Junior Member
Hi everybody!

I've read quite a lot around this topic so far, but I was unable to find
any helpful answer to my question...

My question is about the creation of an injector for XML-files. I'm
planning to do a transformation following these steps:
1. XML-File (.xml) to XML.ecore-Model (injection)
2. XML.ecore-Model to Source.ecore-Model
3. Source.ecore-Model to Target.ecore-Model
4. Target.ecore-Model to XML.ecore-Model
5. XML.ecore-Model to XML-File (extraction)

By now, steps 2-5 are working - all I need is some way to inject an
XML-File to my XML.ecore-Model. For the extraction I'm using a ATL-query -
maybe there is some existing query for the injection of XML? Or any other
approach?

Any hints would be appreciated...

Lucas
Re: [ATL] How to inject XML? [message #105327 is a reply to message #105157] Tue, 19 May 2009 15:53 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 604
Registered: July 2009
Senior Member
Hi Lucas,

Lucas a écrit :
> Hi everybody!
>
> I've read quite a lot around this topic so far, but I was unable to find
> any helpful answer to my question...
>
> My question is about the creation of an injector for XML-files. I'm
> planning to do a transformation following these steps:
> 1. XML-File (.xml) to XML.ecore-Model (injection)
> 2. XML.ecore-Model to Source.ecore-Model
> 3. Source.ecore-Model to Target.ecore-Model
> 4. Target.ecore-Model to XML.ecore-Model
> 5. XML.ecore-Model to XML-File (extraction)
>
> By now, steps 2-5 are working - all I need is some way to inject an
> XML-File to my XML.ecore-Model. For the extraction I'm using a ATL-query
> - maybe there is some existing query for the injection of XML? Or any
> other approach?

By following the instructions from
http://wiki.eclipse.org/AM3/How_Install_AM3_From_SVN, you will install
additional plugins which provide XML injection/extraction facilities:

** From an XML file (.xml file), you will be able to right-click on it
an select "Inject XML file to XML model (Ecore based)" in order to get
the corresponding XML model.
** From an XML model (.xmi file conforming to the XML metamodel), you
will be able to right-click on it an select "Extract XML model to XML
file" in order to get the corresponding XML model.

Note that the XML metamodel is available from the "resources" folder of
the "org.eclipse.am3.core" plugin (file named XML.ecore).

Best regards,

Hugo

>
> Any hints would be appreciated...
>
> Lucas
>


--
-------------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (INRIA & EMN) - Room B206
École des Mines de Nantes
4, rue Alfred Kastler
44307 Nantes Cedex 3 - France
EMail: hugo.bruneliere@inria.fr
-------------------------------------------------


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: [ATL] How to inject XML? [message #105382 is a reply to message #105327] Wed, 20 May 2009 09:51 Go to previous messageGo to next message
Lucas is currently offline Lucas
Messages: 9
Registered: July 2009
Junior Member
Hi Hugo,

thanks for your reply. I finally got it working now :-) Thank you! I tried
using the new AM3-version but there were some build errors which made it
impossible to run the application. I didn't know that I have to use the
old version instead...

I wonder why you don't offer the download of AM3 as Eclipse Plugins,
because the way now (via SVN) requires more effort than necessary...?

Regards,
Lucas

Hugo Bruneliere wrote:

> Hi Lucas,

> Lucas a écrit :
>> Hi everybody!
>>
>> I've read quite a lot around this topic so far, but I was unable to find
>> any helpful answer to my question...
>>
>> My question is about the creation of an injector for XML-files. I'm
>> planning to do a transformation following these steps:
>> 1. XML-File (.xml) to XML.ecore-Model (injection)
>> 2. XML.ecore-Model to Source.ecore-Model
>> 3. Source.ecore-Model to Target.ecore-Model
>> 4. Target.ecore-Model to XML.ecore-Model
>> 5. XML.ecore-Model to XML-File (extraction)
>>
>> By now, steps 2-5 are working - all I need is some way to inject an
>> XML-File to my XML.ecore-Model. For the extraction I'm using a ATL-query
>> - maybe there is some existing query for the injection of XML? Or any
>> other approach?

> By following the instructions from
> http://wiki.eclipse.org/AM3/How_Install_AM3_From_SVN, you will install
> additional plugins which provide XML injection/extraction facilities:

> ** From an XML file (.xml file), you will be able to right-click on it
> an select "Inject XML file to XML model (Ecore based)" in order to get
> the corresponding XML model.
> ** From an XML model (.xmi file conforming to the XML metamodel), you
> will be able to right-click on it an select "Extract XML model to XML
> file" in order to get the corresponding XML model.

> Note that the XML metamodel is available from the "resources" folder of
> the "org.eclipse.am3.core" plugin (file named XML.ecore).

> Best regards,

> Hugo

>>
>> Any hints would be appreciated...
>>
>> Lucas
>>
Re: [ATL] How to inject XML? [message #665424 is a reply to message #105157] Thu, 14 April 2011 15:08 Go to previous messageGo to next message
Emwedish Endargachew is currently offline Emwedish Endargachew
Messages: 27
Registered: April 2011
Junior Member
Hi Lucas,

I know it has been a long time since you posted this questions and i believe u have solved the problem. I am currently facing the same problem, would you be kind and share the idea with me. If u want to send me an email: it is "emwedishberhanu@yahoo.com"

Explaining the working steps would be helpful.

I am currently using different approach, which is creating the metamodel through EMF project and then registering the meta-model and using it as a meta model in ATL transformation. But still it is giving me package with URI is not found which is same as urs.

I appreciate your help

[Updated on: Thu, 14 April 2011 15:09]

Report message to a moderator

Re: [ATL] How to inject XML? [message #665505 is a reply to message #105157] Thu, 14 April 2011 20:34 Go to previous messageGo to next message
Thanasis Naskos is currently offline Thanasis Naskos
Messages: 22
Registered: March 2011
Junior Member
Hi Edwedish,

I've write a plugin (changing the "Hello World" plugin) for a project of mine, running atl transformation programmatically using XML parameters..

Although it's adapted to my project's needs (modisco transformation) it may help you..

I've attached the code below!
package trasform2singleton.actions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
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.launch.ILauncher;
import org.eclipse.m2m.atl.core.service.CoreService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;




/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class SingletonTransformation implements IWorkbenchWindowActionDelegate {
	private static IInjector injector;

	private static IExtractor extractor;

	private static IReferenceModel sourceTargetMetamodel;
	
	private static IReferenceModel XMLMetamodel;

	
	private static String transformationFilepath = "/home/thanasis/Desktop/data/transformation/modiscoJavaModelSingleton.asm";
	private static String libraryFilepath = "/home/thanasis/Desktop/data/transformation/XMLHelpers.asm";
	private static String dataSourceFilepath = "file:///home/thanasis/Desktop/data/toSingleton.javaxmi";
	private static String dataSourceXMLFilepath = "file:///home/thanasis/Desktop/data/parameters.xml";
	private static String dataTargetfilepath = "file:///home/thanasis/Desktop/data/newModel.javaxmi";

	static {
		// ATL public2private transformation
		try {
			injector = CoreService.getInjector("EMF"); //$NON-NLS-1$
			extractor = CoreService.getExtractor("EMF"); //$NON-NLS-1$			
		} catch (ATLCoreException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * The constructor.
	 */
	public SingletonTransformation() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		// parameters.xml creation
		try{
		    // Create file 
		    FileWriter fstream = new FileWriter("/home/thanasis/Desktop/data/parameters.xml");
		        BufferedWriter out = new BufferedWriter(fstream);
		    out.write("<parameters>\n	<param name=\"singletonClass\" value=\"Singleton\"/>\n</parameters>");
		    //Close the output stream
		    out.close();
		 }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		 }
		 
		ModelFactory factory = null;
		try {
			factory = CoreService.getModelFactory("EMF");
			//launch options
			Map<String, Object> options = new HashMap<String, Object>();
			options.put("allowInterModelReferences", Boolean.TRUE);
			
			// Metamodels
			sourceTargetMetamodel = factory.newReferenceModel();
			XMLMetamodel = factory.newReferenceModel();
			injector.inject(XMLMetamodel, "http://www.eclipse.org/MoDisco/Xml/0.1.incubation/XML");
			injector.inject(sourceTargetMetamodel, "http://www.eclipse.org/MoDisco/Java/0.2.incubation/java");
	
			// Getting launcher
			ILauncher launcher = null;
			launcher = CoreService.getLauncher("EMF-specific VM"); //$NON-NLS-1$
			
			launcher.initialize(Collections.<String, Object> emptyMap());
	
			// Creating models
			IModel sourceModel = factory.newModel(sourceTargetMetamodel);
			IModel XMLModel = factory.newModel(XMLMetamodel);
			IModel targetModel = factory.newModel(sourceTargetMetamodel);
	
			// Loading existing model
			injector.inject(sourceModel, dataSourceFilepath);
			injector.inject(XMLModel, dataSourceXMLFilepath);
			
		
			// Launching
			launcher.addInModel(sourceModel, "IN", "MODISCOJAVA");
			launcher.addOutModel(targetModel, "OUT", "MODISCOJAVA");
			launcher.addInModel(XMLModel, "parameters", "XML");
			
			
			URL asmFile1 = new File(transformationFilepath).toURI().toURL();
			URL asmFile2 = new File(libraryFilepath).toURI().toURL();
			InputStream asmInputStream1 = (InputStream) asmFile1.openStream();
			InputStream asmInputStream2 = (InputStream) asmFile2.openStream();
			InputStream[] modules = new InputStream[2];
			modules[0] = asmInputStream1;
			modules[1] = asmInputStream2;
			
			//Object loadedModule = launcher.loadModule(asmFile.openStream());
	
			launcher.launch(ILauncher.RUN_MODE, new NullProgressMonitor(), options, (Object[])modules);
	
			// Saving model
			extractor.extract(targetModel, dataTargetfilepath); //the name of the generated model
			
		} catch (ATLCoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //$NON-NLS-1$
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Transformation Complete");
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
	}
}

Re: [ATL] How to inject XML? [message #665619 is a reply to message #665505] Fri, 15 April 2011 09:39 Go to previous messageGo to next message
Cesar Caves is currently offline Cesar Caves
Messages: 22
Registered: July 2009
Junior Member
Hello everybody:

I have faced the same problem about dealing with the AM3 plugins and I wonder the same question, why they are not offered as jars which can be directly downloaded and installed.... Right now, the way I work is using an available AM3 RCP which has all the stuff needed to work with these issues, although, in fact I only use the injection / extraction for XML files facilities.

Best regards.
Re: [ATL] How to inject XML? [message #665860 is a reply to message #665505] Sun, 17 April 2011 10:57 Go to previous messageGo to next message
Emwedish Endargachew is currently offline Emwedish Endargachew
Messages: 27
Registered: April 2011
Junior Member
Hi Lucas,

I really appreciate for your fast reply. And i would like to ask you more question.

I have tried to understand the code, and i think u have all the steps 1 to 5 in java code. Since i am new for transformation i am not clear with what is going on. and i have the following questions for you.

I wonder why you use java code to implement the injection of XML. Is there no any way that i can add the XML file to the ATL project and then inject the XML using right click and selecting inject xml to .ecore or something? like Km3 to ecore injection.

If it is not possible can u write me little description of the code you wrote and how i can use it. I am confused about the files u have used during implementaiotn, like .asm and stuff.

Also the extension .javaxmi is not clear for me. Never saw such extension before.

Generally if it there is no other way to have direct injection, would you please tell me how i can use this plug in?

I really appreciate for the fast reply
Thanks in advance
Re: [ATL] How to inject XML? [message #665975 is a reply to message #105157] Mon, 18 April 2011 12:36 Go to previous messageGo to next message
Thanasis Naskos is currently offline Thanasis Naskos
Messages: 22
Registered: March 2011
Junior Member
Hi Emwedish,

I think you are mistaken... I've posted the code above "Thanasis Naskos", so I think you are asking me about all this stuff...

.javaxmi -> This has nothing to do with what you want to do. It a model "Modisco Java Model" that I'm using in my transformation

.asm -> Every .atl file, automatically, produces an .asm file that holds the same information in a difference form. You need this file, not the .atl, when you run your transformation programmatically.

the code posted above -> It's a plugin based on "Hello World plugin" which runs transformations programmatically. I've posted the code to help you understand how to inject and extract metamodels and models programmatically, like in these lines
injector.inject(XMLMetamodel, "http://www.eclipse.org/MoDisco/Xml/0.1.incubation/XML");
injector.inject(sourceTargetMetamodel, "http://www.eclipse.org/MoDisco/Java/0.2.incubation/java");

...where I'm injection the metamodels, my transformation needs!
But as I understand I've confused you more, since you don't want to run your transformation programmatically. But it's good to know that there is also this way to do it.

How to inject a metamodel not programmatically -> First you should add the name that is conforms to, in the header of the atl file
module something;
create OUT : XML from IN : MyMetamodel

Then if you right click on the atl file from the Project Explorer and go to "Run As" -> "Run Configurations" it should automatically prompts you to select the appropriate metamodels for every metamodel name of the "create" section..

If this is not helping you don't hesitate to ask anything else...
Re: [ATL] How to inject XML? [message #684626 is a reply to message #665975] Thu, 16 June 2011 00:07 Go to previous message
Emwedish Endargachew is currently offline Emwedish Endargachew
Messages: 27
Registered: April 2011
Junior Member
HI Thanasis Naskos,
sorry for mistaken your name and thanks for the reply. Thanks for giving me the chance to contact you if i have more question. Since you explained it very well, it looks i understand what you mean. As long as i want the transformation for tool integration, i will totally need to automate my transformation. So it is really helpful. However, i want a way to use my XML and XSD files as an input and output of the transformation. Your code looks to me that i can use it at the end of my transformation. After i implementation, then the automation will come. But in my case i need to use XML or XSD as an a source and target models. Do you think using plugin will help me achieve what i am looking for?

Best regards,
Previous Topic:created model does not conform to its metamodel
Next Topic:[ATL] How to transform model and meta model in one transformation
Goto Forum:
  


Current Time: Fri Sep 19 07:55:17 GMT 2014

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

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