Specialization Classes

Specialization classes are a Java language extension for integrating forms of adaptive behavior in an existing program.

In our framework, adaptive behavior is defined on a per-class basis. An adaptive class is defined by attaching a number of alternative implementations to a regular Java class, that complement the existing, default implementation. Each alternative implementation is defined by a specialization class, and is to be used in some specific situation. These situations are defined in terms of the internal state of the (instances of the) class, via predicates on the instance variables.

The adaptive behavior consists of monitoring the internal state of each object, and switching to the right implementation whenever needed. This way, the instance "adapts" itself to the situations described by specialization classes.

When adapting to a new situation, an object does not change its interface - it is still considered an instance of the same class. One can imagine other forms of adaptation, when the interface of an object may change dynamically, but this is incompatible with a statically-typed language such as Java, so we do not consider it here.

Even when keeping the same interface, adaptive behavior can be used in two, rather different ways. First, when adapting to a specific situation, an object might completely change its behavior, so as to react differently to method calls. This form of adaptation can be used as a programming paradigm, first introduced in the Cecil language via predicate classes. For instance, a buffer can redefine its put() method when it becomes full.

The second way is to keep both the same interface and the same behavior, as observed from the outside, but use an optimized implementation for a specific case. From this point of view, the only motivation of adaptation is speed, that is, to speed up the implementation for a particular, "simpler" case. We call this form of adaptation specialization.

An interesting thing about specialization (as opposed to general adaptation) is that it can be done automatically. Indeed, if the predicates on the state have some particular form (variable == value), the optimized implementation corresponding to a specialization class can be derived automatically (at compile time or even at run time) from the default implementation, using a partial evaluator.

We developped a compiler from the Java language extended with specialization classes into standard Java. Our compiler allows one to integrate in a program both types of adaptation - general or by specialization. When used in conjuction with a partial evaluator for Java (we are working on one), it allows either manual or automatic specialization.

See also:

Send any comments to: Philippe.Boinot@irisa.fr