SFB 501 - Project D1: Application System "Buildings"
PSiGenePattern Code Generator |
![[PSiGene]](./pics/psigene_logo.jpg) |
|
Description
The pattern code generator is a Smalltalk program (written in VisualWorks)
capable of generating the complete source code for a building simulator.
It uses the class model of the simulation model and the pattern bindings
defining the simulators functionality as input.
All patterns provided by the catalog are
realized within PSiGene as VisualWorks classes. This implementation
ignores the informal parts of the pattern description (e.g. Intent) and
emphasizes the Participants as well as the
Implementation part. The implemented patterns are descendants
of a common superclass AbstractPattern. This class provides
the protocol needed to bind patterns, to generate code, and to create
error reports for the user.
The following figure (figure 1) shows a small example of how code generation
usually is done in PSiGene. Input to PSiGene is a simple class model
consisting of a class Room that aggregates several
Radiators. Each Radiator has a method q to
calculate the heatflow affecting its surroundings (here: the room in which
the radiator is located). To this class model the pattern
FollowRelationAndSum
is bound. The purpose of this binding is
to follow the radiators relation in order to collect and
sum up the heat flows from all radiators in a certain room.
Part of the pattern
FollowRelationAndSum
is shown on the upper right corner of figure 1. The pattern has two code
templates to generate a method that traverses a relation and sums up values
from adjoining objects. One of these two templates is used during the code
generation depending on the cardinality of the relation that should be
traversed (If the relation radiators would have been of cardinality
1:1 different code than shown in the example would have generated .).
Figure 1
The generation of simulation code is divided into two phases,
which are performed automatically, without manual intervention:
- In the binding phase, an instance of a pattern class is created for
every pattern bound to the simulator's class model. In this phase,
no code is generated. The set of all instances is then checked for
completeness, based on the formal parameters defined by each pattern.
A report is generated for the user of PSiGene. This report includes
information about missing and wrong bindings for every single pattern
instance so that errors in the binding description can
easily be identified and corrected. In addition to the checks
performed by our pattern editor,
these tests include correct library usage and availability of manually
written code (if needed).
- In the generation phase, all pattern instances are asked to
generate code for the objects they are bound to. There is no
`master generator': each pattern instance is responsible for its own
code portion (by providing a code generation method), making the
generation process flexible and easily extendible by simply
implementing new pattern classes.
The implemented patterns are structured as shown in figure 2:
Figure 2
Each pattern class is inherited from a common superclass
AbstractPattern. A unique generation method may exist for every
pattern class so that various code synthesis techniques could be used.
However, to generate code simply by macro replacement, the inherited
methods from AbstractPattern can be used. This
text shows the complete
(Smalltalk-) source code from our pattern FollowRelation.
The pattern class for FollowRelationAndSum inherits from
FollowRelation and only replaces the code templates
(see source code).