Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » ATL » How to add new model elements in refining mode
How to add new model elements in refining mode [message #985534] Thu, 15 November 2012 08:28 Go to next message
Andreas Brieg is currently offline Andreas Brieg
Messages: 48
Registered: November 2012
Member
Since called rules are not yet supported in refining mode, I'm struggling a bit on how to add new elements to the model I'm refining. Specifically I'm talking about the MoDisco Java model http://www.eclipse.org/MoDisco/Java/0.2.incubation/java and I want to add Annotations.

I currently know of two such possibilities. The first one is creating a rule whose input element is of a different type than the output element. For example, if I want to create a Annotation for a method, I could write:

rule addAnnotationToMethod {
	from
		method : Java!MethodDeclaration
	to 
		methodAnnotation : Java!Annotation (
			-- fill in the attributes and references
		)
}


However I'm unable to use this, because the methodAnnotation that would be created by this rule is a dangling object. The problem is that the model is missing the inverse relationship of the association from the method declaration to its declared annotations. So even if the annotation would be created by this rule, the method wouldn't be annotated.

The second possibility, which I'm currently using, makes use of newInstance:

rule addNotNullAnnotation {
	from
		parameterDeclaration: Java!SingleVariableDeclaration 
	to
		modifiedDeclaration: Java!SingleVariableDeclaration (
			annotations <- parameterDeclaration.annotations.
					including(parameterDeclaration.notNullAnnotation())
		)
}

helper context Java!ASTNode def: notNullAnnotation(): Java!Annotation =
	Java!Annotation.newInstance().refSetValue('type', self.notNullAnnotationAccess()).
			refSetValue('originalCompilationUnit', self.originalCompilationUnit).refSetValue('values', Sequence{});

helper context Java!ASTNode def: notNullAnnotationAccess(): Java!TypeAccess =
	Java!TypeAccess.newInstance().refSetValue('type', thisModule.notNullAnnotationType).
			refSetValue('originalCompilationUnit', self.originalCompilationUnit);

helper def: notNullAnnotationType: Java!Type =
	Java!AnnotationTypeDeclaration.allInstances() -> select(e | e.qualifiedName() =
			'javax.validation.constraints.NotNull').first();


However with this method I'm not sure if this will produce valid results in every case. For example what happens, if I will call
Java!Annotation.allInstances()
. Will the returned sequence also contain the elements that have been created by applying rule addNotNullAnnotation so far? If yes, than results would depend on the execution order of rules, which is undefined behaviour.
Another drawback of this method is that, I'm unable to profile, because the ATL profiler uses toString of model elements. But after calling Java!Annotation.newInstance() the instance isn't fully initialized and the toString method invocation fails with a NPE.

So how should I add new elements in refining mode?
Re: How to add new model elements in refining mode [message #985912 is a reply to message #985534] Fri, 16 November 2012 11:31 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 588
Registered: July 2009
Senior Member
Hello,

Have you tried the following:
rule addNotNullAnnotation {
	from
		parameterDeclaration: Java!SingleVariableDeclaration 
	to
		modifiedDeclaration: Java!SingleVariableDeclaration (
			annotations <- parameterDeclaration.annotations->including(newAnnotation)
		),
		newAnnotation: Java!Annotation (
			-- fill in the attributes and references
		)
}

Using imperative calls to methods such as newInstance is not recommended in ATL, as element creation is ensured by the declarative rules.

Hugo


------------------------------------------
Hugo Bruneliere - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: How to add new model elements in refining mode [message #986155 is a reply to message #985912] Mon, 19 November 2012 03:43 Go to previous message
Andreas Brieg is currently offline Andreas Brieg
Messages: 48
Registered: November 2012
Member
Thanks, works great. I guess that the whole sense of having multiple elements in the to clause wasn't clear to me. Now it is, I think.
Previous Topic:ResolveTemp in Lazy Rule relating to another Lazy Rule
Next Topic:How to mix in model elements in refining mode
Goto Forum:
  


Current Time: Thu Apr 17 14:36:22 EDT 2014

Powered by FUDForum. Page generated in 0.01618 seconds