=Paper= {{Paper |id=None |storemode=property |title=Dynamic Composition with Package Templates. |pdfUrl=https://ceur-ws.org/Vol-564/compositionvariability2010_submission_6.pdf |volume=Vol-564 }} ==Dynamic Composition with Package Templates.== https://ceur-ws.org/Vol-564/compositionvariability2010_submission_6.pdf
             Dynamic Composition with Package Templates

               Fredrik Sørensen                       Eyvind W. Axelsen                     Stein Krogdahl
                University of Oslo                     University of Oslo                  University of Oslo
            Department of Informatics              Department of Informatics           Department of Informatics
             P.O. Box 1080, Blindern                P.O. Box 1080, Blindern             P.O. Box 1080, Blindern
              N-0316 Oslo, Norway                    N-0316 Oslo, Norway                 N-0316 Oslo, Norway
               fredrso@ifi.uio.no                     eyvinda@ifi.uio.no                  steinkr@ifi.uio.no


ABSTRACT                                                           separately compiled after the system they are loaded into
We show how package templates, a mechanism for code mod-           has been started.
ularization, can be extended with features for dynamic load-
ing. We pose the question of whether or not this is may a          Although a set of such classes may be written, compiled and
useful mechanism with respect to software composition and          loaded together, they are still considered as separate entities
dynamic configuration of software.                                 and not checked as one coherent entity upon loading. There
                                                                   is no assurance that the individual classes belong to the same
                                                                   version of the extension. In this work, we are looking at a
Categories and Subject Descriptors                                 way to dynamically load an entity that represents a group of
D.3 [Software]: Programming Languages; D.3.3 [Progra-              classes in a package or template. We believe it to be useful
mming Languages]: Language Constructs and Features—                if one is allowed to take a group of statically checked classes,
Classes and objects; D.3.3 [Programming Languages]:                that are compiled and tested together, and adapt them in a
Language Constructs and Features—Modules, packages                 coordinated fashion to an existing system.

General Terms                                                      A recent paper [15] describes a new language mechanism
Languages, Design                                                  called PT (short for Package Templates), which is meant to
                                                                   be a useful tool for software composition. The mechanism
                                                                   is intended for object oriented languages in order to support
Keywords                                                           better code modularization, where statically type-checked
OOP, Modularization, Dynamicity, Templates                         templates can be written independently and subsequently
                                                                   be instantiated in programs with such flexibility that classes
1.   INTRODUCTION                                                  may be merged, methods renamed and new classes added.
There are several challenges when working with a large soft-       Templates may contain hierarchies of classes (single inheri-
ware system, including modularization, separation of con-          tance) and these hierarchies are preserved when a template
cerns and reuse. These challenges are large enough in them-        is used. Also, different instantiations of the same templates
selves when writing, testing and maintaining a large system.       are independent of each other, creating unique types.
However, even more challenges arise when there are many
different variations of a system, when the environment chan-       The basic PT mechanism allows flexibility in package reuse,
ges over time and when new requirements and extensions             program composition and support for readability and reusabil-
may arrive after the initial system has started running and        ity in large systems. The way that multiple classes may be
should not be taken down.                                          affected by instantiating a template gives the language an
                                                                   AOP-like quality. More AOP-specific extensions to PT have
Given these challenges, it seems beneficial to have similar        also been studied in [3].
support for such dynamic features as one has for the static
build of large systems. One mechanism that is useful in this       In this work, we look at a possible extension to the basic
respect is the use of interfaces and abstract classes in writing   package template mechanism that supports dynamic load-
the system and the possibility of loading different implemen-      ing of package templates. We ask to what extent this will
tations into a running system based on configurations and          be a useful tool for dynamic configuration of software sys-
runtime events. These implementations may be written and           tems. Furthermore, we discuss some of the properties of this
                                                                   mechanism.

                                                                   We introduce an extension to PT where templates are pa-
                                                                   rameterized by templates. A template with template param-
                                                                   eters must be instantiated with actual templates that “ex-
                                                                   tend” the formal parameters’ bounds. In standard PT, all
                                                                   instantiations of templates are done at compile-time. How-
                                                                   ever, in this work we look at extending this concept so that
                                                                   templates may be loaded dynamically into a running sys-
tem, not unlike how classes may be loaded dynamically in         package P {
languages like Java. We discuss how this can be achieved           inst T with A => C, B => D;
with template extensions and with parameterized templates.         class C adds { ... }
                                                                   class D adds { ... } // D extends C, see text
                                                                 }
Being able to load classes dynamically into a system is use-
ful since it allows extensions to be written after the system    In this example, a unique instance of the contents of the
has started running. It also allows one to configure and re-     package template T will be created and imported into the
configure a running systems and for a system to configure        package P. In its simplest form, the inst statement just
itself based on discovery of its environment.                    names the template to be instantiated, e.g. inst T, without
                                                                 any other clauses. In the example above the template classes
Dynamically loading classes in a controlled type-checked         A and B are also renamed to C and D, respectively, and ex-
way has advantages over other dynamic linking mechanisms.        pansions are made to these classes. Expansions are written
A compiler and loader will together have checked that the        in adds-clauses, and may add variables and methods, and
loaded class can be used in a type safe way. Doing this at the   also override virtual or implement abstract methods from
levels of templates, each containing several related classes,    the template class.
extends the reach of this checking.
                                                                 An important property of PT is that everything in the in-
The mechanism proposed here for dynamically loading tem-         stantiated template that was typed with classes from this
plates in PT preserves the relation between the classes in the   template (A and B) is re-typed to the corresponding expan-
template and thereby supports family polymorphism [9]. It        sions (C and D) at the time of instantiation (PT rules guar-
has the advantage of the flexible adaption and name change       antee that this is type-safe). Any sub/super-type relations
mechanisms of PT while still being dynamic. Since new            within the template is preserved in the package where it is
types are created when a template is loaded, different ver-      instantiated. Therefore, implicitly D extends C since B ex-
sions of the same software package can safely be used simul-     tends A.
taneously in the same runtime without name clashes. Thus,
PT extended this way becomes more a mechanism of dy-             Another important property is that classes from different,
namic composition than a static mechanism of reuse and           possibly unrelated, templates may also be merged to form
extension.                                                       one new class, upon instantiation. Consider the simple ex-
                                                                 ample below.
We will first present an overview of the basic package tem-
plate mechanism. Then we will present templates that ex-         template T {
tend other templates and template parameterized templates.         class A { int i; A m1(A a) { ... } }
                                                                 }
After that, we will present dynamic loading before a discus-     template U {
sion and a survey of related work.                                 class B { int j; B m2(B b) { ... } }
                                                                 }

2.   OVERVIEW OF THE PACKAGE TEMP-
     LATE MECHANISM                                              Consider now the following usage of these templates:
We here give a brief and general overview of the package         inst T with A => MergeAB;
template mechanism as it is described in [15]. The mech-         inst U with B => MergeAB;
anism is not in itself tied to any particular object-oriented
language, but the examples will be presented in a Java-like      class MergeAB adds {
syntax, and the forthcoming examples will be based on Java.         int k;
                                                                    MergeAB m2(MergeAB ab) { return ab.m1(this); }
                                                                 }
A package template looks much like a regular Java package,
but the classes are always written in one file and a syntax is   These instantiations result in a class MergeAB, that contains
used where curly braces enclose the contents of the template,    the integer variables i, j and k, and the methods m1 and
e.g. as follows:                                                 m2.1 Note that both m1 and m2 now have signatures of the
                                                                 form MergeAB → MergeAB.
template T {
  class A { ... }
  class B extends A { ... }                                      Summing up, some of the useful properties of PT are: It
}                                                                supports writing reusable templates of interdependent, co-
                                                                 operating classes which may be statically type checked with-
Valid contents of a template (with a few exceptions) are also    out any information about their usage. Upon instantiation,
valid as plain Java programs. As such, templates may also        a template class may be customized, and merged with other
be type checked independently of their potential usage(s).       template classes. References within a template to a tem-
                                                                 plate class will be re-typed according to the instantiation.
In PT, a template is instantiated at compile time with an        After all the instantiations, a PT program will have a set of
inst statement like below. This has some significant differ-     regular classes with single inheritance, like an ordinary Java
ences from Java’s import. Most notably, an instantiation         program.
will create a local copy of the template classes, potentially    1
                                                                  The handling of potential name clashes resulting from a
with specified modifications, within the package where the       merge is beyond the scope of this article, but PT has rules
inst statement occurs.                                           and the rename mechanism discussed to deal with this
3.   TEMPLATE EXTENSIONS AND TEMP-                              as override methods from these classes. Furthermore, it may
     LATE PARAMETERS                                            instantiate other templates using a normal inst statement,
In this section we propose an extension to PT where tem-        and add its own classes (such as C in the example below).
plates may have bounded template parameters. Dynamic
                                                                template Use