Introducing Rogue Wave Server > Modeling Services > Static Modeling Services > Inverting Relations
 
Inverting Relations
It is sometimes necessary to know the object that is the origin of a relation. In the network example, we might want to list all input and output lines for a given node. To meet this requirement, we can provide a back pointer from the node to the lines. However, the use of back pointers may lead to situations where a node is not linked to the correct line, and thus may create inconsistencies.
With Rogue Wave® Server, you can automatically and transparently maintain a back pointer from the target of a relation to its origin.
Inverting Ownership Relations
Rogue Wave Server provides two functions, setContext and unsetContext, to maintain the origin of a relation automatically, starting from its target. These functions must be declared in the class of the owned object. The setContext function is called automatically when the relation is created. The unsetContext function is called automatically when the relation is broken. Rogue Wave Server provides the class template IlsInvertedRelation to store the inverted relation. As its arguments, this class template takes the type of the target object and the type of the origin object.
Example
If you want to maintain the owner of a given node (that is, the domain) as a data member of the domain object, you just have to declare an inverse relation using the template IlsInvertedRelation and define the callbacks setContext and unsetContext like this:
class Node:
public IlsObject
{
public:
   ...;
   // inverse relation declaration
   IlsInvertedRelation<Node,Domain> domain;
   // relation inversion callbacks
   void setContext(Domain& d, void*, IlsRelationId){domain=&d;}
   void unsetContext(Domain&, void*, IlsRelationId){domain=0;}
};
The relation IlsInvertedRelation<Node,Domain> will be updated automatically. If you want the owned object to have different types of origin, you have to declare as many pairs of setContext and unsetContext functions as there are origin types. For more information on IlsInvertedRelation, see the Rogue Wave Server Reference Manual.
Inverting Use Relations
In a use relation, a given object can have several origins. In this case, it is necessary to maintain a list of back pointers from the used object to its users. Rogue Wave Server provides the class template IlsInvertedRelationList to help you maintain this list. This class template takes the following two arguments: the type of the target object and the type of the origin-object.
Example
Let us assume that we want to know the number of lines having a given node as input or output node. In this case, we would declare the class template IlsInvertedRelationList in the class Node, which is the class of the used object, like this:
class Node:
public IlsObject
{
public:
Node();
void setContext(Line&, void*, IlsRelationId);
void unsetContext(Line&, void*, IlsRelationId);
private:
   IlsInvertedRelationList<Node,Line> inputLines,outputLines;
};
You will see how to implement these functions in the Tutorial (Part 2 of this manual).

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.