OCLOperators
No edit summary
m ((username removed) (log details removed))
 
(123 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== General operators ==
<message>Write the content here to display this box</message>
To find all available you can open the OCL-Editor and type in a class:
OCL operators define constraints and operations on objects in an object-oriented programming language like Java, C++, or C#. OCL operators can be used in OCL expressions to specify constraints on objects or to perform operations on object properties.


[[File:Ocl-editor 1.png|frameless|433x433px|link=https://wiki.mdriven.net/index.php/File:Ocl-editor_1.png]]
== Examples ==
{| class="wikitable"
Let's say we have a class called "Person" with properties like "name," "age," and "gender." We can use OCL operators to define constraints on the properties of the "Person" class. Here are some examples:
!Operators
!Description
|-
|'''[[Allinstances operator|Allinstances]]'''
|All the objects of the class
|-
|'''[[OCLOperators allInstancesAtTime|allinstancesAtTime]]'''
|All the currently loaded instances
|-
|'''[[OCLOperators allStates|allStates]]'''
|Meta information about available states in state machines the class may contain
|-
|'''[[OCLOperators allSubClasses|allSubClasses]]'''
|Meta information on all the sub classes this class has
|-
|'''[[OCLOperators allSuperTypes|allSuperTypes]]'''
|Meta information on all the super classes – in inheritance order the class has
|-
|'''[[OCLOperators associationEnds|associationEnds]]'''
|Meta information on all the associationEnds
|-
|'''[[OCLOperators asString|Asstring]]'''
|The string representation of the class – the asString operation is available on everything
|-
|'''[[OCLOperators attributes|Attributes]]'''
|Meta information about what attributes the class has
|-
|'''[[OCLOperators constraints|Contraints]]'''
|Meta information on what constraints the class has
|-
|'''[[OCLOperators emptyList|emptylist]]'''
|Returns an empty list typed to hold objects of the class
|-
|'''isDirtyMember'''
|
|-
|'''isNull'''
|
|-
|'''[[OCLOperators nullValue|nullValue]]'''
|A typed null value
|-
|'''[[OCLOperators objectfromExternalId|objectFromExternalId]]'''
|An external identity will be resolved to the object
|-
|'''[[OCLOperators oclAsType|oclAsType]]'''
|The type of the class
|-
|'''[[OCLOperators oclIsKindOf|oclIsKindOf]]'''
|This is to if a class is a subclass or a the class itself and not unrelated
|-
|'''[[OCLOperators oclIsTypeOf|oclIsTypeOf]]'''
|Returns true if
|-
|'''[[OCLOperators oclSingleton|oclSingleton]]'''
|Classes that implements the Singleton pattern – by setting IsSingleton=true – will return the singleton instance with this operator
|-
|'''OclType'''
|
|-
|'''safeCast'''
|
|-
|'''superTypes'''
|
|-
|'''[[OCLOperators taggedValue|TaggedValue]]'''
|Meta information on tagged values set in the class
|-
|'''[[OCLOperators taggedValueOnFeature|TaggedValueOnFeature]]'''
|Meta information on Tagged values set on a named feature in the class
|-
|'''[[OCLOperators typename|Typename]]'''
|The type name as a string
|-
|'''[[OCLOperators ViewModels|ViewModels]]'''
|A tuple with the ViewModels for this class a members
|}


== Collection operators ==
'''1.''' To define a constraint that ensures that the "age" property of a "Person" object is greater than or equal to 18, we can use the greater than or equal to (>=) operator as follows: 
Once you have a collection of objects there are certain operators that are applicable to it.  
context Person inv: self.age >= 18
'''2.''' To define a constraint that ensures that the "gender" property of a "Person" object is either "Male" or "Female," we can use the OR (||) operator as follows: 
context Person inv: self.gender = 'Male' or self.gender = 'Female'
'''3.''' To define a constraint that ensures that the "name" property of a "Person" object starts with an uppercase letter, we can use the dot notation (.) and the regular expression operator (matches) as follows: 
context Person inv: self.name.matches('^[A-Z]')
In addition to defining constraints, OCL operators are useful for performing operations on object properties. For example, we can use the sum() operator to calculate the sum of all the elements in a collection property of a "Person" object as follows:
context Person inv: self.salary.sum() < 100000
OCL operators provide a powerful way to define constraints and operations on objects, making it easy to write correct and maintainable object-oriented programs.


See this page for [[examples on collection operators]]
OCL operators make it easy to write correct and maintainable object-oriented programs.


Again you can use the OCL-Editor to see what they are:
== Examples of OCL Operators  ==
 
[[File:Collection of objects operators.png|frameless|453x453px|link=https://wiki.mdriven.net/index.php/File:Collection_of_objects_operators.png]]
{| class="wikitable"
{| class="wikitable"
!Operators
|'''Type'''
!Description
|'''Description'''
|-
|'''[[OCLOperators any|->any]]'''
|Returns any element contained in ''self'' that validates the condition ''expr'', null otherwise. 
|-
|'''[[OCLOperators append|->append]]'''
|Add another object last
|-
|'''[[OCLOperators asBag|->asBag]]'''
|Collapses to one list
|-
|'''[[OCLOperators asSequence|->asSequence]]'''
|Collapses to one list
|-
|'''[[OCLOperators asSet|->asSet]]'''
|Remove doublets
|-
|'''[[OCLOperators at|->at]]'''
|Get the objects at X where the first index is 1
|-
|'''[[OCLOperators at0|->at0]]'''
|Get the objects at X where the first index is 0
|-
|'''[[OCLOperators collect|->collect]]'''
|Iterate over the collection and build a tuple result
|-
|'''[[OCLOperators count|->count]]'''
|Count how many meet a certain criteria
|-
|'''[[OCLOperators difference|->difference]]'''
|The difference between 2 collections
|-
|'''[[OCLOperators excluding|->excluding]]'''
|The collection except this single object
|-
|'''[[OCLOperators exists|->exists]]'''
|Are there any objects that fulfill the criteria
|-
|'''[[OCLOperators filterOnType|->filterOnType]]'''
|Only keep the ones of a certain type
|-
|'''[[OCLOperators first|->first]]'''
|Return the first object
|-
|'''[[OCLOperators forAll|->forAll]]'''
|Iterate all that fulfills the critera
|-
|'''[[OCLOperators groupBy|->groupBy]]'''
|Build collection of tuples grouped by some aspect
|-
|'''[[OCLOperators includes|->includes]]'''
|Does the collection include the object
|-
|'''[[OCLOperators includesAll|->includesAll]]'''
|Does the collection include the whole other collection
|-
|-
|'''[[OCLOperators including|->including]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_General_Operators General Operators]
|Returns the list with the element in the parameter included.
|used to manipulate and query objects
|-
|-
|'''[[OCLOperators indexOf|->IndexOf]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Collection_Operators Collection Operators]
|The 1 based index of an object in the collection possibly -1 if not existing
|operators applicable to a collection of objects
|-
|-
|'''[[OCLOperators indexOf0|->indexOf0]]'''
| [https://wiki.mdriven.net/index.php/Category:OCL_String_Operators String Operators]
|The 0 based index of an object in the collection possibly -1 if not existing
|used to manipulate and compare string values
|-
|-
|'''[[OCLOperators intersection|->intersection]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Number_Operators Number Operators]
|The intersection of two collections
|used to perform arithmetic and comparison operations on numeric values
|-
|-
|'''[[OCLOperators isEmpty|->isEmpty]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Boolean_Operators Boolean Operators]
|Returns true if the collection is empty
|used to evaluate and compare Boolean expressions
|-
|-
|'''[[OCLOperators last|->last]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_ViewModel_Operators ViewModel Operators](on selfVM variable)
|Returns the last object in the collection
|used to manipulate and interact with data in a ViewModel class of an application.
|-
|'''[[OCLOperators notEmpty|->notEmpty]]'''
|Returns true of the collection is not empty
|-
|'''[[OCLOperators orderBy|->orderBy]]'''
|Sorts the collection on one or more properties
|-
|'''[[OCLOperators orderDescending|->orderDescending]]'''
|Sort the from biggest to smallest
|-
|'''[[OCLOperators orderGeneric|->orderGeneric]]'''
|Sorts the list of properties with interchangeable sort order: (expr1, OclSortDirection::ascending, expr2, OclSortDirection::descending...)
|-
|'''[[OCLOperators prepend|->prepend]]'''
|Add an object in front of the list
|-
|'''[[OCLOperators reject|->reject]]'''
|Returns the objects not matching the criteria
|-
|'''[[OCLOperators select|->select]]'''
|Returns the objects matching the criteria
|-
|'''[[OCLOperators size|->size]]'''
|Returns the number of elements in the collection
|-
|'''[[OCLOperators subSequence|->subsequence]]'''
|Returns a smaller collection from a start to stop
|-
|'''[[OCLOperators symmetricDifference|->symmetricDifference]]'''
|The symmetric difference between the collections; ie all the objects in collection1 or collection2 but not in both
|-
|'''[[OCLOperators union|->union]]'''
|The set of objects in collection1 and objects in collection2
|}
|}


== String operators ==
  [[Category:OCL]]
{| class="wikitable"
[[Category:Beginner]]
!Operators
[[Category:OCLOperators]]
!Description
|-
|[[Escape codes]]
|Escape (special) characters used in OCL
|-
|
|
|-
|
|
|}

Latest revision as of 05:11, 22 August 2024

OCL operators define constraints and operations on objects in an object-oriented programming language like Java, C++, or C#. OCL operators can be used in OCL expressions to specify constraints on objects or to perform operations on object properties.

Examples

Let's say we have a class called "Person" with properties like "name," "age," and "gender." We can use OCL operators to define constraints on the properties of the "Person" class. Here are some examples:

1. To define a constraint that ensures that the "age" property of a "Person" object is greater than or equal to 18, we can use the greater than or equal to (>=) operator as follows:

context Person inv: self.age >= 18

2. To define a constraint that ensures that the "gender" property of a "Person" object is either "Male" or "Female," we can use the OR (||) operator as follows:

context Person inv: self.gender = 'Male' or self.gender = 'Female'

3. To define a constraint that ensures that the "name" property of a "Person" object starts with an uppercase letter, we can use the dot notation (.) and the regular expression operator (matches) as follows:

context Person inv: self.name.matches('^[A-Z]')

In addition to defining constraints, OCL operators are useful for performing operations on object properties. For example, we can use the sum() operator to calculate the sum of all the elements in a collection property of a "Person" object as follows:

context Person inv: self.salary.sum() < 100000

OCL operators provide a powerful way to define constraints and operations on objects, making it easy to write correct and maintainable object-oriented programs.

OCL operators make it easy to write correct and maintainable object-oriented programs.

Examples of OCL Operators

Type Description
General Operators used to manipulate and query objects
Collection Operators operators applicable to a collection of objects
String Operators used to manipulate and compare string values
Number Operators used to perform arithmetic and comparison operations on numeric values
Boolean Operators used to evaluate and compare Boolean expressions
ViewModel Operators(on selfVM variable) used to manipulate and interact with data in a ViewModel class of an application.
This page was edited 29 days ago on 08/22/2024. What links here