Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search


A collection of workable OCL statements. If you use a statement not listed below, please enhance this list for the benefit of the community.

Set Operations

Test for empty collection where 'self' is the class being checked and 'children' is a Set, List or Collection in this class


Test for not empty collection where 'self' is the class being checked and 'children' is a Set, List or Collection in this class


String Operations

Test for String with zero length where 'self' is the class being checked and 'name' is a not nullable String variable in this class > 0

Note from Christian W. Damus: If the 'name' attribute can have a null value, then when it is null, evaluates to OclInvalid, and OclInvalid is not comparable with integers, resulting in OclInvalid again. In checking this constraint, the system will interpret OclInvalid as a failure (boolean false).

Class Operations

Unique text over all instances expressions where 'MyObject' is the Class and 'name' is an String variable in MyObject

Expression 1


Expression 2

MyObject.allInstances()->select(m : MyObject | m <> self and =>isEmpty()

Integer to String Conversion

Definition of toString() operation on OCL's standard Integer data type for less or equal than 6 digits numbers. For another number of digits wanted add more or less powers of ten.

context Integer
def: toString() : String = 
        OrderedSet{1000000, 10000, 1000, 100, 10, 1}->iterate(
            denominator : Integer;
            s : String = ''|
            let numberAsString : String = OrderedSet{
                }->at(self.div(denominator).mod(10) + 1)
                if s='' and numberAsString = '0' then

Showing the names and metaclasses for all named elements in a UML model

The following ECore M2 snippet shows the names of all named elements in a UML model.

  ->select(e | e.oclIsKindOf(NamedElement) and e.oclAsType(NamedElement).name <> null)
  ->collect(n | n.oclAsType(NamedElement).name.concat(' : ').concat(n.eClass().name))

For instance, for the library sample model, it shows:

'Writer : Class'
'Mystery : EnumerationLiteral'
'category : Property'
'Biography : EnumerationLiteral'
'books : Property'
'books : Property'
'Book2 : Class'
'author : Property'
'title : Property'
'ScienceFiction : EnumerationLiteral'
'pages : Property'
'BookCategory : Enumeration'
'writers : Property'
'Library : Class'
'name : Property'
'name : Property'

Finding all elements with a given tagged value

The following UML M2 OCL snippet shows all elements with the Ecore::EAttribute::isTransient property set to true.

  ->select(e | e.getValue(e.getAppliedStereotype('Ecore::EAttribute'), 'isTransient') = true)

Finding all classifiers with a property named 'foo'

  ->select(e | e.oclIsKindOf(Classifier))
  ->collect(e | e.oclAsType(Classifier))
  ->select(c | c.getAllAttributes()->exists(p | = 'foo'))

Finding all classifiers that specialize an arbitrary classifier named 'foo::Bar'

  ->select(e | e.oclIsKindOf(Classifier))
  ->collect(e | e.oclAsType(Classifier))
  ->select(c | c.general->exists(general | general.qualifiedName = 'foo::Bar'))  

Back to the top