OCLOperators allInstances
No edit summary
mNo edit summary
Line 121: Line 121:
!Description
!Description
|-
|-
| ->append
| '''->append'''
|Add another object last
|Add another object last
|-
|-
| ->asBag
| '''->asBag'''
|Collapses to one list
|Collapses to one list
|-
|-
| ->asSequence
| '''->asSequence'''
|Collapses to one list
|Collapses to one list
|-
|-
| ->asset
| '''->asset'''
|Remove doublets
|Remove doublets
|-
|-
| ->at
| '''->at'''
|Get the objects at X where the first index is 1
|Get the objects at X where the first index is 1
|-
|-
| ->at0
| '''->at0'''
|Get the objects at X where the first index is 0
|Get the objects at X where the first index is 0
|-
|-
| ->collect
| '''->collect'''
|Iterate over the collection and build a tuple result
|Iterate over the collection and build a tuple result
|-
|-
| ->count
| '''->count'''
|Count how many meet a certain criteria
|Count how many meet a certain criteria
|-
|-
| ->difference
| '''->difference'''
|The difference between 2 collections
|The difference between 2 collections
|-
|-
| ->excluding
| '''->excluding'''
|The collection except this single object
|The collection except this single object
|-
|-
| ->exists
| '''->exists'''
|Are there any objects that fulfill the criteria
|Are there any objects that fulfill the criteria
|-
|-
| ->filterOnType
| '''->filterOnType'''
|Only keep the ones of a certain type
|Only keep the ones of a certain type
|-
|-
| ->first
| '''->first'''
|Return the first object
|Return the first object
|-
|-
| ->forAll
| '''->forAll'''
|Iterate all that fulfills the critera
|Iterate all that fulfills the critera
|-
|-
| ->groupBy
| '''->groupBy'''
|Build collection of tuples grouped by some aspect
|Build collection of tuples grouped by some aspect
|-
|-
| ->includes
| '''->includes'''
|Does the collection include the object
|Does the collection include the object
|-
|-
| ->includesAll
| '''->includesAll'''
|Does the collection include the whole other collection
|Does the collection include the whole other collection
|-
|-
| ->including
| '''->including'''
|
|
|-
|-
| ->IndexOf
| '''->IndexOf'''
|The 1 based index of an object in the collection possibly -1 if not existing
|The 1 based index of an object in the collection possibly -1 if not existing
|-
|-
| ->indexOf0
| '''->indexOf0'''
|The 0 based index of an object in the collection possibly -1 if not existing
|The 0 based index of an object in the collection possibly -1 if not existing
|-
|-
| ->intersection
| '''->intersection'''
|The intersection of two collections
|The intersection of two collections
|-
|-
| ->isEmpty
| '''->isEmpty'''
|Returns true if the collection is empty
|Returns true if the collection is empty
|-
|-
| ->last
| '''->last'''
|Returns the last object in the collection
|Returns the last object in the collection
|-
|-
| ->notEmpty
| '''->notEmpty'''
|Returns true of the collection is not empty
|Returns true of the collection is not empty
|-
|-
| ->orderBy
| '''->orderBy'''
|Sorts the collection on one or more properties
|Sorts the collection on one or more properties
|-
|-
| ->orderDescending
| '''->orderDescending'''
|Sort the from biggest to smallest
|Sort the from biggest to smallest
|-
|-
| ->orderGeneric
| '''->orderGeneric'''
|Sorts the list of properties with interchangeable sort order: (expr1, OclSortDirection::ascending, expr2, OclSortDirection::descending...)
|Sorts the list of properties with interchangeable sort order: (expr1, OclSortDirection::ascending, expr2, OclSortDirection::descending...)
|-
|-
| ->prepend
| '''->prepend'''
|Add an object in front of the list
|Add an object in front of the list
|-
|-
| ->reject
| '''->reject'''
|Returns the objects not matching the criteria
|Returns the objects not matching the criteria
|-
|-
| ->select
| '''->select'''
|Returns the objects matching the criteria
|Returns the objects matching the criteria
|-
|-
| ->size
| '''->size'''
|Returns the number of elements in the collection
|Returns the number of elements in the collection
|-
|-
|<nowiki>->subsequence</nowiki>
|'''->subsequence'''
|Returns a smaller collection from a start to stop
|Returns a smaller collection from a start to stop
|-
|-
|<nowiki>->symmetricDifference</nowiki>
|'''->symmetricDifference'''
|The symmetric difference between the collections; ie all the objects in collection1 or collection2 but not in both
|The symmetric difference between the collections; ie all the objects in collection1 or collection2 but not in both
|-
|-
| ->union
| '''->union'''
|The set of objects in collection1 and objects in collection2
|The set of objects in collection1 and objects in collection2
|}
|}
One important aspect of OCL that is worth noting is that it expands lists of lists to just a list. An example in plain English; Thing.allinstances.Details – this will come back as a set of details that are all the details from all the Things. If OCL had not expanded lists automatically one could have expected a set of sets containing the details per thing. But this is not the case. The automatically expansion of lists of lists is sometime referred to as flattening of a collection – referring to the reduction of topology in the result.
One important aspect of OCL that is worth noting is that it expands lists of lists to just a list. An example in plain English; Thing.allinstances.Details – this will come back as a set of details that are all the details from all the Things. If OCL had not expanded lists automatically one could have expected a set of sets containing the details per thing. But this is not the case. The automatically expansion of lists of lists is sometime referred to as flattening of a collection – referring to the reduction of topology in the result.

Revision as of 12:44, 13 August 2017

It is a common operator. To find all available you can open the OCL-Editor and type in a class:

Your model is central to all expression you will handle. We will use this model to for the examples:

Allinstances operator.png

Operators Description
Thing.allinstances Gives you a list of all Things
Things.allinstances->select(someInt>3) Only things with someInt bigger than 3
Thing.allinstances->select( (someInt>3) and (someInt<6)) Only things with someInt bigger than 3 but less than 6. Notice the extra parenthesis to or the Boolean expressions together
Things.allinstances->select(x|x.someInt>3) Here we introduce the loop variable x. We separate the definition of x from the usage of x with the pipe sign “|”. Loop variables are optional but if names are unique – but you will need to use them to give precision or to if you want to perform operations on the loop context itself.
Things.allinstances.Details Gives a list of all detail objects that are connected to a Thing. The Detail objects that float around without a Thing will not be in the list
Things.allinstances.Details.Attribute1 A list of nullable strings from the contents from the details attribute1. Note that OCL is null-tolerant – you do not need to check if the Details exists of not – the language handles null checks for you
SubClassThing1.allinstances.Details Inherited features of classes are directly accessible
Thing.allInstances- >select(x|x.safeCast(SubClassThing1). OnlyAvailableInSubClass='Hello') Filtering on Specialization is done with an operator SafeCast. This is null safe so for all objects that do not fit the profile the expression returns false

To find all available you can open the OCL-Editor and type in a class:

Ocl-editor 1.png

The operations listed do this:

Operators Description
Allinstances All the objects of the class
allinstancesAtTime All the currently loaded instances
AllStates Meta information about available states in state machines the class may contain
allSubClasses Meta information on all the sub classes this class has
AllSuperTypes Meta information on all the super classes – in inheritance order the class has
associationEnds Meta information on all the associationEnds
Asstring The string representation of the class – the asString operation is available on everything
Attributes Meta information about what attributes the class has
Contraints Meta information on what constraints the class has
Emptylist Returns an empty list typed to hold objects of the class
IsDirtyMember
isNull
nullValue A typed null value
objectFromExternalId An external identity will be resolved to the object
oclAsType The type of the class
oclIsKindOf This is to if a class is a subclass or a the class itself and not unrelated
oclIsTypeOf Returns true if
oclSingleton Classes that implements the Singleton pattern – by setting IsSingleton=true – will return the singleton instance with this operator
OclType
safeCast
SuperTypes
TaggedValue Meta information on tagged values set in the class
TaggedValueOnFeature Meta information on Tagged values set on a named feature in the class
Typename The type name as a string
ViewModels A tuple with the ViewModels for this class a members

Once you have a collection of objects there are certain operators that are applicable to it. Again you can use the OCL-Editor to see what they are:

Operators Description
->append Add another object last
->asBag Collapses to one list
->asSequence Collapses to one list
->asset Remove doublets
->at Get the objects at X where the first index is 1
->at0 Get the objects at X where the first index is 0
->collect Iterate over the collection and build a tuple result
->count Count how many meet a certain criteria
->difference The difference between 2 collections
->excluding The collection except this single object
->exists Are there any objects that fulfill the criteria
->filterOnType Only keep the ones of a certain type
->first Return the first object
->forAll Iterate all that fulfills the critera
->groupBy Build collection of tuples grouped by some aspect
->includes Does the collection include the object
->includesAll Does the collection include the whole other collection
->including
->IndexOf The 1 based index of an object in the collection possibly -1 if not existing
->indexOf0 The 0 based index of an object in the collection possibly -1 if not existing
->intersection The intersection of two collections
->isEmpty Returns true if the collection is empty
->last Returns the last object in the collection
->notEmpty Returns true of the collection is not empty
->orderBy Sorts the collection on one or more properties
->orderDescending Sort the from biggest to smallest
->orderGeneric Sorts the list of properties with interchangeable sort order: (expr1, OclSortDirection::ascending, expr2, OclSortDirection::descending...)
->prepend Add an object in front of the list
->reject Returns the objects not matching the criteria
->select Returns the objects matching the criteria
->size Returns the number of elements in the collection
->subsequence Returns a smaller collection from a start to stop
->symmetricDifference The symmetric difference between the collections; ie all the objects in collection1 or collection2 but not in both
->union The set of objects in collection1 and objects in collection2

One important aspect of OCL that is worth noting is that it expands lists of lists to just a list. An example in plain English; Thing.allinstances.Details – this will come back as a set of details that are all the details from all the Things. If OCL had not expanded lists automatically one could have expected a set of sets containing the details per thing. But this is not the case. The automatically expansion of lists of lists is sometime referred to as flattening of a collection – referring to the reduction of topology in the result.

This page was edited 100 days ago on 02/10/2024. What links here