UML OCL : An Expression Language - Core -- 29

29
megaplanet OCL : An Expression Language OCL : An Expression Language Basic Types, Enumerated Types, Types Constructors Basic Types, Enumerated Types, Types Constructors Expressions, Operations Expressions, Operations Operations on collections Operations on collections

Transcript of UML OCL : An Expression Language - Core -- 29

Page 1: UML OCL : An Expression Language - Core -- 29

megaplanet

OCL : An Expression LanguageOCL : An Expression Language

Basic Types, Enumerated Types, Types ConstructorsBasic Types, Enumerated Types, Types Constructors

Expressions, OperationsExpressions, Operations

Operations on collectionsOperations on collections

Page 2: UML OCL : An Expression Language - Core -- 29

megaplanet

2

OCL = An Expression LanguageOCL = An Expression Language

� No side effectNo side effect� No explicit iterationNo explicit iteration

� Like a functional languageLike a functional languagebut no functions (in the standard)but no functions (in the standard)

� Can serve as a Can serve as a query languagequery languageThis first part,

« OCL : An Expression language » is almost independent from UML

Page 3: UML OCL : An Expression Language - Core -- 29

megaplanet 3

TypesTypes

Basic typesBasic types� IntegerInteger� RealReal� BooleanBoolean� StringString

Enumerated typesEnumerated types

MetatypesMetatypes� OclTypeOclType� OclAnyOclAny� OclStateOclState� OclExpressionOclExpression

Types from UMLTypes from UMLclassesclasses

associationsassociations

……

Type constructorsType constructors� TupleType( x : T, y : T … )TupleType( x : T, y : T … )� Set(T)Set(T)� OrderedSet(T)OrderedSet(T)� Sequence(T)Sequence(T)� Bag(T)Bag(T)� Collection(T)Collection(T)

Page 4: UML OCL : An Expression Language - Core -- 29

megaplanet

4

Syntax of Expressions Syntax of Expressions (Simplified)

<const> 12 'hello'

<id> children Residence

<expr> <op> <expr> age>18

self like in python, « this » in java

<exprobj> . <prop> self.chairman.age

<exprobj> . <objprop>(<expr>...) self.chairman.increaseSalary(10)

<exprcoll> -> <collprop>(<expr>...) self.employees->collect(salary)

<package>::<package> … :: <element> like in C++, « . » in python and java

if <expr> then <expr> else <expr> endif if age>18 then 1200 else 50*age endif

let <id> : <type> in <expr> .<expr> let e=x*x+y in (e-20)*1,5

Page 5: UML OCL : An Expression Language - Core -- 29

megaplanet

5

ExamplesExamples

self.salary - 100

self.children->isEmpty()

self.children->forall(age>20)

self.getTaxes(1998) / self.children->size()

self.children->select( sex= Sex::male )

self.children->collect(salary)->sum()

self.children.salary->sum()

self.children->union(self.parents)->collect(age)

children

*

Personsex:Sexsalary:realage:integer

parents

0..2

getTaxes()

<<enumeration>>Sex

malefemale

Page 6: UML OCL : An Expression Language - Core -- 29

megaplanet

6

To be Remembered !To be Remembered !

. to get access to properties of objects

->-> to get access to properties of collections to get access to properties of collections

+ some rules to mix collections and objects (see later)

self.taxes(1998) / self.children -> size()

because selfis an object

because self.childrenis a collection of objects

Page 7: UML OCL : An Expression Language - Core -- 29

megaplanet

7

Integer et RealInteger et Real

� IntegerInteger� values : values : 1, -5, 34, 243431, -5, 34, 24343, ..., ...� operations : operations : +, -, *, div, mod, abs, max, min+, -, *, div, mod, abs, max, min

� RealReal� values : values : 1.5, 1.341.5, 1.34, ..., ...� operations : operations : +, -, *, /, floor, round, max, min+, -, *, /, floor, round, max, min

Integer type « conforms » to the type Real

Page 8: UML OCL : An Expression Language - Core -- 29

megaplanet

8

BooleanBoolean

� BooleanBoolean� values : values : truetrue, , falsefalse� operations : operations : notnot, , andand, , oror, , xorxor, , impliesimplies, , if-then-else-endifif-then-else-endif

partial evaluation :partial evaluation : true oror x always true, even when x is undefined

false and and x always false, even when x is undefined

(age<40 implies salaire>1000) and (age>=40 implies salaire>2000)

if age<40 then salaire > 1000 else salaire > 2000 endif

salaire > (if age<40 then 1000 else 2000 endif)

Page 9: UML OCL : An Expression Language - Core -- 29

megaplanet

9

StringString

name = nom.substring(1,1).toUpper().concat( nom.substring(2,nom.size()).toLower())

Strings are not sequence of Sequence(char) (no char type)

Operations:Operations:==

ss..sizesize() ()

s1s1..concatconcat((s2s2) )

s1s1..substringsubstring((i1i1,,i2i2))

ss..toUppertoUpper()()

ss..toLowertoLower()()

Values:Values:

'' '' 'a sentence' 'a sentence'

Page 10: UML OCL : An Expression Language - Core -- 29

megaplanet

10

EnumerationEnumeration

� ValuesValuesDay::Tuesday Day::Tuesday (previous notation: (previous notation: #Tuesday #Tuesday ) )

� OperatorsOperators==, , <><>

No ordering relation

<<enumeration>>Day

MondayTuesdayWednesdayThursdayFridaySaturdaySunday

Page 11: UML OCL : An Expression Language - Core -- 29

megaplanet

11

ExamplesExamples

spouse->notEmpty() implies spouse.sex = Sex::female

épouse.sexe < sexe

husband

0..1

Person

sex : Sex

spouse

0..1

No ordering relationNot alwaysdefined

<<enumeration>>Sex

malefemale

Page 12: UML OCL : An Expression Language - Core -- 29

megaplanet

12

Element vs. singletonElement vs. singleton

In almost all languagesIn almost all languages� one element one element ee� from singleton containing this element from singleton containing this element Set{e}Set{e}

self->size() = 1

Implicit conversion in OCL Implicit conversion in OCL element => singletonelement => singleton

when an collection operation when an collection operation is applied to an elementis applied to an element

elem elem ->-> prop prop Set{Set{elemelem}->}->propprop

Page 13: UML OCL : An Expression Language - Core -- 29

megaplanet

13

CollectionsCollections

General TypeGeneral Type CollectionCollection( ( T T ))

Set(Set(T T ))

{unique}{unique} {nonunique}{nonunique}

{unordered}{unordered}

{ordered}{ordered} Sequence(Sequence(T T ))

Bag(Bag(T T ))

OrderedSet(OrderedSet(T T ))

Page 14: UML OCL : An Expression Language - Core -- 29

megaplanet

14

Use of collections for role navigationUse of collections for role navigation

object . nomderole object . nomderole type:type:

X1 or 0..1

X*

X{ordered} *

X

Set(X)

OrderedSet(X)

X*{nonunique}

Bag(X)

X{ordered, nonunique} *

Sequence(X)

Page 15: UML OCL : An Expression Language - Core -- 29

megaplanet

15

Collection ExpressionsCollection Expressions

ExamplesExamples� SetSet { { 'lundi''lundi', , 'mercredi''mercredi', , 'mardi''mardi' } }� BagBag { { 'lundi''lundi', , 'lundi''lundi', , 'mardi''mardi', , 'lundi''lundi' } }� OrderedSetOrderedSet { { 1010, , 2020, , 55 } }� SequenceSequence { { 'lundi''lundi', , 'lundi''lundi', , 'mardi''mardi', , 'lundi''lundi' } }

To specify rangesTo specify ranges� SequenceSequence { { 1..5, 2..41..5, 2..4 } }

Useful for 'loops' :

Sequence { 0 .. etagesnb-1 } -> forall( i | canStop(i) )

Page 16: UML OCL : An Expression Language - Core -- 29

megaplanet

16

Operations on CollectionsOperations on Collections

� Cardinality: coll -> size()� Emptyness: coll -> isEmpty()� Non emptyness: coll -> notEmpty()� Occurrence number: coll -> count(elem)� Membership: coll -> includes( elem )� Non membership: coll -> excludes( elem )� Inclusion: coll -> includesAll(coll)� Exclusion: coll -> excludesAll(coll)� Sum (of numbers): coll -> sum()

Page 17: UML OCL : An Expression Language - Core -- 29

megaplanet

17

ExamplesExamples

Set { 3, 5, 2, 45, 5 }->size()

Sequence { 1, 2, 45, 9, 3, 9 } ->count(9)

Sequence { 1, 2, 45, 2, 3, 9 } ->includes(45)

Bag { 1, 9, 9, 1 } -> count(9)

c->asSet()->size() = c->size()

c->count(x) = 0

Bag { 1, 9, 0, 1, 2, 9, 1 } -> includesAll( Bag{ 9,1,9} )

Page 18: UML OCL : An Expression Language - Core -- 29

megaplanet

18

Operations on SetsOperations on Sets

� Union Union ens -> ens -> unionunion( ens )( ens )� Intersection Intersection ens -> ens -> intersectionintersection( ens )( ens )� Difference Difference ens1 - ens2ens1 - ens2� Adding an element Adding an element ens -> ens -> includingincluding(elem)(elem)� Removing an element Removing an element ens -> ens -> excludingexcluding(elem)(elem)� Sequence conversion Sequence conversion ens -> ens -> asSequence()asSequence()� Bag conversion Bag conversion ens -> ens -> asBag()asBag()� Conversion to ordered setConversion to ordered set ens -> ens -> asOrderedSet()asOrderedSet()

Page 19: UML OCL : An Expression Language - Core -- 29

megaplanet

19

Filtering: select, reject and anyFiltering: select, reject and any

coll -> select( cond )select elements satisfying the conditionselect elements satisfying the condition

coll -> reject( cond )reject elementsreject elements

coll -> any( cond )select select anyany element satisfying the condition element satisfying the condition

• non deterministnon determinist• useful when there is only one elementuseful when there is only one element• "undefined" if the collection is empty"undefined" if the collection is empty

Page 20: UML OCL : An Expression Language - Core -- 29

megaplanet

20

ExamplesExamples

self.children ->select( age>10 and sex = Sex::Male)

self.children ->reject(e : Person | e.children->isEmpty())->notEmpty()

members->any(title='president')

children*

Member

sex : Sexeage : integertitle : string

parents0..2Club members

*

Page 21: UML OCL : An Expression Language - Core -- 29

megaplanet

21

Syntax Alternatives Syntax Alternatives

self.employees->select(age > 50)

self.employees->select( p | p.age>50 )

self.employees->select( p : Person | p.age>50)

Person

age : integer

Company employees

*

Page 22: UML OCL : An Expression Language - Core -- 29

megaplanet

22

forall, exists, oneforall, exists, one

coll -> coll -> forallforall( cond )( cond )

coll -> coll -> existsexists( cond )( cond )

coll -> coll -> oneone( cond )( cond )

self.enfants->forall(age<10)

self.enfants->exists(sexe=Sexe::Masculin)

self.enfants->one(age>=18)

children*

Personsexe : Sexage : integer

parents0..2

Page 23: UML OCL : An Expression Language - Core -- 29

megaplanet

23

Syntax ComparisonSyntax Comparison

children->forall( p : Person | p.age<10)

∀ c ∈ children . c.age < 10

Page 24: UML OCL : An Expression Language - Core -- 29

megaplanet

24

Quantifiers: more about syntaxQuantifiers: more about syntax

It's possible toIt's possible to� give a name to a variablegive a name to a variable� explicit its typeexplicit its type� use various variables at the same timeuse various variables at the same time

self.children->forall( age < self.age )

self.enfants->forall( e | e.age < self.age - 7)

self.children->forall( e : Personne | e.age < self.age - 7)

self.children->exists( e1,e2 | e1.age = e2.age )

self.children->exists( e1,e2 | e1.age = e2.age and e1<>e2 )

self.children->forall( e1,e2 : Personne |

e1 <> e2 implies e1.firstname <> e2.firstname)

children*

Personage firstname

Page 25: UML OCL : An Expression Language - Core -- 29

megaplanet

25

UnicityUnicity

coll -> isUnique ( expr )True if all elements return a different value for exprTrue if all elements return a different value for expr

self.children -> isUnique ( firstname )

instead of instead of

self.children->forall( p1,p2 : Person |

p1 <> p2 implies p1.firstname <> e2.firstname)

Useful to define the notion of "imported key" for instance

children*

Personage firstname

Page 26: UML OCL : An Expression Language - Core -- 29

megaplanet

26

isUnique vs. {unique}isUnique vs. {unique}

self.children -> isUnique ( firstname )

children*

Personage firstname

{unique}

Page 27: UML OCL : An Expression Language - Core -- 29

megaplanet

27

Image of an expression: collectImage of an expression: collect

coll -> coll -> collectcollect( expr )( expr )� "image" of a function (map, apply, ...)� expr evaluated for each element� result in:

a Bag if coll is a Set or a Bag

a Sequence if coll is a Sequence or a OrderedSet

self.enfants->collect(age) = Bag{10,5,10,7}

self.employés->collect(salaire/10)->sum()

children*

Personage

employees

*Company Employee

salary

Page 28: UML OCL : An Expression Language - Core -- 29

megaplanet

28

Collect: Simplified Syntax!Collect: Simplified Syntax!

.. with a collection with a collection ↔↔ collectcollect

children*

Personage

self.children.age

self.children->collect(age)

Page 29: UML OCL : An Expression Language - Core -- 29

megaplanet

29

Collect Possible Duplicates→Collect Possible Duplicates→

The result is a Bag or Sequence because of possible duplicatesThe result is a Bag or Sequence because of possible duplicates

� To get a setTo get a setself.children.age->asSet()

� . shortcut very usefull to navigate!. shortcut very usefull to navigate!self.children.children.cars

children*

Personage

Carscars

**