【问题标题】:Check if string is a number检查字符串是否为数字
【发布时间】:2017-11-28 02:12:03
【问题描述】:
================
|    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 类型。

编辑 2.(解决方案)

我是这样解决的:

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

下面Vincent Aranega提供的解决方案应该也可以

【问题讨论】:

    标签: constraints uml modeling operation ocl


    【解决方案1】:

    String 上没有太多方法,但toInteger 可以帮助您。如果字符串无法转换为Integer,则返回StringInvalidInteger 值。所以:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多