0

I have a student class that has two associations: Graduate and Undergraduate. I am trying to create an invariant that makes sure no student is associated with both. I am very new to OCL and do not know the approach to this or syntax. I am thinking I need to have something like Student.allInstances->forAll( something here)

But I don't know.

Thanks!

4

3 回答 3

2

UML 甚至在具有这个确切含义的两个关联之间具有图形异或依赖关系,因此如果您希望甚至可以跳过这个特定的 ocl 约束。

于 2014-04-30T20:03:46.260 回答
1

协会是学生的自我参照吗?或者它在关联结束时是否有另一个类?

如果有另一个类“Study”(例如)并且它们之间的关联将是基数 *,那么解决方案可能是:

context Student s:
inv only_one_graduate_type:
  ( (s.graduate -> notEmpty() implies s.undergraduate -> isEmpty()) && 
    (s.undergraduate -> notEmpty() implies s.graduate -> isEmpty()) )

如果您使用Student.allInstances->更好地使用上下文 Student s重写您的 OCL 表达式

于 2014-04-30T07:51:50.843 回答
0

这个怎么样?

context Student:
inv max_one_association:
    not (self.graduate -> notEmpty() && self.undergraduate -> notEmpty())

顺便说一句,如果您可以发布类图,包括 Student 类和受影响的关联,那就太好了。以防万一...

于 2014-04-30T10:26:14.053 回答