[ACCELEO 3] Retrieving the class from a stereotype [message #725736] |
Thu, 15 September 2011 16:12  |
bkhamphousone Messages: 4 Registered: September 2011 |
Junior Member |
|
|
Hi everybody.
I'm facing a problem and I can't find the solution.
Actually, we've got a UML2 Model as source of the generation. Everything works fine until I've to generate something awkward but very useful in my opinion.
There's a Stereotype (IHMMethod) which contains a property name bean. This property has to be a type of another Stereotype which name is DTO.
The stereotype DTO is applied to some classes and the stereotype IHMMethod is applied to Operation type. So we've got this :
Operation <<IHMMethod>> <-- bean <<DTO>> <-- every class stereotyped with DTO
When I access to an Operation type and when this last one got the stereotype IHMMethod, I retrieve the bean property. Unfortunately, it is an eClass and not an Element. So I'm not able to get the class contains in the bean property.
This works fine in Acceleo 2 but not in 3. Maybe it's a bug or maybe the way to get it changed.
Does someone got an idea of how to retrieve it?
Thank you very much.
Edit :
Here is the code to retrieve the object. Furthermore I've made a mistake : it's a DynamicEObjectImpl instead of EClass. I can retrieve the eClass with obj.eClass() but I don't know how to get the class set in the stereotype value.
public class testServices {
public String getBeanIHMMethodService(Operation anOperation) {
String result = null;
if (anOperation != null) {
try {
Stereotype st = anOperation
.getAppliedStereotype("myprofile::IHM Method");
if (st == null) {
result = "st null";
} else {
DynamicEObjectImpl obj = (DynamicEObjectImpl) anOperation.getValue(st, "bean");
if(obj != null){
result = obj.toString();
}else{
result = "nulllqsdaaa";
}
}
} catch (Exception e) {
result = e.toString();
}
} else {
result = "nullaaa";
}
return result;
}
}
I think it's uml2's bug but I'm not sure of it.
[Updated on: Fri, 16 September 2011 07:19] Report message to a moderator
|
|
|
|
|
Re: [ACCELEO 3] Retrieving the class from a stereotype [message #728116 is a reply to message #726786] |
Thu, 22 September 2011 14:54   |
bkhamphousone Messages: 4 Registered: September 2011 |
Junior Member |
|
|
Here is what I've got :
MTL file :
[**
* Return TRUE if an element has the stereotype aQualifiedStereotypeName. FALSE otherwise.
* @param anElement
* @param aQualifiedStereotypeName
*/]
[query public hasStereotype(anElement : Element, aQualifiedStereotypeName : String) : Boolean =
not anElement.getAppliedStereotype(aQualifiedStereotypeName).oclIsUndefined()
/]
[**
* Return the value of a stereotype.
* @param anElement
* @param aQualifiedStereotypeName
* @param anAttributeName
*/]
[query public getStereotypeValue(anElement : Element, aQualifiedStereotypeName : String, anAttributeName : String) : OclAny =
anElement.getValue(anElement.getAppliedStereotype(aQualifiedStereotypeName), anAttributeName)
/]
[template public test(aClass : Class)]
[comment @main/]
[if (aClass.hasStereotype('myProfile::stereotypePere'))]
[file (aClass.name, false, 'UTF-8')]
[aClass.getStereotypeValue('myProfile::stereotypePere', 'fils')/]
[/file]
[/if]
[/template]
With the uml2 export withoutStereotype, I've got as result :
org.eclipse.uml2.uml.internal.impl.ClassImpl@1ebb51b (name: uneClasse, visibility: <unset>) (isLeaf: false, isAbstract: false) (isActive: false)
With the uml2 export withStereotype, I've got as result :
org.eclipse.emf.ecore.impl.DynamicEObjectImpl@49a1c5 (eClass: org.eclipse.emf.ecore.impl.EClassImpl@1fe3aa4 (name: stereotypeFils) (instanceClassName: null) (abstract: false, interface: false))
The only difference between those two is the type of stereotypePere's attribute fils.
withoutStereotype and withStereotype are attached to the post.
The difference is this :
<eStructuralFeatures xmi:type="ecore:EReference" xmi:id="_hzut2eUpEeCCEKmxfMl80Q" name="fils" ordered="false" lowerBound="1">
<eType xmi:type="ecore:EClass" href="h t t p://www.eclipse.org/uml2/3.0.0/UML#//Class"/>
</eStructuralFeatures>
<eStructuralFeatures xmi:type="ecore:EReference" xmi:id="_dOK3-eUpEeCCEKmxfMl80Q" name="fils" ordered="false" lowerBound="1" eType="_dOK3--UpEeCCEKmxfMl80Q"/>
As we can see, the eStructuralFeatures know that the object is a Class but how can I get the eStructuralFeatures as a Class instead of DynamicObject?
[Updated on: Thu, 22 September 2011 15:29] Report message to a moderator
|
|
|
|
Re: [ACCELEO 3] Retrieving the class from a stereotype [message #729439 is a reply to message #728145] |
Mon, 26 September 2011 08:02  |
|
Glad you've found a working solution.
The problem here seems to be that your "stereotypeFils" is defined in a model which metamodel is located somewhere in the workspace. "DynamicEObject" are things we usually see when using "create dynamic instance" from an ecore editor. The "Element.getValue()" method is the only way I know of retrieving a stereotype value, and I have no idea as to how to call that method on a dynamic EObject.
Laurent Goubet
Obeo
|
|
|
Powered by
FUDForum. Page generated in 0.02375 seconds