Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Import EMF metamodel in jet template
Import EMF metamodel in jet template [message #1860949] Mon, 11 September 2023 14:30 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi,

I am currently trying to customise the item provider template. I have already fixed everything in the genmodel so that it uses dynamic templates. However, I have an issue.

In the template I have registered an ecore metamodel and loaded a model instance of the metamodel the values of which I need in order to guide the generation of code from the template.

The root of the metamodel is Family.

In the following code I am trying to create an object of type Family named familyRoot. I have imported family.Family.

<%@ jet package="org.eclipse.emf.codegen.ecore.templates.edit" imports="java.util.* org.eclipse.emf.codegen.ecore.genmodel.* org.eclipse.emf.ecore.resource.ResourceSet org.eclipse.emf.ecore.* org.eclipse.emf.common.util.URI org.eclipse.emf.ecore.resource.Resource org.eclipse.emf.ecore.resource.impl.ResourceSetImpl java.io.File org.eclipse.emf.ecore.EPackage family.Family" class="ItemProvider"%>

<%
/**
 * Copyright (c) 2002-2019 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *   Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 546714
 */
%>
<%GenClass genClass = (GenClass)argument; GenPackage genPackage = genClass.getGenPackage(); GenModel genModel=genPackage.getGenModel();%>
<%final boolean isJDK50 = genModel.getComplianceLevel().getValue() >= GenJDKLevel.JDK50;%>
<%final boolean forceDefaultCase = genModel.isSwitchMissingDefaultCase();String indentDefaultCase = forceDefaultCase ? "\t\t" : "";%>
<%@ include file="../Header.javajetinc"%>
package <%=genPackage.getProviderPackageName()%>;

<%genModel.addImport("org.eclipse.emf.common.notify.AdapterFactory");%>
<%genModel.addImport("org.eclipse.emf.common.notify.Notification");%>
<%String _List = genModel.getImportedName(genModel.useGenerics() ? "java.util.List<org.eclipse.emf.edit.provider.IItemPropertyDescriptor>" : "java.util.List");%>
<%genModel.markImportLocation(stringBuffer);%>
<%-- Add import for XMI handling --%>
<%genModel.addImport("org.eclipse.emf.ecore.resource.Resource");%>
<%genModel.addImport("org.eclipse.emf.ecore.resource.ResourceSet");%>


<%-- Load the XMI model instance from a file --%>
<%String modelFilePath = "/Family/model/family.xmi";%>
<%genModel.addImport("java.io.IOException");%>
<%genModel.addImport("java.util.Collections");%>
<%genModel.addImport("java.util.Map");%>
<%genModel.addImport("org.eclipse.emf.common.util.URI");%>
<%genModel.addImport("org.eclipse.emf.ecore.EObject");%>
<%genModel.addImport("org.eclipse.emf.ecore.resource.ResourceSet");%>
<%genModel.addImport("org.eclipse.emf.ecore.resource.impl.ResourceSetImpl");%>


<%

    ResourceSet resourceSet = new ResourceSetImpl();
    Resource ecoreResource = resourceSet.getResource(URI.createFileURI(new File ("/Users/user/eclipse-workspace/Family/model/family.ecore").getAbsolutePath()), true);
    EObject ecoreRootObject = ecoreResource.getContents().get(0);

    // Get the package and register it dynamically
    EPackage ecorePackage = (EPackage) ecoreRootObject;
    EPackage.Registry.INSTANCE.put(ecorePackage.getNsURI(), ecorePackage);

    Resource xmiResource = resourceSet.getResource(URI.createURI(modelFilePath), true);
    EObject rootObject = xmiResource.getContents().get(0); 
    Family  familyroot = rootObject as Family; 
    String familyName = familyroot.getName();
%>



However, when I generate edit code, a folder named .JETEmitters is generated with java code. This java code that is generated shows an error in line:

import family.Family


Because it is not in the build path. Problem is that this folder is generated when I generate edit code, so I cannot include it in the build path if the project does not exist in the first place.
When I have already generated the .JETEmmiters project and I include family in the build path and generate edit code again, I get the following error:
Build path contains duplicate entry: org.eclipse.jdt.launching.JRE_CONTAINER for project .JETEmmiters 


Do you know of any way around this?

Thanks, John!

[Updated on: Mon, 11 September 2023 15:26]

Report message to a moderator

Re: Import EMF metamodel in jet template [message #1860950 is a reply to message #1860949] Mon, 11 September 2023 14:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
I don't understand what you are trying to do with an instance of the model for which you are generating an item provider. At the start of the template you see this:
<%GenClass genClass = (GenClass)argument; GenPackage genPackage = genClass.getGenPackage(); GenModel genModel=genPackage.getGenModel();%>
You should work with these objects and perhaps with the Ecore model that they wrap. You should not be needing to load an Ecore in some other.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Import EMF metamodel in jet template [message #1860954 is a reply to message #1860950] Mon, 11 September 2023 15:29 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
I am not trying to work with the instance of the model for which I am generating the item provider. The item provider is being generated for another EMF project (e.g., project House). However, the code of the ItemProvider of the project house is dependent on values that are present in model "family.xmi" conforming to "family.ecore".

However, when I import the family.FamilyModel in the item provider of project house, I get an error because family is not in the build path of project house. And I cannot add it because of what I explained in the prior message.
Re: Import EMF metamodel in jet template [message #1860960 is a reply to message #1860954] Tue, 12 September 2023 07:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
You can use the Template Plug-in Variables property of the *.genmodel to define additional plug-in dependencies for the .JETEmitters project.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Import EMF metamodel in jet template [message #1861005 is a reply to message #1860960] Thu, 14 September 2023 08:56 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi,
Thanks for your reply. It worked perfectly. I was wondering if it is possible, to pass a parameter from the .genmodel to the JET template generating the ItemProvider. The reason for that is because I need to use an .xmi model as input to the template. However, I do not want the URI of this model to be hardcoded as the model is different in different scenarios. Therefore I was wondering is there is any way to pass the uri as a parameter in the JET template.
Thanks!
Re: Import EMF metamodel in jet template [message #1861007 is a reply to message #1861005] Thu, 14 September 2023 09:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
The GenModel and Ecore model both support annotations, so you can pass any additional information via an annotation.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Import EMF metamodel in jet template [message #1861008 is a reply to message #1860960] Thu, 14 September 2023 09:58 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Also, it looks like this can only be done manually as a method to setTemplatePluginVariables does not exist
Re: Import EMF metamodel in jet template [message #1861013 is a reply to message #1861008] Thu, 14 September 2023 12:09 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
It's a multi-valued feature so like for all multi-valued features there is only org.eclipse.emf.codegen.ecore.genmodel.GenModel.getTemplatePluginVariables() to which you can add the "variables".

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Runtime editor
Next Topic:Obtaining a permission to quote the EMF book in Ecore documentation
Goto Forum:
  


Current Time: Thu May 02 09:16:23 GMT 2024

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

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

Back to the top