Modeling Services > Context-Sensitive Features > Redefining the cut Function > Overriding the cut Function
 
Overriding the cut Function
You can modify the default behavior of the cut function by overriding this function for the origin object or for the relation.
Overriding the cut Function for a Relation
To override the cut function of a relation, you just have to redefine this function in a class deriving from an instance of the class template specifying the relation. Only the cut function for the IlsOwns and IlsUses relations can be overridden.
The following code declares a one-to-one relation between a car and a wheel.
class Car:
public IlsObject
{
public:
   IlsOwns<Car,Wheel> frontRight, frontLeft, rearRight, rearLeft;
   Car();
};
According to the cut default behavior, when a cut is performed on a wheel, the relation is broken and the car is cut as well. Let us suppose now that there is also an IlsUses relation between potential users and a car. Again, in accordance with the implicit behavior of the cut function, the relation is severed. In this particular case, the default behavior is not appropriate, since we do not want to remove a car just because it has lost one of its wheels. To solve this problem, we can redefine the cut in a CarOwnsWheel class that derive from the relation IlsOwns<Car,Wheel>, like this:
class Car:
public IlsObject
{
public:
   class CarOwnsWheel:
   public IlsOwns<Car,Wheel>
   {
   public:
      CarOwnsWheel(Car& owner): IlsOwns<Car,Wheel>(owner){}
      void cut(ILS_CUT_DIRECTIVE=ILS ALL) {set(0);}
   };
   CarOwnsWheel frontRight, frontLeft, rearRight, rearLeft;
   Car();
};
Now, when a cut is performed, the relation is broken and set to 0. The cut is not propagated to the origin.
Overriding the cut Function of the Origin Object
An airline owns a number of routes, each using a departure and an arrival airport.
The relation between the Airline and the Route is a one-to-many ownership relation with a minimum cardinality set to 0. The relation between the Route and the Airport is a one-to-one use relation.
class Route:
public IlsObject
{
public:
   IlsUses<Route,Airport> departure,arrival;
   Route();
};
 
class Airline:
public IlsObject
{
public:
   IlsOwnsList<Airline,Route> routes;
   Airline();
};
By default, when a departure airport is cut, the departure relation is broken and set to 0. The cut is then propagated to the routes relation which loses one of its items.
Here, we come up against an integrity problem, since the route item which has been removed from the list is still connected to its arrival airport. It is thus necessary to adjust the behavior of the cut function so that the arrival relation is cut when the departure relation is cut and vice versa.
Moreover, when we decide to cut a route, it is not always possible to rely on the global property of the application implying the cut of the route. Smart pointers could still point to a route, thus preventing its removal.
As a consequence, a route can still be connected to its arrival and departure airports. To solve these problems, we just have to redefine the cut function for the type Route, as follows:
class Route:
public IlsObject
{
public:
   Route();
   IlsUses<Route,Airport> departure, arrival;
   void cut(ILS CUT DIRECTIVE d=ILS ALL){departure=arrival=(Airport*)0;
                  IlsObject::cut(d);}
};
Now, if one of the airports—departure or arrival—is cut, that event is propagated to the route and both relations are set to zero. If the route is cut, the redefined cut function is called and the relations departure and arrival are both set to zero.
The cut function can also be redefined for list-relations, set-relations, and array-relations. For details, see the Rogue Wave® Server Reference Manual.

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