2

How to define a new data type in OCL? (example - date)

OCL already has pre-defined types (Integer, String, Real & Boolean). But how can I define a new data type?

Example: I have a class call Person. Person class has following attributes, name : String age : Integer birthday : Date

2 Answers2

1

In OCL expressions, which are always attached to a UML model (typically to a class model) forming their context, you can use the types defined in the model. For instance, in a class model, you can define a datatype Date, and then you can use Date in you OCL expressions.

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • I am using USE:UML-based Specification Environment to model & verify the design using OCL. I can't find a way to define a new type in the UML model in USE. I would be really appreciate if you can give me some guidelines to define a new type in USE. – Rumesh Kavinda Oct 14 '14 at 19:34
  • @RumeshKavinda USE is weird tool invented at University of Bremen, Germany. The tool does not support standard UML class modeling neither standard UML exchange data format *.xmi. You can try to get some support by sending mail at the contact address listed in the readme or by posting a question at the tool's support forum - http://sourceforge.net/p/useocl/discussion/928881/ – xmojmr Oct 15 '14 at 08:27
1

(+1) For a valid question, I did not see who put a "-1" without indicating the reason.

OCL & UML are used togheter. New datatypes are not defined directly in OCL, but, in UML, first. Later, you used the new declared data type in OCL.

Enumeration Values are an example of declaring a new data type.

Suppouse you are modeling a vehicle software. You describe the vehicles, have a motor, and, each motor has a "state" or "status".

The status of a motor are restricted by a enumeration, a set of values.

In a U.M.L. class diagram, the new status is described, as a box, similar to a class, with the values, and, may have other classes, that refer to this new type.

....................................................................
..+----------------------+.........+-----------------------------+..
..|       <<enum>>       |.........|          <<class>>          |..
..|   EngineStatusEnum   |.........|          MotorClass         |..
..+----------------------+.........+-----------------------------+..
..| * Unknown            +---------+ [+] EngineStatusEnum Status |..
..| * Stopped            |.........| [+] ... other members       |..
..| * Running            |.........+------------+----------------+..
..| * Testing            |......................|...................
..| * Damaged            |......................|...................
..+----------------------+.........+--------------+---------\--+....
...................................|                         \ |....
...................................| (Status <= Stopped &&)   \|....
...................................| (Status >= Running)       |....
...................................|                           |....
...................................+---------------------------+....
....................................................................

And, a note displaying some condition in OCL, applying the declared new type.

umlcat
  • 4,091
  • 3
  • 19
  • 29