Page 1 of 2

Calculating value of derived property

PostPosted: Thu Jun 19, 2014 5:58 am
by henrikas@rubedo.lt
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.
Image


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!

Re: Calculating value of derived property

PostPosted: Thu Jun 26, 2014 8:43 am
by rimvydas.vaidelis
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

Re: Calculating value of derived property

PostPosted: Tue Jul 01, 2014 6:15 am
by henrikas@rubedo.lt
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?

Re: Calculating value of derived property

PostPosted: Thu Jul 03, 2014 2:22 am
by rimvydas.vaidelis
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

Re: Calculating value of derived property

PostPosted: Tue Jul 15, 2014 9:43 am
by henrikas@rubedo.lt
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! 8-)

Re: Calculating value of derived property

PostPosted: Wed Jul 16, 2014 8:22 am
by rimvydas.vaidelis
You can use the workaround if it works but it might stop working in the future.

Re: Calculating value of derived property

PostPosted: Thu Apr 21, 2016 9:46 am
by kevin.m.somervill@nasa.gov
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

Re: Calculating value of derived property

PostPosted: Fri Jul 12, 2019 5:06 am
by rickcole@bandce.co.uk
Can i Create a non-calcualted or descriptive value fro new property

Re: Calculating value of derived property

PostPosted: Mon Jul 15, 2019 8:02 am
by rimvydas.vaidelis
You can create a tag.

Re: Calculating value of derived property

PostPosted: Thu Jul 25, 2019 8:04 am
by annordstrom@mix.wvu.edu
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."