Component-Based Software Engineering
ECE493-Topic 5Winter 2007
Lecture 12 – The Object Constraint Language(Part A)
Ladan TahvildariLadan TahvildariAssistant ProfessorAssistant Professor
Dept. of Elect. & Comp. Eng.Dept. of Elect. & Comp. Eng.University of WaterlooUniversity of Waterloo
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 22
Architectural Models and Styles
Architectural models are achieved in UML2 by:– Component-and-connector diagrams, to define component
relationships– Interface descriptions, consisting of:
• method signatures (method names, input and output types) • a semantics that tells us how methods work
Architectural styles are defined in UML2 through:– Explaining the kinds of components that are used in a style– A semantics explaining how components and connections should
behave– Constraints telling us how the components are to be assembled
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 33
Informal Semantics and Constraints
The semantics of architectures and architectural styles has been given informally (in English)
Similarly, the constraints on component assembly for styles has also been given in English
UML2 includes a range of languages for specifying aspects of semantics precisely:
– State-Charts– Activity Graphs– Swimlane Notation– Object Constraint Language (OCL)
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 44
Formal Semantics
Advantages– Informal English specifications can be ambiguous – formal
specifications leave less to interpretation
– The use of a standards such as UML means that formal specifications should eventually become commonly understood by any decent software development team
Disadvantages– English descriptions are easy to write and can be understood by
any member of a development team who understands English
– English descriptions less cost for the software development process (it takes more time to define and comprehend formal semantics, and requires suitably qualified (more expensive) team members
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 55
The Object Constraint Language (OCL)
OCL is used for adding important information to UML models
A business modelling language used within the Syntropy (Boochmethod) in IBM until it was added to UML in 1997
OCL is used – To add constraints, restrictions on values that formed part of an
object-oriented model– To state conditions on execution of a system– To define business rules– To define queries on the model – so questions about a model can
be answered automatically by executing an OCL query
OCL2.0 is a new improved version.
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 66
Building Better ModelsOCL is based on maths and first-order logic but is written like a programming languageOCL expresses information that cannot be expressed well using other diagrams of UMLExample1: Consider the UML class diagram below
– The association between class Flight and class Person indicates that a certain group of persons are passengers on a flight -- the association is (0..*) on the side of the Person class
– The diagram says that the number of passengers is unlimited– In reality, the number of passengers will be restricted to the number of seats on the airplane
that is associated with the flight– OCL can be use to specify such a constraint
OCL CONSTRAINT:
Context FlightInv: passengers->size() <= plane.numberOfSeats
OO “dot” notation for navigating around a model
Inequalities for defining constraints
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 77
Example 2Any human reader of the model will undoubtedly assume that a number of rules must apply to this model.
– A person may have a mortgage on a house only if that house is owned by him-or herself; one cannot obtain a mortgage on the house of one's neighbor or friend.
– The start date for any mortgage must be before the end date.
– The social security number of all persons must be unique.
– A new mortgage will be allowed only when the person's income is sufficient.
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 88
Expression Syntax
OCL Types– Custom
• Defined by UML diagrams
– Predefined • Integer, Real, String and Boolean
– Collection
• Sets, ordered sets, and sequences
• Similar operations on sets as those on collections in Java
(that is, we can define “iterator” expressions – like foreach)
OCL expressions are logical expressions about elements of OCL types– Boolean propositions (true or false values)
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 99
Boolean Operations
a implies bImplies
not anegation
a xor bexclusive or
a and band
a or bor
Notation (where a and b are Booleans)
Operation
Example: Specifying part of a bank machine
incorrect-password implies no-access
and
correct-password implies db-access
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1010
Boolean Inequalities
a > b, a >= bGreater than, greater than or equal to
a = bequals
a <> bnot equals
a = bEquals
a < b, a <= bLess than, less than or equal to
a <> bNot equals
Notation (where a and b are strings, integers or reals)
Operation
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1111
Integers and Reals
Standard operations (-,+,-,*,/)
Modulus a->mod(b)
Absolute value a->abs()
Maximum of a and b: a->max(b)
Minimum of a and b: a->min(b)
Examples:– 12.max(33) = 33
• is a true OCL expression
– 13.mod(2) = 1• is a true OCL expression
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1212
Strings
Concatenation of a and b: a->concat(b)
Size of a: a->size()
a in lower case: a->toLower()
b in upper case: b->toUpper()
Substring of a between i and j: a.substring(i,j)
Examples:– ‘KCL’.size() = 6
• is a false OCL expression
– ‘Computer’.concat(‘ Science’) = ‘Computer Science’• is a true OCL expression
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1313
Set Collection Type
Needed to talk about groups of things in a model
Syntax for declaring a set: Set { elt1, elt2, elt3, … }
Syntax for declaring a type set:Set(Type)
Examples:– Set {‘apple’,’orange’,’strawberry’} is an element of type
Set(String)
– Set {1, 2, 5, 88} is of type Set(Integer)
– Set {HotelRes, LoyaltyProgram, CreditCardBilling}is of type Set(Component)
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1414
Operations on Sets
Returns the intersection of sets s and ts->intersection(t)
Computers the union of s and ts->union(t)
True if s contains at least one elements->notEmpty()
Returns the number of elements in set ss->size()
True if o is not an element of ss->excludes(o)
True if s doesn’t contain any elementss->isEmpty()
True if all elements of set u are not in ss->excludesAll(u)
True if all elements of set u are in ss->includesAll(u)
True if o is an element of ss->includes(o)
Number of times element o occurs in ss->count(o)
DescriptionOperation
Examples - The followings are true OCL expressions about sets:– Set{3,8,2,1}->size() = 4– Set{HotelRes,LoyaltyProgram}->excludes(CreditCardBill)– Set{HotelRes,LoyaltyProgram}->union(Set{CreditCardBill}) =
Set{HotelRes,LoyaltyProgram,CreditCardBill}
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1515
Iterators
Iterators are a powerful kind of OCL operation over collections
– used to perform operations repeatedly over each element of a collectionof the form
set->iterator(body) orset->iterator(variable|body)
– each operation has an OCL expression as its parameter, called the body of the operation
– each operation also has an optional parameter, called an iterator variableused within the body
Iterators are not new – an important programming concept that is borrowed by OCL
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1616
Some Important Iterators Over Sets
Example - The followings are true OCL expressions:Set{4,24,2000,2005}->select(x:Integer|x=>2000) = Set{2000,2005}Set{4,24,2000,2002}->forAll(x:Integer|x.mod(2) = 0)Set{4,24,2000,2005}->exists(x.Integer|x.mod(2) = 1)
Returns a subset of s containing all elements for which expr is falses->reject(expr)
Returns a subset of s containing all elements for which expr is trues->select(expr)
Returns true if expr is true for all elements in the source collections->forAll(expr)
Returns true if there is at least one element in the source collection for which expr is true
s->exists(expr)
Returns the set of objects that result from evaluating expr for each element in the source collection
s->collect(expr)
Returns a random element of set s for which expr is trues->any(expr)
Description – expr is of the form variable|bodyOperation
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1717
Select Iterator
Used when we are interested only in a special subset of a set
The select operation allows us to specify a selection from the original set
Example: – Assume App.components consists of all components in an
application architecture, and that s->IsConnected(Client) returns true if the component s is connected to the component client, then
App.components->select(Client:Component | IsConnected(C,Client))
Returns the subset of all components connected to Client
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1818
forAll operator
The forAll operation is used to specify that a certain condition must hold for all elements of a collection
Example:– if CarInsurance.customers is a list of customers of a car insurance
company and c.age is the age of a customer c, then
CarInsurance.customers-> forAll(c:Customer | c.age <= 70)
Specifies that all customers should be aged 70 or less
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 1919
Modelling with OCL
Used for queries and constraints over UML2 models
OCL syntax covers the following aspects of models:– Initial values – Contracts– Constraints
If you read the reference (the Warmer and Kleppe book from http://www.klasse.nl/ocl/ ), these aspects are detailed for class diagrams, not component diagrams; however the approach is similar
To understand how OCL can be used to provide a formal semantics of components
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 2020
Context of OCL Expressions
An OCL expression annotates a particular model entity (class, interface, datatype, component or entire application) – this is called the context of the expression
Annotation takes the following form:Context ModelEntity[init:/pre:/post:|inv:]
OCL expression about ModelEntity– The context acts as a namespace for the OCL expressions– The OCL expression can refer to aspects of the context:
• the attributes and methods of the class• the provided interfaces of the component• the required interfaces of the component
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 2121
Types of Constraints
An invariant is a constraint that states a condition that must always be met by all instances of the component, type, or interface. An invariant is described using an expression that evaluates to true if the invariant is met. Invariants must be true all the time.
A precondition to an operation is a restriction that must be true at the moment that the operation is going to be executed. The obligations are specified by postconditions.
A postcondition to an operation is a restriction that must be true at the moment that the operation has just ended its execution.
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 2222
Example 2Any human reader of the model will undoubtedly assume that a number of rules must apply to this model.
– A person may have a mortgage on a house only if that house is owned by him-or herself; one cannot obtain a mortgage on the house of one's neighbor or friend.
– The start date for any mortgage must be before the end date.
– The social security number of all persons must be unique.
– A new mortgage will be allowed only when the person's income is sufficient.
January 29, 2007January 29, 2007 ECE493ECE493--T5T5 2323
Example 2 – Augmenting Model with OCL Expressions
context Mortgage inv: security.owner = borrower
context Mortgage inv: startDate < endDate
context Person inv: Person::allInstances()-
>isUnique(socSecNr)
context Person::getMortgage(sum : Money, security : House)
pre: self.mortgages.monthlyPayment->sum() <= self.salary * 0.30
context Person::getMortgage(sum : Money, security : House)
pre: security.value >= security.mortgages.principal->sum()