Why allInstance not for isUnique?

余生颓废 提交于 2020-01-06 07:06:39

问题


I have a class Client with attribute noClient, I wan to verify there is no client with the same noClient.

I have the solution below, but the teacher said its not appropriate. Because the contraint may be repeated. I don't know why. And I need to find another solution.

context Client
inv NoClientUnique: Client.allInstances -> isUnique (noClient)

My problem is, I don't even know what is the problem with the code above to be able to find another solution.

This this a school question. Maybe not enough challenging out there, but I spend hours trying to understand. I'm stuck here.


回答1:


Apart from minor syntactical mistakes (should be allInstances()-> ) I don't see a problem with your expression. Make sure you didn't misunderstood your teacher regarding what the constraint was supposed to constrain




回答2:


I just saw in an example, my teacher created a class Singleton, then use the Singleton as context, and not the Client.

class Singleton
-- nothing here.
end
...
context Singleton
inv SingletonisUnique : Singleton.allInstances -> size() = 1
inv noClientUnique : Client.allInstances -> isUnique(noClient)

I think this is the key to my problem, but I don't understand what's the mechanism there.




回答3:


As far as I know, in a typing vision, the OclExpr in parameter should be a boolean expression to evaluate, which is not the case here.

Of course, the result will vary from one OCL tool to another.




回答4:


Your teacher is correct in that with typical OCL tools evaluation of all Clients will be repeated for each Client.

But your teacher is also wrong in that a decent OCL tool should optimize the self-less invariant to achieve the same effect as the spurious Singleton. Tools should help the users not force users to manually optimize.

More realistically, the container of Client may perhaps be Business and you could more sensibly express the business practice that noClient is unique in Business since it isn't really a Client constraint.




回答5:


Another option would be:

context Client
inv NoClientUnique: Client.allInstances()->forAll(c1, c2 : Client | c1 <> c2 implies c1.noClient <> c2.noClient)


来源:https://stackoverflow.com/questions/22058510/why-allinstance-not-for-isunique

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!