<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Pro jective Template-Based Code Generation</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Zeev Chared</string-name>
          <email>zeev@codiscent.com</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Shmuel Tyszberowicz</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>CodiScent, Ltd.</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>The Academic College of Tel-Aviv Ya o</institution>
        </aff>
      </contrib-group>
      <abstract>
        <p>Template-based code generation (TBCG) is a generative technology that transforms data into structured text, e.g. code, through the use of templates. The data, referred to as the model, represents an abstract design while the templates provide the mold of the target code used by the generator in the process of code production. A classical template is a mixture of text blocks and imperative control code segments. The text blocks' concern is to describe textual patterns while the control code's is to retrieve, manipulate, and substitute the data into place holders within the text. The mixture of these concerns reduces the readability, expressiveness, reusability, and analyzability of the textual patterns and the control code alike. We introduce the projector templates, a TBCG technology that adheres to separation of concerns of the template, model, and the template control code. Inspired by the relational theory, projective template blocks are treated as annotated relations that can derive their input from the source model through the use of relational operators. The projector templates are thus code-free, coherent, their transformation process reusable, and capable of elegantly coping with complex code-generation scenarios.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        The di culty of writing large software systems has led to an increased interest
in generative programming where one program generates another program [
        <xref ref-type="bibr" rid="ref1 ref9">9,
1</xref>
        ]. Generator programs transform abstract data (model) into code making it
possible to manage the software at a favorable abstract level of the model [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>Template-based code generation(TBCG) uses templates to describe the
structure of the target text. It o ers a high level of control over the generated output
providing the level of exibility required by custom generation scenarios.</p>
      <p>
        TBCG is used extensively throughout the industry [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], especially in web
related technologies. Past and recent research attempts to nd ways to further
employ code generation to cope with more complex generation scenarios and
heterogeneous development environment [
        <xref ref-type="bibr" rid="ref11 ref12 ref7">11, 12, 7</xref>
        ]. There are many available
products for TBCG such as Microsoft T4 [
        <xref ref-type="bibr" rid="ref14 ref15">14, 15</xref>
        ], Acceleo [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ], Xpand [
        <xref ref-type="bibr" rid="ref17 ref2">17, 2</xref>
        ]
and XVCL [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]. The approaches commonly employed by most engines are
either wrapping text block within control blocks (T4/Acceleo), or fragmenting
the template among named blocks (Xpand/XVCL). These approaches limit the
templates readability, textual exibility and reusability, ultimately curbing their
e ectiveness as the generation scenarios complexity grows.
      </p>
      <p>
        Ideally, a template should be an abstract representation of the code it
describes. Consequently, our research is focused on inferring the control code from
the templates textual structure without requiring any explicit control-logic code.
This paper presents the projector templates-based code generation technology
that focuses on transforming relational models into text by control code-free
templates that can express complex textual patterns coherently and elegantly.
Relations are analyzable [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] and easily created using available technologies
(Excel/XML). Also, a lot of meta-data is already available in relational format from
various modeling frameworks.
      </p>
      <p>The rest of the paper is organized as follows: in Section 2 we present the
relational view of templates. Section 3 presents the projective templates approach to
common generation scenarios. Section 4 demonstrates the use of the technology
in the context of a use case. Section 5 discusses the technology's bene ts and
limitations, and Section 6 concludes with a short summary.
2</p>
    </sec>
    <sec id="sec-2">
      <title>The Projective Templates Inference Framework</title>
      <p>A projective template is a hierarchy of textual blocks (blocks) where each block
contains text and block variables, called the block attributes that reference
corresponding metadata attributes to be substituted in their place. The block
variable set is used to deduce a relational projection to be performed on a source
metadata relation (model) associated with the template. Using the inferred
projections, the code generation algorithm can extract data from the model, order
it, and substitute it into the template in order to produce the target code.
2.1</p>
      <sec id="sec-2-1">
        <title>Relational View of Templates</title>
        <p>
          A relation is a set of tuples, or rows, that have the same set of column names
called the relation header [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ]. For example, the relation Name-Age = fName,
Ageg contains 2-tuple set where the rst tuple is name and the second is age.
        </p>
        <p>A relational projection operator fA1,. . . ,Ang returns a relation that is
derived from a source relation by selecting a distinct set of rows with the header
fA1,. . . ,Ang. For instance, the projection fNameg applied to the relation
Name-Age returns the distinct set of all the names in it.</p>
        <p>We de ne a new operator called the annotated projection operator (APO)
that takes a projection fA1,. . . ,Ang and a block that references the
projection header's attributes. The operator transforms each projected row into an
annotated row by substituting the projected row's column values in place of the
block variables. Columns that are not referenced by the block do not a ect the
operator's output; hence the projection header can be inferred directly from the
template blocks providing the basis of the projective TBGC.</p>
        <p>We de ne the projection inferred from the block as the induced projection;
the dataset returned from applying the induced projection to a metadata model
as the block's dataset. The image resulting from substituting one row values
in place of the block's variables as an annotated row, the concatenation of the
annotated rows as the block image, and the concatenation of the block images
as the template image.
3</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Approach to Common Generation Scenarios</title>
      <p>This section describes the projective template approach to common generation
scenarios. The output is produced with the Projective Template Generator IDE
by CodiScent, LTD available at www.codiscent.com.</p>
      <p>Child blocks. A template-block can contain other template-blocks in a
parent-child recursive structure. A child block is evaluated for each processed
instance of the parent for the subset of rows associated with that instance. The
child block's image is substituted in place of the child block in the parent
processed instance.</p>
      <p>The following example shows a template for table creation code and the
output of its application to the model M = fTable, Column, DataType, Lengthg.</p>
      <sec id="sec-3-1">
        <title>Model M:</title>
        <p>{{Customer, AddressLine1, varchar, 100} ,
{Customer, AddressLine2, varchar, 100 } ,
...</p>
        <p>{Product, Description, varchar, 50} ...}</p>
      </sec>
      <sec id="sec-3-2">
        <title>Template: [ create table $Table$ ([ $Column$ $DataType$ [($Length$)] ^,\n]) ^\n]</title>
      </sec>
      <sec id="sec-3-3">
        <title>Generated code:</title>
        <p>create table Customer (</p>
      </sec>
      <sec id="sec-3-4">
        <title>AddressLine1 varchar (100) ,</title>
      </sec>
      <sec id="sec-3-5">
        <title>AddressLine2 varchar (100) ,</title>
        <p>...
create table Product (</p>
      </sec>
      <sec id="sec-3-6">
        <title>Description varchar (100), ... )</title>
        <p>Optional Attributes and Required Blocks. A block attribute is required
by default. The block generates an image only for rows with non-empty values
in their required variables. An attribute can be de ned as optional by pre xing
its name with `*'. A child block is optional by default, but can be designated
as required by pre xing the block with \!" . A required child block prevents
the production of its container block's image in case it has no instances. In the
following example the block that contains the keyword `Where' is generated only
when there is a constraints clause.
[ select * from tableX [ where ![ $Condition$ ] ]</p>
        <p>Ordering. A block dataset is sorted in an ascending order of the
concatenated values of its block attributes where the attributes are ordered according
to their order of appearance in the block.</p>
        <p>Silent Attributes. There are generation scenarios that require a sort order
other than the default order. The projective templates resolve that with silent
attributes, designated by pre xing the attribute with a tilde, whose output is
deactivated.</p>
        <p>Instance separators. A block instance separator is designated by a `^'
pre xed string attached to the ending block marker. A block instance separator
is introduced between every pair of adjacent block images, for example, a comma
between arguments in a function call.</p>
        <p>Conditional generation: existence check. There is no need to test for
attributes' null values. A zero instance block is simply not generated.</p>
        <p>Conditional generation: derived attributes. The projective templates
can extend the model by introducing derived attributes de ned at the template
or template group level. Derived attributes are evaluated only if their
instantiation conditions are satis ed. In the following example the block containing
PKAtt (derived from AttName) is generated only for rows that satisfy the PkAtt
instantiation constraint, that is, rows with AttName='ID' :
[@EXTEND Derive PKAtt isa AttName</p>
        <p>where AttName='ID';@
delete $EntityName$ where [$PKAtt$ = 3]]</p>
        <p>Conditional generation: text polymorphism. There are situations where
the generated code varies slightly from instance to instance. For example, the
syntax for loading string values into type-safe objects in C# requires parsing for
non-string data types. The following template demonstrates the generation of a
type-safe conversion code.
@EXTEND
derive NonStringDataType isa Column</p>
        <p>where C#DataType not in (`String')@
public bool LoadValues([string inp$Attribute$])
{[ $Attribute$ =
[$NonStringDataType$.</p>
      </sec>
      <sec id="sec-3-7">
        <title>Parse(]inp$Attribute$[$~NonStringDataType$)] ; ^\n]}}</title>
        <p>The template generates the necessary parse command for non-string data
types. The silent NonStringDataType variable ensures that the parse command
is matched with a closing bracket.</p>
        <p>To summarize, the projective template technology provides an inference
mechanism for all of the template control code tasks, speci cally:</p>
        <p>Data Retrieval. The dataset for each block is de ned by its header and the
instance of the parent block being processed. It is derived by joining the parent
instance values with the source model, projecting the child block header onto
the resulting relation and further ltered the result set according to the block
required/optional attributes' designation and conditional constraints.</p>
        <p>Data Substitution. The projective template code generator processes a
block by iterating its input data. Hence, the imperative iteration control code
construct (for-each) that is common in many template-based technology is
unnecessary.</p>
        <p>Conditional Generation. The model extension mechanism makes it
possible to apply conditional generation without the need of explicit imperative
control code such as an if-then-else construct.
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Use Case</title>
      <p>The following use case demonstrates some of the projective template features.
Following is a template that generates SQL code for comparing tables
contents in di erent database instances highlighting with `***' changed columns in
each record. The metadata model is derived from querying the database system
tables and generating M = fEntityName,ColumnName,DataType,Length,PK),
the relation model. The model is further extended with the derived attributes
PKColumnName and NonStringColumnName that contain columns with PK='PK'
designation and columns that have a non-string data type, correspondingly.
[@EXTEND
Derive PKColumnName isa ColumnName where PK='PK';
Derive NonStringColumnName isa ColumnName</p>
      <p>where DataType not in ('varchar,nvarchar');@
[ select ' $EntityName$ ' as EntityName,'CHANGED ROWS' ,
[ case a.$ColumnName$ when b.$ColumnName$ then ''
else '***' end +
[convert(varchar(80),$~NonStringColumnName$]a.</p>
      <p>$ColumnName$[$~NonStringColumnName$)]</p>
      <p>as $ColumnName$ ^,\n#3]
from DL1..$EntityName$ a join DL2..$EntityName$ b
on [a.$PKColumnName$=b.$PKColumnName$ ^ and ]
where [(a.$ColumnName$&lt;&gt;b.$ColumnName$ or
a.$ColumnName$ is null and b.$ColumnName$ is not null or
b.$ColumnName$ is null and a.$ColumnName$ is not null )
^ or \n]]]</p>
      <p>The outer most block is generated for each EntityName. It includes the rst
selected column with the entity name constant and the from-clause with the
compared tables. The rst child block selects the tables' columns designating
changed data values with `***'. The second child block contains the join clause
and is generated only for PKColumnName attributes. The last child block
contains the where clause that selects record with any changed value. Using a
traditional, imperative approach would require adding data access code to retrieve,
test, lter, iterate and substitute the model data in the template.
5.1</p>
      <sec id="sec-4-1">
        <title>Bene ts</title>
        <p>Bene ts and Limitations
{ Separation of model and t emplate. A projector template is strictly
separated from the template control logic that is inferred by the code generator
in a consistent and predictable manner. Reusing the templates as well as
the metadata models is as easy as associating the relevant templates and
metadata sources.
{ Clarity and Conciseness. The projective templates are clear and readable
as they do not contain any template-control code or data access logic. They
merely represent an abstract description of their target code and thus do not
require the template developer to know any additional language.
{ Standard Control Code Inference. Since the control code is inferred from
the template block structure it is innately reusable among templates and
template blocks as it is always guaranteed to be the same for similar blocks.
{ Format f lexibility . The template is exible with respect to the text it can
generate as there are no formatting limitations imposed by a control
language. Generating a function call with parameters at the same line is as
easy as generating each parameters on a di erent line simply by changing
the separator associated with the block.
{ Change f lexibility . The template is very easy to change since there is no
need to change the control code logic or rearrange the template blocks among
di erent control blocks.
5.2</p>
        <p>
          Limitations
{ Lack of imperative control code. The lack of imperative control code prevents
the transformation from changing the model; however, this limitation is
introduced by design as in our view a transformation should not change its
source model [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ].
{ Change Impact. Changing the model or the template may have a great
impact on the generated code-base. Unless managed correctly, model changes
can become an impediment to development process. That is, however, a
potential issue with any generative technology and the projective templates
approach makes it possible to assess the impact of model changes by
comparing before/after projections.
{ Over generation. Having a powerful generation technology may encourage
the use of code-generation in place of standard coding techniques. On the
other hand, it sometimes makes sense to generate explicit code rather than
use a generic framework as code can be straightforward to understand and
analyze.
6
        </p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>Conclusion</title>
      <p>In this paper we presented the projective templates, a new approach to
templatebased code generation featuring relational projective templates that provide a
coherent and readable description of the target code. The projective template
generator infers the control code by employing relational algebra, treating the
template blocks as annotated relations that operate on the associated source
generating the instance set for each block. We have presented the bene ts of
separating the concerns of textual patterns and control code speci cations, speci
cally, the template readability, consistency, and clarity o ered by the relational
approach as well as the increased opportunity for control code reuse. Future
work may include expanding the projective data selection capabilities to handle
information that resides in several relational sets and non-relational formats.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>B.J.</given-names>
            <surname>Arnold</surname>
          </string-name>
          ,
          <article-title>An Illumination of the Template Enigma</article-title>
          .
          <source>IPA dissertation series</source>
          <year>2011</year>
          -
          <volume>02</volume>
          , Eindhoven University of Technology,
          <year>2011</year>
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>B.</given-names>
            <surname>Klatt</surname>
          </string-name>
          ,
          <article-title>Xpand: A Closer Look at the model2textTransformation Language. Institute for Program Structures</article-title>
          and
          <string-name>
            <given-names>Data</given-names>
            <surname>Organization</surname>
          </string-name>
          ,
          <year>2007</year>
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>C.</given-names>
            <surname>Henthorne</surname>
          </string-name>
          and
          <string-name>
            <given-names>E.</given-names>
            <surname>Tilevich</surname>
          </string-name>
          ,
          <source>Code Generation on Steroids: Enhancing COTS Code Generators via Generative Aspects. Virginia Tech</source>
          ,
          <year>2007</year>
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>D.</given-names>
            <surname>Jackson</surname>
          </string-name>
          ,
          <article-title>Software Abstractions: logic, language, and analysis</article-title>
          . MIT Press Cambridge, MA, 2006
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>E. F.</given-names>
            <surname>Codd</surname>
          </string-name>
          ,
          <article-title>A Relational Model of Data for Large Shared Data Banks</article-title>
          . IBM Research Laboratory, San Jose, CACM
          <volume>13</volume>
          (
          <issue>6</issue>
          ), June 1970
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>E.</given-names>
            <surname>Torlak</surname>
          </string-name>
          and
          <string-name>
            <given-names>D</given-names>
            .
            <surname>Jackson</surname>
          </string-name>
          ,
          <string-name>
            <surname>A</surname>
          </string-name>
          <article-title>Constraint Solver for Software Engineering: Finding Models and Cores of Large Relational Speci cations</article-title>
          .
          <source>Doctor of Philosophy</source>
          , Massachusetts Institute of Technology,
          <year>2009</year>
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <given-names>G.</given-names>
            <surname>Vos</surname>
          </string-name>
          ,
          <article-title>Issues of iterative MDA-based software development processes</article-title>
          ,
          <source>Master thesis</source>
          . University of Twente, the Netherlands,
          <year>2008</year>
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>S.</given-names>
            <surname>Jarzabek</surname>
          </string-name>
          ,
          <article-title>Software Reuse Beyond Components with XVCL</article-title>
          . National University of Singapore,
          <year>2007</year>
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <given-names>T.</given-names>
            <surname>Parr</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A Functional</given-names>
            <surname>Language For Generating Structured Text</surname>
          </string-name>
          . University of San Francisco,
          <year>2006</year>
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10. T. Parr,
          <string-name>
            <surname>Enforcing Strict</surname>
          </string-name>
          Model-View Separation in Template Engines. University of San Francisco,
          <year>2004</year>
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <given-names>W. R.</given-names>
            <surname>Otte</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Gokhale</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D. C.</given-names>
            <surname>Schmidt</surname>
          </string-name>
          , and
          <string-name>
            <given-names>J.</given-names>
            <surname>Willemsen</surname>
          </string-name>
          ,
          <article-title>Infrastructure for Component-Based DDS Application Development</article-title>
          . GPCE,
          <year>2011</year>
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <given-names>Y.</given-names>
            <surname>Zheng</surname>
          </string-name>
          and
          <string-name>
            <given-names>R. N.</given-names>
            <surname>Taylor</surname>
          </string-name>
          , A Rationalization of Confusion,
          <source>Challenges and Techniques in Model Based Software Development</source>
          . University of California Irvine,
          <source>ISR Technical Report UCI-ISR-11-5</source>
          ,
          <fpage>2011</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13.
          <string-name>
            <surname>Acceleo</surname>
          </string-name>
          , http://www.acceleo.org.
          <source>Accessed on 2012</source>
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          14.
          <article-title>Code Generation Using T4 Templates</article-title>
          , http://learneveryday.net/dot-net/
          <article-title>codegeneration-using-t4-templates</article-title>
          .
          <year>2010</year>
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          15.
          <string-name>
            <surname>Microsoft</surname>
          </string-name>
          <article-title>Code Generation and Text Templates (T4</article-title>
          ), http://msdn.microsoft.com/en-us/library/bb126445.aspx.
          <source>Accessed on 2013</source>
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>16. The Free Dictionary, http://www.thefreedictionary.com/template. Accessed on 2013</mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          17. Xpand Reference, http://www.openarchitectureware.org/pub/ newline documentation/4.3.1/html/contents/core reference.
          <source>html. Accessed on 2013</source>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>