Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Acceleo (Cannot access to the my matamodele attributes , references .. )
Acceleo [message #1829344] Wed, 01 July 2020 16:03 Go to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hello all,

I am developing a M2T generator in Acceleo (in Obeo Designer).
I have created my metamodel based on *.ecore (pfe.ecore ) and then I have created an instance model in runtime (MyModels.pfe) using Sirius.

Now I am trying to generate the java code of MyModels.pfe using Accelo -the class root is InterationSpatiale. -check the attachment-



I want to generate a *.java for each of my classes in the diagram
here is my generate.mtl file

[comment encoding = UTF-8 /]
[module generate('http://www.example.org/pfe','http://www.eclipse.org/emf/2002/Ecore')/]

[template public generateElement(aEClass : InteractionSpatiale)]
[comment @main/]
[file (aEClass.name.concat('.java'), false, 'UTF-8')]
public class [aEClass.name/] {

[for (aEAttribute : ecore :: EAttribute | eEClass.getAllAttributes())]
    [aEAttribute.eType.instanceClassName/] [aEAttribute.name/];
[/for]
}
[/file]

[/template]


This is the error message I got
Cannot find operation (getAllAttributes()) for the type (InteractionSpatiale)

I tried "ownedAttribute" and "eAllAttributes" instead of getAllAttributes but the same error.


Ps: the same happens when I want to get access to the references.

Any help is much appreciated.

Regards.
  • Attachment: workspace.PNG
    (Size: 56.94KB, Downloaded 107 times)
Re: Acceleo [message #1829349 is a reply to message #1829344] Wed, 01 July 2020 17:54 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

I would expect aEClass tobe an EClass so that the operations are present.

But since you seem to be attempting to rewrite the EMF JET templates that have been rewritten in Acceleo already, I suggest that you make very sure that you do not want to use the JET templates that are supported and well tested, else learn from the stale Acceleo variants.

Regards

Ed Willink

[Updated on: Wed, 01 July 2020 19:10]

Report message to a moderator

Re: Acceleo [message #1829379 is a reply to message #1829349] Thu, 02 July 2020 09:40 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi Ed Willink,

Thank you for the fast replay !

aEClass : is the variable I am using to represent the EClass.

I am not sure if I get you well about EMF JET , I am trying to write my Acceleo generator to generate the java code of (MyModels.pfe ) , at the first attempt I am not trying to use EMF JET. But I would try it if they are supported and well tested !

Could you give me more explanation about this , it's seems very helpful in my case [what I have notices that "getAllAttributes()" , "ownedAttribute" and "eAllAttributes" are not documented in the AQL official doc .
Quote:
But since you seem to be attempting to rewrite the EMF JET templates that have been rewritten in Acceleo already, I suggest that you make very sure that you do not want to use the JET templates that are supported and well tested, else learn from the stale Acceleo variants.


Best regards.
Selma
Re: Acceleo [message #1829382 is a reply to message #1829379] Thu, 02 July 2020 11:13 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

I suggest you study the normal EMF facilities for generating Java from models before you re-invent the wheel.

The AQL documentation is not appropriate. It is a spin off that avoids Acceleo's re-use of OCL, but if you are generating code via Acceleo, an extended OCL is what you're using.

If you are after generation of non-trivial Java code, you may find the OCL to Java generator helpful.

Ecore, possibly enriched with expressions as Xcore/OCLinEcore can surely generate what you need. Once you have assessed what is already available you can experiment with something more optimized for your application, but for now developing an Acceleo Java generator seems like a premature optimization.

Regards

Ed Willink
Re: Acceleo [message #1829538 is a reply to message #1829382] Mon, 06 July 2020 12:27 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi Ed Willink, Thank you for your replay! I have been doing some research and I figured out that I should add .eCLass() just before .eAllAttributes (myinteractionspatiale.eClass().eAllAttributes ) . Now following that I have a related concern :

I am trying to generate the java code of MyModels.pfe using Accelo -the class root is InterationSpatiale.

I want to generate a *.java for each of my classes in the diagram here is my generate.mtl file

[comment encoding = UTF-8 /]
[module generate('http://www.example.org/pfe')/]
[template public generateElement(myinteractionspatiale : 
InteractionSpatiale)]
[comment @main/]
[file (myinteractionspatiale.name.concat('.java'), false, 'UTF-8')]

public class [myinteractionspatiale.name/] {

[for (aEAttribute : ecore :: EAttribute | 
 myinteractionspatiale.eClass().eAllAttributes)]
   [aEAttribute.eType.instanceClassName/] [aEAttribute.name/];
[/for]
[for (aEReference : EReference 
|myinteractionspatiale.eClass().eAllReferences)]
  [aEReference.eReferenceType.name/] [aEReference.name/];
[/for]
[for (aEAttribute : EAttribute | 
myinteractionspatiale.eClass().eAllAttributes)]
  public [aEAttribute.eType.instanceClassName/] 
  get[aEAttribute.name.toUpperFirst()/] () {
    return this.[aEAttribute.name/];
  }
 [/for]
 [for (aEAttribute : EAttribute | 
 myinteractionspatiale.eClass().eAllAttributes)]
   public void set[aEAttribute.name.toUpperFirst()/] 
   ([aEAttribute.eType.instanceClassName/] [aEAttribute.name/]) {
    this.[aEAttribute.name/] = [aEAttribute.name/];
  }
 [/for]

 [for (aEReference : EReference | 
 myinteractionspatiale.eClass().eAllReferences)]
   public [aEReference.eReferenceType.name/] 
  get[aEReference.name.toUpperFirst()/] () {
    return this.[aEReference.name/];
  }
 [/for]

 [for (aEReference : EReference | 
  myinteractionspatiale.eClass().eAllReferences)]
  public void set[aEReference.name.toUpperFirst()/] 
  ([aEReference.eReferenceType.name/] [aEReference.name/]) {
    this.[aEReference.name/] = [aEReference.name/];}
 [/for]

[for (aEOperation : EOperation | 
myinteractionspatiale.eClass().eAllOperations)]
  public [aEOperation.eType.instanceClassName/] [aEOperation.name/] () {
    }
[/for]

}
 [/file]
[/template]


the generated file I got is App.java is

public class App {

java.lang.String name;
Entite entite;
Evenement evenement;
SystemeCoordonnees systemecoordonnees;
TacheSysteme tachesysteme;
RelationSpatiale relationspatiale;
InteractionSpatiale interactionspatiale;
public java.lang.String getName () {
    return this.name;
}
public void setName (java.lang.String name) {
    this.name = name;
}

public Entite getEntite () {
    return this.entite;
}
public Evenement getEvenement () {
    return this.evenement;
}
public SystemeCoordonnees getSystemecoordonnees () {
    return this.systemecoordonnees;
}
public TacheSysteme getTachesysteme () {
    return this.tachesysteme;
}
public RelationSpatiale getRelationspatiale () {
    return this.relationspatiale;
}
public InteractionSpatiale getInteractionspatiale () {
    return this.interactionspatiale;
}

public void setEntite (Entite entite) {
    this.entite = entite;
}
public void setEvenement (Evenement evenement) {
    this.evenement = evenement;
}
public void setSystemecoordonnees (SystemeCoordonnees systemecoordonnees) 
{
    this.systemecoordonnees = systemecoordonnees;
}
public void setTachesysteme (TacheSysteme tachesysteme) {
    this.tachesysteme = tachesysteme;
}
public void setRelationspatiale (RelationSpatiale relationspatiale) {
    this.relationspatiale = relationspatiale;
}
public void setInteractionspatiale (InteractionSpatiale 
interactionspatiale) {
    this.interactionspatiale = interactionspatiale;
}
}


But what I really wanted is to have a file for each class of the model MyModels.pfe not just the java file of the root class.

My concern is :

1. how to iterate and have a file for each class with its attributes and their values[EFixe.java, EMobile.java ...]
2. I cannot access the values of the attributes: I tried
 [aEAttribute->any(true).value /] 

but it doesn't work

I really appreciate any sort of help.

[Updated on: Mon, 06 July 2020 17:43]

Report message to a moderator

Re: Acceleo [message #1829553 is a reply to message #1829538] Mon, 06 July 2020 18:26 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Have a look at the file construct.

aEAttribute...value looks like you are trying a reflective access, which OCL (Acceleo) does not support. Are you sure that's what you really need? While reflection is notionally necessary, I have yet to see a real requirement. You might try eGet().

Regards

Ed Willink

Re: Acceleo [message #1829556 is a reply to message #1829553] Mon, 06 July 2020 18:47 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi ,

Thank you for the fast replay ,

here is my MyModels.pfe in XML

<?xml version="1.0" encoding="UTF-8"?>
<pfe:InteractionSpatiale xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pfe="http://www.example.org/pfe" name="App">
  <entite xsi:type="pfe:EFixe" Applied="//@relationspatiale.0" Position_Absolue="20" Orientation_Absolue="12" evenement="//@evenement.0"/>
  <entite xsi:type="pfe:EMobile" Applied="//@relationspatiale.0" Position_Absolue="30" Orientation_Absolue="20" evenement="//@evenement.0" VitesseDeplacementAbs="48" VitesseRotationAbs="70" AccelerationDeplacementAbs="12" AccelerationRotationAbs="11"/>
  <evenement xsi:type="pfe:EvenEMobileEFixe" name="Separate" Cause="//@tachesysteme.0"/>
  <tachesysteme name="Play"/>
  <relationspatiale PositionRelative="14" OrientationRelative="14" DistanceRelative="12" evenement="//@evenement.0"/>
</pfe:InteractionSpatiale>


So bsicly, I want the java code of this XML , and for each class it has attributes with values : example " Position_Absolue="20" in the class : EFixe

I want something like this in my generated java code
 
public class EFixe {
int Position_Absolue =20 ;


I have tried eGet to extract the value of the attribute but in vain

Regards.
Re: Acceleo [message #1829595 is a reply to message #1829556] Tue, 07 July 2020 13:02 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

M2T is normally performed on an instance of a user metamodel so that user metamodel expressions such as anInteractionSpatiale.name can be used. This typically requires a rule for each user class that enumerates the known class-specific attributes.

You appear to be attempting to transform at the XML level so that a generic class rule can iterate all attributes, but which requires you to use reflection to access the value of the discovered attribute.

If you really want to transform at the XML level, I suggest you specify a transformation using the http://www.eclipse.org/qvt/2018/XML metamodel that you may obtain by installing at least the org.eclipse.qvtd.xml plugin from the Eclipse QVTd project. To ensure that the XMLModelResource is used to load you should either change your input file name to *.xmlmodel and/or bind the XMLmodelResourceFactoryImpl to your file extension in the Acceleo startup. Warning: http://www.eclipse.org/qvt/2018/XML has been successfully used with QVTo; AFAIAA it has never been used with Acceleo.

Regards

Ed Willink

Re: Acceleo [message #1829614 is a reply to message #1829595] Tue, 07 July 2020 16:08 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi Ed Willink,

Thank you so much for your response! As you have mentioned earlier, about the use of eGet(). I realized that I badly used it!
So, I used it like this EObject#eGet(EStructuralFeature) and it works perfectly

  = [myInteractionSpatiale.eGet(aEAttribute)/] 

I got
public class EFixe{
   int Position_Absolue = 20;
   int Orientation_Absolue = 12; }


Sir, the part you talked about XML :
1. Do you think that there is a better way than transforming at the XML level?
2. About the process of the plugin, I will give it a try immediately.

Thank you!

Regards.

[Updated on: Tue, 07 July 2020 16:09]

Report message to a moderator

Re: Acceleo [message #1829619 is a reply to message #1829614] Tue, 07 July 2020 16:50 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

In principle using an XML metamodel is more elegant/correct, but the http://www.eclipse.org/qvt/2018/XML metamodel is just a better attempt at remedying the lack of a standard XML metamodel.

In contrast the use of eGet is thoroughly non-standard model-wise, but pretty standard EMF-wise and now it works for you.

I'd be inclined to stick with eGet for now and move onto your next problem. (Unless you are an academic researcher keen to explore the alternatives.)

Regards

Ed Willink
Re: Acceleo [message #1829621 is a reply to message #1829619] Tue, 07 July 2020 17:33 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi,

I perfectly understand what you mentioned above, I will go with that !
Exactly! I will be inclined to stick with eGet and continue fixing my problems (no I am not ! I am building my final project for a bachelor's degree).

Thank you for your time.

Regards.



Re: Acceleo [message #1829644 is a reply to message #1829621] Wed, 08 July 2020 08:35 Go to previous messageGo to next message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Hi,
I have created a template for each class in different module files and I tried to call them in the main *.mtl module after importing all the modules.
In the main template that takes an InteractionSpatiale (root) as input and a java file as output: I called template [generateEntiteFixe (p )/] but the problem is the parameter p to pass as input to the template. I tried to extract it from the root (myInteractionSpatiale )
 [ myInteractionSpatiale.eContents().eClass().name  ]

I got this in the java file :
EFixeEMobileEvenEMobileEFixeTacheSystemeRelationSpatiale

which is the name of all my classes concatinated . As I need the name of each class to pass it as a parameter to different templates , I tried to extract class by class :
 [myInteractionSpatiale.eContents().eClass().name->any(str | str = 'EFixe')/] 

I got
 EFixe

So to call the template :
[generateEntiteFixe (myInteractionSpatiale.eContents().eClass().name->any(str | str = 'EFixe')/] 


but it doesn't execute or generate the code of the template ' generateEntiteFixe ' ;
I wan to extract each class of my tree model where the root is [myInteractionSpatiale : InteractionSpatiale ] in order to call my templates in the main module.

any help in what's the problem or how to do it correctly is much appreciated.

Regards.

[Updated on: Wed, 08 July 2020 08:45]

Report message to a moderator

Re: Acceleo [message #1829655 is a reply to message #1829644] Wed, 08 July 2020 09:49 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

The forum is not a first resort every time you have a programming problem. You should attempt to do some debugging before asking for free help.

Regards

Ed Willink
Re: Acceleo [message #1829662 is a reply to message #1829655] Wed, 08 July 2020 11:44 Go to previous message
Sel ma is currently offline Sel maFriend
Messages: 16
Registered: June 2020
Junior Member
Well understood Sir! apologies
Thank you

Regards.
Previous Topic:- Acceleo - Check consistency - multi domain
Next Topic:Best Model-to-text transformation tool
Goto Forum:
  


Current Time: Fri Mar 29 07:26:22 GMT 2024

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

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

Back to the top