Skip to main content



      Home
Home » Modeling » EMF » emf does not resolve reference on load
emf does not resolve reference on load [message #1703201] Wed, 29 July 2015 06:21 Go to next message
Eclipse UserFriend
Hello,

I have a function where I try to load a m1gen-file and then to resolve the resourceSet to get the referenced ecore-file. After that the target of the instantiators should be available. But they aren't. The line
System.out.println("instantiator: " + instantiator.getTarget().getName());
produces every time null value for the name...

@Mojo(name = "generate")
public class GeneratorMojo extends AbstractMojo {

	@Parameter
	private File m1genFile;

	public void execute() throws MojoExecutionException {

		List<ClassInstantiator> instantiators = new ArrayList<ClassInstantiator>();
		List<Goal> goals = new ArrayList<Goal>();

		EPackage.Registry packReg = EPackage.Registry.INSTANCE;
		packReg.put(M1genPackage.eNS_URI, M1genPackage.eINSTANCE);

		Resource.Factory.Registry fReg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = fReg.getExtensionToFactoryMap();
		m.put("m1gen", new XMIResourceFactoryImpl());
		m.put("ecore", new XMIResourceFactoryImpl());
		
		ResourceSet resSetIn = new ResourceSetImpl();
		resSetIn.setPackageRegistry(packReg);
		resSetIn.setResourceFactoryRegistry(fReg);	
		
		Resource resourceIn = resSetIn.getResource(URI.createURI("file:/" + m1genFile.toString()), true);
		
		try {
			resourceIn.load(m);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		EcoreUtil.resolveAll(resSetIn);

		TreeIterator<EObject> instances = resourceIn.getAllContents();

		M1GenModel m1gen = (M1GenModel) instances.next();
		EPackageImpl ePackage = (EPackageImpl) m1gen.getRefers().get(0);		
		
		EObject next = null;
		while (instances.hasNext()) {
			next = instances.next();

			if (next instanceof Goal) {
				goals.add((Goal) next);
			}

			if (next instanceof ClassInstantiator) {
				instantiators.add((ClassInstantiator) next);
			}
		}
		
		for (ClassInstantiator instantiator : instantiators) {
			System.out.println("instantiator: " + instantiator.getTarget().getName());
		}
	}
}

Re: emf does not resolve reference on load [message #1703204 is a reply to message #1703201] Wed, 29 July 2015 06:43 Go to previous messageGo to next message
Eclipse UserFriend
Clemens,

What value does m1genFile have? Is it an absolute path? Better use use
URI.createFileURI than what you did (because on Linux it will produce
file://path), assuming the path is absolute in the first place. You can
also try casting instantiator.getTarget() to InternalEObject so you can
inspect the eProxyURI of it.

On 29/07/2015 12:21 PM, Clemens Schneider wrote:
> Hello,
>
> I have a function where I try to load a m1gen-file and then to resolve the resourceSet to get the referenced ecore-file. After that the target of the instantiators should be available. But they aren't. The line System.out.println("instantiator: " + instantiator.getTarget().getName()); produces every time null value for the name...
>
> @Mojo(name = "generate")
> public class GeneratorMojo extends AbstractMojo {
>
> @Parameter
> private File m1genFile;
>
> public void execute() throws MojoExecutionException {
>
> List<ClassInstantiator> instantiators = new ArrayList<ClassInstantiator>();
> List<Goal> goals = new ArrayList<Goal>();
>
> EPackage.Registry packReg = EPackage.Registry.INSTANCE;
> packReg.put(M1genPackage.eNS_URI, M1genPackage.eINSTANCE);
>
> Resource.Factory.Registry fReg = Resource.Factory.Registry.INSTANCE;
> Map<String, Object> m = fReg.getExtensionToFactoryMap();
> m.put("m1gen", new XMIResourceFactoryImpl());
> m.put("ecore", new XMIResourceFactoryImpl());
>
> ResourceSet resSetIn = new ResourceSetImpl();
> resSetIn.setPackageRegistry(packReg);
> resSetIn.setResourceFactoryRegistry(fReg);
>
> Resource resourceIn = resSetIn.getResource(URI.createURI("file:/" + m1genFile.toString()), true);
>
> try {
> resourceIn.load(m);
> } catch (IOException e1) {
> e1.printStackTrace();
> }
> EcoreUtil.resolveAll(resSetIn);
>
> TreeIterator<EObject> instances = resourceIn.getAllContents();
>
> M1GenModel m1gen = (M1GenModel) instances.next();
> EPackageImpl ePackage = (EPackageImpl) m1gen.getRefers().get(0);
>
> EObject next = null;
> while (instances.hasNext()) {
> next = instances.next();
>
> if (next instanceof Goal) {
> goals.add((Goal) next);
> }
>
> if (next instanceof ClassInstantiator) {
> instantiators.add((ClassInstantiator) next);
> }
> }
>
> for (ClassInstantiator instantiator : instantiators) {
> System.out.println("instantiator: " + instantiator.getTarget().getName());
> }
> }
> }
>
Re: emf does not resolve reference on load [message #1703205 is a reply to message #1703204] Wed, 29 July 2015 07:01 Go to previous message
Eclipse UserFriend
Thank you! It works.

I changed it to:

Resource resourceIn = resSetIn.getResource(URI.createFileURI(m1genFile.toString()), true);


m1gen is:

<m1genFile>C:/Users/cschneid/Desktop/workspace-tdg/model/Company.m1gen</m1genFile>

[Updated on: Wed, 29 July 2015 07:03] by Moderator

Previous Topic:Are there any SoaML EMF implementations?
Next Topic:EcoreTools: multiple Ecore models in one project
Goto Forum:
  


Current Time: Mon Jul 07 13:02:55 EDT 2025

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

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

Back to the top