Condition Examples
Use a constraint to specify that automatic references can be set only if the referenced object meets a specified condition. (You can specify constraints only on automatic references and you cannot specify parameters.)
In the examples in this subsection, the reference properties listed in the following table are defined on the Company class, with an inverse myCompany reference.
Reference | Defined Inverse |
---|---|
allSalaried | EmployeeDictionary, where isSalaried of Employee explicitInverse; |
allWaged | EmployeeDictionary, where isWaged of Employee explicitInverse; |
allContractors | EmployeeDictionary, where isContractor of Employee explicitInverse; |
allTempStaff | EmployeeDictionary, where isTempStaff of Employee explicitInverse; |
allEmployees | EmployeeDictionary, explicitInverse; |
The value of the Inverse Not Required check box on the extended Define Reference dialog determines whether exceptions are raised when no inverse is set, as none of the conditional inverses is set. Both the compatibility and constraints are applied.
-
No inverse is set because the type of the object is not compatible with any of the inverse references
-
No inverse is set because the constraint fails
If no inverse is set and the Inverse Not Required check box is checked, no exception is raised if the compatibility or constraint fails. Similarly, no exception is raised if the last inverse is removed as the result of change to a property and the Inverse Not Required check box is checked.
The following condition examples assume that an Employee class has an integer value payFrequency property, a string value status property, and a myCompany property of type Company. The compareUpper condition is a condition defined on the String primitive type.
isWaged(): Boolean condition; begin return payFrequency = 7; end; isSalaried(): Boolean condition; begin return not isWaged; end; isTempStaff(): Boolean condition; begin return status.compareUpper("T"); end; isContractor(): Boolean condition; begin return not isTempStaff; end;
Conditions on Primitive Types
The following examples are of an isMultipleOf condition method defined on the
isMultipleOf(d : Integer): Boolean condition; begin return self div d = 0; end; isEqual(name: String): Boolean condition; begin return self = name; end;
Using these isEqual and isMultipleOf condition methods, the following example is a primCond condition defined on a Cls1 class with an int attribute of primitive type
primCond(): Boolean condition; begin return str.isEqual("END") or int.isMultipleOf(9); end;
This primCond condition can be used as a constraint because it has no parameters, which applies even though it calls conditions that have constant value parameters.