Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] How to test for empty/null reference?
[Acceleo] How to test for empty/null reference? [message #639585] Wed, 17 November 2010 03:08 Go to next message
Glenview Jeff is currently offline Glenview JeffFriend
Messages: 79
Registered: September 2010
Member
This should be a really easy question to answer, but I can't figure it out from the Acceleo manual.

I can't figure out how to test for a null reference so I can skip it. I have some code that iterates through a structure. The top level type is called an Activity and it contains a reference 0..1 of menu. Not all activities have menus in the model and thus I get the error I pasted below the code.

Thanks for your help!

line 8: [for (anActivity : Activity | myActivities.activities)]
line 9: 	[for (aMenu : Menu | anActivity.menu)]


Invalid loop iteration at line 9 in Module Strings for block for (anActivity.menu.items). Last recorded value of self was flashcards.activities.impl.AnyMemoImpl@14c816b (name: AnyMemo).

Re: [Acceleo] How to test for empty/null reference? [message #639588 is a reply to message #639585] Wed, 17 November 2010 04:08 Go to previous messageGo to next message
Glenview Jeff is currently offline Glenview JeffFriend
Messages: 79
Registered: September 2010
Member
Update: I noticed that if I change the Activity to contain 0..* menus then the for loops work fine. But I want to limit there to be only 0 or 1 menus. Is this an Acceleo bug or am I misunderstanding something?
Re: [Acceleo] How to test for empty/null reference? [message #639611 is a reply to message #639588] Wed, 17 November 2010 07:59 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------090905000309020208070602
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Hi Jeff,

This will work well if you set it to be 0..* because EMF will never
return "null" in such cases : it is a collection, whether it is empty or
not. In the case of "0..1" multiplicity, you have either "null" or an
object.

The problem you have is that you tried to do a "for" loop on such a 0..1
reference. Acceleo accepts "for" on an object which is not a Collection
(it will simply iterate once), but it will refuse iterating on an Object
which is either "null" or "invalid"... which is what you get, and will
not be changed.

I don't understand why you are using a "for" loop at this point. If the
reference is 0..1, you either have something or not. Why not use an "if"?

[for (anActivity : Activity | myActivities.activities)]
[if (not anActivity.oclIsUndefined())]

Laurent Goubet
Obeo

On 17/11/2010 05:08, glenviewjeff wrote:
> Update: I noticed that if I change the Activity to contain 0..* menus
> then the for loops work fine. But I want to limit there to be only 0 or
> 1 menus. Is this an Acceleo bug or am I misunderstanding something?


--------------090905000309020208070602
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"

YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyIj5PYmVvPC9hPg0KZW1haWw7 aW50ZXJuZXQ6
bGF1cmVudC5nb3ViZXRAb2Jlby5mcg0KdXJsOmh0dHA6Ly93d3cub2Jlby5m cg0KdmVyc2lv
bjoyLjENCmVuZDp2Y2FyZA0KDQo=
--------------090905000309020208070602--
Re: [Acceleo] How to test for empty/null reference? [message #639751 is a reply to message #639611] Wed, 17 November 2010 16:39 Go to previous messageGo to next message
Glenview Jeff is currently offline Glenview JeffFriend
Messages: 79
Registered: September 2010
Member
Thanks so much for the prompt help Laurent. I had only used the for loop since I couldn't figure out the syntax to determine if a reference was null.

For future readers, the correct code for my example is:

	[if (not anActivity.menu.oclIsUndefined())]

Re: [Acceleo] How to test for empty/null reference? [message #639778 is a reply to message #639751] Wed, 17 November 2010 18:08 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

The not very obvious OCL idiom is

aVariable->notEmpty()

if aVariable is invalid result is invalid
else if aVariable is null result is false
else result is true
-- a real object

Regards

Ed Willink

On 17/11/2010 16:39, glenviewjeff wrote:
> Thanks so much for the prompt help Laurent. I had only used the for
> loop since I couldn't figure out the syntax to determine if a
> reference was null.
>
> For future readers, the correct code for my example is:
>
> [if (not anActivity.menu.oclIsUndefined())]
>
Re: [Acceleo] How to test for empty/null reference? [message #643185 is a reply to message #639778] Sun, 05 December 2010 20:02 Go to previous messageGo to next message
Glenview Jeff is currently offline Glenview JeffFriend
Messages: 79
Registered: September 2010
Member
Thanks very much Ed. Your method is far more readable. Javadoc or the equivalent for the methods auto-completion on variables would be really helpful.

Jeff
Re: [Acceleo] How to test for empty/null reference? [message #643294 is a reply to message #639585] Mon, 06 December 2010 12:12 Go to previous message
Stephane Begaudeau is currently offline Stephane BegaudeauFriend
Messages: 458
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi,

The documentation will be available in Acceleo 3.1.0 next year, you can see some screenshots here.

http://media.tumblr.com/tumblr_l6qfy1dzrd1qbxlji.png

http://media.tumblr.com/tumblr_l6qfyw1OJF1qbxlji.png

Stéphane Bégaudeau, Obeo
Previous Topic:[check] Problem with check under Helios
Next Topic:Acceleo Java Launcher
Goto Forum:
  


Current Time: Thu Apr 25 02:26:31 GMT 2024

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

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

Back to the top