Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Create intermediate EMF model for additional Validation and for Code generation(Create intermediate EMF model for additional Validation and for Code generation)
Create intermediate EMF model for additional Validation and for Code generation [message #1776919] Wed, 22 November 2017 18:58 Go to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi guys,

is it possible to create an intermediate EMF-Model for additional validation and for later code generation?

The idea is for instance to resolve loops and function in the intermediate model to do additional validation during runtime.

Is that possible?
Where do I have to start (iGenerator)?
Are there any examples?
Would it be a high effort to implement this?

Many thanks for your help!

Best Regards

Chris X


Re: Create intermediate EMF model for additional Validation and for Code generation [message #1776922 is a reply to message #1776919] Wed, 22 November 2017 19:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
How much is this about generation and how much about validation
How big is this transformation? Do you really need it?
I did neither understand the resolve loops and functions thingy you mentioned

You could have a look at the concept of an IDerivedStateComputer.
And the effort depends on your requirements


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1776953 is a reply to message #1776922] Thu, 23 November 2017 08:40 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Christian,

first many thanks for your fast reply. Maybe some examples to make it more clear:

#1

for (int i = 1; i < 4; i++){
    call myfunction(getElementPath("/path/to/ExternalElement" + i))
}


Now I want to validate if all three pathes are valid pathes to EMF-Elements of an external EMF-Model. So at the end I want to translate it to the following representation to validate the pathes during runtime:

    myfunction(/path/to/ExternalElement1)
    myfunction(/path/to/ExternalElement2)
    . . .
    myfunction(/path/to/ExternalElement3)

Therefore the idea is to have an unfolded EMF-Model. Same I want to do with functions.

#2

// main
main{
    element.featureXYZ = true
    doSomething(element)
}
// external file (library)
func doSomething(externalModel::element x){
     verify ("Feature with false is not allowed", element.featureXYZ == true)
}


In the DSL it should be possible to set the values of features of external EMF elements.It should be also possible to have a verify keyword that leads to additional runtime validation. So at the end if the feature of the element would be "false" the element of the doSomething call is highlighted with the error message "Feature with false is not allowed".

To solve both scenarios the approach is to have something like an unfolded EMF model, which stores such information and is updated during runtime.

I would be very happy if you or someone else can give me some hints :)

Many thanks and have a nice day!
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1776956 is a reply to message #1776922] Thu, 23 November 2017 09:03 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

"Is it possible..." It's only software => Yes.

More helpfully, this is what the OCL and QVTd tooling do. Since the OMG-defined information model - AS (Abstract Syntax) is too different from the CS (Concrete Syntax) of the grammar, the tooling uses an Xtext-friendly CS model and an OMG compliant AS model with a CS2AS transformation between them. Auto-generation of the CS2AS transformation is still a work in progress, so currently it is manual code following a fairly obvious pattern assisted by Xtend-generated Visitor hierarchies. Similarly the AS is too different to Java so there is a further CG (Code Generation) model to partition the tree restructuring and optimization from the Java serialization. AS2CG and CG2Java are similarly manually coded with Xtend assistance, awaiting more automation.

You need to much clearer about what and why you need extra models. Xtext is very good at the basic text2model transformation and can accommodate some extra complexities. However if the complexities get too great avoiding extra models can be counterproductive.

Regards

Ed Willink

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777081 is a reply to message #1776956] Fri, 24 November 2017 10:43 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Maybe an example to make it more clear:

for(var i = 1; i < 4; i++){
     results ID{
           ... some code ...
     }
}
description "In this string I can refer to <<results1>>, <<result2>> and <<result3>>"


In my DSL it should be possible to create a result with an ID and with a loop it should be possible to create some results.
After the loop it should be possible to refer to these three results.

My idea was to generate an intermediate EMF-Model which contains i.a. all results and finally represents a report.
In the dsl it should be possible to refer to the elements of the intermediate EMF-Model (Representation of the report) that is generated from the DSL during runtime.
But to refer to all results of the loop I have to resolve the loop to:
result res_1{
    ....
}
 ....
result res_3{
    ....
}


Currently I have two main questions:
1) What is the best way to provide dynamic IDs for the results?
2) And the most important question, what is the best way to create such an intermediate emf-model?
If it's a total stupid idea, what would be a good alternative?

I would be so happy for some hints/starting points.
I'm a xtext beginner and currently I have no clue:D

Many thanks :)

[Updated on: Thu, 30 November 2017 10:58]

Report message to a moderator

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777252 is a reply to message #1777081] Mon, 27 November 2017 15:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
have a look at IDerivedStateComputer. Xtextcasts has an episode on that

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777335 is a reply to message #1777252] Tue, 28 November 2017 15:28 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
@Christian

First many thanks for your help.

One last question, what is the best way to create a new instance of an intermediate in-memory EMF-Model in Xtext? The intermediated EMF-Model will be created by an external defined metamodel and the elements of the intermediate model should be referable.

At which point I have to create the root instance of the intermediate model?
Currently I have no idea where to start.

Again many thanks and have a nice evening
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777354 is a reply to message #1777335] Tue, 28 November 2017 18:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Am not sure. Did you have a look at the iderivedstatecomputer

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777356 is a reply to message #1777354] Tue, 28 November 2017 19:01 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If you use GenModel you will have an XXXFactory.eINSTANCE.createYYY that can do all sorts of useful things with model elements that get put into probably an XXXResourceImpl.getContents(). You will find that XXXPackageImpl.init is an example of an Ecore creation.

Regards

Ed Willink
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777494 is a reply to message #1777356] Wed, 29 November 2017 17:27 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Many thanks for your help :)

Where is the best place in Xtext to register the Factory of my intermediate EMF Model to the resourceSet of Xtext, so that I can create a intermediate model instance for a special scheme like "dynamic" in the resourceSet?
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777503 is a reply to message #1777356] Wed, 29 November 2017 18:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
i dont understand what you mean

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777540 is a reply to message #1777503] Thu, 30 November 2017 06:46 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

XXXStandaloneSetup.init() is where registrations occur. You might extend it, of might provide your own init that calls it.

Regards

Ed Wllink
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777544 is a reply to message #1777503] Thu, 30 November 2017 09:01 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Christian,

sorry for the bad explanation.

I want to have two models. The generated from the dsl and the intermediate model inferred from the generated one. The intermediate model is defined by another EMF-MetaModel.

Now my idea is to generate the intermediate model in the iDerivedStateComputer.
In the IDerivedStateComputer I will have the following lines code:

...
val uri = "dynamic://" + resource.URI.trimFileExtension().appendFileExtension("intermediateModel").path
val res = resource.resourceSet.createResource(URI.createURI(uri), false);
...


At this point I want to create a resource of my intermediate model. But here I'm using the xtext resourceSet which does not know the Factory for my intermediate model. And as Ed describes the factory of the generated model is registered in the standaloneSetup

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("rep", resourceFactory);
IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("rep", serviceProvider);


So for a standaloneSetup it would be possible to register the factory of my intermediateModel here.

But I'm using the environment (eclipse IDE) and not a standaloneSetup. So what is the best place to register the factory of my intermediate model in this case?
I don't need the possibility to show the intermediate Model in some TreeEditor. This is not needed.

@Ed and @Christian

Really many thanks for your help!!! I am happy to get such competent answers and please excuse stupid questions but I'm a newbie :)

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777548 is a reply to message #1777544] Thu, 30 November 2017 09:31 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If you are running within Eclipse you should generate Java from your model so that plugin.xml provides a registration for it.

Regards

Ed Willink
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777564 is a reply to message #1777548] Thu, 30 November 2017 11:31 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Ed,

currently I have the following Setup,

1) The default Xtext-Projects for my DSL (myDSL, myDSL.ide. myDSL.ui ...) and an EMF-Project with the intermediate Model where I generated the java classes for the intermediate model. The EMF-Project is configured as Xtext-Project.

2) In my Grammar-File of the DSL-Project (myDSL) I imported import the intermediate Model and I use generate to generate the Model for the defined grammar.

3) In the .mwe2 I add 'referencedResource' with the path to my intermediate Model

4) Next I created a class XXXDerivedStateComputer and add the necessary bindings to XXXRuntimeModule

5) Now I override the method 'installDerivedState' in XXXDerivedStateComputer
In this method I have the following lines to create a resource for the intermediate Model

...
val uri = "dynamic://" + resource.URI.trimFileExtension().appendFileExtension("intermediateModel").path
val res = resource.resourceSet.createResource(URI.createURI(uri), false);
...


But this line creates a resource of my generated model (grammar model) and not of my intermediate model because the Factory of my intermediate Model is not registered to the resourceSet so far.

So I need a way to register the factory of my intermediate model to the resourceSet that contains also my generated Model, because it should be possible to refer from my DSL to the elements of the dynamic generated intermediate model
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777567 is a reply to message #1777564] Thu, 30 November 2017 11:45 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Sorry, I use models in a boring old traditional way.

I know nothing about XXXDerivedStateComputer or dynamic: My guess is that dynamic: is an Xtext proprietary solution to EMF's dynamic registration limitations. For OCL I prefer to make the standard approaches work.

Hopefully someone else can help you.

Regards

Ed Willink
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777570 is a reply to message #1777567] Thu, 30 November 2017 11:54 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Thank you so much for your help :)

I hope so too :D
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777585 is a reply to message #1777570] Thu, 30 November 2017 14:11 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
I followed the following blog

https://typefox.io/linking-xtext-models-with-other-emf-models

and now it works to create a resource for the intermediate Model in the method "installDerivedState" of the IDeriveStateComputer. So far it's possible to dynamically create a dynamic intermediate model, but in my DSL it's not possible to refer to the elements of the intermediate model ...

Notice: The model is in-memory and not stored as XMI

What could be the reason that I cannot refer to the dynamically created model? Did I miss something?

[Updated on: Thu, 30 November 2017 14:13]

Report message to a moderator

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777586 is a reply to message #1777585] Thu, 30 November 2017 14:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the derived state computer is meant to do the transformation inplace. e.g. to put the new model at slot /1 in the resource.contents. no for creating new resources.
it would be nice if you could show a hello world example as well


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777652 is a reply to message #1777586] Fri, 01 December 2017 09:35 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Christian,

enclosed a small HelloWorld example.

For the DSL you can use the following example code

report XY1{
	result A{
		
	}
	for(1..4){
		result B{
			
		}
		for(1..3){
			result C{
				
			}
		}
	}
	result D{
			
	}
	
	// Now it should be possible to refer to results of the resolved for loop (from the intermediate model)
       // for instance var test = B_1 
}


Currently I generate the intermediate Model in the DerivedStateComputer. But so far it's not possible to refer to it (What is missing?). I'm aware that the idea of the IDerivedStateComputer is to manipulate the existing AST but at the end I need a new intermediate Model (intermediate AST) to do further validations. Maybe the IDerivedStateComputer is the wrong place to create such an intermediate Model. But so far I have no idea where I should start to generate this intermediate Model and what I have to do so that I can refer to it.

PS: For sure for this small example / DSL it would be sufficient to manipulate the existing AST in the IDerivedStateComputer

[Updated on: Fri, 01 December 2017 09:38]

Report message to a moderator

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777658 is a reply to message #1777652] Fri, 01 December 2017 09:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
as i have said: do NOT create new resource.
put derived stuff at resource.contents(1..n)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777678 is a reply to message #1777658] Fri, 01 December 2017 12:15 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Christian,

thanks I remove it. But it's still not clear where to create my intermediate Model defined by an external EMF-MetaModel.

So when I understand you correct then the IDerivedStateComputer is not the right place to generate it. I should only use it to manipulate the current model. But this is not what I want. I want to generate an additional intermediate Model.

[Updated on: Fri, 01 December 2017 12:16]

Report message to a moderator

Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777679 is a reply to message #1777678] Fri, 01 December 2017 12:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
arrrrrrggggggggg!!!!!!

val mymodel = MyFactory.eINSTANCE.createXXXXX
resource.contents.add(mymodel)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create intermediate EMF model for additional Validation and for Code generation [message #1777683 is a reply to message #1777679] Fri, 01 December 2017 13:06 Go to previous message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Christian,

many thanks. Previously I tried this but I did a mistake and got the error "Only one root node is allowed" and so I thought it doesn't work. But now it works fine!

Sorry for upsetting you and again many thanks for your help!!!!
Previous Topic:java.lang.RuntimeException: path to .genmodel is unmapped
Next Topic:Type Resolving Error on Maven Build
Goto Forum:
  


Current Time: Fri Mar 29 02:14:52 GMT 2024

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

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

Back to the top