Relationships in ODL / Inverse Relationships

Relationships in ODL / Inverse Relationships

Relationships in ODL

While we can learn much about an object by examining its attributes, often a critical fact about an object is the way it connects to other objects in the same or another class.

Example (a) : Now, assume we want to add to the declaration of the Movie class from "Attributes in ODL" Example (a), a property that is a set of stars. More specifically, we want each Movie object to connect the set of Star objects that are its stars. The best way to represent this connection between the Movie and Star classes is with a relationship. We may represent this relationship in Movie by a line:

relationship Set<Star> stars;

in the declaration of class Movie. This line may appear in "Attributes in ODL" figure (a) after any of the lines numbered (1) through (5). It says that in each object of class Movie there is a set of references to Star objects. The set of references is called stars. The keyword relationship indicates that stars includes references to other objects, while the keyword Set preceding <Star> specifies that stars references a set of Star objects, rather than a single object, Generally, a type that is a set of elements of some other type T is described in ODL by the keyword Set and angle brackets around the type T.

Inverse Relationships

Just as we might like to access the stars of a given movie, we might like to know the movies in which a given star acted. To get this information into Star objects, we can add the line

relationship Set<Movie> starredIn;

to the declaration of class Star in "Attributes in ODL" Example (b). On the other hand, this line and a similar declaration for Movie omits a very important feature of the relationship between movies and stars. We expect that if a star S is in the stars set for movie M, then movie M is in the starredIn set for star S. We point out this connection between the relationships stars and starredIn by placing in each of their declarations the keyword inverse and the name of the other relationship. If the other relationship is in some other class, as it generally is, then we refer to that relationship by the name of its class, followed by a double colon (: :) and the name of the relationship.

Example (b) : To describe the relationship starredIn of class Star to be the inverse of the relationship stars in class Movie, we revise the declarations of these classes, as shown in following figure (a) (which also includes a definition of class Studio to be discussed later). Line (6) shows the declaration of relationship stars of movies, and says that its inverse is Star: : starredIn. Since relationship starredIn is described in another class, the relationship name is preceded by the name of that class (star) and a double colon. Remember the double colon is used whenever we refer to something described in another class, such as a property or type name.

Likewise, relationship starredIn is declared in line (11). Its inverse is declared by that line to be stars of class Movie, as it must  be, because inverses always are linked in pairs.

As a general rule, if a relationship R for class C correlates with object x of class C with objects y1,y2, . . . , yn of class D, then the inverse relationship of R correlates with each of the yi's the object x (perhaps along with other objects). Often, it helps to visualize a relationship R from class C to class D as a list of pairs, or tuples, of a relation. The concept is the same as the "relationship set" we used to explain E/R relationships in “Instances of an E/R Diagram”. Each pair comprises an object x from class C and an associated object y of class D: as:



Then the inverse relationship for R is the set of pairs with the components reversed as;

 Some ODL classes and their relationships



Notice that this rule works even if C and D are the same class. There are some relationships that logically run from a class to itself, such as "child of" from the class "Persons" to itself.


Tags