2006-12-02

OCL aliases

This is just a copy/paste of a reply I made in a newsgroup, but I think it is quite informative so here it is....

KEY
Square brackets denote a class [Person]
Rounded brackets denote a role (Pets)

Let's say you have a Car class and a Garage class, an individual car is regularly serviced at a specific garage so you have the following association

[Garage] (Garage) 1----0..* (ServicableCars) [Car]

Car.allInstances->select(Garage.Code = '1234')
The above OCL will fail because "Garage" is a class so the parser is expecting stuff like "allInstances". So you might think this should work

Car.allInstances->select(self.Garage.Code = '1234')
but it doesn't because "self" refers to the root object and not the object at the parsed node where it is specified. This is what aliases are for:

{aliasname} + {pipe}

Car.allInstances->select(currentCar | currentCar.Garage.Code = '1234')
This was possible in Bold too (native windows predecessor to ECO), but in Bold you could differentiate between members and classes through case sensitivity. PascalCase always meant a class whereas camelCase always meant a member, so this would work in Bold but doesn't in ECO as it is not case sensitive

Car.allInstances->select(garage.code = '1234')
Although ECO is not case sensitive you will notice that I still use the
sensitive format for clarity :-)

No comments: