Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Is it possible to change sum() behavior?
Is it possible to change sum() behavior? [message #38719] Wed, 26 September 2007 07:14 Go to next message
Hao Zhang is currently offline Hao ZhangFriend
Messages: 161
Registered: July 2009
Senior Member
Hi,

I found evaluating following expression would returns null:

Commodity.allInstances()->select(id='aaa').weight->sum()

if there is no commodity whose id is "aaa". I'm wondering whether it is
possible to let above expression returns Integer 0 rather than null, so that
I can safely evaluate this expression:

Commodity.allInstances()->select(id='aaa').weight->sum() > 0

otherwise evaluating above expression would result an exception. Of course I
can write following expression to judge whether the set is empty, but it is
somehow too complex:

let coms:Set(Commodity) = Commodity.allInstances()->select(id='aaa') in
not coms->isEmpty() and coms.weight->sum()>0

Can I archive this by customizing the environment, or there is other
recommended way for this situation?

Regards,
Hao
Re: Is it possible to change sum() behavior? [message #38787 is a reply to message #38719] Wed, 26 September 2007 13:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Hao,

If you expect to find at most one commodity whose id is 'aaa', then you
might find the "any" iterator to be convenient:

Commodity.allInstances()->any(id='aaa').weight

which will give you a weight or else OclInvalid if there is no such
commodity. (OclInvalid returns true to an oclIsUndefined() test just as
null does).

However, that's all beside the point, because the sum of an empty collection
of integers is supposed to be zero, not null. The specification of this
operation makes that clear.

Would you mind raising a bug? That would be better than asking you to work
around this with a custom EvaluationEnvironment implementation, which would
be feasible but shouldn't be necessary.

Thanks,

Christian


Hao Zhang wrote:

> Hi,
>
> I found evaluating following expression would returns null:
>
> Commodity.allInstances()->select(id='aaa').weight->sum()
>
> if there is no commodity whose id is "aaa". I'm wondering whether it is
> possible to let above expression returns Integer 0 rather than null, so
> that I can safely evaluate this expression:
>
> Commodity.allInstances()->select(id='aaa').weight->sum() > 0
>
> otherwise evaluating above expression would result an exception. Of course
> I can write following expression to judge whether the set is empty, but it
> is somehow too complex:
>
> let coms:Set(Commodity) = Commodity.allInstances()->select(id='aaa')
> in
> not coms->isEmpty() and coms.weight->sum()>0
>
> Can I archive this by customizing the environment, or there is other
> recommended way for this situation?
>
> Regards,
> Hao
Re: Is it possible to change sum() behavior? [message #39226 is a reply to message #38787] Thu, 27 September 2007 01:43 Go to previous message
Hao Zhang is currently offline Hao ZhangFriend
Messages: 161
Registered: July 2009
Senior Member
I created
https://bugs.eclipse.org/bugs/show_bug.cgi?id=204753

Thanks for your instruction.

"Christian W. Damus" <cdamus@ca.ibm.com>
??????:fddnut$6la$1@build.eclipse.org...
> Hi, Hao,
>
> If you expect to find at most one commodity whose id is 'aaa', then you
> might find the "any" iterator to be convenient:
>
> Commodity.allInstances()->any(id='aaa').weight
>
> which will give you a weight or else OclInvalid if there is no such
> commodity. (OclInvalid returns true to an oclIsUndefined() test just as
> null does).
>
> However, that's all beside the point, because the sum of an empty
> collection
> of integers is supposed to be zero, not null. The specification of this
> operation makes that clear.
>
> Would you mind raising a bug? That would be better than asking you to
> work
> around this with a custom EvaluationEnvironment implementation, which
> would
> be feasible but shouldn't be necessary.
>
> Thanks,
>
> Christian
>
>
> Hao Zhang wrote:
>
>> Hi,
>>
>> I found evaluating following expression would returns null:
>>
>> Commodity.allInstances()->select(id='aaa').weight->sum()
>>
>> if there is no commodity whose id is "aaa". I'm wondering whether it is
>> possible to let above expression returns Integer 0 rather than null, so
>> that I can safely evaluate this expression:
>>
>> Commodity.allInstances()->select(id='aaa').weight->sum() > 0
>>
>> otherwise evaluating above expression would result an exception. Of
>> course
>> I can write following expression to judge whether the set is empty, but
>> it
>> is somehow too complex:
>>
>> let coms:Set(Commodity) = Commodity.allInstances()->select(id='aaa')
>> in
>> not coms->isEmpty() and coms.weight->sum()>0
>>
>> Can I archive this by customizing the environment, or there is other
>> recommended way for this situation?
>>
>> Regards,
>> Hao
>
Previous Topic:[Announce] MDT OCL 1.2.0 I200709261803 is available
Next Topic:attribute query
Goto Forum:
  


Current Time: Sat Aug 17 22:29:59 GMT 2024

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

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

Back to the top