Example 3.12 demonstrates the usage of the sub-Model concept in a simple example implementing a function for a numerical function analysis program expecting MDL classes of the type SimpleFunc. An alternative implementation using multiple inheritance is given in Example 3.13.
Instance Function2Evaluate = SiFunc; // Select the MDL class
NewModel SimpleFunc { // Define the common interface
Interface { // in a common base class
protected Parameter<double> x; // independent variable
protected Parameter<double> y; // dependent variable
}
}
NewModel SinusFunc : SimpleFunc { // inherit the interface
evaluate { :y = sin(:x); }
}
NewModel DivFunc : SimpleFunc { // inherit the interface
evaluate { :y /= :x; }
}
NewModel SiFunc : SimpleFunc { // inherit the interface
Instance N = SinusFunc; // aggregate the functionality
Instance D = DivFunc; // of other MDL classes
Local {
// double_eps for a 53 bit IEEE mantissa
Parameter<double> dbl_EPS = exp(-52*ln(2));
Parameter<double> eps = 2.*sqrt(6.*dbl_EPS);
}
evaluate {
if ( fabs(:x) > eps ) {
call N.evaluate;
call D.evaluate;
} else {
:y = 1.0;
}
}
}
Example 3.12: Aggregation of MDL classes
To alter the default value of an interface parameter of a sub-Model, the following statement is applicable:
Parameter subModelName.parameterName = expr;
The interface parameter parameterName of the sub-Model instance subModelName adopts the result of expr as its new default value.