Dans l'article précédent, nous avons vu une introduction et quelques bases d'OCL (Object Constraint Language).
Le moment est arrivé, pour aller un peu plus loin dans des éléments de syntaxe plus complexes toujours avec notre diagramme de classe (voir http://www.urbanisation-si.com/modelisation-de-systeme-uml-n-est-rien-sans-ocl ).
Exemple d'énumération :
context Person inv: gender = Gender::male
Réutiliser une variable :
context Person inv:
let income : Integer = self.job.salary->sum() in
if isUnemployed then
income < 100
else
income >= 100
endif
Pour réutiliser des variables/opérations à partir de plusieurs expressions OCL :
context Person
def: income : Integer = self.job.salary->sum()
def: nickname : String = ‘Little Red Rooster’
def: hasTitle(t : String) : Boolean = self.job->exists(title = t)
Pour les opérations/attributs statiques :
context MyClass
static def : globalId() : Integer = ...
Quand on est certain qu'un type courant d'un objet est un sous-type alors on peut le transtyper (caster) :
object.oclAsType(Type2) --- change le type statique de l'expression en Type2
Les opérateurs de naviagation sont . pour les attributs/opérations et -> pour les collections, opération ou itération :
anObject.name aString.indexOf(':')
aBag->elementType aSet->union(anotherSet) aSet->collect(name)
Les mots clés :
and body context def derive else endif endpackage false if implies in init inv invalid let not null or package
post pre self static then true xor
Les mots restreints :
Bag Boolean Collection Integer OclAny OclInvalid OclMessage OclVoid OrderedSet Real Sequence Set
String Tuple UnlimitedNatural
Commentaire : tout ce qui suit 2 signes moins -- et se trouve avant la fin de la ligne est un commentaire.
Les attributs :
context Person inv:
self.age > 0
Les opérations
L'appel :
aPerson.income(aDate)
aPerson.income(aDate).bonus = 300 and
aPerson.income(aDate).result = 5000
La définition :
context Person::income (d: Date) : Integer
post: result = age * 1000
Les associations :
context Company
inv: self.manager.isUnemployed = false
inv: self.employee->notEmpty()
context Person inv:
self.employer->size() < 3
context Person inv:
self.wife->notEmpty() implies self.wife.gender = Gender::female
context Person inv:
(self.wife->notEmpty() implies self.wife.age >= 18) and
(self.husband->notEmpty() implies self.husband.age >= 18)
context Company inv:
self.employee->size() <= 50
On ne peut pas tout dévoiler tout de suite, le reste fera l'objet d'un prochain article.
"Les seuls beaux yeux sont ceux qui vous regardent avec tendresse."
Coco Chanel
Voir aussi :
http://www.urbanisation-si.com/
http://urbanisation-si.blog4ever.net
http://urbanisation-si.wix.com/blog
http://urbanisme-si.wix.com/blog
http://urbanisation-si.wix.com/urbanisation-si
http://urbanisation-si.over-blog.com/
http://rhonamaxwel.over-blog.com/
http://urbanisation-des-si.blogspot.fr/