| Home » Archived » AM3 » Injection and EMF generated API
 Goto Forum:| 
| Injection and EMF generated API [message #655816] | Tue, 22 February 2011 12:20  |  | 
| Eclipse User  |  |  |  |  | Hello 
 I'm facing a problem when I try to use a EMF resource after an injection.
 I have writen an injector from XML to my metamodel. But when I try to access the resource the model objects are DynamicEObjectImpl even if the generated EMF code is available ! I get the following error:
 !MESSAGE Unable to create view ID fr.irit.ifclipse.views.IFConfigurationView: org.eclipse.emf.ecore.impl.DynamicEObjectImpl cannot be cast to fr.irit.ifclipse.metamodel.IFConfig.IFConfig
 
 
 I have posted in the EMF forum, but since I'm using the AM3 XMLInjector, may be you can help me.
 
 my post on EMF forum:  http://www.eclipse.org/forums/index.php?t=rview&goto=655 772#msg_655772
 
 And this is the code of the injector:
 
 
 
package fr.irit.ifclipse.xml.injector;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.m2m.atl.core.ATLCoreException;
import org.eclipse.m2m.atl.core.emf.EMFExtractor;
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.emf.EMFReferenceModel;
import org.eclipse.m2m.atl.drivers.emf4atl.ASMEMFModel;
import org.eclipse.m2m.atl.drivers.emf4atl.AtlEMFModelHandler;
import org.eclipse.m2m.atl.engine.vm.AtlLauncher;
import org.eclipse.m2m.atl.engine.vm.AtlModelHandler;
import org.eclipse.m2m.atl.engine.vm.ModelLoader;
import org.eclipse.m2m.atl.projectors.xml.XMLInjector;
public class IFConfigurationXMLInjectorImpl implements
		IFConfigurationXMLInjector {
	private static final String IF_CONFIGURATION_ECORE_PATH = "fr.irit.ifclipse.metamodel/src/fr/irit/ifclipse/metamodel/metamodel/IFConfiguration.ecore";
	private static final String XML_ECORE_PATH = "org.eclipse.m2m.atl.projectors.xml/src/org/eclipse/m2m/atl/projectors/xml/resources/XML.ecore";
	private AtlEMFModelHandler modelHandler;
	private ModelLoader modelLoader;
	private ASMEMFModel XMLMetamodel;
	private ASMEMFModel IFConfigurationMetamodel;
	private URI XML_ecoreURI;
	private URI IFConfiguration_ecoreURI;
	private URL XML2IFConfiguration_PATH;
	/*
	 * inject an XML inputstream into an IFConfiguration resource
	 */
	@Override
	public Resource inject(InputStream sourceXML) {
		// load the MetaM and M
		createResources();
		Resource result = null;
		EMFInjector emfInjector = new EMFInjector();
		try {
			// create an EMFModel instance of XML
			EMFReferenceModel XMLReferenceModel = (EMFReferenceModel) (new EMFModelFactory())
					.newReferenceModel();
			emfInjector.inject(XMLReferenceModel, XML_ecoreURI.toString());
			EMFModel sourceXMLEMFModel = (EMFModel) (new EMFModelFactory())
					.newModel(XMLReferenceModel);
			// inject the inputstream in the XML EMFModel
			XMLInjector xmlInjector = new XMLInjector();
			xmlInjector.inject(sourceXMLEMFModel, sourceXML, null);
			// transform the XML EMFModel to IFConfig EMFModel using
			// XML2IFConfig.asm
			result = xml2ifConfigTrafo(sourceXMLEMFModel);
		} catch (ATLCoreException e) {
			e.printStackTrace();
		}
		return result;
	}
	private void createResources() {
		modelHandler = (AtlEMFModelHandler) AtlModelHandler
				.getDefault(AtlModelHandler.AMH_EMF);
		modelLoader = modelHandler.createModelLoader();
		XML_ecoreURI = URI.createPlatformPluginURI(XML_ECORE_PATH, true);
		IFConfiguration_ecoreURI = URI.createPlatformPluginURI(
				IF_CONFIGURATION_ECORE_PATH, true);
		XML2IFConfiguration_PATH = IFConfigurationXMLInjectorImpl.class
				.getResource("resources/XML2IFConfig.asm");
	}
	private Resource xml2ifConfigTrafo(final EMFModel sourceXMLEMFModel) {
		Resource result = null;
		Map<String, Object> models = new HashMap<String, Object>();
		initMetamodels(models);
		// get/create models
		try {
			// TODO: transform the 2 EMFModels into ASMEMFModels
			final PipedOutputStream outputStream = new PipedOutputStream();
			PipedInputStream inputStream = new PipedInputStream(outputStream);
			new Thread(new Runnable() {
				public void run() {
					try {
						(new EMFExtractor()).extract(sourceXMLEMFModel,
								outputStream, null);
					} catch (ATLCoreException e) {
						e.printStackTrace();
					}
					try {
						outputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
			ASMEMFModel XMLInputModel = (ASMEMFModel) modelLoader.loadModel(
					"IN", XMLMetamodel, inputStream);
			inputStream.close();
			URI saveURI = URI
					.createFileURI("d:/workspaces/wp_irit_ifgui/fr.irit.ifclipse/src/fr/irit/ifclipse/xml/savedXML.xml.xmi");
			Resource XMLExtent = XMLInputModel.getExtent();
			XMLExtent.setURI(saveURI);
			XMLExtent.save(Collections.EMPTY_MAP);
			//
			ASMEMFModel IFConfigOutputModel = (ASMEMFModel) modelLoader
					.newModel("OUT", "", IFConfigurationMetamodel);
			// load models
			models.put("IN", XMLInputModel);
			models.put("OUT", IFConfigOutputModel);
			// launch
			AtlLauncher.getDefault().launch(this.XML2IFConfiguration_PATH,
					Collections.EMPTY_MAP, models, Collections.EMPTY_MAP,
					Collections.EMPTY_LIST, Collections.EMPTY_MAP);
			result = IFConfigOutputModel.getExtent();
			URI saveURI_IF = URI
					.createFileURI("d:/workspaces/wp_irit_ifgui/fr.irit.ifclipse/src/fr/irit/ifclipse/xml/savedIFConfig.ifconfig.xmi");
			result.setURI(saveURI_IF);
			result.save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	// loads the metamodels in a Map<String, Object>
	private void initMetamodels(Map<String, Object> models) {
		try {
			XMLMetamodel = (ASMEMFModel) modelLoader.loadModel("XML",
					modelLoader.getMOF(), XML_ecoreURI.toString());
			IFConfigurationMetamodel = (ASMEMFModel) modelLoader.loadModel(
					"IFConfiguration", modelLoader.getMOF(),
					IFConfiguration_ecoreURI.toString());
			models.put("XML", XMLMetamodel);
			models.put("IFConfiguration", IFConfigurationMetamodel);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public Resource inject(Resource IFConfigResource, InputStream sourceXML,
			Map<String, Object> options) {
		return null;
	}
}
 |  |  |  |  | 
| Re: Injection and EMF generated API [message #655898 is a reply to message #655816] | Wed, 23 February 2011 04:00   |  | 
| Eclipse User  |  |  |  |  | Le 22/02/2011 18:20, ElArbi a écrit : > Hello
 >
 > I'm facing a problem when I try to use a EMF resource after an injection.
 > I have writen an injector from XML to my metamodel. But when I try to
 > access the resource the model objects are DynamicEObjectImpl even if the
 > generated EMF code is available ! I get the following error:
 > !MESSAGE Unable to create view ID
 > fr.irit.ifclipse.views.IFConfigurationView:
 > org.eclipse.emf.ecore.impl.DynamicEObjectImpl cannot be cast to
 > fr.irit.ifclipse.metamodel.IFConfig.IFConfig
 >
 >
 > I have posted in the EMF forum, but since I'm using the AM3 XMLInjector,
 > may be you can help me.
 >
 > my post on EMF forum:
 >  http://www.eclipse.org/forums/index.php?t=rview&goto=655 772#msg_655772
 >
 > And this is the code of the injector:
 >
 >
 > package fr.irit.ifclipse.xml.injector;
 >
 > import java.io.IOException;
 > import java.io.InputStream;
 > import java.io.PipedInputStream;
 > import java.io.PipedOutputStream;
 > import java.net.URL;
 > import java.util.Collections;
 > import java.util.HashMap;
 > import java.util.Map;
 >
 > import org.eclipse.emf.common.util.URI;
 > import org.eclipse.emf.ecore.resource.Resource;
 > import org.eclipse.m2m.atl.core.ATLCoreException;
 > import org.eclipse.m2m.atl.core.emf.EMFExtractor;
 > 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.emf.EMFReferenceModel;
 > import org.eclipse.m2m.atl.drivers.emf4atl.ASMEMFModel;
 > import org.eclipse.m2m.atl.drivers.emf4atl.AtlEMFModelHandler;
 > import org.eclipse.m2m.atl.engine.vm.AtlLauncher;
 > import org.eclipse.m2m.atl.engine.vm.AtlModelHandler;
 > import org.eclipse.m2m.atl.engine.vm.ModelLoader;
 > import org.eclipse.m2m.atl.projectors.xml.XMLInjector;
 >
 > public class IFConfigurationXMLInjectorImpl implements
 > IFConfigurationXMLInjector {
 >
 > private static final String IF_CONFIGURATION_ECORE_PATH =
 > " fr.irit.ifclipse.metamodel/src/fr/irit/ifclipse/metamodel/me tamodel/IFConfiguration.ecore ";
 >
 > private static final String XML_ECORE_PATH =
 > " org.eclipse.m2m.atl.projectors.xml/src/org/eclipse/m2m/atl/p rojectors/xml/resources/XML.ecore ";
 >
 > private AtlEMFModelHandler modelHandler;
 > private ModelLoader modelLoader;
 > private ASMEMFModel XMLMetamodel;
 > private ASMEMFModel IFConfigurationMetamodel;
 > private URI XML_ecoreURI;
 > private URI IFConfiguration_ecoreURI;
 > private URL XML2IFConfiguration_PATH;
 >
 > /*
 > * inject an XML inputstream into an IFConfiguration resource
 > */
 > @Override
 > public Resource inject(InputStream sourceXML) {
 > // load the MetaM and M
 > createResources();
 > Resource result = null;
 >
 > EMFInjector emfInjector = new EMFInjector();
 > try {
 > // create an EMFModel instance of XML
 > EMFReferenceModel XMLReferenceModel = (EMFReferenceModel) (new
 > EMFModelFactory())
 > .newReferenceModel();
 > emfInjector.inject(XMLReferenceModel, XML_ecoreURI.toString());
 > EMFModel sourceXMLEMFModel = (EMFModel) (new EMFModelFactory())
 
 You must use the same EMF factory for all (meta)models implied in your
 injection/transformation
 
 > .newModel(XMLReferenceModel);
 >
 > // inject the inputstream in the XML EMFModel
 > XMLInjector xmlInjector = new XMLInjector();
 > xmlInjector.inject(sourceXMLEMFModel, sourceXML, null);
 >
 > // transform the XML EMFModel to IFConfig EMFModel using
 > // XML2IFConfig.asm
 > result = xml2ifConfigTrafo(sourceXMLEMFModel);
 >
 >
 > } catch (ATLCoreException e) {
 > e.printStackTrace();
 > }
 > return result;
 >
 > }
 >
 > private void createResources() {
 > modelHandler = (AtlEMFModelHandler) AtlModelHandler
 > .getDefault(AtlModelHandler.AMH_EMF);
 > modelLoader = modelHandler.createModelLoader();
 > XML_ecoreURI = URI.createPlatformPluginURI(XML_ECORE_PATH, true);
 > IFConfiguration_ecoreURI = URI.createPlatformPluginURI(
 > IF_CONFIGURATION_ECORE_PATH, true);
 > XML2IFConfiguration_PATH = IFConfigurationXMLInjectorImpl.class
 > .getResource("resources/XML2IFConfig.asm");
 >
 > }
 >
 > private Resource xml2ifConfigTrafo(final EMFModel sourceXMLEMFModel) {
 > Resource result = null;
 >
 > Map<String, Object> models = new HashMap<String, Object>();
 >
 > initMetamodels(models);
 > // get/create models
 > try {
 >
 > // TODO: transform the 2 EMFModels into ASMEMFModels
 > final PipedOutputStream outputStream = new PipedOutputStream();
 > PipedInputStream inputStream = new PipedInputStream(outputStream);
 >
 > new Thread(new Runnable() {
 > public void run() {
 > try {
 >
 > (new EMFExtractor()).extract(sourceXMLEMFModel,
 > outputStream, null);
 > } catch (ATLCoreException e) {
 > e.printStackTrace();
 > }
 > try {
 > outputStream.close();
 > } catch (IOException e) {
 > // TODO Auto-generated catch block
 > e.printStackTrace();
 > }
 > }
 > }).start();
 >
 > ASMEMFModel XMLInputModel = (ASMEMFModel) modelLoader.loadModel(
 > "IN", XMLMetamodel, inputStream);
 > inputStream.close();
 >
 > URI saveURI = URI
 > .createFileURI(" d:/workspaces/wp_irit_ifgui/fr.irit.ifclipse/src/fr/irit/ifc lipse/xml/savedXML.xml.xmi ");
 >
 > Resource XMLExtent = XMLInputModel.getExtent();
 > XMLExtent.setURI(saveURI);
 > XMLExtent.save(Collections.EMPTY_MAP);
 >
 > //
 > ASMEMFModel IFConfigOutputModel = (ASMEMFModel) modelLoader
 > .newModel("OUT", "", IFConfigurationMetamodel);
 >
 > // load models
 > models.put("IN", XMLInputModel);
 > models.put("OUT", IFConfigOutputModel);
 >
 > // launch
 >  AtlLauncher.getDefault().launch(this.XML2IFConfiguration_PAT H,
 > Collections.EMPTY_MAP, models, Collections.EMPTY_MAP,
 > Collections.EMPTY_LIST, Collections.EMPTY_MAP);
 >
 > result = IFConfigOutputModel.getExtent();
 > URI saveURI_IF = URI
 > .createFileURI(" d:/workspaces/wp_irit_ifgui/fr.irit.ifclipse/src/fr/irit/ifc lipse/xml/savedIFConfig.ifconfig.xmi ");
 >
 > result.setURI(saveURI_IF);
 > result.save(Collections.EMPTY_MAP);
 >
 > } catch (IOException e) {
 > // TODO Auto-generated catch block
 > e.printStackTrace();
 > }
 > return result;
 > }
 >
 > // loads the metamodels in a Map<String, Object>
 > private void initMetamodels(Map<String, Object> models) {
 >
 > try {
 > XMLMetamodel = (ASMEMFModel) modelLoader.loadModel("XML",
 > modelLoader.getMOF(), XML_ecoreURI.toString());
 > IFConfigurationMetamodel = (ASMEMFModel) modelLoader.loadModel(
 > "IFConfiguration", modelLoader.getMOF(),
 > IFConfiguration_ecoreURI.toString());
 >
 > models.put("XML", XMLMetamodel);
 > models.put("IFConfiguration", IFConfigurationMetamodel);
 >
 > } catch (IOException e) {
 > e.printStackTrace();
 > }
 > }
 >
 > @Override
 > public Resource inject(Resource IFConfigResource, InputStream sourceXML,
 > Map<String, Object> options) {
 > return null;
 >
 > }
 >
 > }
 >
 >
 
 
 --
 Cordialement
 
 Vincent MAHÉ
 
 Ingénieur Expert - Projet IDM++ - Équipe AtlanMod
 École des Mines de Nantes
 La Chantrerie - 4, rue Alfred Kastler
 B.P. 20722 - F-44307 NANTES Cedex 3
 Tel: (33)2 51 85 81 00
 |  |  |  |  |  |  |  |  |  |  | 
| Re: Injection and EMF generated API [message #656236 is a reply to message #655925] | Thu, 24 February 2011 11:21  |  | 
| Eclipse User  |  |  |  |  | I finally found the problem (and a solution): 
 I was loading the package from it's serialized form => it's factory will instantiate the dynamic EObject implementation.
 
 So using my package namespace URI instead solved the problem. I replaced the following code:
 
 
 URI IFConfiguration_ecoreURI = URI.createPlatformPluginURI(IF_CONFIGURATION_ECORE_PATH, true);
...
IFConfigurationMetamodel = (ASMEMFModel) modelLoader.loadModel(
					"IFConfiguration", modelLoader.getMOF(),
					IFConfiguration_ecoreURI.toString());
 with this one
 
 
 
URI IFConfiguration_NS_URI = URI.createURI("uri:http://irit.fr/ifconfiguration");
...
IFConfigurationMetamodel = (ASMEMFModel) modelLoader.loadModel(
					"IFConfiguration", modelLoader.getMOF(),
					IFConfiguration_NS_URI.toString());
 And it works !
 
 Thanks.
 |  |  |  | 
 
 
 Current Time: Wed Oct 29 20:07:25 EDT 2025 
 Powered by FUDForum . Page generated in 0.04264 seconds |