How to use Xtext(M2M with Xtend) and GMF to create diagramm? (Beginner)

580 Views Asked by At

I have started to work with Eclipse and I'm still a beginner. So I want to get some help from here. I use Xtext to read my DSL. And then I want to show the instance with a diagram, which I have already defined with GMF. But the ecore(MFilesystem) in Xtext and the ecore(Filesystem) in GMF are different. So I use the Xtend to change the model and create an EObject(Filesystem). But i don't know, how the GMF use the resource from Xtent to create a diagram. I show my problem here:

1: I have defined the ecore in xtext(Grammmar):

grammar org.xtext.Filesystem with org.eclipse.xtext.common.Terminals

 generate filesystem "http://www.xtext.org/Filesystem"

 MFilesystem:
(mfiles+=MFile | mfolder+=MFolder)* ;

 MFile:
'myfile' name=ID ';'
;

 MFolder:
'myfolder' name = ID ';'
;

https://i.stack.imgur.com/QrFrk.jpg

2: I have defined the ecore in GMF

http://farm9.staticflickr.com/8048/8082263442_149b8e8b0f.jpg

http://farm9.staticflickr.com/8336/8082305888_824c467570.jpg

3: I try to run the runtime for Xtext

http://farm9.staticflickr.com/8475/8082313936_d06206d838.jpg

and create the diagramm from my DSL file (test.filesystem).

http://farm9.staticflickr.com/8323/8082338379_f35894ba57_z.jpg

4: Then I have this problem

http://farm9.staticflickr.com/8332/8082347529_3ca68fd520.jpg

5: So I try to use Xtend to change my model in Xtext

http://farm9.staticflickr.com/8476/8082347146_dc220f9ca9.jpg

package org.xtext.generator

import org.eclipse.emf.ecore.resource.Resource

import org.eclipse.xtext.generator.IFileSystemAccess

import org.eclipse.xtext.generator.IGenerator

import org.gmf.filesystem.filesystem.Filesystem

import org.gmf.filesystem.filesystem.FilesystemFactory

import org.gmf.filesystem.filesystem.impl.FilesystemFactoryImpl

import org.xtext.filesystem.MFile

import org.xtext.filesystem.MFolder


class FilesystemGenerator implements IGenerator  {

    Filesystem myfilesystem

    FilesystemFactory myfilesystemFactory

    override void doGenerate(Resource resource, IFileSystemAccess fsa) {

        //TODO implement me

        val ast = resource.contents.get(0)

        myfilesystemFactory = new FilesystemFactoryImpl()

        myfilesystem = myfilesystemFactory.createFilesystem()

        val fi = myfilesystemFactory.createFile()

        var astFi = (ast.eContents.get(0)) as MFile

        fi.name = astFi.name

        val fo = myfilesystemFactory.createFolder()

        var astFo = (ast.eContents.get(1)) as MFolder

        fo.name = astFo.name

       }

}

6: But it does not work. I think, I have done something wrong. So I want to know, how I can do so that the GMF uses myfilesystem to create a diagram.

Thanks.

1

There are 1 best solutions below

0
On

The Xtext directive

generate filesystem "http://www.xtext.org/Filesystem"

generates a new metamodel instead of importing an existing one. Instead, specify

import "platform:/resource/org.gmf.filesystem/model/filesystem.ecore"

to refer to the metamodel that your GMF editor is using. See this post for more info.