UML约束 {xor} 在两个关联之间

huangapple go评论111阅读模式
英文:

UML constraint {xor} between two associations

问题

The solution you provided correctly represents the described example.

英文:

I am currently struggeling with contraints in UML. Considering the following example:

The class A have an association to the class B and an association to the class C. Both associations are bidirectional. The Class A can have exactly one instance of B or one instance of C, but not B and C at the same time. In the other direction, B can have only one instance of A, and C can have 0..* instances of A.

I tried to solve this problem with an {XOR} constraint, which is described here https://www.uml-diagrams.org/constraint.html. Unfortunately, there are few examples that can be used as a guidance.
Here an example of my solution:
UML约束 {xor} 在两个关联之间

Does the solution correctly represents the described example?

答案1

得分: 3

UML 图形对于许多事情都很好,但当需要文本表达时,就需要使用 OCL 约束。我认为 "xor" 试图将图形推向不明智的地步。即使您成功使用它,您的用户或工具也会这样做吗?请使用 OCL 约束。我远非确信 "xor" 会对 B 的确切多重性重新定义。我认为您需要 0..1,然后确实需要使用 OCL。

context A
inv AxorB: self.B->size() + self.C->size() = 1

英文:

UML graphics are good for many things, but there comes a point when textual expressions are needed; hence OCL constraints. IMHO the "xor" is an attempt to push graphics beyond what is sensible. Even if you manage to use it correctly, will your users or tools do likewise? Use an OCL constraint. I'm far from convinced that the exactly 1 multiplicity for a B is redefined by the "xor". I think you need 0..1 and then you really do need OCL.

context A
inv AxorB: self.B->size() + self.C->size() = 1

答案2

得分: 2

你的图表不一致。多重性规定每个A都连接到一个B 一个C,但约束规定每个A都连接到一个B 一个C(互斥)。

B和C的多重性应该是 0..1 而不是 1

约束可以进一步限制链接,但约束永远不会减少多重性所施加的限制。

我建议将约束图画得更靠右一些,以便更清楚约束适用于关联的右侧。

更多备注:

  • {xor} 表示在UML规范中提到,但没有明确定义。如果要明确,您可以使用OCL而不是(请参见Ed的答案)。
  • 在您的图表中,导航性未定义。要表示关联是双向的,您可以在每个关联的两侧都添加箭头标记(请参见Christophe的答案)。
英文:

Your diagram is inconsistent. The multiplicities specify that every A is connected to a B and a C, but the constraint specifies that every A is connected to a B or a C (exclusively).

The multiplicities on the side of B and C should be 0..1 instead of 1.

A constraint may further restrict the links, but a constraint will never reduce the restrictions imposed by the multiplicities.

I would advise to draw the constraint a bit more to the right, so that it is more clear that the constraint applies to the right hand sides of the associations.

Further remarks:

  • The {xor} notation is mentioned in the UML specifications, but not well defined. If you want to unambiguous, you might use OCL instead (see Ed's answer).
  • In your diagram, the navigabilities are undefined. To indicate that the associations are bidirectional, you could add arrowheads on both sides of every association (see Christophe's answer).

答案3

得分: 1

如图所示,B和C是100%正交的,但通常情况下,备用功能也可能共享一些抽象的共性,因此,对于B和C都扩展提供共享功能的抽象BorC可能更好。然后,A只与抽象BorC有一个相对简单的双向关系。

英文:

As drawn, B and C are 100% orthogonal, but it is often the case that alternate functionalities also share some abstract commonalities, so it might be better for B and C to both extend the abstract BorC that provides the shared functionality. Then A just has a relatively simple regular bidirectional relationship with the abstract BorC.

huangapple
  • 本文由 发表于 2023年6月8日 20:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定