Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » generate JET2 template programatically
generate JET2 template programatically [message #64852] Wed, 22 July 2009 09:30 Go to next message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
hello,

I developed templates in a Jet transformation project. I managed to run
them using the run configuration command.

My question is the following:

How to run JET2 templates from code? What methods do I use from the JET2
API and how?

thank's

Gloria
Re: generate JET2 template programatically [message #64896 is a reply to message #64852] Thu, 23 July 2009 15:43 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Gloria:

I've been asked variations of this before, so I have created a new JET FAQ
article. Let me know if this answers your question:

http://wiki.eclipse.org/M2T-JET-FAQ/How_do_I_run_a_JET_templ ate_from_Java%3F

And, after re-reading your post, it may be that you just want to run the
entire transformation from Java. In that case, take a look at:

http://help.eclipse.org/galileo/topic/org.eclipse.jet.doc/re ferences/javadoc/org/eclipse/jet/JET2Platform.html

In particulary, look at the runTransformOnXXX family of methods, which can
load your model from a workspace resource (runTransformOnResource), from a
string representation of your resource (runTransformOnString), or from an
in-memory representation of your model (runTransformOnObject).

Paul
Re: generate JET2 template programatically [message #64916 is a reply to message #64896] Fri, 24 July 2009 13:11 Go to previous messageGo to next message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
HI Paul,

I think that the second wey is suitable for me but I haven't managed to
try it. The reason is that I haven't the good parameters.


The JET2Platform.runTransformOnString()method requires three arguments: a
String Id, an IResource and a IProgressMonitor.

Then, here is my code:

ModelDesTestCasePackage.eINSTANCE.eClass();

// register XMI resource factory XMI for extension
".modeldestestcase"
Resource.Factory.Registry reg =
Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("modeldestestcase",new XMIResourceFactoryImpl());

// create some resource set
ResourceSet resSet = new ResourceSetImpl();

String ecoreFile= "TestOpe1";//the name of the ecore file

// find out the resource named 'ecoreFile' (already in the
project under "src" folder...
Resource resource =
resSet.getResource(URI.createURI("file:/D:/Mes
documents/Installation/DSLToolKit3/dsltk-incubation-I2008112 8-0606.win32.win32.x86/workspace/generationTestCaseModel/mod el/ "+ecoreFile+".modeldestestcase"),
true);

JET2Platform.runTransformOnObject ("mytransformationID",
resource, new NullProgressMonitor());


then it returns that error lines:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError:
org/osgi/framework/BundleException
at main.menu.actionPerformed(menu.java:99)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unk nown
Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException:
org.osgi.framework.BundleException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 27 more

thank for help
Re: generate JET2 template programatically [message #64934 is a reply to message #64916] Fri, 24 July 2009 17:04 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Gloria:


From your stack trace, it appears that you are not running within Eclipse.
If so, then this is your problem.

JET has dependencies on Eclipse. It is not sufficient to just include all
the required Eclipse plug-ins in your ClassPath. JET requires an active
Eclipse/OSGi runtime.

This doesn't mean that you must have the Eclipse UI visible, but it does
mean that your code needs to be in an Eclipse plug-in, and that you need
to start an Eclipse instance rather than just a Java JVM to run your code.

Assuming you are not interested in using the Eclipse UI, then take a look
at the org.eclipse.core.runtime.applications extension point:

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/reference/extension-points/org_eclipse_cor e_runtime_applications.html

You create a plug-in that uses this extension and provides an
implementation of IApplication:

http://help.eclipse.org/galileo/topic/org.eclipse.platform.d oc.isv/reference/api/org/eclipse/equinox/app/IApplication.ht ml

Once coded, install the plug-in (along with your JET transformation
plug-in) in an eclipse instance. Then you can start it from the command
line as follows:

eclipse -application your-plugin-id-goes-hear
Re: generate JET2 template programatically [message #65034 is a reply to message #64934] Mon, 27 July 2009 08:18 Go to previous messageGo to next message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
Hello Paul,

Thank's for replying to me. I don't know what made you think I wasn't
working with Eclipse. I work with an Eclipse Application (DSL Toolkit). My
project is developed in a plug-in.

Gloria
Re: generate JET2 template programatically [message #65134 is a reply to message #64934] Mon, 27 July 2009 11:01 Go to previous messageGo to next message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Paul,

I managed to resolve the problem with the
org.osgi.framework.BundleException importing the
org.osgi.framework.BundleException package.

Then I modify my code to run the main.jet template :

public class TestJetExecutionProgramatically {


public static JET2Context createJETContext(Object modelRoot,
final Map<String, ?> variables) {

Map<String, Object> copiedVariables = new HashMap<String, Object>(
variables != null ? variables :
Collections.<String, Object> emptyMap());

// ensure that c:iterate can set the XPath context object

copiedVariables.put("org.eclipse.jet.taglib.control.iterateSetsContext ",
Boolean.TRUE);

final JET2Context context = new JET2Context(modelRoot,
copiedVariables);

// this statement has the side effect of initializing tag handling
TransformContextExtender.getInstance(context);
return context;}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//The object 'Conteneur' is the emf modelroot.
ModelDesTestCaseFactoryImpl f = new ModelDesTestCaseFactoryImpl();
Conteneur monConteneur = f.createConteneur();//creation du Conteneur

JET2Context context = createJETContext(monConteneur, null);
BufferedJET2Writer writer = new BodyContentWriter();

//MyTemplateClass is the corresponding class to the main.jet template I
want to run


JET2Template template = new MyTemplateClass();
template.generate(context, writer);

}
}



and I got that error:


Exception in thread "main" java.lang.NullPointerException
at
org.eclipse.jet.taglib.TagLibraryManager.getTagLibrary(TagLi braryManager.java:89)
at
org.eclipse.jet.internal.runtime.TagFactoryImpl.createRuntim eTag(TagFactoryImpl.java:61)
at my.template.pkg.MyTemplateClass.generate(MyTemplateClass.jav a:96)
at
main.TestJetExecutionProgramatically.main(TestJetExecutionPr ogramatically.java:63)


I think I'm not far from the goal I'm reaching. Thank's for your help.

Gloria
Re: generate JET2 template programatically [message #65177 is a reply to message #65134] Mon, 27 July 2009 12:06 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Gloria:

Your test program may launched from Eclipse, but it is NOT running in an
active Eclipse instance. I can tell by looking at your stack trace - it
starts from a 'main' method in one of your classes - Eclipse plug-ins
don't typically do this:

> Exception in thread "main" java.lang.NullPointerException
> at
>
org.eclipse.jet.taglib.TagLibraryManager.getTagLibrary(TagLi braryManager.java:89)
> at
>
org.eclipse.jet.internal.runtime.TagFactoryImpl.createRuntim eTag(TagFactoryImpl.java:61)
> at my.template.pkg.MyTemplateClass.generate(MyTemplateClass.jav a:96)
> at
>
main.TestJetExecutionProgramatically.main(TestJetExecutionPr ogramatically.java:63)


If you want to do a quick test, I suggest the following:

1) create a new Plug-in project:
a) Click File > New > Project ... Select Plug-in Project
b) Enter a plug-in name (e.g. com.myorg.testjet). Click Next
c) In the second wizard page, ensure 'This plug-in will make
contributions to the UI' is checked. Click Next.
d) On the 'Templates' wizard page, select 'Hello, World Command'. Click
Finish. (Instead of clicking Finish, you can click Next, and change the
names of some of the things generated.)

The wizard will open the Plug-in Manifest editor on your new project.

2) Add required dependencies. You will need at least JET and EMF...
a) In the Plug-in Manifest editor, click the Dependencies tab
b) Under Required Plug-ins, click Add, and add org.eclipse.jet
c) Repeat to add org.eclipse.emf.ecore
d) Just in case, add org.eclipse.emf.ecore.xmi
e) Click File > Save.

3) Code you test code in the generated SampleHandler.java class to run the
JET transformation:

public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window =
HandlerUtil.getActiveWorkbenchWindowChecked(event);

URI docURI = URI.createFileURI("... your fully qualified doc
URI...");
ResourceSet rs = new ResourceSetImpl();
Resource resource = rs.createResource(docURI);
try {
resource.load(null);
} catch (IOException e) {
throw new ExecutionException("failed to load resource: " +
docURI.toString(), e);
}

IStatus status =
JET2Platform.runTransformOnObject("your-jet-tx-id", resource, new
NullProgressMonitor());

MessageDialog.openInformation(
window.getShell(),
"Jettest",
"returned status: " + status.getMessage());
return null;
}

4) Run your test code in an Eclipse Application (also known as an Eclipse
Runtime workbench):
a) Click Run > Run Configurations
b) Select 'Eclipse Application' and the 'new' icon
c) I suggest you give the 'configuration' a more meaningful name than
'New_configuration'. Something like: "test programmatic JET invocation'.
d) Click Run. A second instance of Eclipse will start - it will include
your test code.
e) You can invoke your test code by clicking Sample Menu > Sample
Command

Tip. You might want to show the Error Log view (Window > Show View >
Other, then look for General > Error Log). Errors such as failing to open
the EMF Resource will only show up in the error log.

Lastly, I am guessing you are new to Eclipse development. I still find the
Eclipse FAQ very useful:

http://wiki.eclipse.org/index.php/Eclipse_FAQs

You may find the All about Plug-ins particularly interesting:

http://wiki.eclipse.org/index.php/Eclipse_FAQs#All_about_Plu g-ins

Let me know how things work out.

Paul
Re: generate JET2 template programatically [message #351890 is a reply to message #65177] Tue, 28 July 2009 09:59 Go to previous messageGo to next message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Paul,

The tutorial you write to me is very good and easy to understand.

I tried it.

Then,by clicking Sample Command, I got a message dialog :

"returned status:erros occured during execution"

but I don't get no error log.
I'm searching for solutions at the moment but if you have another piece of
advice, do not hesitate.


You're very right, I new to Eclipse development. I'm gonna check the links
you passed on me.


I thank you so much.

Gloria
Re: generate JET2 template programatically [message #358438 is a reply to message #351890] Tue, 28 July 2009 12:53 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Gloria:

The error probably indicates that there was an execution error while the
JET transformation was running.

To get better information on the error, I suggest the following:

1) Use the following code to report on the IStatus returned from
JET2Platform.runTransformOnObject:

StatusManager.getManager.handle(status);

(You may need to open META-INF/MANIFEST.MF, go to the Dependencies tab,
and add org.eclipse.ui.workbench as a dependency to get this to compile.)

This should pop up a dialog box with all the details.

2) You could also write the status to the Eclipse log (which you could
then inspect using Window > Show View > Other, and then selecting General
> Error Log):

if(!status.isOK()) {
Activator.getDefault().getLog().log(status)
}

The Activator class should have been created when you created your
plug-in. You may need to do Source > Organize Imports (CTRL+SHIFT+O) to
get the class imported.

As for recommended reading, the Eclipse books I learned from are all
getting a little old :-(

The Eclipse FAQs web site (there was a book, too), is based on Eclipse
3.0, but I still use it.

I also used the "Java Developer's Guide to Eclipse". It too, is getting
old, but it is still very popular:

http://www.jdg2e.com/

Paul

Paul
Re: generate JET2 template programatically [message #372605 is a reply to message #358438] Wed, 29 July 2009 09:53 Go to previous message
gloria guitteaud is currently offline gloria guitteaudFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Paul,

Using the following code you gave me:
"StatusManager.getManager().handle(status);"

Then, it shew me the error log and I found the mistake. The issue was that
when I was trying to find out, I had added some code everywhere and I had
forgotten to remove something then.

Now, the project runs very well. I thank you so much.

I have just finished my studies and I've been employed by a company as
trainee. My manager wants me to work and earn skills on modeling tool.
They are no people in the company that is really qualified in modeling
project.
Then, thank's for your help.

Gloria
Previous Topic:Model Loader for UML
Next Topic:roadmap for m2m and m2t
Goto Forum:
  


Current Time: Thu Mar 28 09:01:57 GMT 2024

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

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

Back to the top