500 likes | 517 Views
Learn about the Object Constraint Language (OCL) and how it can help improve documentation, precision, and clarity in your object-oriented models. Explore OCL types, rule basics, checking attribute values, implies rules, rules across associations, rules on collections, and more.
E N D
IS351-System Analysis Lab 7: Object Constraint Language (OCL)
Object constraint language (OCL) • What is constraint? A constraint is a restriction on one or more values of (part of) an object-oriented model or system e.g: class diagram. • Why add constrains to model? • Better documentation • More precision • More cleared and understandable UML models
OCL characteristics: • OCL constraints are declarative • They specify what must be true not what must be done • OCL constraints have formal syntax and semantics • OCL constraints have no side effects • Evaluating an OCL expression does not change the state of the system • Are evaluated in a specified context, either a class or an operation • All constraints apply to all instances. • OCL expression return true or false.
OCL Rule Basics • Need to define five things for a rule • Context: The classifier with which the rule is associated • Type: Whether it is invariant, Pre, or post condition. • Name: The name of the rule • OCL: The rule expressed precisely in OCL • Error Message: A textual description of the business rule using the vocabulary of the business user. *Mandatory fields are underlined
Example • Class: • Constraint: balance must be greater than 1000 • Syntax: • ContextSavingsAccount • InvbalanceCheck: Balance>1000 • --@error The balance must be greater than Zero SavingsAccount balance > 1000 Expression Context
Rule Types • Checking Attribute Values • Implies / if …then rules • Rules across classes • Rules on collections
1. Checking Attribute Values 1-to-m OR m-to-m Person-Company All persons’ salary>1000 There is a person’s salary>1000 1-to-1 Person-Company Context Person Inv: self.employment->exists(salary > 1000) Context Person Inv:self.employment->forAll(salary > 1000) Context Person Inv: self.employment.salary > 1000
2. Implies Rule • Used for rules of form ‘if … then …’ • Constraint: If the amount of a Payment is greater than 1000, securityDetails must be provided. Context Payment Inv: self.amount > 1000 implies securityDetails<>null
3. Rules Across Associations • The payer Party and the recipient Party to a payment cannot have the same name Context Payment Inv: self.payer.name <> self.recipient.name
3. Rules Across Associations • Constraint: The number of guests in each room doesn’t exceed the number of beds in the room. Context Room Inv: self.numberOfBeds >=guest -> size() • Indicates a set of Guest class instances • Points at current instance as this in java
4. Rules on collections • Whenever the link in a constraint results in a set collection of objects, you can use one of the collection operations by putting an arrow between the rolename and the operation • Three different collections: • Set: Every element can appear only once • Bag: elements can appear more than once (also called multiset) • Sequence: A multiset, in which the elements are ordered (ordered Bag) • Syntax: • collection->operation(arguments)
Collection operations • size(): number of elements in the collection • isEmpty(): True, if the collection is empty • notEmpty(): True, if the collection is not empty • sum(): The addition of all the elements in the collection • count(object): number ofoccurrences of theobject in the collection • includes(object): True if the object is an element of the collection • includesAll(collection): True if all elements of the parameter collection are members of the collection
Collection operations • collect(): Makes a projection on object attributes, returns set of collection • excludes(object): True if the object is not an element of the collection • excludesAll(collection): True if all elements of the parameter collection are not members of the collection • forAll(expression) True if for all elements expression is true (expression must be a Boolean expression) • select(expr:OclExpression): Subset of all elements of the collection, for which the OCL expression expr is true
4. Rules on collections • Example Constraint: There must be at least one payment with value greater than 100 US dollars included in the bulk payments. Context BulkPayments Inv: self.payment->exists(p | p.amount > 100)
recursive relationship • Constraint: A course can’t be a requirement for itself Context Course Inv: < > self.requirements->includes (self) Context Course Inv: self.requirements->excludes (self)
Exercise • Question 1: • Modules can be taken iff they have more than seven students registered Context Module Inv: self.taken_by->size() > 7
Exercise • Question 2: • The assessments for a module must total 100% Context Module Inv: self.set_work.collect(weight)->sum ( ) = 100
Exercise • Question 3: • Students must register for 120 credits each year Context Student inv: self.takes.collect(credit)->sum ( ) = 120
Exercise • Question 4: • Students must take at least 90 credits of CS modules each year Context Student Inv: self.takes->select(code.substring(1,2)=‘CS’).collect(credit)-> sum() >=90
Exercise • Question 5: • All modules must have at least one assessment worth over 50% Context Module Inv: self.set_work->exists (weight > 50)
Define classes and associations • We will define a simple class diagram containing 2 classes and 1 association
Define OCL rules • We will define 2 OCL Constraints on class diagram • the student GPA shouldn't be greater than 4 • the student GPA should be greater than or equal to the department minimum GPA • Open a new Notepad file and define the classes, associatoin and OCL rules. After finishing, save the file with <.use> extension
Open views • You can open the view you want from the upper menu • We will open 4 views: object count, link count, class invariant and object diagram • You can also open the class diagram view to view the class diagram
Adding objects • We will add 6 objects • 4 objects for the departments (InformationSystem, ComputerScience, InformationTechnology, DecisionSupport) and 2 students (S1, S2) • From the menu state choose -> create object • Select the class and add the object • You will notice the change in the number of objects in object count
We can double click any object and modify its attribute • We should also link the 6 objects together to solve the violation problem • Insert the following command in the cmd window: • !insert (S1,InformationSystem) into enrolledIn • !insert (S2,InformationSystem) into enrolledIn • !insert (S2,InformationTechnology) into enrolledIn • !insert (S2,ComputerScience) into enrolledIn • !insert (S1,DecisionSupport) into enrolledIn
Validate expressions • If we try to validate the OCL expressions we entered • We will find in class variant view that student GPA OCL return false • Modify the GPA of S1 to be 3 to remove violations