• Aucun résultat trouvé

MODELING COMPLEX SYSTEMS

Thinking Adaptively

4.2 MODELING COMPLEX SYSTEMS

class structure hooks

adaptive program

Figure 4.3: Programming with hooks

This suggests that class hierarchy information should be abstracted out and the programs should be written relatively independent of the hierarchy.

4.2 MODELING COMPLEX SYSTEMS

We rst present adaptive software as a new modeling technique for complex systems and then relate adaptive and object-oriented software. The explanation in terms of modeling has the advantage that it allows us to dene adaptive programming without reference to object-oriented programming.

According to the Encyclopedia of Computer Science and Engineering [Ral83], a complete model frequently includes both a structural model and a process model. The structural model describes the organization of the system and the process model describes the operation or behavior of the system. With this context, we can give a concise informal denition of adaptive programs.

An adaptive program is a generic process model parameterized by graph con-straints which dene compatible structural models as parameters of the process model.

The graph constraints indicate what kind of paths need to exist in the structural models that are graphs. The compatible structural models are also called customizers. A traditional model is the result of applying a customizer to an adaptive program. The innovative feature

84 CHAPTER 4. THINKING ADAPTIVELY

of adaptive programs is that they use graph constraints to specify possible customizers. An example of a graph constraint is that certain paths exist using or avoiding certain edges.

Adaptive models have several advantages over traditional models.

Adaptive models focus on the essence of the system being modeled and are therefore simpler and shorter than traditional models. Adaptive models are not distorted by an accidental structure that is currently in use, but which makes the model unnecessarily complex.

Many behaviors require the same customization. Therefore customizers are eectively reused.

Graph constraints allow for easy internal changes to the model before customization, that is, adaptive models can be reused easily in unplanned ways. They are not only adaptive but also adaptable should the adaptiveness not be sucient. This means that when we apply an adaptive program to a customizing class structure and we don't get the desired behavior, the adjustment necessary at the adaptive level is usually smaller than the adjustment at the object-oriented language level. We distinguish between adaptable and adaptive programs. An adaptable program is one that is easy to change manually to a new context and an adaptive program adjusts automatically to a new context.

Graph constraints allow for implicit parameterization of models without giving explicit parameters. Adaptive models can be rened easily before they are customized.

The above list discusses the advantages of an adaptive model with respect to traditional modeling. With the current interest in object-oriented modeling, it is useful to discuss the relationship between adaptive and object-oriented modeling.

Object-oriented modeling is a special case of adaptive modeling in that object-oriented modeling uses very strong graph constraints that determine only a small number of customiz-ers. Here we view an object-oriented model as consisting only of the methods. The same set of methods works with several class structures that dier by the number of inheritance relationships between the class structures.

For example, the two methods

void A::t() {c -> t();}

void B::t() {print ("hello");}

work with the class structure in Fig. 4.4. Class A has one part (data member, instance

A B

c

Figure 4.4: Adaptability of object-oriented program variable) named c of class B.

4.2. MODELINGCOMPLEXSYSTEMS 85

A B

c

C

Figure 4.5: Adaptability of object-oriented program

And the same two methods also work with the class structure in Fig. 4.5. Class A has one part (instance variable) named c of class B. Class C inherits from class B and class C has no additional parts.

Advantages of adaptive over object-oriented modeling are

An adaptive model describes a large family of object-oriented models and is therefore at a higher level of abstraction. An object-oriented model assigns methods to classes at program-write-time whereas an adaptive program delays many of those bindings past write-time.

An adaptive model together with the customizer is usually shorter than the corre-sponding object-oriented model (i.e., the result of the application of the customizer to the adaptive model). The reason is that the same parts of the customizer are usually applied many times.

Adaptive models can be specialized more exibly than object-oriented models since they have many specialization points that are not controlled by explicit parameters.

Prex and sux wrappers play an important role.

There is no run-time performance penalty over object-oriented models due to inlining compiler technology.

There are only a few disadvantages of adaptive over object-oriented modeling:

Writers of adaptive models need to learn a small extension to object-oriented languages (the propagation pattern and class dictionary notation). They need to learn a new debugging technique (see Chapter 13).

There are no other disadvantages of adaptive models that do not also apply to object-oriented models. The reason is that adaptive software is simply a concise represen-tation of object-oriented software. The conciseness does not introduce new prob-lems but improves generality of the software and solves (via propagation) the small-methods problem of object-oriented software. The small-small-methods problem says that large object-oriented applications tend to contain a signicant number of methods only two or three lines long.

To explain the dierence between adaptive and object-oriented models further, we show the following relationships.

86 CHAPTER 4. THINKING ADAPTIVELY

Every object-oriented program is an adaptive program (no graph constraints are used to specify customizers, and all methods are programmed). Only a few of the object-oriented programs are \good" adaptive programs. By a \good" adaptive program we mean a pro-gram that makes minimal assumptions on its class structures. Every adaptive propro-gram corresponds, through customization, to a family of object-oriented programs.

We have compared adaptive software development with object-oriented software devel-opment. We briey compare object-oriented software development with traditional software development. Despite hype to the contrary, object-oriented development incurs extra cost over procedural development.

Design time

We need to design a class hierarchy using information about the domain. In our experience, this is a signicant cost factor since often designers try to nd an optimal class hierarchy. The reason why they want an optimal class hierarchy is that it should change only minimally over the life-cycle of the software. Unfortunately, this is an illusion and a lot of time is wasted since class hierarchies will always change. Adaptive software reduces the need for an optimal class hierarchy since changes in the class hierarchy can be absorbed easily by adaptive software.

Maintenance time

In an object-oriented program it is more dicult to nd the exact line of code providing some function because of virtual functions and the prevalence of many small methods.

To understand a program, we have to wade through many small methods that do simple things. Adaptive software eliminates the small methods problem and therefore helps with program understanding at maintenance time.

4.3 JUSTIFICATION

Any new software development method needs good reasons for its existence since many meth-ods are already available and practitioners have diculty in choosing one. Here are some of the reasons why adaptive software is worthwhile. Adaptive software is easier to maintain than object-oriented software since it is written with fewer assumptions on class structures.

Adaptive software is usually considerably shorter than the corresponding object-oriented software and therefore, it requires less typing than object-oriented software. Adaptive soft-ware has a higher probability of being reusable than corresponding object-oriented softsoft-ware.

The reason is that one adaptive program denes an innite collection of object-oriented programs. Adaptive software is generic software that does not have to be parameterized explicitly. The software can be exibly extended in unforeseen ways by adding arguments and by wrapping code around already existing code.

In a commercial software development environment, it is important that software is de-signed to be reusable from the beginning. Often no time is left to make the software reusable later. And with object-oriented programs, it is often unclear how to make a class reusable|

much eort is wasted on useless generalization. Adaptive software makes a contribution in this area since adaptive programs are never harder to write than object-oriented programs, but are usually more reusable (an adaptive program denes a big family of object-oriented programs). Furthermore the best adaptive programs are much more expressive than the

4.4. CUSTOMIZATION 87 corresponding object-oriented programs and, in the worst case, an adaptive program is an object-oriented program. Adaptive software may be used whenever object-oriented software is used since it can only improve the exibility of the software. Adaptive software can always be made at least as good as the best object-oriented software that can be written for a given application.

An advantage of adaptive software is that it allows for initial error and subsequent adjustment. Many problems are complex, and initially it is not clear how to best structure the classes. Often we know the best class structure only after the project is completed. Not knowing how to start can easily lead to procrastination. With adaptive software, we can easily make a rst guess at the class structure and then write the functionality with minimal dependency on the class structure. Changing to a better class structure is then much easier.

Adaptive software is a good tool to teach advanced object-oriented software develop-ment. Adaptive software provides for a better understanding and better use of object-oriented software. The object-object-oriented concepts are learned or reinforced as a side eect when the adaptive concepts are learned.

4.4 CUSTOMIZATION

Adaptive programs are written in terms of partial class structures and they need to be customized into object-oriented programs. An adaptive program can be used with any complete class structure that is compatible with the given partial class structure. The customization of an adaptive program can be achieved in at least three ways:

A few sample objects can be selected that describe by example on what kind of objects the program is supposed to operate.

A graph similar to an entity relationship diagram can be used to complete a given partial class structure (see Fig. 4.2).

A grammar similar to a BNF (Backus-Naur-Form) grammar can be used to com-plete a given partial class structure and to specify an application-specic language simultaneously.

An object-oriented program is typically built by customizing several adaptive programs into the same class structure, as we have shown with the conglomerate example. This usually requires renaming to avoid name conicts.

Adaptive software uses a graphical approach to develop object-oriented software. When developing adaptive software we can proceed in two ways.

Structure rst

First develop a class structure, check it for design-rule violations, and check it for completeness with respect to the structure of the objects it has to represent. Then for each subtask to be implemented, choose groups of collaborating classes that are described using minimal knowledge about the class structure in a propagation pattern.

The resulting propagation patterns are customized with the class structure.

Functionality rst

88 CHAPTER 4. THINKING ADAPTIVELY

For each subtask to be implemented, choose a partial class structure that contains the important classes for the subtask. The functionality is then implemented generically for this partial class structure. Later, the program is customized to an object-oriented program by using a compatible class structure that has been checked for completeness and design-rule violations.

In both approaches, much of software development consists of graph manipulation.

Adaptive software introduces a new way to parameterize object-oriented software that goes beyond parameterized classes or templates. An adaptive program describes an object-oriented program parameterized by classes and their relationships. A parameter has a type that is a predicate on a graph. It is interesting that the parameters inuence the input and output language and the detailed structure of the classes and object-oriented programs.

In other words, an adaptive program is written without knowing the input language or the detailed structure of the resulting object-oriented program!