OCLSnippets

From Eclipsepedia

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.


Contents

Set Operations

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

self.children->isEmpty()

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

self.children->notEmpty()


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

self.name.size() > 0

Note from Christian W. Damus: If the 'name' attribute can have a null value, then when it is null, self.name.size() 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

MyObject.allInstances()->isUnique(name)

Expression 2

MyObject.allInstances()->select(m : MyObject | m <> self and m.name = self.name)->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{
                    '0','1','2','3','4','5','6','7','8','9'
                }->at(self.div(denominator).mod(10) + 1)
            in
                if s='' and numberAsString = '0' then
                    s
                else
                    s.concat(numberAsString)
                endif
        )

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.

self.allOwnedElements()
  ->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:

  
Results:
'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.

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

Finding all classifiers with a property named 'foo'

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

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

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