The EMF Parsley DSL for Developing EMF Applications
Total Page:16
File Type:pdf, Size:1020Kb
The EMF Parsley DSL for Developing EMF Applications Lorenzo Bettini∗ Dipartimento di Informatica, Universita` di Torino, Torino, Italy Keywords: EMF, Modeling, Eclipse, DSL, User Interface. Abstract: The Eclipse Modeling Framework (EMF) is the official Eclipse modeling framework. It provides code gener- ation facilities for building tools and applications based on structured data models. The Eclipse project EMF Parsley enhances the EMF reflective mechanisms in order to make the development of EMF applications eas- ier by hiding most EMF internal details and by using dependency injection for customizing all the aspects of such applications. In this paper we show the main features of the EMF Parsley DSL that aims at making the development of EMF applications even easier and faster. The DSL is built with Xtext and Xbase, thus it features full Eclipse IDE support and full interoperability with Java. 1 INTRODUCTION The very first version of EMF Parsley was first presented in (Bettini, 2014). The framework has The Eclipse Modeling Framework (EMF) (Steinberg evolved a lot since then, concerning its usability, et al., 2008) is the official Eclipse modeling frame- mostly thanks to the EMF Parsley DSL, implemented work with code generation facilities for building tools in Xtext: customizations can be specified in a com- and applications based on structured data models. pact form in a single file. EMF Parsley, an official Eclipse project2, is a This paper extends previous presentations since it framework to quickly and easily develop user inter- concentrates on the DSL and all its new features that faces based on EMF models. EMF Parsley aims at have been recently added. The paper also describes in filling a few gaps in EMF development in general: the more details the architecture of EMF Parsley in such development of user interfaces based on EMF still re- a way that we think its design choices could be reused quires an extensive setup and the knowledge of many also in other frameworks targeting the same goals. internal details. On the contrary, EMF Parsley hides Since the DSL is implemented with Xtext (Bettini, most of the complexity of such internal details. The 2013a), it comes with a powerful and professional framework provides some UI components that can be Eclipse IDE support. used out-of-the-box (including trees, tables, dialogs Moreover, we also leverage Xbase (Efftinge et al., and forms, and views and editors). Programmers can 2012), a reusable expression language that can be use these widgets as the building blocks for their ap- used in Xtext DSLs. plications and customize them, or use them as a refer- In particular, Xbase’s type system is compliant ence implementation to build their own components with the Java type system (including generics). This based on our framework. Specification of custom be- also implies that a DSL that uses Xbase will have ac- haviors is based on the types of elements of the EMF cess to all the existing Java libraries. model, using polymorphic method dispatch mecha- The paper is structured as follows: in Section 2 we nism that allows to write cleaner and declarative code. describe the architecture and the main ingredients of All custom code is then “injected” in the framework our framework. Section 3 describes the EMF Parsley with Google Guice, a Dependency Injection frame- DSL with some examples. Section 4 describes some work. related work. Section 5 concludes the paper and hints for future directions. ∗Work partially supported by MIUR (proj. CINA), Ate- neo/CSP (proj. SALT), and ICT COST Action IC1201 BETTY. The paper was partly supported by RCP Vision, www.rcp-vision.com. 2https://www.eclipse.org/emf-parsley. 301 Bettini, L. The EMF Parsley DSL for Developing EMF Applications. DOI: 10.5220/0005743803010308 In Proceedings of the 4th International Conference on Model-Driven Engineering and Software Development (MODELSWARD 2016), pages 301-308 ISBN: 978-989-758-168-7 Copyright c 2016 by SCITEPRESS – Science and Technology Publications, Lda. All rights reserved MODELSWARD 2016 - 4th International Conference on Model-Driven Engineering and Software Development 2 EMF PARSLEY 1 @Inject ViewerFactory initializer; 3 TreeViewer viewer = new TreeViewer(parent, ...); In this Section we describe the overall architecture initializer.initialize(viewer, resource); of EMF Parsley. This Eclipse project is still in the incubation phase and it will ship the first stable re- Listing 1: Initialization of a viewer with EMF Parsley. lease soon. The main design choice of EMF Parsley public class MyLabelProvider extends ViewerLabelProvider is to split responsibilities into small classes in order { 2 public String text(Book book) { to make it easy and straightforward to customize each return "Book: " + book.getTitle(); single aspect of an EMF application. Differently from 4 } public String image(Book book) the standard EMF application development workflow, { in EMF Parsley the programmer is not required to 6 return "book.png"; // other customizations } modify monolithic generated classes. 8 Dependency Injection. In order to handle the } Listing 2: An example of customization of labeling in customized behaviors in a consistent way, we heav- EMF Parsley. ily use Google Guice, a Dependency Injection frame- work. This has several benefits: (i) We do not need Custom implementations of specific interfaces to reimplement all the reflective mechanisms of and classes are injected in the framework’s classes, EMF.Edit, which already provides good defaults; so that all the other classes in the framework will be (ii) EMF Parsley applications seamlessly interoperate assured to use the custom version. Google Guice uses with existing EMF.Edit customizations; (iii) This in- Java annotations, @Inject, for specifying the fields teraction fosters an incremental or partial porting of that will be injected, and a module is responsible for an EMF application to EMF Parsley. Initializing a configuring the bindings for the actual implementa- tree viewer with EMF Parsley is just a matter of a few tion classes. lines of code, Listing 1. We took inspiration from Xtext, where Google Declarative Customizations. In EMF Parsley we Guice is heavily used. EMF Parsley uses the en- provide declarative customization mechanisms that hancements that Xtext added to Guice’s module are easier to use than the standard EMF.Edit frame- API: Google Guice bindings are specified by declar- work. We use the class PolymorphicDispatcher, ing methods that have the shape bind<ClassName> implemented in Xtext, for performing overloaded where ClassName is the name of the class for which method dispatching according to the runtime type of we want to specify a binding. arguments, a mechanism known as dynamic over- Of course, the programmer can also use the stan- loading. dard Google Guice mechanisms for specifying the This enables a declarative way of specifying cus- bindings. As we will see later, when using the EMF tom behaviors according to the class of objects of an Parsley DSL, all these bindings will be generated au- EMF model. tomatically. For example, by implementing a custom Viewer- EMF.Edit. EMF.Edit (Steinberg et al., 2008) is LabelProvider (a label provider of our framework), the part of the EMF framework with generic reusable the programmer can specify the text and image for la- classes for editing EMF models. bels of the objects of the model by simply defining It provides a command framework, with a set of several methods text and image, respectively, using generic command implementation classes for build- the classes of the model to be customized as param- ing editors that support undo and redo mechanisms. eters. An example is shown in Listing 2 (using the The problem is that all these mechanisms have to EMF Library example). be setup and initialized correctly in order to achieve This code is much more readable and easier to the desired behavior. This initialization phase takes write than the usual instanceof cascades and casts. many lines of code, and usually requires some deeper Note also that we only need to specify the image file knowledge of EMF internals. name, and EMF Parsley will automatically retrieve For this reason, EMF Parsley is built on top of such file from the “icons” directory of the containing EMF.Edit, but it hides all the above mentioned in- Eclipse project and create the image object. Most of ternal details and it automatically sets all EMF.Edit the customizations in EMF Parsley follow the same mechanisms. In particular, the customizations that declarative pattern. are specified using EMF Parsley have the precedence, Injecting this customization is just a matter of and, in the absence of customizations, the default be- defining the binding in the Guice module, as shown haviors are delegated to EMF.Edit. in Listing 3. 302 The EMF Parsley DSL for Developing EMF Applications public class MyCustomModule extends EmfParsleyGuiceModule 1 module org.eclipse.emf.parsley.examples.librarytreeform { { 2 public Class<? extends ILabelProvider> bindILabelProvider() parts { { return MyLabelProvider.class; 3 viewpart views.librarytreeform { 4 ... viewname "My Library Tree Form" } 5 viewclass SaveableTreeFormView } } Listing 3: Binding the custom label provider. 7 ... } EMF Parsley pro- Listing 4: An example of module definition in EMF Reusable UI Components. Parsley DSL. vides an infrastructure of Java classes and the injec- tion mechanisms to configure any Eclipse UI compo- [ param1, param2, ... | body ] The types of nent, e.g., views and editors. It also provides some of parameters can be omitted if they can be inferred from these components that can be used out-of-the-box and the context. For these reasons, Xbase expressions are that can be easily customized. We provide Eclipse ed- quite compact, very readable, and look like they are itors for editing EMF models. We also provide views written in an untyped language. that can also edit EMF models. Specification Structure. A specification in EMF Such views can represent EMF models with a tree, Parsley DSL, i.e., an input file, consists of a mod- a table, a form, or a combination of them.