检查字符串是否为数字

Check if string is a number

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

我有 class Person 和 属性 id 那是 String 类型。我必须检查 id 是一个包含 11 位数字的数字。我在想这样的事情:

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

但我感觉不对。

编辑。

现在我确定它不正确, l.oclIsTypeOf(Integer) 总是 return false,因为当 idString 类型时,oclIsTypeOf 应该只在 OclAny 上调用。

编辑 2.(解决方案)

我是这样解决的:

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

下面 Vincent Aranega 提供的解决方案也应该有效

String 上没有那么多方法,但是 toInteger 可以帮助您。如果字符串无法转换为 Integer,则它 returns StringInvalidInteger 值。所以:

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

应该可以解决问题! (在 Acceleo 中测试成功)