Home » Modeling » TMF (Xtext) » [XText]Text generation(Text generation in XText)
[XText]Text generation [message #657058] |
Tue, 01 March 2011 13:41 |
mina 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 #658703 is a reply to message #657058] |
Wed, 09 March 2011 14:33 |
mina 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 #659394 is a reply to message #657058] |
Sun, 13 March 2011 15:23 |
mina 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 |
Ed Willink 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 |
Henrik Lindberg 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 #659647 is a reply to message #659589] |
Mon, 14 March 2011 23:55 |
Henrik Lindberg 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 #659765 is a reply to message #659662] |
Tue, 15 March 2011 14:30 |
Henrik Lindberg 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
|
|
| | | | | | | |
Goto Forum:
Current Time: Thu Apr 25 17:20:57 GMT 2024
Powered by FUDForum. Page generated in 0.04779 seconds
|