[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.tmf] Re: [xtext] adding EOperations to the inferred ECore model

Hi Henrik,

You can use this example to get started. It adds the operations "void doFoo()" and "Bar getBar()" to the class "Foo". The method body simply delegates to the static methods "void my.company.FooHelper.doFoo(Foo)" and "Bar my.company.FooHelper.getBar(Foo)" which you then need to provide.

import xtext;
import ecore;

process(GeneratedMetamodel this) :
	process(ePackage)
;

process(EPackage this) :
	eClassifiers.typeSelect(EClass).process()
;

process(EClass this) :
switch (name) {
case "Foo": (addOperation("doFoo", null) -> addOperation("getBar", ePackage.getEClassifier("Bar")))
default: null
}
;


addOperation(EClass this, String name, EClassifier type) :
	let op  = newOperation(name, type) :
		newDelegatingBodyAnnotation(op)
;

create EOperation newOperation(EClass owner, String name, EClassifier type) :
	setName(name) -> setEType(type) -> owner.eOperations.add(this)
;

create EAnnotation newDelegatingBodyAnnotation(EOperation op) :
let d = new EStringToStringMapEntry :
setSource("http://www.eclipse.org/emf/2002/GenModel";) ->
d.setKey("body") ->
d.setValue((op.eType != null ? "return " : "") + "my.company." + op.eContainingClass.name + "Helper." + op.name + "(this);") ->
details.add(d) ->
op.eAnnotations.add(this)
;



Hope that helps,

--knut

Henrik Rentz-Reichert wrote:
Hi Moritz,

thanks for the hint.

Can you please give me a short example of how I can add an EOperation to
an inferred model element using the post processing hook?

Thanks,
Henrik

Moritz Eysholdt wrote:
Hi Henrik,

yes, there is a post processing hook which allows to customize the
inferred Ecore  model.

see:
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.xtext.doc/help/metamodelInference.html


regard, Moritz

Hi,

as discussed e.g. in
http://www.eclipse.org/newsportal/article.php?id=434&group=eclipse.modeling.tmf#434


there may be circumstances where it is desirable to extend the inferred ECore model.

One of the things one might wish to add are EOperations in EClasses.

Is there a way to do this without being overwritten by re-generation?

Thanks,
Henrik