Part 1 OCL Common Expressions

An introduction to OCL - the object contraints language - by example. The session also introduce the new MDriven debugger and shows how to get instant results of modelling efforts.

To make your experience more comfortable, we set the main tags mentioned in the video to the right bar menu of this mini player. Choose the interesting subtitle on the list and immediately get to the exact theme timeplace in the video. Now you can pick any topic to be instructed without watching the whole video.

Common expressions
    .create .allinstances .select .asstring .representation
at0 and at operator assign operator orderby orderbyDescending loopvar ocl editor overhaul support execution of expressions union operator ocl use in the model (string representation) collect operator member of ECOModel Root abstract classes and its inheritance specialization .OclType derived attribute

Raw subtitles text

hi and welcome to the session on ocl

expressions start with a simple model

containing of fruits orange as one

property

weight type integer and gonna start a

system running this saving data to an xml file

that we save on in temp file "fruit"

I'm going to use the new debugger

so if you haven't seen this before

it's a good introduction to that as well

and the new debugger allows you to

write actions as long as ocl, so we

could do orange.create and then

we have actually created one orange object

an instance of the class, so let's

create some oranges and once we have them we

can do orange.all instances

which is a common ocl operator to show all

instances of a certain class

as we see the weight is 0. It's the default value for

integer that's not nullable

and we can index the list of all instances with

the  "at(0) operator

so at 0 there's also an at operator, but at(0) is

zero based the at operator is one based

so starting from zero to count minus 1

or length minus one

and let's do an action at first object at list

assigning the weight to 18

so this is the assign operator colon equals and

we can set the weight values

something like that and then we'd save,

now we have saved them to the xml file

that we stated initially and in ocl we can

do a lot of different operations

let's check out the "orderby", orderby(weight)

and there's also an orderby this ending

and in all these iterations, there

can be a loop variable and

it's separated with the pipe sign, so that's

what you here, the loopvar

it is just each object iterating

in the loop, so it will be of the type

that interested iteration, so orderDescending,

or orderby

results an operation for first and of

course one for last that the given a list

just returns one object and you can

combine ocl expressions

so order descending last

the ocl editor has been given an

overhaul to better support

support you as you type your expressions it's

not perfect and we will see some issues with it

but it's getting there

select is a common ocl operator

that we want to limit our results at

here we select on the weight larger than 20,

but the loop variable is optional,

so you do not always need it

all kind of comparison operators are

available larger than or equal etc.

let's add another class, apple

also has a weight

and if I do apple.all instances apple is

not a type name, because I need to

reread the model in order to have my

executor to know about it

so once I re-read and it doesn't give me

the error, but there are no apples of course

so write action: and then Apple.create

get two of those, five and the new debugger

here interprets all continuous lines

as one expression, so you need to

leave an empty roll between

expressions that are not supposed to

belong to each others

and of course it defaults to ocl but as soon as you

write action: it will do action language instead

which allows you to set values to operate on values to change state

normally ocl is totally free from side effects

but the action language that is unique for MDriven

allows you to operate objects as well

with the ocl syntax so we're

setting some weights on the apples

and we saved that state

so Apple.all instances also have the

oranges since before

these are the oranges

these are the apples and what we can do

is combine them, the operator called "union"

so there we are together, but we don't

see not very clear what they are

so let's add the default string

representation to the classes

this is one place where you actually use ocl

in the model of course you use it all

over the model but this is just one of

the places "orange with weight of

and then we grab our "selves" weight and append

grams, but since weight is an integer

we need to convert it to as #sString in

order to get use a +operator on strings

let's do the same thing for the Apple

but right Apple instead save the model

yes still don't see anything because we

haven't said that we want

to see it as string, the default string

representation on the obvious now I see them

what I can do is use to collect

operator, to build a tuple of pieces from

the result that I'm working on

so the loop variable will be one of the

objects in the iteration and that should

have weight and I can do that as string

if I can spell, but now we see that weigh it,

this is not correct weight is not a

member of eco model root

why does it say EcoModelRoot well

because that's the common denominator

between apple and orange currently,

so here we have discovered a need for an

abstraction really apple and orange are

really sharing things

we should make that available in the model

so that we can operate on it

so let's interview the abstraction

called fruit and we model this with the

generalization arrow

and we want to add the attribute

weight to fruit, but doing so we will find that there's error

reported on model,

because now orange and apple inherit weight from fruit and they

have fruit on their own so that's not allowed

but renaming it to new weight instead of weight,

so we can see that we get it work

new weight, then it's ok but of course

the new wa is 0, because we haven't set

but now we can do act on the

abstraction the fruit fruit.allinstansec

as string, that will return both

oranges and apples,

but it reports common denominator as new weight is the

only thing they share

so let's say that this is the weight and

there is actually no need for a unique

weight to apples or unique weight on oranges

it's just weight of the fruit

then we see the weight on the fruit

so this is why modeling is so good

because it works exactly as we think

about things in daily life, we use

abstractions all the time just getting

around life, just grabbing a chair on our

new office that we've never visited before

him instantly knowing that it

will work as any other chair is our ability to abstract

but having the fruits can we access

their specialization what they're

actually called what kind of fruit they are

yes, there's some ocl-type operator with

the type name that gives us this information

orange and apple, there we see, so that would

allow us to break out the hard-coded orange

to self ocl-type typename

remove the orange and of course if we do

it there we might as well

yeah let's, look at this first

orange.string is still ok, so if we can

do that we might as well move it up to fruit

going to remove it from the

subclasses just have it on the

superclass fruit, so like that, re-read

yay works like before but with less code or less ocl

the fruit is actually abstract we don't think of fruit as being

there's no, nothing existing just

being fruit is either orange or Apple in our model

so I mark it as abstract and

the class name is drawn italic instead

I can create apples, but I'm not supposed

to be able to create fruit

can't instantiate abstract class fruit, this

will help us to avoid doing illegal

things as our model evolves, orange I can

create oranges and apples but not fruits

that's ok and I can access fruit all instances or

just the oranges or just the apples

and I can act on fruits checking their

weight and I get us all the fruits

having a weight larger than 20 grams both oranges

and apples and move this or rather copy it

to a new attribute, attributes

this attribute is called presentation

not going to make it derived, derived

attribute can be backed by an derivation ocl

and ocl expression this is another

place where you commonly use ocl going

to set the same thing as in the default

string representation, but since I

defined it as a derived attribute might

as well use it in the default string representation

reduce the definitions,

re-read the model, since we changed it and

now when I execute I see I have a new

property on the object fruit called presentation

that works for apples and the first

Apple is Apple would wait 15 grams

let's see, if I can change its weight

and watch when I change the weight both

the derived presentation and the weight changes

this is an extremely large and

positive thing with derived attributes

they subscribe to every change that you

see in the model reduces the amount of

code you need to write or the amount of

ocl, so that was the first part of the ocl

I'm going to come back straight away

with another part continuing with

the same sample thank you for watching, so far

This page was edited 92 days ago on 02/10/2024. What links here