list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Derived Attributes

When declaring an attribute in Umple, you can specify an arbitrary expression after the equals sign to create an attribute that will be computed. There will be no set method on such an attribute.

Note that unless you use the simplest of expressions, you will be limited to only being able to generate code for the language of the expression.

You should make sure you call the get methods provided in the Umple-generated API (rather than directly accessing variables) and avoid having any side-effects in your expressions. Currently this is not enforced, but may be in the future.

For other examples of derived attributes see the sections on the Delegation pattern and sorted associations.

Example

class Point 
{
    // Cartesian coordinates
    Float x;
    Float y;
    
    // Polar coordinates
    Float rho = {
      Math.sqrt(Math.pow(getX(), 2)
      + Math.pow(getY(), 2))
    }
    Float theta =
      {Math.toDegrees(Math.atan2(getY(),getX()))}
}
// @@@skipcppcompile

      

Load the above code into UmpleOnline

 

Example with the Language of the Expression Being Specified

class Rectangle {
  Double height;
  Double width;
  Double area =
    Java {height*width} Php {$height*$width}
}
// @@@skipcppcompile

      

Load the above code into UmpleOnline

 

Syntax


derivedAttribute- : [=modifier:immutable
    |settable
    |internal
    |defaulted
    |const
    |fixml]? [[typedName]] =
  ( [[moreCode]]
  )+