16.1. Functions ("Calculation Definitions") and Constraints

Calculations

A SysML2 calculation is, like a KerML function evaluation, a kind of action that has a result value.

A calculation definition is a kind of action definition that has a distinguished parameter with direction out called the result parameter (which is usually the only out parameter). [7.19]

In a calculation definition, the result parameter is declared as an out parameter using the keyword return instead of out, as in the following example:

calc def Average {
  in scores[1..*] : Rational;
  return : Rational = sum( scores) / size( scores);
}

A calculation property ("usage") is an action property that is typed by a calculation definition (i.e., by a function).

Constraints

A constraint type definition is, like a KerML Predicate, a Boolean function. A constraint property is either typed by a constraint type or defined by an inline Boolean expression.

part def FuelTank {
  attribute fuelLevel : Real;
  attribute readonly maxFuelLevel : Real;
}
constraint def IsFull {
  in tank : FuelTank;
  tank.fuelLevel >= tank.maxFuelLevel  // result expression
}
part def Vehicle {
  part fuelTank : FuelTank;
  constraint isFull : IsFull {
    in tank = fuelTank;
  }
}

In general, a constraint property may be satisfied sometimes and violated other times. However, an assert constraint property asserts that the result of a given constraint must be always true at all times, as in the following example:

part def FuelTank {
  attribute fuelLevel : Real;
  attribute readonly maxFuelLevel : Real;
}
part def Vehicle {
  part tank : FuelTank;
  assert constraint { tank.fuelLevel <= tank.maxFuelLevel }
}