Tuples

Data structures in OPL can be constructed using tuples that cluster together closely related data. This topic describes how to declare tuples, use keys on tuples, and initialize tuples.

Declaring tuples

For example, the declaration:


tuple Point {
       int x;
       int y;
    }; 
Point point[i in 1..3] = <i, i+1>;

declares a tuple Point consisting of two fields x and y of type integer. Once a tuple type T has been declared, tuples, arrays of tuples, sets of tuples of type T, tuples of tuples can be declared, as in:


Point p = <2,3>; 
Point point[i in 1..3] = <i, i+1>;
{Point} points = {<1,2>, <2,3>}; 
tuple Rectangle {
    Point ll; 
    Point ur; 
}

These declarations respectively declare a point, an array of three points, a set of two points, and a tuple type where the fields are points. The various fields of a tuple can be accessed in the traditional way by suffixing the tuple name with a dot and the field name, as in


Point p = <2,3>; 
int x = p.x; 

which initializes x to the field x of tuple p. Note that the field names are local to the scope of the tuples.

Note:

Multidimensional arrays are not supported in tuples.

Keys in tuple declarations

As in database systems, tuple structures can be associated with keys. Tuple keys enable you to access data organized in tuples using a set of unique identifiers. In the following example, the nurse tuple is declared with the key name of type string.

Declaring a tuple using a single key (nurses.mod)

tuple nurse {
  key string name;
  int seniority;
  int qualification;
  int payRate;
}

Then, supposing Isabelle must not work more than 20 hours a week, just write:


NurseWorkTime[<"Isabelle">]<=20;

leaving out the fields with no keys. This is equivalent to:


NurseWorkTime[<"Isabelle",3,1,16>]<=20;

Using keys in tuple declarations has practical consequences, in particular:

  • The key field can be used as a unique identifier for the tuple, for example the field name in Declaring a tuple using a single key (nurses.mod). In this example, it means that there will be no two tuples with the same name in a set of tuples of the type nurse. If a user inadvertently attempts to add two different tuples with the same name, OPL raises an error.

  • Defining keys enables you to access elements of the tuple set by using only the value of the key field (name in the nurses example). Slicing is one of the features that benefit from it: you can slice on the tuple set using only key fields.

You can also declare a tuple using a non singleton set of keys, such as the shift tuple of the nurses example below.

Declaring a tuple using a set of keys (nurses.mod)


tuple shift {
   key string departmentName;
   key string day;
   key int startTime;
   key int endTime;
   int minRequirement;
   int maxRequirement;   
}

In this example, a shift is uniquely identified by the department name, the date, and start and end times, all defined as key fields.

Initializing tuples

You initialize tuples by giving the list of the values of the various fields, as in:


Point p = <2,3>; 

which initializes p.x to 2 and p.y to 3. See Initializing tuples for details.