170 likes | 327 Views
Constraints and Invariants. Topics: constraints, invariants, contracts, OCL, Alloy, precondition, postcondition What are they? – able to read and understand them How to use them during modeling ? – able to write constraints and know when to apply them. Constraints.
E N D
Constraints and Invariants • Topics: constraints, invariants, contracts, OCL, Alloy, precondition, postcondition • What are they? – able to read and understand them • How to use them during modeling ? – able to write constraints and know when to apply them
Constraints • Constraint: a mathematical concept A condition or restriction expressed in natural language text or in a machine readable language for the purpose of declaring some of the semantics of an element. Boolean expressions • Constraints at design level – restriction on one or more values of OO model/system • Constraints at code level – restriction on values, ranges and relations of program variables (e.g., assertion)
Two Types of Constraints • Inequality Constraints: x>1 • Non-binding (different values can satisfy the constraints) • Equality Constraints: x = 1 • Binding (not varied) • The constraints define a feasible set of candidate solutions
Common Types of Constraints used in Modeling • Class invariant • a constraint that must always be met by all instances of the class • Precondition of an operation • a constraint that must always be true BEFORE the execution of the operation • Postcondition of an operation • a constraint that must always be true AFTER the execution of the operation
Invariants • Definition: a constraint that should be true for an object during its complete lifetime • Invariants at design level – rules that should hold for the real-life objects after which the software objects are modeled • Invariants at code level – constraints hold at the program points regarding values, ranges and relations of program variables independent of program inputs
OCL (Object Constraint Language) • Originated from a business modeling language in IBM in 1990s • Standard “add-on” to the UML for better models – e.g, PIM (platform-independent model) for model driven architecture • A formal and simple modeling language - mathematically based (set theory and predicate logic) • Non-ambiguity, compared to UML diagrams - quick/easy to grasp the meaning, but ambiguous • Add details to UML models • Auto-check and auto-code generation • No complex math notations for easy understanding
OCL Overview • A typed language – can be type checked during modeling • OCL is used during modeling before executable exists, for specifying constraints and queries on models • Declarative – specify what to do, not how to do • Expressions that define queries, reference values, state conditions, business rules
OCL: Invariants for Association Flight Airplane 0..* flights 1 plane flightnr: Integer AvailableSeats: Integer numberOfSeats: Integer flights 0..* passengers 0..* Person Context flight Inv: passengers->size()<= plan.numberOfSeats name: String
contextMortgage 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
OCL: learn as a language • Expressions • Types • Operations • Syntax and semantics • First, specifying constraints • Basic expressions • Types and operations Next class, specifying queries
Context-inv Context: elements you want to restrict Inv: invariants • Every OCL expression is bound to a specific context. • The context is often the element that the constraint restricts • The context may be denoted within the expression using the keyword ‘self’. • ‘self’ is implicit in all OCL expressions • Similar to ‘this’ in C++
Example: context -inv • Flight capacity constraint: The maximum number of passengers that can be on a flight must be less than or equal to 1,000. context Flight inv capacity: self.maxNrPassengers <= 1000 Note: self can be omitted context Flight inv capacity: maxNrPassengers <= 1000
Notation in UML Diagrams • Constraints may be denoted within the UML model or in a separate document. • the expression: context Flight invself.duration < 4 • is identical to: context Flight inv duration < 4 • is identical to: Flight duration: Integer inv: duration < 4
Context-init clause Flight Defining initial attribute value Context Flight::maxNrPassengers:Integer init: 100 Defining initial association end value context Flight::passengers:Set(Passenger) init: Set{} departTime: Time /arrivalTime: Time duration : Interval maxNrPassengers: Integer 1 passengers * Passenger $minAge: Integer age: Integer needsAssistance: Boolean book(f : Flight)
Context-pre/post A class named Account has an attribute balance and an operation overdraft() that returns true if the balance is less than 0 and false otherwise. context Account::overdraft():Boolean pre : -- none post : result = (balance < 0)
More complex pre and post conditions The operation birthdayOccurs() adds 1 to the customer age. context Customer::birthdayOccurs() pre : -- none post : age = age@pre + 1 context Account::safeWithdraw(amt:Integer) pre : balance > amt post : balance = balance@pre - amt
Constructs for Postconditions • result Keyword – indicates the return value from the operation • context Transaction::getProgram() : LoyaltyProgram post: result = self.card.Membership.programs • oclIsNew operation – Can determine whether a new object is created during the execution of an operation • context Loyalty::enrollAndCreateCustomer(n : String, d: Date ) : Customer pre : -- none post : result.oclIsNew() and result.name = n and result.dateOfBirth = d and participants includes(result)