OCLOperators
No edit summary
m ((username removed) (log details removed))
 
(128 intermediate revisions by 7 users not shown)
Line 1: Line 1:
The operations listed do this:
OCL operators define constraints and operations on objects in an object-oriented programming language like Java, C++, or C#. They are also used in OCL expressions to specify constraints on objects or perform operations on object properties.
 
For example, 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:
# 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:  <code>context Person: self.age >= 18</code>
# 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:  <code>context Person: self.gender = 'Male' or self.gender = 'Female'</code>
# 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:  <code>context Person: self.name.matches('^[A-Z]')</code>
In addition to defining constraints, OCL operators can also perform 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: <code>context Person: self.salary.sum() < 100000</code>
 
OCL operators make it easy to write correct and maintainable object-oriented programs.
 
== Examples of OCL Operators  ==
{| class="wikitable"
{| class="wikitable"
!Operators
|'''Type'''
!Description
|'''Description'''
|-
|-
|'''[[Allinstances operator|Allinstances]]'''
|[https://wiki.mdriven.net/index.php/Category:OCL_General_Operators General Operators]
|All the objects of the class
|used to manipulate and query objects
|-
|-
|'''allinstancesAtTime'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Collection_Operators Collection Operators]
|All the currently loaded instances
|operators applicable to a collection of objects
|-
|-
|'''[[OCLOperators allStates|AllStates]]'''
| [https://wiki.mdriven.net/index.php/Category:OCL_String_Operators String Operators]
|Meta information about available states in state machines the class may contain
|used to manipulate and compare string values
|-
|-
|'''allSubClasses'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Number_Operators Number Operators]
|Meta information on all the sub classes this class has
|used to perform arithmetic and comparison operations on numeric values
|-
|-
|'''AllSuperTypes'''
|[https://wiki.mdriven.net/index.php/Category:OCL_Boolean_Operators Boolean Operators]
|Meta information on all the super classes – in inheritance order the class has
|used to evaluate and compare Boolean expressions
|-
|-
|'''associationEnds'''
|[https://wiki.mdriven.net/index.php/Category:OCL_ViewModel_Operators ViewModel Operators](on selfVM variable)
|Meta information on all the associationEnds
|used to manipulate and interact with data in a ViewModel class of an application.
|-
|-
|'''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
|}
Collection operators:
{| class="wikitable"
!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
|}
|}
  [[Category:OCL]]
[[Category:Beginner]]
[[Category:OCLOperators]]

Latest revision as of 05:27, 11 January 2024

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

For example, 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: 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: 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: self.name.matches('^[A-Z]')

In addition to defining constraints, OCL operators can also perform 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: self.salary.sum() < 100000

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 130 days ago on 01/11/2024. What links here