<!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>Auto-generation of Model Visitor Frameworks</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Adolfo Sanchez-Barbudo Herrera</string-name>
          <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">UK</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>The visitor pattern, a well known Gang of Four design pattern, provides a suitable way to add operational behaviour to models. However, as soon as the number of metamodels and visitor implementations start to grow, some of the pattern shortcomings make its usage less convenient. This paper presents how the synergy between the Visitor pattern and MDE has been addressed by two open source projects: Eclipse OCL and QVTd. As a result, a visitors framework generator is proposed to alleviate some of the visitor pattern shortcomings.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Challenge</title>
      <p>
        The visitor pattern is one of the behavioural patterns described in the Gang of
Four software design patterns [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. The exibility that this pattern provides, by
the means of allowing the decoupled addition of behaviour to a collection of
objects without any need to change their corresponding classes, makes this pattern
attractive in object oriented software design and related research [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. In
ModelDriven Engineering (MDE), research on visitors has received less attention, with
a few known works [
        <xref ref-type="bibr" rid="ref3 ref4 ref7">3, 4</xref>
        ].
      </p>
      <p>
        Whereas applying the visitor pattern to models might be considered
straightforward in the Eclipse Modeling Framework (EMF) [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], it turns to be a task that
is, at least, as tedious as performing it in a traditional programming language.
For example, every X EClass requires an accept EOperation; a visitor EClass
requires a visitX EOperation for every X EClass. If the number of involved
metamodels (and subsequently metaclasses) grows large, the shortcomings
associated to the visitor pattern hinder its adoption.
      </p>
      <p>Provided the potential bene ts that the visitor pattern can bring to models,
this paper focuses on the challenge presented by the limitations of using the
visitor pattern in MDE, which we propose to overcome by reducing the amount
of manual intervention required to realize the visitor pattern. Speci cally, this
challenge has been tackled in the context of the Eclipse OCL1 and Eclipse QVTd2
projects by the means of a visitors framework generator.
1 https://projects.eclipse.org/projects/modeling.mdt.ocl
2 https://projects.eclipse.org/projects/modeling.mmt.qvtd</p>
    </sec>
    <sec id="sec-2">
      <title>Why a Visitors Framework Generator in MDE?</title>
      <p>Eclipse OCL and Eclipse QVTd are two projects conceived to support MDE
by providing model management languages and tools, in which a substantial
number of EMF based metamodels are involved. The reasons of introducing
model visitors come from the original visitor pattern: the operational behaviour
of a model is centralised in a visitor class; and the metamodel doesn't need to
be changed every time a new behaviour is needed, which is convenient to third
party consumers who can not alter the metamodel at all.</p>
      <p>
        However, given the substantial number of metamodels and comprised
metaclasses, introducing the pattern is rather a tedious task. Additionally, evolving
the visitors along with any change to the underlying metamodel is troublesome,
as identi ed in the original design pattern [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. To alleviate this situation, MDE
techniques can be adopted. In particular, automated code generation can be
used to produce visitor frameworks from metamodel de nitions. On top of the
framework, actual visitor implementations can provide particular operational
behaviours (e.g. evaluators, pretty printers, etc.).
      </p>
      <p>
        With the proposed generator we not only leverage the barrier of model
visitors creation, but also we can alleviate some of the shortcomings of the visitor
pattern in some metamodel evolution scenarios [
        <xref ref-type="bibr" rid="ref4 ref7">4</xref>
        ]: for example, by having an
automatically generated framework of visitors for a speci c metamodel, we could
keep existing visitor implementations working when a new metaclass is added,
by just regenerating the model speci c visitors framework.
3
      </p>
    </sec>
    <sec id="sec-3">
      <title>Visitors Framework Generator in Eclipse OCL/QVTd</title>
      <p>In Eclipse OCL there is a MDE-based tool which facilitates the generation of
metamodel-speci c visitors framework. The main features to highlight:</p>
      <p>High degree of automation: For a given metamodel, the visitor pattern
is weaved into the generated metamodel implementation, so that for every
nonabstract X class an accept method is generated, and whose implementation will
delegate to the corresponding visitX method of a Visitor interface, as depicted
by Figure 1.</p>
      <p>Additionally, a framework of abstract visitors with various purposes3 is also
generated. Figure 2 gives a brief overview of the framework for a particular OCL
metamodel.
Support for derived metamodels: QVT languages reuse OCL as their
expression languages. Therefore, the tooling provides generation of visitors which
can extend the visitors generated for a di erent metamodel they might extend
and/or use. Figure 3 shows a snippet of a particular QVTd visitor of the
generated visitors framework.
3 Getting into details of the framework is beyond the scope of this paper</p>
    </sec>
    <sec id="sec-4">
      <title>Visitors Framework Generator Implementation</title>
      <p>As the reader might understand, we can't get into details in this short paper
about the generated visitors framework or provide implementation details about
the own generator. However, in this section we give a brief overview of the
developed tooling as well as mentioning some of the involved third party tools.</p>
      <p>
        Figure 4 depicts the overall approach. The input Ê to the generator is an
EMF [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] GenModel ler referring to an Ecore le that corresponds to the target
meta-model. The GenModel contains additional annotation to provide
information relevant to the generator (e.g. fully quali ed name for the Visitor and
Visitable interfaces). The generator Ë is based on the Model Work ow Engine
(MWE) [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] technology, and the developed MWE components are responsible for:
{ Generating a 'visitable' model java implementation Ì. This involves
invoking the EMF generator, which will generate the enhanced implementation
by inserting the corresponding accept methods for every model class. We
developed EMF JET templates to achieve this insertion.
{ Generating the model-speci c visitors framework Í. This includes
generating the Visitor and Visitable interfaces and a set of default abstract visitors
implementation. Giving implementation details about these default visitors
goes beyond the goal of this paper.
      </p>
      <p>With all of this, creating speci c behaviours for the model can be achieved
by implementing manual visitors Î that extend the appropriate default visitor
of the generated framework.</p>
      <p>Alternatively, by the means of additional MDE-based tools Ï, we could also
generate more speci c visitors Ð that comprise a particular modelled behaviour.</p>
    </sec>
    <sec id="sec-5">
      <title>Visitors usage in Eclipse OCL/QVTd</title>
      <p>To conclude, we provide an overview about how the visitors framework generator
is extensively used within Eclipse OCL and QVTd projects. Table 1 provides
detailed measurements about the generated visitors usage. The table variables
are de ned as follows:
{ Vn: Denotes the number of involved metamodels on which the visitor pattern
has been applied.
{ Va: Denotes the number of visitors classes which are automatically generated
as part of the visitors framework4.
{ Vm: Denotes the number of visitors classes which are manually implemented,
and which rely on the automatically generated visitors framework.</p>
      <p>Project Vn Va Vm
OCL 7 48 67</p>
      <p>QVTd 10 89 76</p>
      <p>Table 1. Measurements of visitors usage in Eclipse OCL &amp; QVTd</p>
      <p>Given the high amount of visitor implementations, and the substantial
autogenerated infrastructure that the model speci c visitors frameworks provide, it
can be concluded that both the visitor pattern and the framework generator
have turned to be fundamental within the Eclipse OCL and QVTd projects.</p>
      <p>Acknowledgement. I gratefully acknowledge the support of the
Engineering and Physical Sciences Research Council (UK) via the LSCITS initiative.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Erich</surname>
            <given-names>Gamma</given-names>
          </string-name>
          , Richard Helm, John Vlissides, and Ralph Johnson.
          <article-title>Design patterns: elements of reusable object-oriented software</article-title>
          .
          <source>Addison-Wesley</source>
          ,
          <year>1994</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Tanumoy</given-names>
            <surname>Pati</surname>
          </string-name>
          and
          <string-name>
            <surname>James H Hill</surname>
          </string-name>
          .
          <article-title>A survey report of enhancements to the visitor software design pattern</article-title>
          .
          <source>Software: Practice and Experience</source>
          ,
          <volume>44</volume>
          (
          <issue>6</issue>
          ):
          <volume>699</volume>
          {
          <fpage>733</fpage>
          ,
          <year>2014</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>Holger</given-names>
            <surname>Krahn</surname>
          </string-name>
          , Bernhard Rumpe, and
          <article-title>Steven Volkel. Monticore: a framework for compositional development of domain speci c languages</article-title>
          .
          <source>International journal on software tools for technology transfer</source>
          ,
          <volume>12</volume>
          (
          <issue>5</issue>
          ):
          <volume>353</volume>
          {
          <fpage>372</fpage>
          ,
          <year>2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>Adolfo</given-names>
            <surname>Sanchez-Barbudo</surname>
          </string-name>
          <string-name>
            <given-names>Herrera</given-names>
            , Edward D. Willink, Richard F. Paige,
            <surname>Louis M. Rose</surname>
          </string-name>
          , and
          <string-name>
            <surname>Dimitrios</surname>
            <given-names>S.</given-names>
          </string-name>
          <string-name>
            <surname>Kolovos</surname>
          </string-name>
          .
          <article-title>Automatic application of visitors to evolving domain-speci c languages</article-title>
          . In Sam Simpson, editor,
          <source>Sixth York Doctoral Symposium</source>
          , pages
          <volume>46</volume>
          {
          <fpage>54</fpage>
          . University of York,
          <year>October 2013</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>Dave</given-names>
            <surname>Steinberg</surname>
          </string-name>
          , Frank Budinsky, Marcelo Paternostro, and Ed Merks.
          <source>EMF: Eclipse Modeling Framework. Addison-Wesley</source>
          ,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Modeling</surname>
          </string-name>
          <article-title>Work ow Engine 2 Documentation</article-title>
          . On-Line: https://eclipse.org/ Xtext/documentation/306_mwe2.html.
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          <article-title>4 Interfaces such as Visitor/Visitable are excluded</article-title>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>