Search code examples
constraintsumlmodelingoperationocl

Check if string is a number


================
|    Person    |
|--------------|
|- id : String |
|--------------|
================

I have class Person with property id that is String type. I have to check that id is a number that contains 11 digits. I thinking about something like this:

context Person::id:String
inv:    self.id->forAll(l|l.oclIsTypeOf(Integer))
        and
        self.id.size() = 11 

but I have feeling that is not correct.

EDIT.

Now im sure it's not correct, l.oclIsTypeOf(Integer) always return false, because is oclIsTypeOf should be only called on OclAny, when id is a String type.

EDIT 2. (Solution)

I solved it like this:

context Person::id:String
inv:    not self.id.toInteger().oclIsInvalid()
        and
        self.id.size() = 11 

Solution provided by Vincent Aranega below should works too


Solution

  • There is not so much methods on String, but the toInteger one can help you there. It returns the Integer value of a String or Invalid if the string cannot be converted to an Integer. So:

    context Person::id:String
    inv:    not self.id.toInteger().oclIsUndefined() 
            and self.id.size() = 11
    

    should do the trick! (tested with success in Acceleo)