OCL expression help -- concat Strings with separator [message #65899] |
Tue, 20 January 2009 14:34  |
Eclipse User |
|
|
|
Originally posted by: jgreen.upb.de
Hi,
this is not a technical question, I just need help with OCL :'(
I'm trying to create an OCL expression to do the following. Suppose I have an Element "Statements" containing other
elements "Statement" via an 0..n reference, not ordered. A "Statement" element has a string attribute "expr". Now I need
to create a concatenated String with all the statement's string exprs separated by ' or '. However, there must not be an
"or" after the last one.
Example:
Model:
Statements
+-Statement,expr: "a=0"
+-Statement,expr: "b=3"
+-Statement,expr: "c=2"
Desired result:
"a=0 or b=3 or c=2"
Any ideas?
Thanks
Joel
|
|
|
Re: OCL expression help -- concat Strings with separator [message #65922 is a reply to message #65899] |
Tue, 20 January 2009 15:08   |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Joel,
Questions of this nature are perfectly welcome, here. We like to keep
our OCL sharp. ;-)
Try this (assuming that the Statements property referencing the
Statement class is named "statement"):
self.statement->iterate(stmt; result : String = '' |
if result = '' then
stmt.expr
else
result.concat(' or ').concat(stmt.expr)
endif
Cheers,
Christian
Joel Greenyer wrote:
> Hi,
> this is not a technical question, I just need help with OCL :'(
> I'm trying to create an OCL expression to do the following. Suppose I
> have an Element "Statements" containing other elements "Statement" via
> an 0..n reference, not ordered. A "Statement" element has a string
> attribute "expr". Now I need to create a concatenated String with all
> the statement's string exprs separated by ' or '. However, there must
> not be an "or" after the last one.
>
> Example:
>
> Model:
>
> Statements
> +-Statement,expr: "a=0"
> +-Statement,expr: "b=3"
> +-Statement,expr: "c=2"
>
> Desired result:
> "a=0 or b=3 or c=2"
>
> Any ideas?
>
> Thanks
>
> Joel
|
|
|
|
Re: OCL expression help -- concat Strings with separator [message #65965 is a reply to message #65946] |
Tue, 20 January 2009 15:37  |
Eclipse User |
|
|
|
Originally posted by: jgreen.upb.de
Perfect, thanks!
I was always thinking to _append_ the ' or ' and thus I couldn't find a way to tell the last iteration.
Bad thinking me -- good thinking you :)
Cheers
Joel
Christian W. Damus wrote:
> Whoops -- forgot the closing paren:
>
> self.statement->iterate(stmt; result : String = '' |
> if result = '' then
> stmt.expr
> else
> result.concat(' or ').concat(stmt.expr)
> endif)
>
> cW
>
> Christian W. Damus wrote:
>> Hi, Joel,
>>
>> Questions of this nature are perfectly welcome, here. We like to keep
>> our OCL sharp. ;-)
>>
>> Try this (assuming that the Statements property referencing the
>> Statement class is named "statement"):
>>
>> self.statement->iterate(stmt; result : String = '' |
>> if result = '' then
>> stmt.expr
>> else
>> result.concat(' or ').concat(stmt.expr)
>> endif
>>
>> Cheers,
>>
>> Christian
>>
>> Joel Greenyer wrote:
>
> -----8<-----
|
|
|
Powered by
FUDForum. Page generated in 0.04619 seconds