src/swipl-devel/packages/xpce/man/refmanual/md/constraint.md

class constraint {#class-constraint}

A constraint is a relation between two objects that must be maintained. A constraint is defined by a relation object. Class relation is a super-class for all predefined relations.

Constraints deal properly with cycles. Contradictory constraints lead to unpredictable results.

Constraints are commonly used to maintain relations between graphicals. They can also be used to maintain a relation between dialog objects and model objects. The example below illustrates both usages.

:- pce_global(@center, new(identity(center))).

constraint_demo :-
    new(P, picture('Constraint Demo')),
    send(new(D, dialog), below, P),

    send(P, display, new(B1, box(100,100))),
    send(P, display, new(B2, box(50,50))),
    send_list([B1, B2], recogniser,
          handler_group(new(resize_gesture),
                new(move_gesture))),
    new(_, constraint(B1, B2, @center)),

    send(D, append, new(S1, slider(center_x, 0, 500, 200))),
    send(D, append, new(S2, slider(center_y, 0, 500, 200))),
    send_list([S1, S2], drag, @on),
    send(D, append, button(quit, message(D, destroy))),

    new(_, constraint(S1, B1,
                      identity(selection, center_x))),
    new(_, constraint(S2, B2,
                      identity(selection, center_y))),

    send(D, open).

Bugs:

Currently, the constraint mechanism implies that everytime a send-message is accepted by a constrainted object, the associated relation is evaluated. This is unnecessary expensive and limits the application area of constraints to exist only between objects that do not receive time-critical messages.

It is not possible to create a relation with ITSELF. The following does - not* work: make_square(S, W) :- new(S, box(W,W)), new(_, constraint(S, S, indentity(width, height))).

@see class connection @see class hyper @see class interceptor @see class relation @see object<-all_constraints

Instance variables {#class-constraint-instvars}

Send methods {#class-constraint-send}



Try the rswipl package in your browser

Any scripts or data that you put into this service are public.

rswipl documentation built on June 16, 2026, 5:07 p.m.