Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Additional Classes
Additional Classes [message #819094] Mon, 12 March 2012 13:39 Go to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Hello everybody,
is it possible to write a additional jet-Template, to create for every class in the ecore-model a new class in the java-source when I generate the model code?

Best regards
Re: Additional Classes [message #819121 is a reply to message #819094] Mon, 12 March 2012 14:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Have a look at File->New->Example...->Eclipse Modeling Framework ->
Generator Extension Example

On 12/03/2012 2:39 PM, Thomas B wrote:
> Hello everybody,
> is it possible to write a additional jet-Template, to create for every
> class in the ecore-model a new class in the java-source when I
> generate the model code?
>
> Best regards


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #819135 is a reply to message #819121] Mon, 12 March 2012 14:40 Go to previous messageGo to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Hi Ed, thanks for your fast reply.

I used the example and create a directory 'model' with my ecore and genmodel file. For the jet template I used the example from the emf book (Page 377).
Can you shortly explain me how I used a template to create for every class in the model a java file.

Thank you
Re: Additional Classes [message #819173 is a reply to message #819135] Mon, 12 March 2012 15:27 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Did you get the example working? Do you see the templates for the extra
things that are being generated? What extra things do you notice it
generates?


On 12/03/2012 3:40 PM, Thomas B wrote:
> Hi Ed, thanks for your fast reply.
>
> I used the example and create a directory 'model' with my ecore and
> genmodel file. For the jet template I used the example from the emf
> book (Page 377).
> Can you shortly explain me how I used a template to create for every
> class in the model a java file.
>
> Thank you


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #819677 is a reply to message #819173] Tue, 13 March 2012 07:29 Go to previous messageGo to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Hi Ed,
the JET-File create a file with the name CLASS.java in the main directory, but no java file for every class in the ecore model.
If I use the unmodified example, the jet-template creates the validator.java in the package org.eclipse.emf.examples.generator.validator.templates.model.

The code of the CLASS.java file:
import java.util.*;
import org.eclipse.emf.codegen.ecore.genmodel.*;

public class CLASS
{
  protected static String nl;
  public static synchronized CLASS create(String lineSeparator)
  {
    nl = lineSeparator;
    CLASS result = new CLASS();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = "/**" + NL + " * <copyright>" + NL + " * </copyright>" + NL + " *" + NL + " * ";
  protected final String TEXT_3 = "Id";
  protected final String TEXT_4 = NL + " */";
  protected final String TEXT_5 = NL + NL + "package ";
  protected final String TEXT_6 = ".validation;" + NL + "\t\t\t\t\t\t\timport org.eclipse.emf.validation.EMFValidator;" + NL + "\t\t\t\t\t\t\tpublic interface ";
  protected final String TEXT_7 = "Validator" + NL + "\t\t\t\t\t\t\textends EMFValidator" + NL + "\t\t\t\t\t\t\t{";
  protected final String TEXT_8 = NL + "    boolean validate";
  protected final String TEXT_9 = "(";
  protected final String TEXT_10 = " value);";
  protected final String TEXT_11 = NL + "}" + NL + NL;
  protected final String TEXT_12 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(TEXT_2);
    stringBuffer.append("$");
    stringBuffer.append(TEXT_3);
    stringBuffer.append("$");
    stringBuffer.append(TEXT_4);
    GenClass genClass = (GenClass)argument;
  GenPackage genPackage = genClass.getGenPackage();
    stringBuffer.append(TEXT_5);
    stringBuffer.append(genPackage.getInterfacePackageName());
    stringBuffer.append(TEXT_6);
    stringBuffer.append(genClass.getInterfaceName());
    stringBuffer.append(TEXT_7);
    for (Iterator i=genClass.getGenFeatures().iterator();
       i.hasNext();) {
  GenFeature f = (GenFeature)i.next();
    if (f.isChangeable()) {
    stringBuffer.append(TEXT_8);
    stringBuffer.append(f.getCapName());
    stringBuffer.append(TEXT_9);
    stringBuffer.append(f.getType());
    stringBuffer.append(TEXT_10);
    }
    }
    stringBuffer.append(TEXT_11);
    stringBuffer.append(TEXT_12);
    return stringBuffer.toString();
  }
}

Re: Additional Classes [message #819700 is a reply to message #819677] Tue, 13 March 2012 08:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Thomas,

The example has templates/model/ModelDescription.txt and
templates/model/Validator.javajet, right? I expect to see one
description.txt and a validator per EClass when you use the extension in
the runtime workbench.


On 13/03/2012 8:29 AM, Thomas B wrote:
> Hi Ed,
> the JET-File create a file with the name CLASS.java in the main
> directory, but no java file for every class in the ecore model.
> If I use the unmodified example, the jet-template creates the
> validator.java in the package
> org.eclipse.emf.examples.generator.validator.templates.model.
>
> The code of the CLASS.java file:
> import java.util.*;
> import org.eclipse.emf.codegen.ecore.genmodel.*;
>
> public class CLASS
> {
> protected static String nl;
> public static synchronized CLASS create(String lineSeparator)
> {
> nl = lineSeparator;
> CLASS result = new CLASS();
> nl = null;
> return result;
> }
>
> public final String NL = nl == null ?
> (System.getProperties().getProperty("line.separator")) : nl;
> protected final String TEXT_1 = "";
> protected final String TEXT_2 = "/**" + NL + " * <copyright>" + NL +
> " * </copyright>" + NL + " *" + NL + " * ";
> protected final String TEXT_3 = "Id";
> protected final String TEXT_4 = NL + " */";
> protected final String TEXT_5 = NL + NL + "package ";
> protected final String TEXT_6 = ".validation;" + NL +
> "\t\t\t\t\t\t\timport org.eclipse.emf.validation.EMFValidator;" + NL +
> "\t\t\t\t\t\t\tpublic interface ";
> protected final String TEXT_7 = "Validator" + NL +
> "\t\t\t\t\t\t\textends EMFValidator" + NL + "\t\t\t\t\t\t\t{";
> protected final String TEXT_8 = NL + " boolean validate";
> protected final String TEXT_9 = "(";
> protected final String TEXT_10 = " value);";
> protected final String TEXT_11 = NL + "}" + NL + NL;
> protected final String TEXT_12 = NL;
>
> public String generate(Object argument)
> {
> final StringBuffer stringBuffer = new StringBuffer();
> stringBuffer.append(TEXT_1);
> stringBuffer.append(TEXT_2);
> stringBuffer.append("$");
> stringBuffer.append(TEXT_3);
> stringBuffer.append("$");
> stringBuffer.append(TEXT_4);
> GenClass genClass = (GenClass)argument;
> GenPackage genPackage = genClass.getGenPackage();
> stringBuffer.append(TEXT_5);
> stringBuffer.append(genPackage.getInterfacePackageName());
> stringBuffer.append(TEXT_6);
> stringBuffer.append(genClass.getInterfaceName());
> stringBuffer.append(TEXT_7);
> for (Iterator i=genClass.getGenFeatures().iterator();
> i.hasNext();) {
> GenFeature f = (GenFeature)i.next();
> if (f.isChangeable()) {
> stringBuffer.append(TEXT_8);
> stringBuffer.append(f.getCapName());
> stringBuffer.append(TEXT_9);
> stringBuffer.append(f.getType());
> stringBuffer.append(TEXT_10);
> }
> }
> stringBuffer.append(TEXT_11);
> stringBuffer.append(TEXT_12);
> return stringBuffer.toString();
> }
> }
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #819740 is a reply to message #819700] Tue, 13 March 2012 09:17 Go to previous messageGo to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Hi Ed,
what I want, is that my JET-Template create for every EClass in my ecore-model a Java-Class. So if I click on the genmodel to "Generate Model Code" it should generate the standard code and additional the Java-Classes from the JET-Template.
Is this possible and if so how?

Thank you for your patience

Ed Merks wrote on Tue, 13 March 2012 04:11
Thomas,

The example has templates/model/ModelDescription.txt and
templates/model/Validator.javajet, right? I expect to see one
description.txt and a validator per EClass when you use the extension in
the runtime workbench.

Re: Additional Classes [message #819750 is a reply to message #819740] Tue, 13 March 2012 09:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Thomas,

Comments below.

On 13/03/2012 10:17 AM, Thomas B wrote:
> Hi Ed,
> what I want, is that my JET-Template create for every EClass in my
> ecore-model a Java-Class.
Like in the example...
> So if I click on the genmodel to "Generate Model Code" it should
> generate the standard code and additional the Java-Classes from the
> JET-Template.
> Is this possible and if so how?
Yes, the example does this.
>
> Thank you for your patience
Do you see the things in the example I asked about. I.e., the
Validator.javajet, which is a per-GenClass template. Setting a
breakpoint in GenClassValidatorGeneratorAdapter.generateModel should
give you the idea of what to do.
>
> Ed Merks wrote on Tue, 13 March 2012 04:11
>> Thomas,
>>
>> The example has templates/model/ModelDescription.txt and
>> templates/model/Validator.javajet, right? I expect to see one
>> description.txt and a validator per EClass when you use the extension
>> in the runtime workbench.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #819761 is a reply to message #819750] Tue, 13 March 2012 09:48 Go to previous messageGo to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Ed,
I have the Validator.javajet and the ModelDescription.txtjet in the template/model folder. But both templates generate only one file "ModelDescritption.java" and "Validator.java" in the Package "org.eclipse.emf.examples.generator.validator.templates.model".

What I have done until now
- Create the Example Project with the standard name "org.eclipse.emf.examples.generator.validator"
- Create a new folder "model"
- In this folder I create my ecore file and the associated genmodel
Re: Additional Classes [message #819775 is a reply to message #819761] Tue, 13 March 2012 10:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Thomas,

Comments below.

On 13/03/2012 10:48 AM, Thomas B wrote:
> Ed,
> I have the Validator.javajet and the ModelDescription.txtjet in the
> template/model folder. But both templates generate only one file
> "ModelDescritption.java" and "Validator.java" in the Package
> "org.eclipse.emf.examples.generator.validator.templates.model".
Yes, but those are the templates that will be used to generate
model-specific code.
>
> What I have done until now
> - Create the Example Project with the standard name
> "org.eclipse.emf.examples.generator.validator"
> - Create a new folder "model"
> - In this folder I create my ecore file and the associated genmodel
I think you've completely missed the point of the example. It's an
extension to the code generator. You have to deploy it into a running
IDE. That's why I talked about starting a runtime workbench. When you
do that, everything you generate in that IDE will use that extension and
will generate the additional artifacts specified by the extension.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #819822 is a reply to message #819775] Tue, 13 March 2012 11:08 Go to previous messageGo to next message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Ok thank you, now I understand the example.
Sorry for my (silly) questions, but I'm a beginner with emf.

Is it also possible, to change or extend the existing jet templates, that new classes will be generated for one project, without starting a new runtime workbench.
For example in the ibm redbook (redbooks.ibm.com/redbooks/pdfs/sg246302.pdf) at page 79 to 86 is a example to change the copyright information and generate new methods... by changing the existing templates.
Is there a possibility to generate new classes in this way?
Re: Additional Classes [message #820071 is a reply to message #819822] Tue, 13 March 2012 17:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Thomas,

You can use dynamic templates but if you want additional templates you
have to extend the generator as shown in the example. PDE has a nice
export capability that lets you export your extension (your workspace
bundles) into the IDE's installation so that should work nicely. I.e.,
File->Export...->Plug-in Development Environment->Deployable Bundles and
Fragments choosing the third radio button. There's a GenModel property
for the copyright; it won't change files once they exist though.


On 13/03/2012 12:08 PM, Thomas B wrote:
> Ok thank you, now I understand the example.
> Sorry for my (silly) questions, but I'm a beginner with emf.
>
> Is it also possible, to change or extend the existing jet templates,
> that new classes will be generated for one project, without starting a
> new runtime workbench.
> For example in the ibm redbook
> (redbooks.ibm.com/redbooks/pdfs/sg246302.pdf) at page 79 to 86 is a
> example to change the copyright information and generate new
> methods... by changing the existing templates.
> Is there a possibility to generate new classes in this way?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Additional Classes [message #820741 is a reply to message #820071] Wed, 14 March 2012 14:04 Go to previous message
Thomas B is currently offline Thomas BFriend
Messages: 9
Registered: March 2012
Junior Member
Ok.
Thanks for your help and tips.
Previous Topic:[Xcore] how to convert GenModel to Xcore ?
Next Topic:[CDO/Hibernate] How to re-save changed model instance
Goto Forum:
  


Current Time: Fri Apr 26 11:00:54 GMT 2024

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

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

Back to the top