2017-06-24 147 views
3
================ 
| Person | 
|--------------| 
|- id : String | 
|--------------| 
================ 

Person类物业idString类型。我必须检查id是一个包含11位数的数字。我想这样的事情:检查字符串是一个数字

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

但我觉得这是不正确的。

编辑。

现在我确定这是不正确的, l.oclIsTypeOf(Integer)总是返回false,因为是oclIsTypeOf只应呼吁OclAny,当idString类型。

EDIT 2(解决方案)

我解决了它是这样的:由Vincent Aranega提供

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

下面的解决方案应该也工作

回答

3

上没有String这么多的方法,但toInteger可以帮助你。如果字符串不能转换为Integer,它将返回或Invalid的值Integer。所以:

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

应该做的伎俩! (在Acceleo中成功测试)

相关问题