Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtend] Setting a class type variable
[Xtend] Setting a class type variable [message #1736523] Thu, 30 June 2016 07:58 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?

[Updated on: Thu, 30 June 2016 08:04]

Report message to a moderator

Re: [Xtend] Setting a class type variable [message #1736524 is a reply to message #1736523] Thu, 30 June 2016 08:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14738
Registered: July 2009
Senior Member
Hi,

sounds like a usecase for IDerivedStateComputer

have a look at http://xtextcasts.org/episodes/18-model-optimization


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: [Xtend] Setting a class type variable [message #1736528 is a reply to message #1736524] Thu, 30 June 2016 08:30 Go to previous messageGo to next message
chris yo is currently offline chris yoFriend
Messages: 146
Registered: February 2013
Senior Member
Hi Christian,
Thanks for the tip!
One of the solution I thought of is to make a derived feature in xcore so I will have a minimal code change.

I modified the class Directory like this:
class Directory {
String name
String address
derived Information xInfo get {
 MySingleton::instance.getInfo(name)
}
}


But I am getting an error saying that "The generic attribute type must not refer to a class".
Does this mean that its not possible to do it as a derived feature?
I am also thinking of using the containment/container reference but the xcore wiki did not state how to set it. I hope you have some idea how..?

[Updated on: Thu, 30 June 2016 08:31]

Report message to a moderator

Re: [Xtend] Setting a class type variable [message #1736534 is a reply to message #1736528] Thu, 30 June 2016 08:59 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14738
Registered: July 2009
Senior Member
xcore questions are better asked in the emf forum.

derived is for attributes only maybe ?!?

my derived is a xtext derived and not a xcore derived.


another problem is:

you cannot have 2 parents.
why then this singleton thingy ?!?

what does
MySingleton::instance.getInfo(name)
do ?!?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Previous Topic:Limit the feature scope of a type
Next Topic:xtext A conflict occurred for ALT+CTRL+H error
Goto Forum:
  


Current Time: Fri Dec 13 15:22:16 GMT 2024

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

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

Back to the top