next up previous contents index
Next: Ordering constraints Up: Constraints Previous: Composite constraints

Defining constraints

We can define our own constraints. A constraint declaration takes the following form:

       constraint Name(FormalParameterList) ConstraintBody
It defines a constraint, called Name, among a list of formal parameters. FormalParameterList is a list of type and name pairs, and ConstraintBody is a constraint block.  

Constraint definitions do not reside in any class, and the constraints defined can be used by any classes. This is a source for debate because it does not compliant with the pure object-oriented design principle. Nevertheless, as a relation is not directional, putting it into any one of the involved classes would make description unnatural.

The following defines the symbolic constraint inside(O1,O2) for two rectangles:

      constraint inside(Rectangle O1, Rectangle O2) {
           O1.x <= O2.x;
           O1.y <= O2.y;
           O1.x + O1.width <= O2.x + O2.width;
           O1.y + O2.height <= O2.y + O2.height;
      }
A call to a constraint definition is macro-expended to the constraint body.



Neng-Fa ZHOU
Sat Apr 18 16:14:29 JST 1998