How can (inv:, def:, init:, derive:, pre:, post:, body: ) type constraints be modeled?
All of the variants, defined in OCL 2.0 spec can be modeled (inv:, def:, init:, derive:, pre:, post:, body: ), but each is modeled in a slightly different way. Correspondingly, the header line in the OCL expression editor is generated according to model situation.*
inv:* is easiest - any simple constraint having an OCL2.0 expression language is treated as invariant. They should have a field constrainedElement filled in, pointing to some class (constrainedElement=SomeClass). To be extra thorough in following the OCL spec, you should also apply the <<invariant>> stereotype on the constraint, but this is not necessary - it is implied.
Note that any other OCL constraints/expressions, that the handling code can not classify into the more specific categories (as defined below) are treated as inv: constraints.*
def:* - definition constraints are modeled in the same way as invariant constraints, but the constraint must have <<definition>> stereotype applied. The concrete element to be defined (additional field or additional operation) is placed inside of the body.*
init:* - this has to be an expression (not a constraint!) placed in some property's defaultValue field. So, you need to create some property in your class, then open specification of that property, then rightclick the Default Value field, go to >Value Specification>Opaque Expression. After this you can enter the OCL into the field in the same manner as you enter OCL into the specification field of the constraint*.
derive:* - is modeled the same way as init, but the property has to be derived ( isDerived=true ).*
pre:, *post: - these are constraints on some operation of the class. However these must be placed NOT as a simple constraints(rightclick>New Element>Constraint) BUT in a special designated fields(metaproperties) of the operation. Open operation specification window, switch into Expert mode, see the Precondition, Postcondition fields. Click the necesary field, click [+], then choose Constraint. Fill in the OCL constraint as necessary.*
body:* - body condition is filled in the same way as pre and post conditions, but there is an additional hoop to jump through - for some reason, I do not know why, Body Condition field is marked as not shown in Expert mode (neither in Standart). So in specification window of Operation, click Customize button, find Body Condition field, and flip the radio button to Expert, and OK the dialog. After this, Body Condition field will appear in Expert mode specification window of operation in the same way Precondition and Postcondition fields do.
Small nuance: operation can have multiple pre and post conditions but only one body condition.
All these things are described/mandated in the OCL spec (06-05-01.pdf for OCL2.0), chapter 12: The Use of OCL Expressions in UML Models.
Modeling of these expression types is also briefly mentioned in MagicDraw UserManual.pdf. See chapter 8 Model Analysis>Validation>Advanced Topics>Modeling other types OCL2.0 constraints/expressions