## Calculating value of derived property

Discussions about Domain Specific Language customizations

Moderator: Moderators

### Calculating value of derived property

Hello,
Context:
I've created a stereotype to extend Class entity with new properties, e.g. new entity is Risk and it has properties Probability and Severity of type float. I'm also adding a derived property named Magnitude through Risk customization.

Problem:
I want to calculate value of Magnitude automatically based on other properties values, i.e. Magnitude = Severity * Probability. Somehow I feel that OCL should do the job, however I'm unable to figure out how to make it work. I even tried to set OCL expression of derived property to 'self = 0.1', hoping to see that every Risk instance has Magnitude automatically set to 0.1, but it didn't work - value was undefined.

Question:
is it even possible to calculate arithmetic value of derived property using OCL? Or maybe there is even more simple solution?

Thanks!
henrikas@rubedo.lt
Forum Newbie

Posts: 5
Posts Rating:0
Joined: Thu Jun 19, 2014 5:44 am
Full name: Henrikas

### Re: Calculating value of derived property

Hello,

You have to change types of Probability, Severity and Magnitude properties to Real (UML Standard Profile::UML2 Metamodel::PrimitiveTypes::Real).
Also multiplicity should be changed to 1.

This should help.

Best Regards,
Rimvydas Vaidelis
rimvydas.vaidelis
Customer Support

Posts: 262
Posts Rating:14
Joined: Fri May 28, 2010 4:45 am

### Re: Calculating value of derived property

Thanks for your response,
changing type to 'Real' helped a bit - now default values of Risk entity new properties are set automatically. However, OCL expression can only read properties, but not assign new value. E.g. expression
Code: Select all
`context Risk[Class]::Magnitude:Real derive: self.Magnitude = 0.5`

would display 'Error while parsing OCL', whenever I try to create an instance of type 'Risk'. I've also tried another expression:
Code: Select all
`context Risk[Class]::Magnitude:Real derive: self.Rate = 0.5`

which shows no error, but value of Rate is still not assigned. It seems that OCL can recognize self.Rate and assignment statement, but value is assigned only temporary?

Note:
'Magnitude' is a derived property
'Rate' is a property defined by 'Risk' stereotype.

EDIT: figured it out! Expression must not contain assignment, but only desired value

Code: Select all
`context Risk[Class]::Magnitude:Real derive: self.Probability * self.Rate`

The only question remaining - why this derived property is not updated every time I change properties it depends on?
henrikas@rubedo.lt
Forum Newbie

Posts: 5
Posts Rating:0
Joined: Thu Jun 19, 2014 5:44 am
Full name: Henrikas

### Re: Calculating value of derived property

Hello,

OCL expression should calculate a value. You can not specify assigning of the value to a property (this will be performed by other mechanism but not OCL).

Code: Select all
`self.Magnitude = 0.5`

should be changed to:

Code: Select all
`0.5`

The property is not updated because MD does not know how to do that. MD provides the support for updating a property value which is calculated using OCL.
You should implement (in Java) the property listener configuration and specify it in the Criterion Editor dialog. Specify qualified name of the implementation class, add the jar that contains the implementation class to the MD classpath and restart MD.

Implementation of the listener configuration:
Code: Select all
`package com.nomagic.magicdraw.examples.expression;import com.nomagic.magicdraw.validation.SmartListenerConfigurationProvider;import com.nomagic.uml2.ext.jmi.smartlistener.SmartListenerConfig;import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;import com.nomagic.uml2.impl.PropertyNames;import javax.annotation.CheckForNull;import java.util.Collection;import java.util.Collections;import java.util.List;import java.util.Map;public class TagsChangeConfigurationProvider implements SmartListenerConfigurationProvider{   @CheckForNull   @Override   public Map<Class<? extends Element>, Collection<SmartListenerConfig>> getListenerConfigurations()   {      SmartListenerConfig config = new SmartListenerConfig();      config.listenTo(PropertyNames.APPLIED_STEREOTYPE_INSTANCE, SmartListenerConfig.INSTANCE_SLOT_VALUE_CONFIG);      List<SmartListenerConfig> configs = Collections.singletonList(config);      // Element.class fits for all UML elements but if your expression will be used for a more specific type then specify the type here      return Collections.<Class<? extends Element>, Collection<SmartListenerConfig>>singletonMap(Element.class, configs);   }}`

Best regards,
Rimvydas Vaidelis
rimvydas.vaidelis
Customer Support

Posts: 262
Posts Rating:14
Joined: Fri May 28, 2010 4:45 am

### Re: Calculating value of derived property

The property is not updated because MD does not know how to do that. MD provides the support for updating a property value which is calculated using OCL.
You should implement (in Java) the property listener configuration and specify it in the Criterion Editor dialog. Specify qualified name of the implementation class, add the jar that contains the implementation class to the MD classpath and restart MD.

Figured out how to solve it without java listener: just added Meta Chain expressions pointing to properties 'Probability' and 'Severity' together with OCL expression and now derived property 'Magnitude' is updated in real time!
henrikas@rubedo.lt
Forum Newbie

Posts: 5
Posts Rating:0
Joined: Thu Jun 19, 2014 5:44 am
Full name: Henrikas

### Re: Calculating value of derived property

You can use the workaround if it works but it might stop working in the future.
rimvydas.vaidelis
Customer Support

Posts: 262
Posts Rating:14
Joined: Fri May 28, 2010 4:45 am

### Re: Calculating value of derived property

Hello,
"just added Meta Chain expressions pointing to properties 'Probability' and 'Severity' together with OCL expression and now derived property 'Magnitude' is updated in real time!"

Can you post how you did this?

./ks
kevin.m.somervill@nasa.gov
Forum Newbie

Posts: 15
Posts Rating:1
Joined: Fri Mar 14, 2014 9:20 am

### Re: Calculating value of derived property

Can i Create a non-calcualted or descriptive value fro new property
rickcole@bandce.co.uk
Forum Newbie

Posts: 2
Posts Rating:0
Joined: Fri Jul 12, 2019 4:23 am

### Re: Calculating value of derived property

You can create a tag.
rimvydas.vaidelis
Customer Support

Posts: 262
Posts Rating:14
Joined: Fri May 28, 2010 4:45 am

### Re: Calculating value of derived property

I am trying to complete a very similar task; I have a stereotype Risk, with two properties Likelihood and Impact and a derived property Severity, which should be calculated as Likelihood and Impact. However, when I enter its OCL expression as self.Likelihood * self.Impact, I only receive a "Can not evaluate OCL error."
annordstrom@mix.wvu.edu
Forum Newbie

Posts: 2
Posts Rating:0
Joined: Mon Jun 17, 2019 7:22 am

Next

Return to DSL

### Who is online

Users browsing this forum: No registered users and 0 guests