10.5 Tuples and Tuple Types


It is possible to compose several values into a tuple . A tuple consists of named parts , each of which can have a distinct type. Each tuple is a value itself. As OCL is a strongly typed language, each tuple has its own type. These tuple types do not have a name . They are either implicitly defined by a certain tuple value, or explicitly, but namelessly, defined; for instance, in the type expression of a local variable. Some examples of tuple values, and the way to write them, are:

 Tuple {name: String = 'John',  age: Integer = 10} Tuple {a: Collection(Integer) = Set{1, 3, 4},        b: String = 'foo',        c: String = 'bar'} 

The parts of a tuple are enclosed in curly brackets, and separated by commas. The type names are optional, and the order of the parts is unimportant. Thus, the following three expressions indicate the same tuple:

 Tuple {name: String = 'John', age: Integer = 10} Tuple {name = 'John', age = 10} Tuple {age = 10, name = 'John'} 

Some examples of tuple types, and the way to write them, are:

 TupleType(name: String, age: Integer) TupleType(a: Collection(Integer),           b: String,           c: String) 

Here, the parts of the tuple are enclosed in parentheses, again separated by commas. The type names are mandatory, but the order of the parts is still unimportant. In a tuple value, the values of the parts may be indicated by arbitrary OCL expressions; for example, in the model from Figure 10-1, we may write

  context  Person  def  : statistics : Set(TupleType(company: Company,                                 numEmployees: Integer,                                 wellpaidEmployees: Set(Person),                                 totalSalary: Integer)) =      managedCompanies->collect(c          Tuple { company: Company = c,                 numEmployees: Integer = c.employees->size(),                 wellpaidEmployees: Set(Person) =                    c.Job->select(salary>10000).employees->asSet(),                 totalSalary: Integer = c.Job.salary->sum()               }      ) 

The preceding expression results in a bag of tuples summarizing the company, the number of employees, the best-paid employees, and the total salary costs of each company a person manages .

The parts of a tuple are accessed by their names, using the same dot notation that is used for accessing attributes. Thus, the following two statements are true, if somewhat pointless:

 Tuple {x: Integer = 5, y: String = 'hi'}.x = 5 Tuple {x: Integer = 5, y: String = 'hi'}.y = 'hi' 

A more meaningful example, using the definition of statistics above, is:

  context  Company::isTopManager( p: Person ) : Boolean  body  : p.statistics->sortedBy(totalSalary)                    ->last().company = self 

The operation isTopManager is a query operation that returns true if the given person manages the contextual instance and he or she manages a set of employees that together command the highest total salary. In this expression, both totalSalary and company are accessing tuple parts.



Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net