如何指定一个Class的唯一标识符

How to specify the unique identifier of a Class

我必须为Employee class 指出每个员工都可以通过他的个人编号清楚地识别。不知道是不是我想的太复杂了,因为我没有真正的想法。

属性: 最终 int personelNumber ...

您没有提供您的元模型,并且清楚地告诉每个员工他们的 personelNumber 是单值的,因此必然是唯一的。大概是在某个范围内,比如 Company,personelNumber 应该是唯一的,所以答案通常是这样的。

context Company
inv UniquePersonelNumber: employees->isUnique(personelNumber)

可以在以下问题中找到两个可选的 OCL 表达式: Why allInstance not for isUnique?

在你的情况下,它将是:

context Employee
inv personalNumberUnique : Employee.allInstances() -> isUnique(personalNumber)

您甚至不需要 OCL 约束来在 UML 中表达它。 属性 元类上有一个 属性 isID 确保了这一点:

来自 UML 2.5 规范 § 9.5.3(第 111 页)

A Property may be marked, via the property isID, as being (part of) the identifier (if any) for Classifiers of which it is a member. The interpretation of this is left open but this could be mapped to implementations such as primary keys for relational database tables or ID attributes in XML. If multiple Properties are marked as isID (possibly in generalizing Classifiers) then it is the combination of the (Property, value) tuples that will logically provide the uniqueness for any instance. Hence there is no need for any specification of order and it is possible for some of the Property values to be empty. If the Property is multivalued then all values are included.

此 属性 的表示法与其他约束的表示法类似 使用 {id} 在属性的名称和类型之后