Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns.

Similar presentations


Presentation on theme: "1 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns."— Presentation transcript:

1 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 2 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Un métamodèle

3 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 dun modèle : –typage des éléments –Cardinalité des associations Ça ne suffit pas!

4 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 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 6 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Bank_Account {balance>=lowest} balance: Money lowest: 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 dune classe dans un état stable … –Exprimée en OCL (Object Constraint Language) e.g. {balance >= lowest} Navigation au travers des associations

7 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 8 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Postcondition: De la responsabilité de limplementeur Specification de ce qui doit être vraie à la fin de lexécution réussie dune 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 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: Money lowest: 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 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 Car 1 owner ownings * ownership Context Car inv: self.owner.age >= 18 Context Car inv: self.owner.age >= 18

11 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 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 isEmpty Context Person inv: age isEmpty

13 13 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Collect Operation possible syntax –collection->collect(elem:T | expr) –collection->collect(expr) –collection.expr For instance: –ownings->collect(passenger) Shortcut: –ownings.passenger

14 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 notEmpty context Person inv: ownings->select(mileage notEmpty

15 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(mileage<100000)

16 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)))

17 17 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Other OCL Operations exists (expr) –true if expr holds for at least one element of the collection includes(elem), excludes(elem) –True if elem belongs (resp. does not belong) to the collection includesAll(coll) –True if all elements from coll are also here union (coll), intersection (coll) –Classical set operation asSet, asBag, asSequence –Type conversion

18 18 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Un diagramme de classes pour résumer (from the OCL specification)

19 19 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC An example class invariant The following all say the same thing –numberOfEmployees > 50 –self.numberOfEmployees > 50 –context Company inv: self.numberOfEmployees > 50 –context c : Company inv: c.numberOfEmployees > 50 –context c : Company inv enoughEmployees: c.numberOfEmployees > 50

20 20 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Example preconditions and postconditions context Typename::operationName(param1 : Integer ): Integer pre parameterOk: param1 > 5 post resultOk: result < 0

21 21 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Use of @pre in postconditions You often want to write a postcondition that states what has changed with respect to a precondition –Use property@pre to refer to the value of property prior to execution e.g. context Company::hireEmployee(p : Person) pre : not employee->includes(p) post: employees->includes(p) and stockprice() = stockprice@pre() + 10


Download ppt "1 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns."

Similar presentations


Ads by Google