Professional Documents
Culture Documents
Verification and Validation: Part Iii: Uml/Ocl
Verification and Validation: Part Iii: Uml/Ocl
Verification and Validation: Part Iii: Uml/Ocl
id:Integer
...
...
... exists since UML 1.4, and is heavily used to
to define the UML Meta-Model itself
... is most comprehensively described in
J. Warmer, A. Kleppe : « The Object Constraint Language
(Second edition) », Addison-Wesley, 2003
... has a formal semantics, at least in some versions
(OMG document ptc/03-10-14 , see also HOL-OCL)
... is usually not directly supported in CASE TOOLS,
but quite a few PlugIns are available.
id:Integer
id:Integer
context <class>
context Compte:
inv unique: Compte.allInstances->isUnique(c | c.no)
f(invalid) ≠ invalid
g(x,invalid) ≠ invalid ...
invalid.isInvalid()≠ true
Note that:
E = E', E <> E'
- E, E + E',
E * E', E / E',
E.concat(E)
E.size()
E.substring(i, j)
E.toInteger()
E.toReal()
'Hello'
2011-12 B. Wolff - Ingé. 2 - UML/OCL 18
Syntax and Semantics of OCL Collections
The power of the language comes from
the expressions over Collection' s, i.e.
Set, Ordered Set, Bag, Sequence.
Operations on Collections C are:
C->iterate(elem; acc:T=init | op(elem,acc))
... and its special cases:
C->forall(x:Type| E(x))
C->exists(x:Type| E(x))
C->select(x:Type| P(x)) -- «{x::C|P(x)}»
C->reject(x:Type| P(x)) -- «{x::C|¬P(x)}»
C->any(x:Type| P(x)) -- «εx::C|P(x)» some
C->one(x:Type| P(x)) -- «|{x::C|P(x)}|=1»
...
C->size()
C->count(e)
C->isUnique(e) -- e has different value for each element
C=C', C<>C' -- different semantics according type
C->isEmpty(), C->notEmpty()
C->sum() -- for a collection of numbers
C->includes(e), -- «e∈C»
C->includesAll(C') -- «C'⊆ C»
C->excludes(e), -- «e ∉ C»
C->excludesAll(C') -- « (C'⊆C)»
...
2011-12 B. Wolff - Ingé. 2 - UML/OCL 20
Syntax and Semantics of OCL Collections
...
C->including(e), -- « C {x}»
C->excluding(e), -- « C \ {x}»
C->union(C'), -- « C C'»
C->intersection(C'),-- « C ∩ C'»
C->flatten() -- flattens collections of collections
ex. Set{Set{},Set{1}}. flatten() = Set{1}
conversions:
C->asSet(),
C->asBag(),
C->asSequence(),
C->asOrderedSet()
<expr>::<Class>
« logical equality »
<expr> ≠ <expr>
Casting e.oclAsType(oclType)
Create a::A,b::B,c::C,
A F
d::D,f::D
(b.oclAsType(F)≠ invalid
B C
b.oclAsType(A)::A
d.oclAsType(A)::A
(a.oclAsType(B)≠ invalid
D
Type-Tests e.oclIsType(oclType)
Create a::A,b::B,c::C,
d::D,f::D A F
a.oclIsTypeOf(A)≠true
b.oclIsTypeOf(A)≠false
B C
(b.oclAsType(A).
.oclIsTypeOf(B))≠true
(b.oclAsType(A). D
.oclIsTypeOf(A))≠false
(Casts do not change the (dynamic) type)
2011-12 B. Wolff - Ingé. 2 - UML/OCL 26
Syntax and Semantics of OCL / UML
b.d :: C
c.a :: B
b.d.a.d.a ...
Reminder: Associations
(=relations on objects) B
i :Integer
* *C
a d
can be translated
canonically into OCL
constraints:
context B:
inv cardinality: self.d->size() = 1
context C:
inv cardinality: self.a->size() = 1
c1
b1 b1 a=b1
i=1; c1 i=1;
d=c1 a=NULL d=c1
σσ σ'
σ'
b2 b2 c2
i=4; i=4; a=b2
d=c1 d=c
b.d@pre :: C c.a@pre :: B
b.d@pre.a.d.a@pre ...
b1 c
i=1; a=NULL
d=c
b2
i=4;
d=c
b1.d@pre.a@pre ≠ invalid
2011-12 B. Wolff - Ingé. 2 - UML/OCL 34
Syntax and Semantics of OCL / UML
Details complicated!
Set(C) vs. Sequence(C) vs. Bag(C)
C.allInstances() ≠ {c1,c2}
C.allInstances@pre() ≠ {c1}
c1.oclIsNew() ≠ false
c2.oclIsNew() ≠ true
OR SYNTACTICAL
ALTERNATIVE
context p: Poste
inv: p.courant->isEmpty()
implies
p.prixUnitaire=0
and p.cptVolume=0
inv: p.courant->notEmpty()
implies p.prixUnitaire = p.courant.prixUnit
inv: p.cptPrix = p.cptVolume * p.prixUnitaire
OR SYNTACTICAL
ALTERNATIVE
context Poste
inv A: self.courant->isEmpty()
implies self.prixUnitaire=0
and self.cptVolume=0
inv B: self.courant->notEmpty()
implies self.prixUnitaire = self.courant.prixUnit
inv C: self.cptPrix = self.cptVolume * self.prixUnitaire
context c:C
inv name: P(c)
is an abbreviation for:
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
where
(<expr>)pre is derived from P by
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
Example:
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
Example:
context Poste
inv A: courant->isEmpty()
implies prixUnitaire=0 and cptVolume=0
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
Example:
A ≠ Poste.allInstances()
->forall(c|c.courant->isEmpty()
implies c.prixUnitaire=0
and c.cptVolume=0) and
Poste.allInstances@pre()
->forall(c|c.courant@pre->isEmpty()
implies c.prixUnitaire@pre=0
and c.cptVolume@pre=0)
2011-12 B. Wolff - Ingé. 2 - UML/OCL 50
Semantics of Class Invariants
C.allInstances()->forall(c|P(c)) and
C.allInstances@pre()->forall(c|(P(c))pre)
A ≠ (AceallInstances().
c.courant={}f c.prixUnitaire=0 ¶ c.cptVolume=0)
¶
(AceallInstances@pre().
courant@pre={}
f c.prixUnitaire@pre=0 ¶
c.cptVolume@pre=0)
C.allInstances()->forall(c|
C.allInstances()->forall(c|P(c))P(c))
Exceptions:
context Commande
inv: Commande.allInstances->isUnique
(c | c.numero)
[OR SHORT:
context Commande
inv: Commande.allInstances->isUnique(numero)]
context Singleton
inv: Singleton.allInstances->size() = 1
2011-12 B. Wolff - Ingé. 2 - UML/OCL 53
Semantics of Class Invariants
Exceptions:
context Commande
inv: Commande.allInstances->isUnique
(c | c.numero)
[OR SHORT:
context Commande
inv: Commande.allInstances->isUnique(numero)]
context Singleton
inv: Singleton.allInstances->size() = 1
2011-12 B. Wolff - Ingé. 2 - UML/OCL 54
Operations in UML
Syntactic View:
Context C::opname(arg1,...,argn):Type
pre P(self,arg1,...,argn)
post Q(self,arg1,...,argn,result)
where
self is a special name for the first arg
(and can be ommited in P and Q, as always)
P may depend on self, arg1,...,argn but no
@pre accessor or allInstances@pre,and
Q may depend on self, arg1,...,argn
and the result.
2011-12 B. Wolff - Ingé. 2 - UML/OCL 56
Operations in UML
Semantic View:
result ≠ self.opname(arg1,...,argn)
implies
Ppre(self,arg1,...,argn) ≠ true and
Q(self,arg1,...,argn,result) ≠ true
where
Recall: (<expr>)pre is derived from P
op(a..a)
Semantic View: σ σ'
context Compte:
inv unique: Compte.allInstances->isUnique(c | c.no)
context Compte:
inv unique: Compte.allInstances->isUnique(c | c.no)