Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [XText]Text generation(Text generation in XText)
[XText]Text generation [message #657058] Tue, 01 March 2011 13:41 Go to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
hi,

I have a problem when i try to generate a file with XText generation's way.

My grammar is:
Model:
contents=Content;

Content:
(elements+=(elem1 | elem2 )+;

elem1:
'elem1' name=ID;

elem2:
'elem2' name=ID;


my template.xpt file is :

«IMPORT xtext::example::mydsl»

«EXTENSION templates::Extensions»

«DEFINE main FOR Model-»


«FILE "gen.txt"-»
This is an example of a generated file.
«ENDFILE-»

«ENDDEFINE»


My generator.mwe2 file is:
module workflow.MyDslGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var fileEncoding = "Cp1252"
var modelPath = "src/model"

Workflow {

component = org.eclipse.xtext.mwe.Reader {
// lookup all resources on the classpath
// useJavaClassPath = true

// or define search scope explicitly
path = modelPath

// this class will be generated by the xtext generator 
register = example.MyDslStandaloneSetup {}
load = {
//slot = "" Model haven't a name 
type = "Model"
}
}

component = org.eclipse.xpand2.Generator {
expand = "templates::Template::main FOREACH Model"
outlet = {
path = targetDir
}
fileEncoding = fileEncoding
}
}


My probleme is that model element haven't a name (an ID)

The report is:
0 [main] DEBUG org.eclipse.xtext.mwe.Reader - Resource Pathes : [src/model]
31 [main] INFO org.eclipse.xpand2.Generator - No meta models configured, using JavaBeans as default.
343 [main] DEBUG xt.validation.ResourceValidatorImpl - Syntax check OK! Resource: file:/G:/workspace/org.example.mydsl.generator/src/model/ Example.txt
484 [main] WARN org.eclipse.xtext.mwe.SlotEntry - Could not find any exported element of type 'Content' -> Slot '' is empty.
593 [main] ERROR org.eclipse.xpand2.Generator - Error in Component of type org.eclipse.xpand2.Generator: 
EvaluationException : Couldn't find type or property 'Model'
[64,5] on line 1 'Model'

593 [main] ERROR mf.mwe2.launch.runtime.Mwe2Launcher - Problems running workflow workflow.MyDslGenerator: Couldn't find type or property 'Model'
java.lang.RuntimeException: Problems running workflow workflow.MyDslGenerator: Couldn't find type or property 'Model'
at  org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runne r.java:82)
at  org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runne r.java:52)
at  org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Lau ncher.java:74)
at  org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2La uncher.java:35)
Caused by: org.eclipse.emf.mwe.core.WorkflowInterruptedException: Couldn't find type or property 'Model'
at  org.eclipse.xtend.expression.AbstractExpressionsUsingWorkflo  wComponent.invokeInternal(AbstractExpressionsUsingWorkflowCo mponent.java:247)
at  org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invok e(AbstractWorkflowComponent.java:126)
at  org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.ja va:34)
at  org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invok e(AbstractWorkflowComponent.java:201)
at  org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkf  lowComponent.invoke(AbstractCompositeWorkflowComponent.java: 31)
at  org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow. java:19)
at  org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runne r.java:80)
... 3 more

is there anyone who has a solution for this problem???
noted that I would not add a name for Model element

thank you in advance

[Updated on: Sat, 16 April 2011 11:23]

Report message to a moderator

Re: [XText]Text generation [message #657161 is a reply to message #657058] Tue, 01 March 2011 18:40 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

you could:

-give Model an artificial name (e.g. the URI of the resource using a name provider), that way they will visible to the index which is used for calculating the slot contents
-write your own reader component, that fills the slot with model elements
-load the elements of the models into the slot and start generating from them
--calculate the set of models from the elements stored in the slot and then start generating from those models

to name some possibilities. In any case you should give the slot a name and use the slot name in the xpand call.

Alex

Re: [XText]Text generation [message #658112 is a reply to message #657161] Sun, 06 March 2011 16:24 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Hi Alex,

Can you give me please an example!
I am a beginner in XText and I did not understand your solution.

Thank you in advance,
Mina
Re: [XText]Text generation [message #658142 is a reply to message #658112] Mon, 07 March 2011 05:21 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

as a first brief reply: this question pops up constantly. Please use a search for "Xtext slot empty".

Alex
Re: [XText]Text generation [message #658703 is a reply to message #657058] Wed, 09 March 2011 14:33 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Hi Alex,
Quote:
-give Model an artificial name (e.g. the URI of the resource using a name provider), that way they will visible to the index which is used for calculating the slot contents

you mean give Model a name inside the grammar ?
Quote:
-write your own reader component, that fills the slot with model elements

I think it is too difficult for me, I am sstill a beginner with XText
Quote:
-load the elements of the models into the slot and start generating from them

I try this. the problem I encountered is that all elements are typed by the super class Element, and I can't access to the contents of Element1 or Element2 instances. How can I solve this problem.
Quote:
-calculate the set of models from the elements stored in the slot and then start generating from those models

I also try this, with a method that returns the Container of an elements (Ie Model). But after that, I have the same problem as before.

How to manipulate Element1 or Element2 instances instead of Element instance ?

[Updated on: Thu, 10 March 2011 10:28]

Report message to a moderator

Re: [XText]Text generation [message #659049 is a reply to message #658703] Thu, 10 March 2011 19:29 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

as to the model name, I don't mean a name attribute in the grammar. You implement an IQualifiedNameProvider (extending DefaultDeclaraticeQualifiedNameProvider, defining a method String qualifiedName(Model m){return nameCalculatedFor(m);})
This process should be described in some of the search results for "Xtext slot empty).

With respect to generating from Element directly (or the container calculated from an element) the answer is polymorphic dispatch (see the Xtend/Xpand documentation). As this is a very central concept of Xtend/Xpand, you should spend some time reading up on it and trying out things.

You basically define the "same" template for different types
<<DEFINE main FOR elem1>>
<<DEFINE main FOR elem2>>

In case you try to expand template for some x.eContainer() the editor will complain of a missing template for type (E)Object. Simply provide a dummy template for that type and one for the type you actually expect the container to be. As I said, which template is used is determined during runtime (the one for the most specific type of the container).

The same works for Xtend functions that have the same name but different parameter types.

String getName(Model m): "a Model does not have a name";
String getName(Content c): "Content only has elements but no name";
String getName(Object e): "this metod is called,e.g., for elements of type elem2 as you forgot to define the getName-Method for that specific type";
String getName(elem1 e): e.name;

The interpreter will automatically choose the definition for the most specific matching type. Although you also can type cast in Xtend/Xpand, in this case you don't have to.

Alex
Re: [XText]Text generation [message #659394 is a reply to message #657058] Sun, 13 March 2011 15:23 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Hi Alex.

Thank you for your answers. I tried this and it works fine.
I have the model and I can browse each of its elements and generate text from them.

Now i encountered another problem, I would like to generate text mixed with the source file content (for example, code comments), directly in the source file, not in another, example :
- source file
element "elem1" {
   ....
}

element "elem2" {
   ....
}


after generation :
remark "comments for elem1"
element "elem1" {
   ....
}

remark "comments for elem2"
element "elem2" {
   ....
}

I explore two possibilities.
The first is to regenerate all the text (code + comments) in place of the source file content.
I successed, but I can't recover the original source code. I just can regenerate it from the objects (with toString()) but I lose the original layout.

It is important for me to keep the original layout, how can I do that ??

The second solution (and I think the best) would be to directly insert text in the source file, but I have no idea on how to do that.

Can you giva me a solution, please to recover the original layout of elements or directly insert text in the file
Re: [XText]Text generation [message #659403 is a reply to message #659394] Sun, 13 March 2011 16:20 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

It seems as if your model to text problem is non-trivial.

You might consider using a M2T tool such as Acceleo to use the grammar
model to produce your text.

Regards

Ed Willink

On 13/03/2011 15:23, mina wrote:
> Hi Alex.
>
> Thank you for your answers. I tried this and it works fine.
> I have the model and I can browse each of its elements and generate
> text from them.
>
> Now i encountered another problem, I would like to generate text mixed
> with the source file content (for example, code comments), directly in
> the source file, not in another, example :
> - source file
>
> element "elem1" {
> ....
> }
>
> element "elem2" {
> ....
> }
>
>
> after generation :
> remark "comments for elem1"
> element "elem1" {
> ....
> }
>
> remark "comments for elem2"
> element "elem2" {
> ....
> }
> I explore two possibilities.
> The first is to regenerate all the text (code + comments) in place of
> the source file content.
> I successed, but I can't recover the original source code. I just can
> regenerate it from the objects (with toString()) but I lose the
> original layout.
>
> It is important for me to keep the original layout, how can I do that ??
>
> The second solution (and I think the best) would be to directly insert
> text in the source file, but I have no idea on how to do that.
>
> Can you giva me a solution, please to recover the original layout of
> elements or directly insert text in the file
Re: [XText]Text generation [message #659434 is a reply to message #659394] Sun, 13 March 2011 21:56 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
I have been working with similar issues.
If your comments are like javadoc, it is quite easy since:
- such comments can be declared as terminals and be non hidden
- they are simply included in the grammar as optional elements

And you are done.

If your comments are regular comments (i.e. something like //, /* */ or
#) then it is more problematic since you typically want these comments
to be processed using hidden().

I found half of the solution (which was enough for me) going from
parsing text to having the comments associated with the correct model
element. The trick was to do this after parsing and linking and to store
the comment text using a model adapter. A working implementation is
found in the Cloudsmith/Geppetto project at github.

I also wanted to serialize the comments to text (the case when there is
only a model and no parse result (aka. node model)), but this turned out
to be more difficult, and I did not complete this task as I could not
figure out where to place an override that would enable me to output
text from my comment adapter *before* the entire element.

Regards
- henrik
On 3/13/11 4:23 PM, mina wrote:
> Hi Alex.
>
> Thank you for your answers. I tried this and it works fine.
> I have the model and I can browse each of its elements and generate text
> from them.
>
> Now i encountered another problem, I would like to generate text mixed
> with the source file content (for example, code comments), directly in
> the source file, not in another, example :
> - source file
>
> element "elem1" {
> ....
> }
>
> element "elem2" {
> ....
> }
>
>
> after generation :
> remark "comments for elem1"
> element "elem1" {
> ....
> }
>
> remark "comments for elem2"
> element "elem2" {
> ....
> }
> I explore two possibilities.
> The first is to regenerate all the text (code + comments) in place of
> the source file content.
> I successed, but I can't recover the original source code. I just can
> regenerate it from the objects (with toString()) but I lose the original
> layout.
>
> It is important for me to keep the original layout, how can I do that ??
>
> The second solution (and I think the best) would be to directly insert
> text in the source file, but I have no idea on how to do that.
>
> Can you giva me a solution, please to recover the original layout of
> elements or directly insert text in the file
Re: [XText]Text generation [message #659589 is a reply to message #657058] Mon, 14 March 2011 17:16 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Hi all,

May be I wasn't clear in my previous post.
The comments are like javadoc, i.e. they follow a specific structure and contains element's informations.

The difficulty is that I want to automatically generate the comments.
For example, I have a .mydsl file with several elements (but no comments), and then, I run the comments generation.
The file resulting must contains my original elements preceded by generated comments.

Actually, I can do that, but I need to regenerate elements.
The elements are regenrate according the format I defined in the toString() method, but I want to generate them in their original format.

I hope it was clear.
Re: [XText]Text generation [message #659647 is a reply to message #659589] Mon, 14 March 2011 23:55 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
If I understand this correctly, you could do this as the textual level.
How exactly depends on if you are doing this in standalone mode or while
editing - but in principle I would expect something like this would work:

- iterate over the model, for the semantic objects where you want to
insert comments before them, get the associated node and remember its
offset in the text.
- iterate over the collected offsets and insert the comments in the
text, accumulating the additional offset introduced by the inserted text.
- reparse

Since you did not make any changes to the non comment text, the original
layout is naturally preserved.

Look at how "textual quickfixes" are done.

- henrik

On 3/14/11 6:16 PM, mina wrote:
> Hi all,
>
> May be I wasn't clear in my previous post.
> The comments are like javadoc, i.e. they follow a specific structure and
> contains element's informations.
>
> The difficulty is that I want to automatically generate the comments.
> For example, I have a .mydsl file with several elements (but no
> comments), and then, I run the comments generation.
> The file resulting must contains my original elements preceded by
> generated comments.
> Actually, I can do that, but I need to regenerate elements.
> The elements are regenrate according the format I defined in the
> toString() method, but I want to generate them in their original format.
>
> I hope it was clear.
Re: [XText]Text generation [message #659662 is a reply to message #659647] Tue, 15 March 2011 05:09 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

there is an even dirtier way for doing the modifications textually Wink (i.e. this is not recommended as a general way of doing things)
If you don't mind to corrupt the node model (which is OK if you don't work further with the model), you can directly insert the comments into the text of the existing nodes and then serialise the root node (as that simply concatenates the text).

Get your model element (elem1), use NodeUtil to navigate to the keyword leaf node of the parse tree (with text "elem1"), replace its text with "mycomment\nelemn1", do so for all other model elements where you want to add a comment, get the root node of the parse tree, serialise and reparse the model.

And again, don't do this Wink

Alex
Re: [XText]Text generation [message #659765 is a reply to message #659662] Tue, 15 March 2011 14:30 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
That sounds like a solution to what I wanted to do as well, and since I
do this in standalone mode, screwing up the node model does not seem
that bad.

- henrik

On 3/15/11 6:09 AM, Alexander Nittka wrote:
> Hi,
>
> there is an even dirtier way for doing the modifications textually ;)
> (i.e. this is not recommended as a general way of doing things)
> If you don't mind to corrupt the node model (which is OK if you don't
> work further with the model), you can directly insert the comments into
> the text of the existing nodes and then serialise the root node (as that
> simply concatenates the text).
>
> Get your model element (elem1), use NodeUtil to navigate to the keyword
> leaf node of the parse tree (with text "elem1"), replace its text with
> "mycomment\nelemn1", do so for all other model elements where you want
> to add a comment, get the root node of the parse tree, serialise and
> reparse the model.
>
> And again, don't do this ;)
>
> Alex
Re: [XText]Text generation [message #659799 is a reply to message #657058] Tue, 15 March 2011 16:12 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
If you process the nodes in reversed order it's easier as you don't have to handle the offsets changes.
Re: [XText]Text generation [message #664954 is a reply to message #659647] Tue, 12 April 2011 15:08 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Henrik Lindberg wrote on Mon, 14 March 2011 19:55
If I understand this correctly, you could do this as the textual level.
How exactly depends on if you are doing this in standalone mode or while
editing - but in principle I would expect something like this would work:

- iterate over the model, for the semantic objects where you want to
insert comments before them, get the associated node and remember its
offset in the text.
- iterate over the collected offsets and insert the comments in the
text, accumulating the additional offset introduced by the inserted text.
- reparse

Since you did not make any changes to the non comment text, the original
layout is naturally preserved.

Look at how "textual quickfixes" are done.

- henrik



This is exactly what I want to do !
Where have I to do these treatments ?

I can't use Alex' solution because I can't corrupt the model. I have to work with the model even after th comment generation.
Re: [XText]Text generation [message #664991 is a reply to message #664954] Tue, 12 April 2011 17:35 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 4/12/11 5:08 PM, mina wrote:
> This is exactly what I want to do !
> Where have I to do these treatments ?
>
Depends on when you want your "comment processing" to take place.
For all loads of applicable resources? On demand? If so from where?

- henrik
Re: [XText]Text generation [message #665085 is a reply to message #664991] Wed, 13 April 2011 08:42 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
Henrik Lindberg wrote on Tue, 12 April 2011 13:35
Depends on when you want your "comment processing" to take place.
For all loads of applicable resources? On demand? If so from where?



What is the easiest way ?
The best would be to run the process automatically in background, but another way would be good.
What are the different possibilities ?
Re: [XText]Text generation [message #665153 is a reply to message #665085] Wed, 13 April 2011 12:56 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 4/13/11 10:42 AM, mina wrote:
> Henrik Lindberg wrote on Tue, 12 April 2011 13:35
>> Depends on when you want your "comment processing" to take place.
>> For all loads of applicable resources? On demand? If so from where?
>
>
> What is the easiest way ?
> The best would be to run the process automatically in background, but
> another way would be good. What are the different possibilities ?

well, there are quite a few ways to get this done - as I said it depends
on how you want it to work.

What do you expect when a user is editing? Should this happen while the
user is editing? At the same time? Do you plan to read a resource,
modify it and then save it? Or do you want files to be modified when
loaded? If so, should they come up dirty, or be saved.

The easiest is naturally do modify all files in a batch without worrying
about files being open i dirty state in editors and concurrently being
edited.

Next easiest thing I guess would be to process on save - similar to the
java "auto source cleanup".

- henrik
Re: [XText]Text generation [message #665343 is a reply to message #665153] Thu, 14 April 2011 11:52 Go to previous messageGo to next message
mina  is currently offline mina Friend
Messages: 10
Registered: March 2011
Junior Member
It should happens when the user is editing, but files shouldn't come up dirty.
The priority is to preserve files integrity. If it is not possible, the process must be run manually by the user. The way doesn't matter (with a button, a quickfix...).
Re: [XText]Text generation [message #665376 is a reply to message #665343] Thu, 14 April 2011 13:40 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
How about checking the integrity when validating (i.e. an error marker),
and offer a quick fix to make it valid ? This way, it is not automated,
but works in similar fashion to how such issues are solved in other
editors, and you avoid many of the concurrency issues.

- henrik

On 4/14/11 1:52 PM, mina wrote:
> It should happens when the user is editing, but files shouldn't come up
> dirty.
> The priority is to preserve files integrity. If it is not possible, the
> process must be run manually by the user. The way doesn't matter (with a
> button, a quickfix...).
Previous Topic:[xtext 2.0] supress confirmation "Undoing ... affects elements outside of ..." on undo ref
Next Topic:XText
Goto Forum:
  


Current Time: Thu Apr 25 17:20:57 GMT 2024

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

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

Back to the top