0

I am learning OCL (using "USE"), I've a question about the isUnique() constraint here's the following example:

  • We want to establish the unique constraint of customer numbers through the class full as follows

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

but this expression is far from optimal , because it is possible that the constraint is validated repeatedly. Please anyone can explain me when this is the case and why, and please if you could give me another way to express the unique constraint of Client.noClient using an optimal. i'll appreciate any help.

Machavity
  • 30,841
  • 27
  • 92
  • 100
cbInfo009
  • 71
  • 1
  • 4
  • 14

2 Answers2

0

OCL is a declarative language. Therefore you express what you want to happen, not how to do it. It doesn't make sense to discuss about how optimal is an OCL expression when optimal referes to the execution time. The translation engine should then be able to translate this declarative expression into the most efficient imperative traversal of the object graph in order to verify it.

Jordi Cabot
  • 8,058
  • 2
  • 33
  • 39
  • Unfortunately today, perhaps all, OCL tools lack comprehensive optimization of allInstances() so paying attention to redundant execution can be very beneficial. – Ed Willink Sep 21 '18 at 14:50
0

Today, you can avoid the inefficiency by placing the constraint in a neutral scoping class such as perhaps the ClientManager that just needs to assert once that all-my-clients are unique.

Realistically there should always be such a scoping class, since if you take your subsystem and instantiate it multiple times, the Constraint that all Clients are unique is a fallacy. There is no reason why two different companies shouldn't have the same client if uniqueness is defined by a set of attributes rather than object identity.

(It has been suggested that it should be possible to have Package constraints so that in this case you could have used the Package as the neutral location.)

Ed Willink
  • 1,205
  • 7
  • 8