Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » OCL expression help -- concat Strings with separator
OCL expression help -- concat Strings with separator [message #65899] Tue, 20 January 2009 19:34 Go to next message
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 20:08 Go to previous messageGo to next message
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 #65946 is a reply to message #65922] Tue, 20 January 2009 20:12 Go to previous messageGo to next message
Eclipse User
Originally posted by: cdamus.zeligsoft.com

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<-----
Re: OCL expression help -- concat Strings with separator [message #65965 is a reply to message #65946] Tue, 20 January 2009 20:37 Go to previous message
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<-----
Previous Topic:[Announce] MDT OCL 1.3.0 I200901131906 is available
Next Topic:[Announce] MDT OCL 1.3.0 I200901201600 is available
Goto Forum:
  


Current Time: Fri Sep 19 17:55:00 GMT 2014

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

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