Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Translate Template Proposal Pattern into Xtext Model(Programmatically access the template proposals and save their pattern as Xtext model)
Translate Template Proposal Pattern into Xtext Model [message #1820591] Thu, 23 January 2020 15:16 Go to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
I followed https://www.eclipse.org/Xtext/documentation/310_eclipse_support.html#templates to add default template proposals to my Xtext plugin. Those templates and probably templates added by the user at some point should be used during the creation of a new model, which is done via a wizard. There the user enters some information and new model files are created.
I produced a minimal example:
The Grammar
Model:
	lastnames+=LastName*
	greetings+=Greeting*;

Greeting:
	'Hello' name=ID lastname=[LastName] '!';

LastName:
	'lastname' name=ID;

Example Template:
<template autoinsert="true" context="MyDsl.Greeting" deleted="false" description="Just to explain the question" enabled="true" name="MinimalExample" id="1">
Hello ${name} Smith!
</template>

As you can see I don't use a reference to another object "LastName" but just write text. I know that for this to work, there has to be a "lastname Smith" in my model, there is.
I can get all the templates for a specific context with this:
TemplateStore templateStore = Activator.getDefault().getInjector("MyDSL").getInstance(TemplateStore.class);
Template[] templates = templateStore.getTemplates("MyDSL.Greetings");
and access their pattern. But that is just a String, I need the actual model objects to e.g. give the Greeting "Hello ${name} Smith!" a name programmatically.
Now finally the question:

Is it possible to extract the model entities from this pattern, so that I have a LastName object with name:=Smith and a Greeting object with name:=null (or sth else?) and lastname:=referenceToLastNameWith(name=Smith)?

What I thought of so far is: Replacing all variables in the template pattern String by something random, save it as a .mydsl resource and load it with
(Model) resourceSet.getResource(uri, true).getContents().get(0);
but it feels like a big detour.

Thanks for your help,
Saphira
Re: Translate Template Proposal Pattern into Xtext Model [message #1820592 is a reply to message #1820591] Thu, 23 January 2020 15:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

can you please elaborate what you actually want to achieve


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Translate Template Proposal Pattern into Xtext Model [message #1820593 is a reply to message #1820592] Thu, 23 January 2020 16:07 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 157
Registered: February 2016
Senior Member
Hi Marie,

you may also take a look at the Statemachine example shipped with the Xtext framework to get some inspiration:
https://github.com/eclipse/xtext-eclipse/blob/master/org.eclipse.xtext.xtext.ui.examples/projects/fowlerdsl/org.eclipse.xtext.example.fowlerdsl.ui/templates/templates.xml

Tamás
Re: Translate Template Proposal Pattern into Xtext Model [message #1820594 is a reply to message #1820592] Thu, 23 January 2020 16:17 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
Hi, thanks for the quick reply.
I want to

    [x] Load the templates
    [ ] Translate the template pattern into a model consisting of model entities (here: LastName and Greeting)
    [ ] Change the model (here: change the "name" of the extracted Greeting)

Sorry for not being clear enough. By model entity I mean an EObject like I get with this:
MyDSLFactory.eINSTANCE.createGreeting()


Thanks @Tamas, I will have a look

[Updated on: Thu, 23 January 2020 16:18]

Report message to a moderator

Re: Translate Template Proposal Pattern into Xtext Model [message #1820602 is a reply to message #1820594] Thu, 23 January 2020 18:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
but why would you want to do that. what is your usecase?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Translate Template Proposal Pattern into Xtext Model [message #1820701 is a reply to message #1820602] Mon, 27 January 2020 09:32 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
Some parts of the models that are created with the help of the wizard are user group (e.g. company) specific. So after installing my plugin, the user can define certain parts of the model that can be used in all future model generations and also can be exported to be used in other eclipse installations. The real model is about databases in docker containers. One default template for a MariaDB looks like this:
database $(name) : MariaDB {
    	environment {
		MYSQL_ROOT_PASSWORD = example;
	}
}

with
dbtype MariaDB {
    default image = mariadb:latest;
}

somewhere else.
The $(name) will be read from somewhere else (another model created in a different plugin) during the model creation (via wizard) and has to be added to the entity "database".
Re: Translate Template Proposal Pattern into Xtext Model [message #1820706 is a reply to message #1820701] Mon, 27 January 2020 12:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
but why do you need to read the templates then?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Translate Template Proposal Pattern into Xtext Model [message #1820707 is a reply to message #1820706] Mon, 27 January 2020 13:00 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
the template above is only the default template. the user is able to add templates (during runtime) like:
database $(name) : MariaDB {
    	environment {
		MYSQL_ROOT_PASSWORD = example ;
                MYSQL_USER = John ;
                MYSQL_PASSWORD = John'sPassword ;
	}
        command = --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci ;
}

The user then can choose between different templates (here the one above or the default one from the last post) in the model creation wizard.

I will try the Value Converter https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#value-converter

[Updated on: Mon, 27 January 2020 13:02]

Report message to a moderator

Re: Translate Template Proposal Pattern into Xtext Model [message #1820959 is a reply to message #1820707] Mon, 03 February 2020 13:56 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
So I tried the org.eclipse.xtext.parser.IParser and parsed the template pattern:
IParseResult result = parser.parse(new StringReader(template.getPattern()));

This creates a correct model if there are no variables (like $(name)) in the pattern. Ok so no variables in patters, not perfect but tolerable. The remaining problem is the reference to another model object: dbtype (see two posts above https://www.eclipse.org/forums/index.php?t=msg&th=1102127&goto=1820701&#msg_1820701). This will remain null in the database model. It would be nice if it wasn't null but a dbtype object with name "MariaDB" and other attributes null, but I guess that's not how EMF EObjects work, right?
Re: Translate Template Proposal Pattern into Xtext Model [message #1820987 is a reply to message #1820959] Tue, 04 February 2020 06:10 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

EMF EObjects or rather EReferences are very capable of resolving whatever intermodel reference is appropriate. Of course if you neglect to define the reference, the reference will be null. I suspect that you need to examine Xtext's Scope provider capabilities so that your references are resolved in ways that suit your requirements.

Regards

Ed Willink
Re: Translate Template Proposal Pattern into Xtext Model [message #1820998 is a reply to message #1820987] Tue, 04 February 2020 10:43 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
Thanks for the tip Ed, sometimes just the right word to google is missing! So I had a look into the ScopeProvider and I think I can solve my problem using it. If the reference doesn't exist, I will create a dummy one. But getScope() is not called during parsing (IParser.parse()), since it depends on resources (I think). Any tips for that?

At least in the process I learned never to use getResource() explicitly (which I do a lot in my plugin).
https://de.slideshare.net/holgerschill/deep-dive-into-xtext-scoping-local-and-global-scopes-explained
Re: Translate Template Proposal Pattern into Xtext Model [message #1821002 is a reply to message #1820998] Tue, 04 February 2020 12:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i still dont get your usecase

- parsing the model
- resovling stuff
- evaluate the templates
...


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 04 February 2020 12:51]

Report message to a moderator

Re: Translate Template Proposal Pattern into Xtext Model [message #1821003 is a reply to message #1821002] Tue, 04 February 2020 13:19 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
Christian Dietrich wrote on Tue, 04 February 2020 12:51

- parsing the model
- resovling stuff
- evaluate the templates
...

So this list represents your understanding of what I want to do? or am doing? or should do?

Concerning the first point "parsing the model": I don't have a model to parse, I only have a String taken from the template which I want to turn into a model without the need to save it as a model resource (file). So I also don't understand what you mean by "evaluate the template". I'm sorry, I can't explain it better if I don't know what exactly you don't understand or how you understood it so far, maybe you could elaborate a bit?
Thanks for making time.
Re: Translate Template Proposal Pattern into Xtext Model [message #1821006 is a reply to message #1821003] Tue, 04 February 2020 13:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
well as the templates contain variables the template as is
is not even parseable at all depending on the template.
these variables need to be resolved as the content assist does
it e.g. for the preview on the template.

thus just "parsing" a template will never work


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Translate Template Proposal Pattern into Xtext Model [message #1821011 is a reply to message #1821006] Tue, 04 February 2020 14:15 Go to previous messageGo to next message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
Ok, so the underlying problem is that those templates were made to be used when writing a model in runtime using the xtext editor and I try to use them in a different way (i.e. turning them into models programmatically). So probably those templates are not a good solution for my task.
Why I wanted to use templates is because they are easily exported and imported and stored in the eclipse persistent storage without having any additional programming effort.
Maybe I should just use model files and add import and export buttons to my wizard. And create a preference page to manage them.
Do you have an idea what else I could do?
Re: Translate Template Proposal Pattern into Xtext Model [message #1821012 is a reply to message #1821011] Tue, 04 February 2020 14:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
did you debug (e.g. at template variable resolvers) how the proposal provider calculates the proposal preview.
maybe you can do something similar programmatically


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Translate Template Proposal Pattern into Xtext Model [message #1821103 is a reply to message #1821012] Wed, 05 February 2020 16:08 Go to previous message
Marie-Saphira Flug is currently offline Marie-Saphira FlugFriend
Messages: 21
Registered: January 2019
Junior Member
So I resolved the variables with
TemplateTranslator templateTranslator = new TemplateTranslator();
TemplateBuffer buffer = null;
try {
	buffer = templateTranslator.translate(template);
} catch (TemplateException e) {
	e.printStackTrace();
}

The buffer contains the template pattern as a String where all variables (e.g. ${name}) are replaced by their names (here: name) and a TemplateVariable array. The String can be parsed as above.
The crossreference to the dbtype (which will never exist) is resolved by hand (buffer.getString().contains(dbTypeName)), a dbType is created and added to the parsed model. It's not perfect but sufficient.

Thanks for your help!

[Updated on: Wed, 05 February 2020 16:13]

Report message to a moderator

Previous Topic:Creating VSIX extension
Next Topic:Documentation generation based on grammar
Goto Forum:
  


Current Time: Tue Apr 16 18:05:45 GMT 2024

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

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

Back to the top