|
|
|
|
|
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 |
|
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
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 #555822 is a reply to message #555820] |
Sun, 29 August 2010 08:48 |
|
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 #555828 is a reply to message #555826] |
Sun, 29 August 2010 09:55 |
|
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 |
|
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
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 #555863 is a reply to message #555862] |
Sun, 29 August 2010 18:36 |
|
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 #555867 is a reply to message #555866] |
Sun, 29 August 2010 19:41 |
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05774 seconds