im ntu software development methods, fall2004 software development methods, fall 2004 ocl -- 1...
Post on 20-Dec-2015
223 views
TRANSCRIPT
IM NTUIM NTU
OCLOCL -- -- 11Software Development Methods, FallSoftware Development Methods, Fall 20042004
Object Constraint Object Constraint LanguageLanguage
(OCL)(OCL)
Yih-Kuen TsayYih-Kuen Tsay
Dept. of Information Dept. of Information ManagementManagement
National Taiwan UniversityNational Taiwan University
IM NTUIM NTU
OCLOCL -- -- 22Software Development Methods, FallSoftware Development Methods, Fall 20042004
OutlineOutline
► IntroductionIntroduction►Relation with UML ModelsRelation with UML Models►Basic Values and TypesBasic Values and Types►Objects and PropertiesObjects and Properties►Collection OperationsCollection Operations
IM NTUIM NTU
OCLOCL -- -- 33Software Development Methods, FallSoftware Development Methods, Fall 20042004
About OCLAbout OCL
►A A formalformal language for writing language for writing expressionsexpressions (such as invariants) on (such as invariants) on UML models.UML models.
► It can also be used to specify It can also be used to specify queriesqueries..►OCL expressions are pure OCL expressions are pure
specifications specifications without side effectswithout side effects..►OCL is a typed language.OCL is a typed language.►Version 2.0 in the finalization process.Version 2.0 in the finalization process.
IM NTUIM NTU
OCLOCL -- -- 44Software Development Methods, FallSoftware Development Methods, Fall 20042004
Why OCLWhy OCL
►UML diagrams do not provide all the UML diagrams do not provide all the relevant aspects of a specification.relevant aspects of a specification.
►Additional constraints expressed by a Additional constraints expressed by a natural language are ambiguous.natural language are ambiguous.
►Traditional formal languages are Traditional formal languages are precise, but hard to use.precise, but hard to use.
►OCL tries to be formal and yet easy OCL tries to be formal and yet easy to use.to use.
IM NTUIM NTU
OCLOCL -- -- 55Software Development Methods, FallSoftware Development Methods, Fall 20042004
Where to Use UMLWhere to Use UML
► As a query languageAs a query language► Invariants on classes and types.Invariants on classes and types.► Pre- and post-conditions on operationsPre- and post-conditions on operations►GuardsGuards► Target sets for messages and actionsTarget sets for messages and actions► Constraints on operationsConstraints on operations►Derivation rules for attributesDerivation rules for attributes
IM NTUIM NTU
OCLOCL -- -- 66Software Development Methods, FallSoftware Development Methods, Fall 20042004
Basic FormBasic Form
context TypeName inv:'this is an OCL expression with stereotype <
<invariant>> in thecontext of TypeName' = 'another string'
IM NTUIM NTU
OCLOCL -- -- 77Software Development Methods, FallSoftware Development Methods, Fall 20042004
Class Diagram ExampleClass Diagram Example
IM NTUIM NTU
OCLOCL -- -- 88Software Development Methods, FallSoftware Development Methods, Fall 20042004
Relation with UML ModelsRelation with UML Models
►Each OCL expression is written in the Each OCL expression is written in the contextcontext of an instance of a specific of an instance of a specific type.type.
►The reserved word The reserved word selfself is used to refer is used to refer to the contextual instance.to the contextual instance.
►An explicit context declaration can be An explicit context declaration can be omitted if the OCL expression is omitted if the OCL expression is properly placed in a diagram.properly placed in a diagram.
IM NTUIM NTU
OCLOCL -- -- 99Software Development Methods, FallSoftware Development Methods, Fall 20042004
InvariantsInvariants
► Inside the class diagram example:self.numberOfEmployees > 50specifies that the number of employees must always exceed 50.
► Alternatively,context Company inv:self.numberOfEmployees > 50
► A different name can be used for self :context c : Company inv:c.numberOfEmployees > 50
► The invariant itself can also be given a name (after inv).
IM NTUIM NTU
OCLOCL -- -- 1010Software Development Methods, FallSoftware Development Methods, Fall 20042004
Pre- and Post-CondtionsPre- and Post-Condtions►Basic form:
context Typename::operationName(param1 : Type1, ... ): ReturnTypepre : param1 > ...post: result = ...
IM NTUIM NTU
OCLOCL -- -- 1111Software Development Methods, FallSoftware Development Methods, Fall 20042004
Pre- and Post-Condtions (cont.)Pre- and Post-Condtions (cont.)►Example:
context Person::income(d : Date) : Integerpost: result = 5000
► Names may be given:Names may be given:context Typename::operationName(param1 : Type1, ... ): ReturnTypepre parameterOk: param1 > ...post resultOk : result = ...
IM NTUIM NTU
OCLOCL -- -- 1212Software Development Methods, FallSoftware Development Methods, Fall 20042004
Package ContextPackage Context
package Package::SubPackagecontext X inv:... some invariant ...context X::operationName(..)pre: ... some precondition ...endpackage
IM NTUIM NTU
OCLOCL -- -- 1313Software Development Methods, FallSoftware Development Methods, Fall 20042004
Operation Body ExpressionOperation Body Expression
context Person::getCurrentSpouse() : Person
pre: self.isMarried = truebody: self.mariages->select( m | m.ended =
false ).spouse
IM NTUIM NTU
OCLOCL -- -- 1414Software Development Methods, FallSoftware Development Methods, Fall 20042004
Initial and Derived ValuesInitial and Derived Values
context Person::income : Integerinit: parents.income->sum() * 1% -- pocket allowancederive: if underAgethen parents.income->sum() * 1% -- pocket allowanceelse job.salary -- income from regular jobendif
IM NTUIM NTU
OCLOCL -- -- 1515Software Development Methods, FallSoftware Development Methods, Fall 20042004
Basic TypesBasic Types
►Boolean: true, false►Integer: 1, -5, 2, 34, 26524, ...►Real: 1.5, 3.14, ...►String: 'To be or not to be...'
IM NTUIM NTU
OCLOCL -- -- 1616Software Development Methods, FallSoftware Development Methods, Fall 20042004
Basic OperationsBasic Operations
►Integer: *, +, -, /, abs()►Real: *, +, -, /, floor()►Boolean: and, or, xor, not, implies, if-then-
else►String: concat(), size(), substring()
IM NTUIM NTU
OCLOCL -- -- 1717Software Development Methods, FallSoftware Development Methods, Fall 20042004
Other TypesOther Types
►Classifiers All classifiers of a UML model are types in its O
CL expressions.►Enumerations
IM NTUIM NTU
OCLOCL -- -- 1818Software Development Methods, FallSoftware Development Methods, Fall 20042004
Sub-expressionsSub-expressions
context Person inv:let income : Integer = self.job.salary->sum()
inif isUnemployed thenincome < 100elseincome >= 100endif
IM NTUIM NTU
OCLOCL -- -- 1919Software Development Methods, FallSoftware Development Methods, Fall 20042004
Definition ExpressionsDefinition Expressions
context Persondef: income : Integer = self.job.salary->sum()def: nickname : String = ’Little Red Rooster’def: hasTitle(t : String) : Boolean = self.job->exists
(title = t)
IM NTUIM NTU
OCLOCL -- -- 2020Software Development Methods, FallSoftware Development Methods, Fall 20042004
More about Types and More about Types and OperationsOperations►Type conformanceType conformance►Casting (re-typing)Casting (re-typing)►Precedence rulesPrecedence rules► Infix operatorsInfix operators►KeywordsKeywords►CommentsComments
IM NTUIM NTU
OCLOCL -- -- 2121Software Development Methods, FallSoftware Development Methods, Fall 20042004
PropertiesProperties
A property is one ofA property is one of►An AttributeAn Attribute
context Person inv:self.age > 0
►An AssociationEndAn AssociationEnd►An Operation with An Operation with isQueryisQuery►A Method with A Method with isQueryisQuery
IM NTUIM NTU
OCLOCL -- -- 2222Software Development Methods, FallSoftware Development Methods, Fall 20042004
Previous Values in Post-Previous Values in Post-ConditionsConditions► context Person::birthdayHappens()
post: age = age@pre + 1► context Company::hireEmployee(p : Person)
post: employees = employees@pre->including(p) andstockprice() = stockprice@pre() + 10
IM NTUIM NTU
OCLOCL -- -- 2323Software Development Methods, FallSoftware Development Methods, Fall 20042004
Previous Values in Post-Conditions Previous Values in Post-Conditions (cont.)(cont.)[email protected] -- takes the old value of property
b of a, say x-- and then the new value of c of x.
[email protected]@pre -- takes the old value of property b of a, say x
-- and then the old value of c of x.
IM NTUIM NTU
OCLOCL -- -- 2424Software Development Methods, FallSoftware Development Methods, Fall 20042004
CollectionsCollections
►SetSet►BagBag►OrderedSetOrderedSet►SequenceSequence
IM NTUIM NTU
OCLOCL -- -- 2525Software Development Methods, FallSoftware Development Methods, Fall 20042004
Collection OperationsCollection Operations
►Select Select context Company inv:self.employee->select(age > 50)->notEmpty()
►RejectRejectcontext Company inv:self.employee->reject( isMarried )->isEmpty()
IM NTUIM NTU
OCLOCL -- -- 2626Software Development Methods, FallSoftware Development Methods, Fall 20042004
ForAll OperationForAll Operationcontext Companyinv: self.employee->forAll( age <= 65 )inv: self.employee->forAll( p | p.age <= 65 )inv: self.employee->forAll( p : Person | p.age <= 65
)
context Company inv:self.employee->forAll( e1, e2 : Person |e1 <> e2 implies e1.forename <> e2.forename)
IM NTUIM NTU
OCLOCL -- -- 2727Software Development Methods, FallSoftware Development Methods, Fall 20042004
Exists OperationExists Operation
context Company inv:self.employee->exists( forename = 'Jack' )UML OCL 2.0 Adopted Specification 29
context Company inv:self.employee->exists( p | p.forename = 'Jack' )
context Company inv:self.employee->exists( p : Person | p.forename = 'Jack' )
IM NTUIM NTU
OCLOCL -- -- 2828Software Development Methods, FallSoftware Development Methods, Fall 20042004
Iterate OperationIterate Operation
►Reject, Select, forAll, Exists, and Collect can all be described in terms of interate.
►Example:collection->collect(x : T | x.property)-- is identical to:collection->iterate(x : T; acc : T2 = Bag{} |
acc->including(x.property))