Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » composition of one model in xtext from many different parts
composition of one model in xtext from many different parts [message #550162] Thu, 29 July 2010 12:09 Go to next message
Cong Kinh NGUYEN is currently offline Cong Kinh NGUYENFriend
Messages: 47
Registered: February 2010
Member
Hi everybody,

I'm thinking about one grammar to create models in xtext that one model in xtext is composed of many different parts (each part is stored in one file) because I want to reuse some parts of them for other models. Could you tell me if that is possible in xtext version 1.0.0. If so, could you tell me how to do that? If not, is there a plan to support it?

Thanks in advance,
Kinh
Re: composition of one model in xtext from many different parts [message #550169 is a reply to message #550162] Thu, 29 July 2010 12:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi Kinh,

for me it is not clear what you are asking about. Do you speak of parts on Grammar / Meta level or on Model level?

On Grammar Level there is a Single inhertiance possible.
On Model Level you can create of cource as many model files as you want and your computer can handle.

Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #550177 is a reply to message #550169] Thu, 29 July 2010 13:04 Go to previous messageGo to next message
Cong Kinh NGUYEN is currently offline Cong Kinh NGUYENFriend
Messages: 47
Registered: February 2010
Member
Hi Christian,

In fact, I want to create one model (on Model Level) for example model1.mydsl that it is composed from many parts: part1, part2, part3 where each part is stored on one file. Note that I only have one model, but it is divided into 3 parts and stored on three files. Is that possible? or each model has to be stored on one file?

Regards,
Kinh

Christian Dietrich wrote on Thu, 29 July 2010 08:50
Hi Kinh,

for me it is not clear what you are asking about. Do you speak of parts on Grammar / Meta level or on Model level?

On Grammar Level there is a Single inhertiance possible.
On Model Level you can create of cource as many model files as you want and your computer can handle.

Regards
Christian

Re: composition of one model in xtext from many different parts [message #550178 is a reply to message #550177] Thu, 29 July 2010 13:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

if you use Xtext 1.0.0 with defaults "the model" is out of the box the set of "all elements in many model files". It does not (really) matter in which file it is stored.

Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #550180 is a reply to message #550178] Thu, 29 July 2010 13:15 Go to previous messageGo to next message
Cong Kinh NGUYEN is currently offline Cong Kinh NGUYENFriend
Messages: 47
Registered: February 2010
Member
I am using Xtext 1.0.0. But I am sorry, I don't understand how to do that. Could you tell me more detail or give me links to do that?

Regards,
Kinh

Christian Dietrich wrote on Thu, 29 July 2010 09:06
Hi,

if you use Xtext 1.0.0 with defaults "the model" is out of the box the set of "all elements in many model files". It does not (really) matter in which file it is stored.

Regards
Christian

Re: composition of one model in xtext from many different parts [message #550184 is a reply to message #550180] Thu, 29 July 2010 13:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

take that simple Grammar

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

ModelPart:
	elements+=Element*;
	
Element:
	"element" name=ID ("refs" refs=[Element])? ";"
;



and these model (both in the src folder of a java project)

a.mydsl
element A;

b.mydsl
element B refs A;
element C refs B;


and hey the refs work (inter files)

and take this codegenerator

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 = org.xtext.example.mydsl.MyDslStandaloneSetup {}
		load = {
			slot = "elements"
			type = "Element"
		}
	}

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



«IMPORT org::xtext::example::mydsl::myDsl»

«DEFINE main FOR Element»
«FILE name+".txt"»
This is element «name»
«ENDFILE»
«ENDDEFINE»


and hey you get 3 files as result

A.txt
B.txt
C.txt

I hope this shows you that it works out of the box

Regards
Christian


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

[Updated on: Thu, 29 July 2010 13:28]

Report message to a moderator

Re: composition of one model in xtext from many different parts [message #550191 is a reply to message #550184] Thu, 29 July 2010 13:47 Go to previous messageGo to next message
Cong Kinh NGUYEN is currently offline Cong Kinh NGUYENFriend
Messages: 47
Registered: February 2010
Member
OK, thank you so much. That's just what I want.

Regards,
Kinh
Re: composition of one model in xtext from many different parts [message #554309 is a reply to message #550191] Sat, 21 August 2010 09:26 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello,

Sorry for barging in here, but I'm strugeling with the same issue. I want to spread my model over several files. The example given in this thread seems to work, but it doesn't.

When you close the file a.mydsl in the editor and restart eclipse, you will find that while editing b.mydsl there is no knowledge of the existence of element A anymore. Then when reopening a.mydsl again, b.mydsl still doesn't know about element A until element A is edited and the file (re)saved.

Further, I would like to have some controll over what files get loaded into the current model. For instance I could have two model parts that are mutually exclusive (for instance because they define the same elements, but differently). The parts are intended to be used in different models.

model1.mydsl uses a1.mydsl and ...
model2.mydsl uses a2.mydsl and ...

How could one achive this?

Kind regards,

Louis
Re: composition of one model in xtext from many different parts [message #554310 is a reply to message #554309] Sat, 21 August 2010 09:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Louis,

yes it is possible to use explicit import. therefore have a look into the docs (section Global Scopes Based On Explicit Imports (ImportURI Mechanism))

btw the problems you have with the default behaviour i cannot reproduce.

Regards
Christian


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

[Updated on: Sat, 21 August 2010 10:02]

Report message to a moderator

Re: composition of one model in xtext from many different parts [message #554333 is a reply to message #554310] Sat, 21 August 2010 19:04 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Christian,

thanks for your reply.

I will study the section of the documentation you mention and do some experiments with it.

As to the reproduction, it was/is very easy. I donwloaded the Linux 64 bit helios distribution with xtext installed from the itemis site. Untarred it on my recently updated installation of centos 5, updated eclipse via the update manager and tried the example in this thread.
- Deleted my workspace
- Opened a new xtext project (accepted the defaults)
- replaced the contents of the MyDsl.xtext file with what was given in the thread
- ran the mwe2 file
- started a 2nd eclipse to test the generated editor (shift alt x e)
- created a general project "test"
- added two files to the project a.mydsl and b.mydsl and gave them the contents as in this thread
- closed a.mydsl in the editor
- closed the 2nd eclipse
- started the 2nd eclipse again (shift alt x e)
- No errors are reported yet
- Edit the line that references A (I deleted the A and typed an A again. So effectively no change) and then save (ctrl-s) and the error will appear
- loading a.mydsl and making no real change like above (add a space and remove it) follwed by a save makes the error go away again...

With this more detailed discription can you reproduce this behaviour? Or should I consider that something is wrong with my installation?

Kind regards,

Louis
Re: composition of one model in xtext from many different parts [message #554334 is a reply to message #554333] Sat, 21 August 2010 19:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Louis,

maybe this might be a Linux / 64bit related problem. feel free to file a bug with your description.

have you tried to put the models into the source folder of
a java project? - then another mechanism will be used that might work.

Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #554335 is a reply to message #554334] Sat, 21 August 2010 19:26 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hi Christian,

thats it. Creating a java project and putting the file sin there changed the behaviour. How unexpected. Can you refer to where this is described or shed some light on this? But more importantly should I always create a java project when testing xtext editors? Or is it something special for this project/way of starting the editor...

Kind regards,

Louis
Re: composition of one model in xtext from many different parts [message #554337 is a reply to message #554335] Sat, 21 August 2010 19:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

you find this in the docs in the section Scoping. especially JDT-Based Container Manager vs. Eclipse Project-Based Containers

~Christian


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

[Updated on: Sat, 21 August 2010 19:38]

Report message to a moderator

Re: composition of one model in xtext from many different parts [message #554338 is a reply to message #554337] Sat, 21 August 2010 19:42 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hi Christian,

I remember reading that and couldn't figure out what its relevance was Confused
Still lots to learn Smile

Thanks for you help. I have some reading and experimenting to do.

/Louis
Re: composition of one model in xtext from many different parts [message #554438 is a reply to message #554338] Mon, 23 August 2010 07:34 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Yeah, lots of difficult corners in the Xtext framework Very Happy

Anyway, in general, the generated Xtext editors might exhibit some default behavior which isn't present in the standalone situation (e.g., for the generator) so as soon as you want to have a generator you'll usually have to re-implement that default behavior. That also leaves the opportunity to exert some more control over the specific behavior, also for the editor.

Scoping and especially using global or local scoping to do imports, is an important subject to understand.


Re: composition of one model in xtext from many different parts [message #555820 is a reply to message #554438] Sun, 29 August 2010 06:33 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello,

so after doing more reading and experimenting, finally I figured out that if you use the importURI mechanism in the grammar, you also need to change two fragments in the mwe2 file of the language generator.

fragment = scoping.ImportURIScopingFragment {}
fragment = exporting.SimpleNamesFragment {}

Is this correct or should there be more changes?

Now I'm trying to get the same behavior from the Generator project. I'm hoping that there is already a component that could be configured in the mwe2 file of the generator that takes care of the importURI model elements (and possibly the ones found in the imported model) and create a "complete" model in output slot that can be used for M2M or M2T. Does such a component exist already or must it be created?

Kind regards,

Louis
Re: composition of one model in xtext from many different parts [message #555822 is a reply to message #555820] Sun, 29 August 2010 08:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Louis,

to get imports running this should be ok but note:
this is an import for referencing elements of the imported models but not for d somthing like a (recursive) include to get a complete model - the may even be of another metamodel. So there might be no reader component that has the feature you want to have (starting with a "root" model and recusively scanning all imported models). The org.eclipse.xtext.mwe.Reader of course works - but this takes not explicit root model.

Regards
Christian


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

[Updated on: Sun, 29 August 2010 08:50]

Report message to a moderator

Re: composition of one model in xtext from many different parts [message #555826 is a reply to message #555822] Sun, 29 August 2010 09:20 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Christian,

It is starting to make sense to me. Some different questions come to mind:

1- When a refernce is made to an element from an imported model, how should the referenced object be found?
2- What should be done to get the imported Model parts inside one root model.
3- At workflow level, there is a slot containing the model. When using importURI, does that mean that the slot contains multiple instances of a model (each imported file one model instance).

Thanks for your answers.

Louis
Re: composition of one model in xtext from many different parts [message #555828 is a reply to message #555826] Sun, 29 August 2010 09:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

(1) This is done by Xtext
(2.a) Reference them withing the root model and follow the references
(2.b) calculate the "semantic overall model" either in the generator or a own reader component using org.eclipse.xtext.EcoreUtil2.getResource
(3) the slot is just a key value pair in a map -> so what you put there depends from what you reader does.


To make this a bit more clear here what the default xtext reader does:

It takes all models on the path (maybe fitered) reads then and than puts all resources of certain type as a list to the slot - so it reads multiple models.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #555831 is a reply to message #555828] Sun, 29 August 2010 10:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,
a digged a bit more and may found a solution:

starting with a grammar like this:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	"model" name=ID "{"
    imports+=Import*
	greetings+=Greeting*
	"}";
	
Import:
	'import' importURI=STRING
;	
	
Greeting:
	'Hello' name=ID ('from' from=[Greeting])?'!';



and model files

a.mydsl
model a {
Hello A!
}


b.mydsl
model b {
import "src/model/a.mydsl"
Hello B!
}


(with be a root file)

and using the uri based reader:

module workflow.MyDslGenerator

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

var targetDir = "src-gen"
var fileEncoding = "Cp1252"

Workflow {
	
	component = org.eclipse.xtext.mwe.UriBasedReader {
		uri = "src/model/b.mydsl"
		register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
				
		load = {
			slot = "models"
			type = "Model"
		}
	}

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


and this extensions

Set[Greeting] getAllGreetings(Model m) :
	JAVA templates.JavaExtensions.getAllGreetings(org.xtext.example.mydsl.myDsl.Model);


package templates;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.EcoreUtil2;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Import;
import org.xtext.example.mydsl.myDsl.Model;

public class JavaExtensions {
	
	public static Set<Greeting> getAllGreetings(Model m) {
		Resource context = m.eResource();
		List<String> uris = new ArrayList<String>();
		for (Import i : m.getImports()) {
			uris.add(i.getImportURI());
		}
		Set<Greeting> result = new HashSet<Greeting>();
			for (String uri : uris) {
			Resource resource = EcoreUtil2.getResource(context, uri);
			for (EObject o : resource.getContents()) {
				result.addAll(EcoreUtil2.getAllContentsOfType(o, Greeting.class));
			}
		}
		result.addAll(m.getGreetings());
		return result;
		
	}

}



and this template

«IMPORT org::xtext::example::mydsl::myDsl»

«EXTENSION templates::Extensions»

«DEFINE main FOR Model-»
«FOREACH this.getAllGreetings() AS g»
«FILE g.name+".txt"»
«ENDFILE»
«ENDFOREACH»
«ENDDEFINE»


you get A.txt and B.txt generated what is what you want, isn't it?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #555835 is a reply to message #555831] Sun, 29 August 2010 11:46 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Christian,

thanks for this example. I will try it.

Can you say something about how often this set will be constructed? or is it cached?

/Louis
Re: composition of one model in xtext from many different parts [message #555836 is a reply to message #555835] Sun, 29 August 2010 11:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

as it it will not be cached and be produced once i think - depending on what you do in the generator. But using a cached extension should not be a problem - or you pack this i a own workflow component - when it will be called once for sure.

Regards
Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #555840 is a reply to message #555836] Sun, 29 August 2010 12:56 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hi Christian,

thats all clear. I will start using this mechanism.

/Louis
Re: composition of one model in xtext from many different parts [message #555862 is a reply to message #555840] Sun, 29 August 2010 18:22 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Christian,

after some experimenting I have the following remarks:
1- Naming the model instance at the root looks a bit awkward in the greetings example and I think it is a bit to much to make it mandatory for my target language.
2- When importing, the imported models are added to the model slot, so the expansion is called for each of these models. Only one of these models is te true root model. I don't know how to know which of the invocations of the template is related to my root model.
3- When nesting imports (a imports b, b imports c and c defines d) a doesn't know about d in the validator. having a imprt b also solves that, but makes the import loose its strength somewhat.

Learned a lot today Very Happy

Thanks for your help!

/Louis
Re: composition of one model in xtext from many different parts [message #555863 is a reply to message #555862] Sun, 29 August 2010 18:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Louis,

(1) i did this to perevent the sample from beeing ugly and i was to lazy to configure that elements can be exported without a name - else the uglyness - search the forum on how to read a Element as root that has no name.

(2) i dont understand this - using the workflow as i defined (with the uri based reader) it adds only the root of the b.mydsl to the slot - i guess you used the std reader.

(3) if you want to have explicit imports you get explicit imports - so why would one use d in a and not importing c? - this makes no sence - imüports are imports and no includes - if this question is not about the model but about generation
take the sample code i provided and write the transitive stuff yourself.

~Christian


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

[Updated on: Sun, 29 August 2010 18:45]

Report message to a moderator

Re: composition of one model in xtext from many different parts [message #555866 is a reply to message #555863] Sun, 29 August 2010 19:35 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hi Christian,

1) I will do that, I'm searching the forum for related topics quite frequently.
2) No sorry, I checked again, the UriBasedReader is in the workflow of the generator project. For me the template gets invoked twice when I have an import that holds an object that gets referenced from the root model (where the import is done).
3) It's not a problem it's just not what I expected. Probably it's because I'm more experienced with C and C++.

Finally let me say that I'm spending quite some time in getting to understand what xText can and cannot do and appreciate very much the support I receive through the forum. Many thanks to everybody who is giving *free* support, especially to newbies, here!


Regards,

Louis
Re: composition of one model in xtext from many different parts [message #555867 is a reply to message #555866] Sun, 29 August 2010 19:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

(2) Yes this is why i take Model as root and not Greeting - using the set duplicate greetings will be filtered out - the reader reads greetings - and the generator erty definition cals all greetings - and calls the greeting stuff for each (once), If you mix that you have workaround the problem somehow.

(3) Hope you will give something back when beeing more experienced with Xtext and help other newbies.

~Christian



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: composition of one model in xtext from many different parts [message #555869 is a reply to message #555867] Sun, 29 August 2010 20:04 Go to previous messageGo to next message
Louis Stamsma is currently offline Louis StamsmaFriend
Messages: 20
Registered: July 2009
Junior Member
Hello Christian,

2) My plan is to make a generator that also performs a M2M transformation. I also want to get some experience with that. So I want to run the next transformation on the "complete" root model only. I'll try and figure it out...

4) Thats the plan, answering some forum questions in the future.

A last question if I may: Where would I find the documentation of the UriBasedReader?

Thanks again,

Louis
Re: composition of one model in xtext from many different parts [message #555872 is a reply to message #555869] Sun, 29 August 2010 20:23 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

(2) for this kind of m2m i would write a custom workflow component - or would at least use xtend. (XtendComponent) - instead of a xpand generator.

i fear there is not docs on UriBasedReader.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtend/Xpand nature and content assist?
Next Topic:Improving xtext building
Goto Forum:
  


Current Time: Thu Apr 25 16:55:47 GMT 2024

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

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

Back to the top