Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » emf does not resolve reference on load
emf does not resolve reference on load [message #1703201] Wed, 29 July 2015 10:21 Go to next message
Clemens Schneider is currently offline Clemens SchneiderFriend
Messages: 3
Registered: June 2015
Junior Member
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 10:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
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());
> }
> }
> }
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: emf does not resolve reference on load [message #1703205 is a reply to message #1703204] Wed, 29 July 2015 11:01 Go to previous message
Clemens Schneider is currently offline Clemens SchneiderFriend
Messages: 3
Registered: June 2015
Junior Member
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 11:03]

Report message to a moderator

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


Current Time: Thu Apr 25 12:21:24 GMT 2024

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

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

Back to the top