Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » converting a integer to a string.
converting a integer to a string. [message #34960] Wed, 01 August 2007 03:11 Go to next message
Eclipse User
Originally posted by: firstname.name.gmail.com

Hi i would like to use the concat operation with an integer value. Let's say
that I want to make a message in the context of a property to print its
lower/upper bound.

I tried at first (self is a property):
'lower value '.concat(self.lower)

this expression is not evaluated because self.lower is Integer and concat
accepts only String.

After checked that this following query works
self.lower.oclAsType(uml::String)

I tried:
'lower value '.concat(self.lower.oclAsType(uml::String))

I get something odd:
Cannot find operation (concat(String)) for the type (String)

and if I do this:
self.lower.oclAsType(uml::String).concat(' is the lower')

it returns to me:
Instance Specification OclInvalid

A quick look at the ocl specification indicates that a string may be converted
to an integer (or real), but i didn't find any methods to convert a integer to a
string.

how may i deal with this. Do I have to create a helper.

thanks

--
F. Lagarde
Re: converting a integer to a string. [message #34994 is a reply to message #34960] Wed, 01 August 2007 09:24 Go to previous messageGo to next message
Eclipse User
Originally posted by: cdamus.ca.ibm.com

Hi, François,

See some replies in-line, below.

cW

François Lagarde wrote:

> Hi i would like to use the concat operation with an integer value. Let's
> say that I want to make a message in the context of a property to print
> its lower/upper bound.
>
> I tried at first (self is a property):
> 'lower value '.concat(self.lower)
>
> this expression is not evaluated because self.lower is Integer and concat
> accepts only String.

Right, that is expected.


> After checked that this following query works
> self.lower.oclAsType(uml::String)

The oclAsType() only performs type-casting, not coersion of values between
unrelated types. This is not expected to work.


> I tried:
> 'lower value '.concat(self.lower.oclAsType(uml::String))
>
> I get something odd:
> Cannot find operation (concat(String)) for the type (String)

Are you using the UML environment or the Ecore environment? If the former,
then this looks like a bug, because OCL's String primitive type *is*
uml::String (despite it being represented as a distinct type in the
parser). The OCL String and uml::String should be considered identical.


> and if I do this:
> self.lower.oclAsType(uml::String).concat(' is the lower')
>
> it returns to me:
> Instance Specification OclInvalid

That is expected, as this indicates a ClassCastException (cannot cast
Integer to String in Java or OCL).


> A quick look at the ocl specification indicates that a string may be
> converted to an integer (or real), but i didn't find any methods to
> convert a integer to a string.

That's right. The OCL spec forgot to provide the reverse conversion.


> how may i deal with this. Do I have to create a helper.

That is your best bet.

>
> thanks
>
Re: converting a integer to a string. [message #35028 is a reply to message #34994] Wed, 01 August 2007 09:39 Go to previous messageGo to next message
Eclipse User
Originally posted by: firstname.name.gmail.com

In the last post, on 08/01 about 03h, "Christian" (Christian W Damus) wrote:

>> I tried: 'lower value '.concat(self.lower.oclAsType(uml::String))
>> I get something odd: Cannot find operation (concat(String)) for the
>> type (String)

Christian> Are you using the UML environment or the Ecore environment?
Christian> If the former, then this looks like a bug,

I am using the UML Environment.

I succeeded to do what I wanted thanks to lowerValue.stringValue(). This is
not even a workaround but works for me.

Christian> That's right. The OCL spec forgot to provide the reverse
Christian> conversion.

that's really strange. I guess that I will be not the only one who wants to
convert a integer to a String.

thanks for your help.

--
F. Lagarde
Re: converting a integer to a string. [message #51925 is a reply to message #35028] Sun, 02 March 2008 17:06 Go to previous messageGo to next message
Vladimir Orany is currently offline Vladimir Orany
Messages: 9
Registered: July 2009
Junior Member
François Lagarde napsal(a):
> In the last post, on 08/01 about 03h, "Christian" (Christian W Damus) wrote:
>
> >> I tried: 'lower value '.concat(self.lower.oclAsType(uml::String))
> >> I get something odd: Cannot find operation (concat(String)) for the
> >> type (String)
>
> Christian> Are you using the UML environment or the Ecore environment?
> Christian> If the former, then this looks like a bug,
>
> I am using the UML Environment.
>
> I succeeded to do what I wanted thanks to lowerValue.stringValue(). This is
> not even a workaround but works for me.
>
> Christian> That's right. The OCL spec forgot to provide the reverse
> Christian> conversion.
>
> that's really strange. I guess that I will be not the only one who wants to
> convert a integer to a String.
>
> thanks for your help.
>

Salud François and others,
I know this this thread is quite old but I think the unanswred question
is still actual.
I solve this problem my way which looks like this:
'prefix'.concat(
let
number: Integer = 1234
in
OrderedSet{1000000, 10000, 1000, 100, 10, 1}->iterate(
denominator : Integer;
s : String = ''|
let numberAsString : String= OrderedSet{
'0','1','2','3','4','5','6','7','8','9'
}->at(number.div(denominator).mod(10) + 1)
in
if s='' and numberAsString = '0' then
s
else
s.concat(numberAsString)
endif
)
)
I know this isn't really nice looking, but it works fine. You just need
to add as much powers of ten as you expect the lenght of your number. I
use it for id generation in one GMF project and milion is really enough
for me (it would be a thousand too :) )

I hope this helps someone

vo
Re: converting a integer to a string. [message #51977 is a reply to message #51925] Mon, 03 March 2008 10:41 Go to previous messageGo to next message
Eclipse User
Originally posted by: cdamus.ca.ibm.com

Hi, Vladimir,

Nice work! That's a very clever, tidy solution, indeed. I beg to differ
with your comment that it "isn't really nice looking." To me, it looks
beautiful! Of course, I'm quite accustomed to reading OCL :-)

This would make an excellent contribution to the MDT OCL Wiki's very
slim "Snippets" department:

http://wiki.eclipse.org/OCLSnippets

Would you care to add it? I would only make one additional suggestion,
which is that this would be very conveniently used in a def: expression to
actually define this operation on OCL's standard Integer data type. Then,
the 'number' let-variable could be replaced by 'self'.

Thanks!

Christian


Vladimir Orany wrote:

> François Lagarde napsal(a):
>> In the last post, on 08/01 about 03h, "Christian" (Christian W Damus)
>> wrote:
>>
>> >> I tried: 'lower value
>> >> '.concat(self.lower.oclAsType(uml::String)) I get something
>> >> odd: Cannot find operation (concat(String)) for the type
>> >> (String)
>>
>> Christian> Are you using the UML environment or the Ecore
>> environment? Christian> If the former, then this looks like a bug,
>>
>> I am using the UML Environment.
>>
>> I succeeded to do what I wanted thanks to lowerValue.stringValue(). This
>> is not even a workaround but works for me.
>>
>> Christian> That's right. The OCL spec forgot to provide the
>> reverse Christian> conversion.
>>
>> that's really strange. I guess that I will be not the only one who wants
>> to convert a integer to a String.
>>
>> thanks for your help.
>>
>
> Salud François and others,
> I know this this thread is quite old but I think the unanswred question
> is still actual.
> I solve this problem my way which looks like this:
> 'prefix'.concat(
> let
> number: Integer = 1234
> in
> OrderedSet{1000000, 10000, 1000, 100, 10, 1}->iterate(
> denominator : Integer;
> s : String = ''|
> let numberAsString : String= OrderedSet{
> '0','1','2','3','4','5','6','7','8','9'
> }->at(number.div(denominator).mod(10) + 1)
> in
> if s='' and numberAsString = '0' then
> s
> else
> s.concat(numberAsString)
> endif
> )
> )
> I know this isn't really nice looking, but it works fine. You just need
> to add as much powers of ten as you expect the lenght of your number. I
> use it for id generation in one GMF project and milion is really enough
> for me (it would be a thousand too :) )
>
> I hope this helps someone
>
> vo
Re: converting a integer to a string. [message #52210 is a reply to message #51977] Tue, 04 March 2008 14:26 Go to previous messageGo to next message
Vladimir Orany is currently offline Vladimir Orany
Messages: 9
Registered: July 2009
Junior Member
Hi, Christian,

It's done. Snippets looks thicker now :) Could you please look on it and
check it whether it is correct (especially my english isn't perfect) and
whether it is as you suppose to be.

I'm glad you liked it
Thanks

Vladimir

Christian W. Damus napsal(a):
> Hi, Vladimir,
>
> Nice work! That's a very clever, tidy solution, indeed. I beg to differ
> with your comment that it "isn't really nice looking." To me, it looks
> beautiful! Of course, I'm quite accustomed to reading OCL :-)
>
> This would make an excellent contribution to the MDT OCL Wiki's very
> slim "Snippets" department:
>
> http://wiki.eclipse.org/OCLSnippets
>
> Would you care to add it? I would only make one additional suggestion,
> which is that this would be very conveniently used in a def: expression to
> actually define this operation on OCL's standard Integer data type. Then,
> the 'number' let-variable could be replaced by 'self'.
>
> Thanks!
>
> Christian
>
>
> Vladimir Orany wrote:
>
>> François Lagarde napsal(a):
>>> In the last post, on 08/01 about 03h, "Christian" (Christian W Damus)
>>> wrote:
>>>
>>> >> I tried: 'lower value
>>> >> '.concat(self.lower.oclAsType(uml::String)) I get something
>>> >> odd: Cannot find operation (concat(String)) for the type
>>> >> (String)
>>>
>>> Christian> Are you using the UML environment or the Ecore
>>> environment? Christian> If the former, then this looks like a bug,
>>>
>>> I am using the UML Environment.
>>>
>>> I succeeded to do what I wanted thanks to lowerValue.stringValue(). This
>>> is not even a workaround but works for me.
>>>
>>> Christian> That's right. The OCL spec forgot to provide the
>>> reverse Christian> conversion.
>>>
>>> that's really strange. I guess that I will be not the only one who wants
>>> to convert a integer to a String.
>>>
>>> thanks for your help.
>>>
>> Salud François and others,
>> I know this this thread is quite old but I think the unanswred question
>> is still actual.
>> I solve this problem my way which looks like this:
>> 'prefix'.concat(
>> let
>> number: Integer = 1234
>> in
>> OrderedSet{1000000, 10000, 1000, 100, 10, 1}->iterate(
>> denominator : Integer;
>> s : String = ''|
>> let numberAsString : String= OrderedSet{
>> '0','1','2','3','4','5','6','7','8','9'
>> }->at(number.div(denominator).mod(10) + 1)
>> in
>> if s='' and numberAsString = '0' then
>> s
>> else
>> s.concat(numberAsString)
>> endif
>> )
>> )
>> I know this isn't really nice looking, but it works fine. You just need
>> to add as much powers of ten as you expect the lenght of your number. I
>> use it for id generation in one GMF project and milion is really enough
>> for me (it would be a thousand too :) )
>>
>> I hope this helps someone
>>
>> vo
>
Re: converting a integer to a string. [message #52236 is a reply to message #52210] Wed, 05 March 2008 07:50 Go to previous message
Eclipse User
Originally posted by: cdamus.ca.ibm.com

Hi, Vladimir,

It looks perfect. Thanks for the contribution!

This is an excellent example, demonstrating several different OCL concepts,
including additional-operation definition and the seemingly
difficult "iterate" expression.

cW

Vladimir Orany wrote:

> Hi, Christian,
>
> It's done. Snippets looks thicker now :) Could you please look on it and
> check it whether it is correct (especially my english isn't perfect) and
> whether it is as you suppose to be.
>
> I'm glad you liked it
> Thanks
>
> Vladimir

-----8<-----
Previous Topic:EClassifier for primitive variables
Next Topic:Support for UML 2 collaborations
Goto Forum:
  


Current Time: Wed Jul 23 08:03:51 EDT 2014

Powered by FUDForum. Page generated in 0.03111 seconds