Home » Modeling » OCL » Cardinality constraint for nonnavigable associations ?
Cardinality constraint for nonnavigable associations ? [message #49110] 
Sat, 19 January 2008 18:05 
Eclipse User 



Originally posted by: maiera.de.ibm.com
We are using nonnavigable associations (the ends are owned by the
association class) and we have situations, where we want to specify an
OCL constraint (at the model level) on the cardinality of an end. For
specific reasons, we cannot use the cardinality definition of the end
itself, it really has to be a constraint.
Let's assume we have classes C1 and C2, and association A1 with ends c1
referencing class C1 and c2 referencing class C2.
Let's further assume the constraint to be specified is the equivalent of
a cardinality of 1 on end c1, i.e. each instance of C2 is associated via
A1 to exactly one instance of C1.
I think I understand how to specify such a constraint if c1 is navigable
(i.e. c1 is owned by class C2):
Constraint on class C2:
inv: self.c1>size()=1
I interpret this OCL to apply for each instance of the class it is
defined on, in this case each instance of C2. This to me seems to be
expressing exactly the intended constraint: For each instance of C2, the
number of occurences of its owned end c1 is exactly 1.
Now if c1 is nonnavigable, I have difficulties to come up with the
corresponding OCL statement, nor do I even understand on which object
the constraint should be defined.
It seems to me that I still need to define the constraint on class C2.,
because I want to make a statement that is supposed to apply to each
instance of C2, but there is no end c1 anymore in the scope of class C2
(since it is now owned by the association A1).
The constraint on association A1:
inv: self.c1>size()=1
does not seem to express the intended constraint, because it says that
for each instance of A1, its end c1 has to have exactly one occurence.
That is always the case, because each instance of A1 always references
only one instance of each end, regardless of any cardinality constraints.
Question: What is the right way to express a cardinality constraint for
nonnavigable ends ?
Andy



Re: Cardinality constraint for nonnavigable associations ? [message #49215 is a reply to message #49110] 
Mon, 21 January 2008 14:16 
Eclipse User 



Originally posted by: cdamus.ca.ibm.com
Hi, Andy,
Regardless of end ownership or navigability, the multiplicity declared by an
association end always constrains the cardinality from the perspective of
the classifiers at the association ends.
Thus, even in your association that owns both of its ends (and perhaps they
aren't navigable, either), "c1 [1]" indicates that for any C2, exactly one
C1 is linked to it.
OCL has the power to navigate nonnavigable association ends, even unowned,
so you could alternatively do:
context C2
self.c1>size() = 1
exactly as you suggested for the navigable case. This depends, of course,
on the name "c1" not otherwise being used for a property in the C2
namespace.
MDT OCL's UML binding actually supports evaluation of this kind of
navigation of nonnavigable association ends, for models generated to code
from UML2. And since 1.2 M3, we even support navigation of unnamed (and
nonnavigable) ends; see the N&N page for details:
http://wiki.eclipse.org/MDT_1.1_New_and_Noteworthy#Navigatio n_of_Unnamed_Association_Ends
HTH,
Christian
Andreas Maier wrote:
> We are using nonnavigable associations (the ends are owned by the
> association class) and we have situations, where we want to specify an
> OCL constraint (at the model level) on the cardinality of an end. For
> specific reasons, we cannot use the cardinality definition of the end
> itself, it really has to be a constraint.
>
> Let's assume we have classes C1 and C2, and association A1 with ends c1
> referencing class C1 and c2 referencing class C2.
>
> Let's further assume the constraint to be specified is the equivalent of
> a cardinality of 1 on end c1, i.e. each instance of C2 is associated via
> A1 to exactly one instance of C1.
>
> I think I understand how to specify such a constraint if c1 is navigable
> (i.e. c1 is owned by class C2):
>
> Constraint on class C2:
> inv: self.c1>size()=1
>
> I interpret this OCL to apply for each instance of the class it is
> defined on, in this case each instance of C2. This to me seems to be
> expressing exactly the intended constraint: For each instance of C2, the
> number of occurences of its owned end c1 is exactly 1.
>
> Now if c1 is nonnavigable, I have difficulties to come up with the
> corresponding OCL statement, nor do I even understand on which object
> the constraint should be defined.
>
> It seems to me that I still need to define the constraint on class C2.,
> because I want to make a statement that is supposed to apply to each
> instance of C2, but there is no end c1 anymore in the scope of class C2
> (since it is now owned by the association A1).
>
> The constraint on association A1:
> inv: self.c1>size()=1
> does not seem to express the intended constraint, because it says that
> for each instance of A1, its end c1 has to have exactly one occurence.
> That is always the case, because each instance of A1 always references
> only one instance of each end, regardless of any cardinality constraints.
>
> Question: What is the right way to express a cardinality constraint for
> nonnavigable ends ?
>
> Andy



Goto Forum:
Current Time: Tue Mar 31 21:08:07 GMT 2015
Powered by FUDForum. Page generated in 0.02848 seconds
