Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [xcore] How to set a class type variable
[xcore] How to set a class type variable [message #1736545] Thu, 30 June 2016 09:38 Go to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
I have an xcore file with the following classes:
class Information {
String sName
String birthday
String age
}

class Directory {
String name
String address
contains Information xInfo
}


The variables in the class Directory is set by xtext thru the grammar configuration by the user, except for xInfo. I need to set the xInfo after all the grammar has been read.

The xInfo variable is set during validation. So, I have an xtend method that sets this.
public static val aDirs = new ArrayList<Directory>
public static val aInfo = new ArrayList<Information>

def public setInfo()
{
   for (m: aDirs)
   {
       val x = aInfo.findFirst[m.name.equals(SName)]
       if (x != null)
           m.xInfo = x //print also shows that this is correctly set.
   }
}

def public printInfo()
{
  for (m: aDirs)
  {
    if (m.xInfo == null) // ERROR: m.xInfo is null again!
    {
      // do nothing
    }
  }
}



With the two methods above, I called setInfo() and then printInfo(), but in printInfo(), xInfo variable is null again.

aDirs and aInfos are stored during transform.


Am I doing this right? How can we set a class type in xtend?
Re: [xcore] How to set a class type variable [message #1736571 is a reply to message #1736545] Thu, 30 June 2016 11:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Comments below.


On 30.06.2016 05:38, chris yo wrote:
> I have an xcore file with the following classes:
>
> class Information {
> String sName
> String birthday
> String age
> }
>
> class Directory {
> String name
> String address
> contains Information xInfo
> }
>
>
> The variables in the class Directory is set by xtext thru the grammar
> configuration by the user, except for xInfo. I need to set the xInfo
> after all the grammar has been read.
>
> The xInfo variable is set during validation. So, I have an xtend
> method that sets this.
>
> public static val aDirs = new ArrayList<Directory>
> public static val aInfo = new ArrayList<Information>
>
> def public setInfo()
> {
> for (m: aDirs)
> {
> val x = aInfo.findFirst[m.name.equals(SName)]
> if (x != null)
> m.xInfo = x //print also shows that this is correctly set.
Perhaps you're moving the x instance from one containment reference to
another, i.e., removing it from wherever it's currently contained.
> }
> }
>
> def public printInfo()
> {
> for (m: aDirs)
> {
> if (m.xInfo == null) // ERROR: m.xInfo is null again!
> {
> // do nothing
> }
> }
> }
>
>
>
> With the two methods above, I called setInfo() and then printInfo(),
> but in printInfo(), xInfo variable is null again.
>
> aDirs and aInfos are stored during transform.
>
>
> Am I doing this right? How can we set a class type in xtend?
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [xcore] How to set a class type variable [message #1736641 is a reply to message #1736571] Thu, 30 June 2016 18:13 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hi Ed,

Hmm.. You may be right. May I know how to properly implement it if I am moving one containment reference to another?
Do I need to do something before I can set the variable?
Re: [xcore] How to set a class type variable [message #1736652 is a reply to message #1736641] Thu, 30 June 2016 21:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Each containment reference needs it's own copy (EcoreUtil.copy), or it
shouldn't be containment reference but rather refer to an instance
contained elsewhere.


On 30.06.2016 14:13, chris yo wrote:
> Hi Ed,
>
> Hmm.. You may be right. May I know how to properly implement it if I
> am moving one containment reference to another?
> Do I need to do something before I can set the variable?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [xcore] How to set a class type variable [message #1736665 is a reply to message #1736652] Fri, 01 July 2016 02:52 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hi Ed,

I tried to change it to a reference. So my xcore is now like this:

class Information {
String sName
String birthday
String age
}

class Directory {
String name
String address
refers Information xInfo
}


The implementation code stays the same. But I am still getting the same problem.
Does it still need its own copy in this case?
Re: [xcore] How to set a class type variable [message #1736740 is a reply to message #1736665] Fri, 01 July 2016 12:00 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Chris,

Comments below.


On 30.06.2016 22:52, chris yo wrote:
> Hi Ed,
>
> I tried to change it to a reference. So my xcore is now like this:
>
>
> class Information {
> String sName
> String birthday
> String age
> }
>
> class Directory {
> String name
> String address
> refers Information xInfo
> }
>
>
> The implementation code stays the same. But I am still getting the
> same problem.
It was never clear to me what your original problem was. I did explain
that validation is not the place to change your model.
> Does it still need its own copy in this case?
No, in this case the Information needs to be contained somewhere else.

In any case, you should set breakpoints and figure out what is going on.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[xcore] derived feature does not allow class types
Next Topic:Reading the model file
Goto Forum:
  


Current Time: Sat Apr 20 02:33:00 GMT 2024

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

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

Back to the top