1 ille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns and Aspects within MDA De Jean-Marc Jézéquel
Mar 26, 2015
1
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Survol de Object Constraint Language
& IDM
À partir du cours
Contracts, Patterns and Aspects within MDA De Jean-Marc Jézéquel
2
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Un métamodèle
3
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Conformité entre modèles et métamodele
• Un métamodele en UML contraint la structure d’un modèle : – typage des éléments
– Cardinalité des associations
• Ça ne suffit pas!
4
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Un métamodèle : des contraintes
-- (C1) Error: the name of a Classifier must be unique within its package.context Classifier inv:not self.package.contents->exists(e | (e <> self) and (e.name = self.name))
5
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Un métamodèle : des contraintes supplémentaires
-- (C2) Error: the name of a StructuralFeature must be unique within its Class --and its supertypes.
context StructuralFeature inv:not self.owner.allStructuralFeatures()-> exists(e |(e <> self) and (e.name = self.name))
6
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Bank_Account{balance>=lowest}
balance: Moneylowest: Money
deposit (Money)withdraw(Money)
Invariants de classe en UML
• Contraintes ajoutées à un modèle UML – notation: between { }
• Invariant = expression booléenne – Vraie pour toutes les instances d’une classe dans un état stable …
– Exprimée en OCL (Object Constraint Language)• e.g. {balance >= lowest}
• Navigation au travers des associations
7
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Precondition: De la responsabilité du client
• Spécification de ce qui doit être vraie pour être autorisé à appeler une méthode – example: amount > 0
• Notation en UML– {«precondition» OCL boolean expression}
– Abbreviation: {pre: OCL boolean expression}
8
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Postcondition: De la responsabilité de l’implementeur
• Specification de ce qui doit être vraie à la fin de l’exécution réussie d’une méthode – example: balance = balance @pre + amount
• Notation en UML– {«postcondition» OCL boolean expression}
– Abbreviation: {post: OCL boolean expression}
– “Operator for previous value (idem old Eiffel):”• OCL expression @pre
9
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
To be Abstract and Precise with the UML
Bank_Account {balance>=lowest}
balance: Moneylowest: Money
deposit (amount: Money) {pre: amount> 0} {post: balance = balance @pre + amount}withdraw(amount: Money) {pre: amount> 0 and montant<=balance-lowest} {post: balance = balance @pre - amount}
10
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Non-local contracts: navigating associations
• Each association is a navigation path– The context of an OCL expression is the starting point
– Rolenames are used to select which association is to be traversed (or target classname if only one)
Person Car1 owner ownings *ownership
Context Car inv:self.owner.age >= 18
Context Car inv:self.owner.age >= 18
11
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Navigation of 0..* associations
• Through navigation, we no longer get a scalar but a collection of objects
• OCL defines 3 sub-types of collection– Set : when navigation of a 0..* association
• Context Person inv: ownings return a Set[Car]• Each element is in the Set at most once
– Bag : if more than one navigation step• An element can be present more than once in the Bag
– Sequence : navigation of an association {ordered}• It is an ordered Bag
• Many predefined operations on type collection
Syntaxe : Collection->operation
Syntaxe : Collection->operation
12
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
Basic operations on collections
• isEmpty– true if collection has no element
• notEmpty– true if collection has at least one element
• size– Number of elements in the collection
• count (elem)– Number of occurrences of element elem in the collection
Context Person inv: age<18 implies ownings->isEmpty
Context Person inv: age<18 implies ownings->isEmpty
14
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
select Operation
• possible syntax– collection->select(elem:T | expr)– collection->select(elem | expr) – collection->select(expr)
• Selects the subset of collection for which property expr holds
• e.g.
• shortcut:
context Person inv:ownings->select(v: Car | v.mileage<100000)->notEmpty
context Person inv:ownings->select(v: Car | v.mileage<100000)->notEmpty
context Person inv:ownings->select(mileage<100000)->notEmpty
context Person inv:ownings->select(mileage<100000)->notEmpty
15
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
forAll Operation
• possibles syntax– collection->forall(elem:T | expr)– collection->forall(elem | expr) – collection->forall(expr)
• True iff expr holds for each element of the collection • e.g.
• shortcut:
context Person inv:ownings->forall(v: Car | v.mileage<100000)
context Person inv:ownings->forall(v: Car | v.mileage<100000)
context Person inv:ownings->forall(mileage<100000)
context Person inv:ownings->forall(mileage<100000)
16
Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC
forAll with two variables
• Considers each pair in the Cartesian product of employees
context Company inv:
self.employee->forAll( e1, e2 : Person |
e1 <> e2 implies e1.forename <> e2.forename)
This is the same as
self.employee->forAll(e1 | self.employee->
forAll (e2 | e1 <> e2 implies
e1.forename <> e2.forename)))