<!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>Code Generation as a Service</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Robert Crocombe</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Dimitris Kolovos</string-name>
          <email>dimitris.kolovosg@york.ac.uk</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Department of Computer Science, University of York</institution>
          ,
          <country country="UK">United Kingdom</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>Code generation is an important tool in model-driven engineering. It eases the transition between designing models and implementing systems in code. This project aims to make code generation a web service, adding a layer of abstraction between the user's models and the generator used to produce their code.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>The goal of this project is to develop a web service that allows users to publish
code generators and execute them online. Generators are publicly available so
anyone can use them on their own models. A user uploads their models to be
processed and receives the output les produced by their chosen generator.</p>
      <p>The motivation for this project is having the ability to generate code from
models without the need for locally-available software. This removes the e ort
of learning and maintaining the software from the user. It bene ts the situation
where the individual writing the code generator is di erent to the one designing
the models, like a team or client. The model designer does not need to know
how the generator works but can bene t from the output it creates using their
own models. The same abstraction can be placed on the service itself. While the
internal system may change - to be optimised or made faster for example - the
API stays the same.</p>
      <p>Code generation as a service also bene ts situations where text is generated
from very large models depicting a complex system. Computing in the cloud
can have increased processing power and resources over a local machine, so
code generation could take a shorter amount of time to complete, aiding the
productivity of the user.
An investigation of previous work in this area has been carried out and will be
presented in this section. This process helps determine whether the project is
novel enough to pursue, and the potential bene ts of the nished system.</p>
      <p>Recently, there has been an interest for model-driven engineering solutions
in the cloud. Other elds of technology have, in recent years, adopted web-based
services successfully. For example, cloud storage of les and document editing,
integrated development environments (IDEs) accessed through the web, and
cloud computation.</p>
      <p>
        As people that make use of MDE are also software engineers, it is interesting
to take a look at what side of software engineering the current services focus
on. There are currently many services that provide standard IDE facilities like
a code editor with syntax highlighting and error detection (such as Koding [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]
and Cloud 9 [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]), some of which include code compilation (such as Visual Studio
Online [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ] and IDE One [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ]). It is this aspect of these services that most
closely resembles the aim of this project, as it involves uploading the source to
the service, processing it and returning an output.
      </p>
      <p>
        While there has been lots of progress made with online code compilation, code
generation has been less prevalent. APIMATIC [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] is a service that will generate
source code in multiple languages from a REST API model. Virtual Developer [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]
is a company o ering a marketplace for code generators and support in setting up
their own software for code generation. Current solutions take a similar business
model as some of the online IDE services by asking for a monthly fee and the tools
are accessed via a web browser. Virtual Developer's distinction from this project
is its goal to be more of a hosting platform for code generator technologies (like
their own modelling tool or MetaEdit+ [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ]), including having the client setup or
pay to setup the tools themselves.
      </p>
      <p>
        One of the most fully-featured services available online is GenMyModel [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. It
features a browser-based UML diagram editor and code generation from UML.
Unlike APIMATIC it does not limit users to a REST API - anything can be
modelled. It also appears to be easier to use than Virtual Developer as there's
no setup to perform or self-hosted tools required. GenMyModel still limits the
user to their own UML tools and the limited set of languages that can be output.
      </p>
      <p>
        This project aims to have some novel di erences in its implementation.
Because it will primarily use Epsilon [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ] to perform model-to-text transformations,
the generated output can be made for any language. The service will be
implemented with an API, so any client program can utilise it. An experienced
developer could write a client for their own uses for example. Finally, generators
can be reused or made public for others, improving the options available for
users of the service.
3
      </p>
    </sec>
    <sec id="sec-2">
      <title>Service Implementation</title>
      <p>This section will describe how the service is used by a client as a step-by-step
process, and detail how the service works internally. The service was written in
Java, chosen because of its portability and maturity of support.</p>
      <p>Representational State Transfer (REST) was chosen as the web service
architecture because of the ease in which a client can be implemented to interface
with a RESTful service. It was desirable to allow anyone to write a client for the
service. Requests made to the service also work well with the RESTful style. For
example, getting a list of generators with a GET request, or publishing a new
one with a POST request.
3.1</p>
      <p>Publishing a Generator
One of the main aims of the project is allowing users to execute generators that
others have made, and reuse generators without having to upload them to the
service again. To this end, generators are `published' to the service and available
for anyone to use on their own models. A system consists of an input, a process,
and an output. Generators act as the processing stage of the system, with a
model as the input and text as output. Because generators should be reusable,
they have to be independent from the models they can parse.</p>
      <p>
        Apache Ant [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], a tool for automating the software build process, is used
by the service to execute generators. This design decision was made because of
the advantages of having Ant handle all code execution instead of performing it
inside the service. Epsilon already has Ant tasks for every language it supports.
The Epsilon Generation Language (EGL) is a domain-speci c language (DSL)
used by Epsilon to generate text from models. Epsilon handles all execution of
the EGL generator, the service only needs to execute the Ant build. This greatly
simpli es the work the service has to do, as well as o er greater exibility to the
generator maker. They can include other code such as the Epsilon Validation
Language which validates models. An Ant project allows for custom properties
to be de ned, removing the need for a separate le de ning metadata about the
generator.
&lt;project name="pacs-emf-gen" default="main"&gt;
&lt;property name="model" value="" description="The input EMF model"/&gt;
&lt;target name="main"&gt;
&lt;epsilon.emf.register file="test.ecore"/&gt;
&lt;epsilon.emf.loadModel name="M" modelfile="${model}"
metamodeluri="http://cs.york.ac.uk/"
read="true" store="true"/&gt;
&lt;epsilon.egl src="test.egl" outputroot="${outputRoot}"&gt;
      </p>
      <p>&lt;model ref="M"/&gt;
&lt;/epsilon.egl&gt;
&lt;/target&gt;
&lt;/project&gt;</p>
      <p>Figure 1 is an example of how an Ant build le might be constructed for a
generator. XML nodes starting with \epsilon" are tasks for generating the output
text. The Ant build lists \test.ecore" as the metamodel in which all models need
to conform to. \Test.egl" is also listed as the EGL le that will be executed
against the model. Both these les need to be included in upload sent to the
service.</p>
      <p>
        While planning the service, a decision needed to be made about how
generators would be stored/accessed on the service. The straightforward approach
would be to directly upload the les to the service. Instead, it was decided that
generators would be retrieved from GitHub [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] and cloned to service's le
system. This adds several advantages over direct upload. It allows a generator to
be updated automatically if one is modi ed using a Git pull request. There are
indirect bene ts to this approach as well. The generator can be uniquely
identi ed by its GitHub name (`owner/repository') as administration is all handled
by GitHub. The names are also guaranteed to be URL friendly. A generator can
be worked on in collaboration and Git encourages source control and backup.
      </p>
      <p>POST /service/generators?owner=robcrocombe&amp;repo=emf-gen
In this project, a job refers to the single process of creating a request for text to
be generated from a model, executing that request, and returning the output to
the client. A job request should select which generator to use and contain the
necessary metadata required for the generator to run.</p>
      <p>An important design decision made was how the client would wait for a job
to be completed. The amount of time it takes for code generation to run depends
on the size of the model and complexity of the generator. Jobs normally take
longer than the usual HTTP request so there needed to be a suitable system for
dealing with this. The solution was that a job POST request would initialise a
separate thread for the job and return a unique ID. The client then uses the ID
to poll the service for the status of the job, ideally at regular intervals. When
the job is complete a status request will instead return a zip le containing the
generated output.</p>
      <p>When a client wishes to create a job, a POST request is sent to the /job
endpoint in the service. The request body has a `multipart/form-data' content
type. This allows les to be attached as elds in the form. Every request must
contain a JSON le containing the metadata needed by the service.</p>
      <p>Figure 3 is an example of the JSON le. Here, the client speci es which
generator to use with the \githubOwner" and \repoName" elds. Any string
properties can be given as an array here. The properties and les are both
inserted into the build.xml le before Apache Ant executes it.
{
}
githubOwner: "robcrocombe",
repoName: "emf-gen",
properties:
[ {
name: "debug",
value: "true"
} ],
files:
[ {
} ]
fieldName: "file1",
propertyName: "model",
filePath: "test.model"</p>
      <p>For example, the le with \model" as the \propertyName" will be put into
the \model" property in gure 1.</p>
      <p>Each item in the \ les" array contains three elds. \ eldName" links the
item where the le has been attached in the form. These two elds must have
the same name. \propertyName" is the name of property in the Ant build this
le is for. \ lePath" is the path of the le including the extension. This eld must
be a relative path. This means, for example, if the eld contains
\directory/subdirectory/test.model" the le will be put into that folder structure relative to
the job folder on the server.</p>
      <p>This upload method also supports folders. To do this, each le must be
included as an item in the \ les" array, with their le path including the folder
they belong to. The property name is not included, as the le on its own is not a
property value. Instead, the folder is added to the \properties" array, the name
eld being the name of the property in the Ant build.xml, and the value being
the name of the folder. This works because les are still Ant properties, they
just have to be declared separately so they can be uploaded to the service.</p>
      <p>Figure 4 is a sequence diagram summarising how a job is created, how its
status is polled, and the output received by the client. The advantage of this
solution is that it scales well. If the processing time is short, the rst poll would
return the output. If it is long, there is little overhead to requesting the status
regularly. An alternative solution was to open a small server on the client
program and when the job is complete it will send the output to the client like red
event. This was not as preferable because it was desired that the client would
be lightweight and simple to implement. The service should also stay RESTful
so the client should be the only entity making requests.</p>
    </sec>
    <sec id="sec-3">
      <title>Conclusions</title>
      <p>This paper has covered reasons for pursuing this project, its development progress,
and how the service can be consumed. If this project was to be made available
for public use some features would have to be developed further. For example,
currently only public GitHub repositories can be published to the service, as
private repositories would need authentication. Private repositories would be
important for users if they wish to work on closed-source projects.</p>
      <p>Fleshing out generator publishing would also be desirable. For example adding
user accounts to keep track of generators and allow them to be updated by their
owners.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>Apache</given-names>
            <surname>Software</surname>
          </string-name>
          <article-title>Foundation: Apache ant (</article-title>
          <year>Jul 2015</year>
          ), http://ant.apache.org
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2. APIMATIC:
          <string-name>
            <surname>Apimatic</surname>
          </string-name>
          (Jul
          <year>2015</year>
          ), http://apimatic.io
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Cloud9</surname>
            <given-names>IDE</given-names>
          </string-name>
          ,
          <article-title>Inc: Cloud 9 development environment</article-title>
          (
          <year>Jul 2015</year>
          ), http://c9.io
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>Generative</given-names>
            <surname>Software</surname>
          </string-name>
          <string-name>
            <surname>GmbH</surname>
          </string-name>
          :
          <article-title>Virtual developer (</article-title>
          <year>Jul 2015</year>
          ), http://www.virtualdeveloper.com
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>Generative</given-names>
            <surname>Software</surname>
          </string-name>
          <string-name>
            <surname>GmbH</surname>
          </string-name>
          :
          <article-title>Virtual developer modelling</article-title>
          (
          <year>Jul 2015</year>
          ), http://www.virtual-developer.com/en/modeling
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6. GenMyModel: Genmymodel (Jul
          <year>2015</year>
          ), http://www.genmymodel.com
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7. GitHub, Inc: Github (Jul
          <year>2015</year>
          ), http://github.com
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>Koding</surname>
          </string-name>
          ,
          <article-title>Inc: Koding cloud development environment</article-title>
          (
          <year>Jul 2015</year>
          ), http://koding.com
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Kolovos</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          et al.:
          <source>Epsilon (Jul</source>
          <year>2015</year>
          ), http://www.eclipse.org/epsilon
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Microsoft</surname>
          </string-name>
          :
          <article-title>Visual studio online</article-title>
          (
          <year>Jul 2015</year>
          ), http://www.visualstudio.com/products/whatis
          <article-title>-visual-studio-online-vs</article-title>
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11. Sphere Research Labs: IDE One (
          <year>Jul 2015</year>
          ), http://ideone.com
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>