Skip to main content



      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 05:38 Go to next message
Eclipse UserFriend
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 07:41 Go to previous messageGo to next message
Eclipse UserFriend
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?
>
Re: [xcore] How to set a class type variable [message #1736641 is a reply to message #1736571] Thu, 30 June 2016 14:13 Go to previous messageGo to next message
Eclipse UserFriend
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 17:49 Go to previous messageGo to next message
Eclipse UserFriend
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?
Re: [xcore] How to set a class type variable [message #1736665 is a reply to message #1736652] Thu, 30 June 2016 22:52 Go to previous messageGo to next message
Eclipse UserFriend
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 08:00 Go to previous message
Eclipse UserFriend
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.
Previous Topic:[xcore] derived feature does not allow class types
Next Topic:Reading the model file
Goto Forum:
  


Current Time: Tue Jul 15 04:41:31 EDT 2025

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

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

Back to the top