Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to serialize DSL-Files to UML-Model
How to serialize DSL-Files to UML-Model [message #1148254] Mon, 21 October 2013 11:27 Go to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hello everyone!
I am trying to serialize my dsl files to .uml (XMI) files. To achieve this I read several posts in this forum. Like:
http://www.eclipse.org/forums/index.php/t/292631/

And I also try to implement this serializiation mechanism with the following code:
Injector injector = new CdalfStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);

		URI uri = URI.createURI("a.cdalf");
		// Resource xtextResource = resourceSet.createResource(uri);
		Resource xtextResource = resourceSet.getResource(uri, true);

		EcoreUtil.resolveAll(xtextResource);

		Resource xmiResource = resourceSet.createResource(URI.createURI("test.uml"));
		xmiResource.getContents().add(xtextResource.getContents().get(0));
		try {
			xmiResource.save(null);
		} catch (IOException e) {
			e.printStackTrace();

		}


To try out this code, I created a DSL-File of my grammer ("a.cdalf") and now want to serialize it to .uml .
But while running this example-code I get the following exceptions:

Exception in thread "main" org.eclipse.xtext.parser.ParseException:
 java.lang.IllegalStateException: Unresolved proxy http://www.eclipse.org/uml2/4.0.0/UML#//Package. Make sure the EPackage has been registered.
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:105)
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.parse(AbstractAntlrParser.java:84)
	at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:62)
...


The problem might be my grammer. Because I don't create any grammer but importing an existing metamodel (import "http://www.eclipse.org/uml2/4.0.0/UML") for customizing its textual representation.

Do anyone maybe know a mechanism how I may serialize DSL-Files which based on a grammer that imports an other metamodel?

Thanks alot,
Alex
Re: How to serialize DSL-Files to UML-Model [message #1148390 is a reply to message #1148254] Mon, 21 October 2013 13:18 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Problem solved!

The following code was used in a Eclipseplugin and executed via an plugin-action after the DSL-modelfile was written and parsed correctly.
Injector injector = new CdalfStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);

		URI uri = URI.createURI("a.cdalf");
		// Resource xtextResource = resourceSet.createResource(uri);
		Resource xtextResource = resourceSet.getResource(uri, true);

		EcoreUtil.resolveAll(xtextResource);

		Resource xmiResource = resourceSet.createResource(URI.createURI("test.uml"));
		xmiResource.getContents().add(xtextResource.getContents().get(0));
		try {
			xmiResource.save(null);
		} catch (IOException e) {
			e.printStackTrace();

		}
Re: How to serialize DSL-Files to UML-Model [message #1148422 is a reply to message #1148390] Mon, 21 October 2013 13:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

calling CdalfStandaloneSetup in an eclipse plugin is bad.
have a look at http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html on how to obtain an injector


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to serialize DSL-Files to UML-Model [message #1148468 is a reply to message #1148422] Mon, 21 October 2013 14:27 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Thanks for your hint!
I will have a look at the link.
Re: How to serialize DSL-Files to UML-Model [message #1148522 is a reply to message #1148468] Mon, 21 October 2013 15:12 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hello Christian,

is there maybe an other way to serialize my DSL-model without a Guice Injector? I don't realy understand how to use this ExecutableExtensionFactory which is generated by Xtext.
The idea was to implement a eclipse plugin that allows the user to perform some actions on my DSL-files. So I can serialize the current DSL-Model.

Maybe someome knows any other possibility to solve this problem.

Thanks,
Alex
Re: How to serialize DSL-Files to UML-Model [message #1148580 is a reply to message #1148522] Mon, 21 October 2013 16:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

have a look at the plugin.xml.

Instead of writing class="my.Class" you write class="my.Factory:my.Class" and you can use @Inject within your handler class (or whatever you use)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to serialize DSL-Files to UML-Model [message #1150089 is a reply to message #1148580] Tue, 22 October 2013 14:26 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,
thanks again for the reply.
I followed the instractions from followig sites to implement a command that fulfils a serialization of the xtext-model.
Here the sides:

http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/
http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html

I've done the following steps:
- Created a Command with a Handler
<extension
        point="org.eclipse.ui.handlers">
     <handler
           class="...SerializationHandler"
           commandId="...ui.SerializationCommand">
     </handler>
  </extension>
       
<extension
        point="org.eclipse.ui.commands">
     <command
           id="...ui.SerializationCommand"
           name="Serialize UML">
     </command>
  </extension>
   
  <extension
        point="org.eclipse.ui.menus">
     <menuContribution
           locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
        <command
              commandId="...ui.SerializationCommand"
               style="push">
               <visibleWhen
                  checkEnabled="false">
                  <iterate>
       <adapt type="org.eclipse.core.resources.IResource">
          <test property="org.eclipse.core.resources.name"
                value="*.cdalf"/>
       </adapt>
    </iterate>
            </visibleWhen>
        </command>
     </menuContribution>
  </extension>


-Created the necessary HandlerClass (SerializationHandler) which makes use of the @Inject
public class SerializationHandler extends AbstractHandler implements IHandler {
	//

	@Inject
	private Injector injector;

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		System.out.println("@ SERIALIZATION HANDLER 1");
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			Object firstElement = structuredSelection.getFirstElement();
			if (firstElement instanceof IFile) {
				IFile model = (IFile) firstElement;
				// create a model URI for the selected xtext-file
				URI modelURI = URI.createPlatformResourceURI(model.getFullPath().toString(), true);
				IProject project = model.getProject();

				System.out.println("@ SERIALIZATION HANDLER 2");

				// create an output folder for the serialized uml-model
				IFolder srcGenFolder = project.getFolder("model");
				if (!srcGenFolder.exists()) {
					try {
						srcGenFolder.create(true, true, new NullProgressMonitor());
					} catch (CoreException e) {
						return null;
					}

				}

				try {
					// run the serializiation
					serializeXtext(modelURI, project);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		//
		// IWorkbenchWindow window =
		// HandlerUtil.getActiveWorkbenchWindowChecked(event);
		// MessageDialog.openInformation(
		// window.getShell(),
		// "ZZZ",
		// "Hello, Eclipse world");
		return null;
	}

	/**
	 * Serialization of the parsed DSL-Model which is represented in an .cdalf
	 * file
	 * 
	 * @param modelURI
	 * @param target
	 * @throws IOException
	 */
	protected void serializeXtext(URI modelURI, IContainer target) throws IOException {
		String xtextExtention = modelURI.fileExtension();
		File modelName = new File(modelURI.path());
		String fullUMLModelName = modelName.getName().substring(0,
				modelName.getName().length() - xtextExtention.length() - 1)
				+ ".uml";
		//
		/*
		 * TODO: Check if file is saved without problems
		 */

		// injector = new
		// CdalfStandaloneSetup().createInjectorAndDoEMFRegistration();

		// load the xtext model to an xtextresourceset
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		Resource xtextResource = resourceSet.getResource(modelURI, true);

		EcoreUtil.resolveAll(xtextResource);

		// store in a xmi-resoure
		Resource xmiResource = resourceSet.createResource(URI.createURI(target.getFullPath() + "/" + fullUMLModelName));
		xmiResource.getContents().add(xtextResource.getContents().get(0));
		try {
			xmiResource.save(null);
		} catch (IOException e) {
			e.printStackTrace();

		}
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}


But the problem is now, that my command isn't visible in the runtime-eclipse. Did I somethning wrong? I checked the configuration of the plugin.xml several times but I stillt can't find
the mistake.

Thanks for your answres,
Alex

[Updated on: Mon, 17 November 2014 15:07]

Report message to a moderator

Re: How to serialize DSL-Files to UML-Model [message #1150142 is a reply to message #1150089] Tue, 22 October 2013 15:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

sure it isnt?
did you have a look at the package explorer (rightclick on the file) not the project explorer?
do you get any errors on console?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to serialize DSL-Files to UML-Model [message #1150176 is a reply to message #1150142] Tue, 22 October 2013 15:49 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Solved!
Everything looks fine after I changed the eclipse-runtime-worspace. (So I realy don't know what the problem was...)
Do I now use the dependency injection mechanism right?

Thank you very much for your help!
Re: How to serialize DSL-Files to UML-Model [message #1150206 is a reply to message #1150176] Tue, 22 October 2013 16:13 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
yes you do.

you could polish it up and inject a

Provider<XtextResourceSet> instead of the injector itself.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How does an Xtext-generated plugin write to the workspace log?
Next Topic:[solved] how to get the parent name in XTEND
Goto Forum:
  


Current Time: Thu Mar 28 16:04:47 GMT 2024

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

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

Back to the top