32
Enriching EMF Models with Scala Filip Krikava I3S Laboratory, CNRS, France (quick overview)

Enriching EMF Models with Scala (quick overview)

Embed Size (px)

DESCRIPTION

Talk given at EclipseCon'12 Europe Modeling Symposium.

Citation preview

Page 1: Enriching EMF Models with Scala (quick overview)

Enriching EMF Models with Scala

Filip KrikavaI3S Laboratory, CNRS, France

(quick overview)

Page 2: Enriching EMF Models with Scala (quick overview)

Enriching EMF Models

• Implementing

• derived properties

• operations’ bodies

• constrains

using

Page 3: Enriching EMF Models with Scala (quick overview)

Enriching EMF Models

• Implementing

• derived properties

• operations’ bodies

• constrains

using

Page 4: Enriching EMF Models with Scala (quick overview)

Motivation

Page 5: Enriching EMF Models with Scala (quick overview)

MotivationLibrary

+ getBookByName(EString) : Book [0..1]

Book

- name : String [0..1]- isbn : String [1..1]

Writer

- name : String [1..1]

0..*

writers

1..*

writers0..*

books

0..*books

Page 6: Enriching EMF Models with Scala (quick overview)

Motivation

How to make sure the books ISBN in the model are unique?

Page 7: Enriching EMF Models with Scala (quick overview)

Implementation (model)

• Add a new Ecore annotation

• Add a new operation

Page 8: Enriching EMF Models with Scala (quick overview)

! * @generated! */! public boolean validateBook_UniqueISBN(Book book,! ! ! DiagnosticChain diagnostics, Map<Object, Object> context) {! ! // TODO implement the constraint! ! // -> specify the condition that violates the constraint! ! // -> verify the diagnostic details, including severity, code, and! ! // message! ! // Ensure that you remove @generated or mark it @generated NOT! ! if (false) {! ! ! if (diagnostics != null) {! ! ! ! diagnostics.add(createDiagnostic(Diagnostic.ERROR,! ! ! ! ! ! DIAGNOSTIC_SOURCE, 0,! ! ! ! ! ! "_UI_GenericConstraint_diagnostic", new Object[] {! ! ! ! ! ! ! ! "UniqueISBN", getObjectLabel(book, context) },! ! ! ! ! ! new Object[] { book }, context));! ! ! }! ! ! return false;! ! }! ! return true;! }

Implementation (Java)

Page 9: Enriching EMF Models with Scala (quick overview)

! * @generated NOT! */! public boolean validateBook_UniqueISBN(Book book,! ! ! DiagnosticChain diagnostics, Map<Object, Object> context) {! ! boolean violated = false;! !! ! for (Book e : book.getLibrary().getBooks()) {! ! ! if (e != book && e.getIsbn().equals(book.getIsbn())) {! ! ! ! violated = true;! ! ! ! break;! ! ! }! ! }! !! ! if (violated) {! ! ! if (diagnostics != null) {! ! ! ! diagnostics.add(createDiagnostic(Diagnostic.ERROR,! ! ! ! ! ! DIAGNOSTIC_SOURCE, 0,! ! ! ! ! ! "_UI_GenericConstraint_diagnostic", new Object[] {! ! ! ! ! ! ! ! "UniqueISBN", getObjectLabel(book, context) },! ! ! ! ! ! new Object[] { book }, context));! ! ! }! ! ! return false;! ! }! ! return true;! }

Implementation (Java)

Page 10: Enriching EMF Models with Scala (quick overview)

• Flexible,

• but

• violates generation gap pattern

• not very expressive

Implementation (Java)

Page 11: Enriching EMF Models with Scala (quick overview)

We could use a domain-specific language

Page 12: Enriching EMF Models with Scala (quick overview)

Implementation (OCL)

! context Book! ! inv invariant_UniqueISBN:! ! self.library.books->forAll (e | e <> self implies e.isbn <> self.isbn)

Page 13: Enriching EMF Models with Scala (quick overview)

• Higher level of abstractions,

• but

• scalability problems of

• the language

• the tooling support

On the Use of an Internal DSL for Enriching EMF Models

Filip K

ˇ

rikava

Université Nice

Sophia Antipolis, France

I3S - CNRS UMR 7271

[email protected]

Philippe Collet

Université Nice

Sophia Antipolis, France

I3S - CNRS UMR 7271

[email protected]

ABSTRACTThe Object Constraint Language (OCL) is widely used to enrichmodeling languages with structural constraints, side effect free queryoperations implementation and contracts. OCL was designed to besmall and compact language with appealing short “to-the-point”expressions. When trying to apply it to larger EMF models someshortcomings appear in the language expressions, the invariant con-structs as well as in the supporting tools.

In this paper we argue that some of these shortcomings are mainlyrelated to the scalability of the OCL language and its trade-offs be-tween domain-specificity and general-purpose. We present an al-ternative approach based on an internal DSL in Scala. By usingthis modern multi-paradigm programing language we can realizean internal DSL with similar features found in OCL while takingfull advantage of the host language including state-of-the-art toolsupport. In particular, we discuss the mapping between the OCLand Scala concepts together with some additional constructs forbetter scalability in both expressiveness and reusability of the ex-pressions.

1. INTRODUCTIONOCL is used to complement the limited expressiveness of the

structural constraints of the modeling languages like UML (Uni-fied Modeling Language) or EMF (Eclipse Modeling Framework).Such model constraints are captured as state invariants using a side-effect free expression language that supports first order predicatelogic with model querying and navigation facilities [18, 15]. More-over, these expressions can be further used to include additionalinformation to the model such as operation contracts in form of preand post conditions, and implementation of derived features andoperation bodies. OCL is also embedded in context of other toolssuch as the Object Management Group QVT model transformation.

OCL is an appealing and expressive language, but when appliedto larger EMF models using Eclipse OCL1, we found a number ofshortcomings in the language expressions, the invariant constructsas well as in the supporting tools. While some of these problemsare already well identified in the literature either as research agendaor accompanied with some solutions (c.f. Section 2), the lack of anoverall integration eventually led us to look for alternatives. Ac-cording to us, some of these shortcomings are in general related tosome scalability issues as a result of trade-offs between domain-specificity and general-purpose.

In this paper we present an alternative approach based on an in-ternal DSL in Scala2, a statically typed object-oriented and func-tional programming language that runs on top of a Java VirtualMachine (JVM). Besides the seamless integration with EMF, some

1http://goo.gl/TECuz2http://www.scala-lang.org/

Scala features, such as support for higher-order functions, rich col-lection libraries and implicit type conversions, allow us to writevery similar OCL-like expressions, but also leverage from the manylibraries found in the Java and Scala ecosystems. Besides Scala alsocomes with state-of-the-art tool support.

The rest of the paper is organized as follows. In Section 2 wedescribe the main shortcomings of OCL based on our experience.Section 3 shows how we use Scala as an alternative approach toenrich EMF models. It is followed by Section 4 where the im-plementation details are presented together with an evaluation. InSection 5, we discuss related work. We briefly conclude and outlinefuture work in Section 6.

2. SOME SHORTCOMINGS OF OCLIn this section we present a list of shortcomings we came across

while using OCL in an EMF based MDE toolchain, but varioususages of OCL reveal different pros and cons. In our study wewere concerned with the practical side of OCL rather than a formalone like in [14] or [4].

For each of the point we also report on works in which similarproblems were reported. Despite the fact that many of these issueshave already been identified or addressed, the lack of an overallintegration is a crucial issue, which, according to us, influences theslow adoption of OCL in the industry.

2.1 OCL ExpressionsOne of the key points that Anders Ivner mentions in the foreword

to the second edition of The Object Constraint Language [18] is“Second, it is compact, yet powerful. You can write short and to-the-point expressions that do a lot”. While this is true for manyof the short and straight-forward expressions, when the complex-ity grows our ease of reading and writing of these expressions de-creases radically. This might be especially hard for the new userswhen they move from the tutorial like expressions to real worldones.

Complex expressions are hard to write and maintainOCL constraints and queries are defined in form of expressions thatcan be chained together. The underlying linearity of this chainingoften leads to long and complex expressions that are difficult tounderstand and maintain. Since the debugging support in OCL israther limited, mostly only simple logging or tracing is possible,maintaining of these expressions is particularly hard.

In [10] Correa et al. provide some refactoring techniques to sim-plify some of these complex expressions. Ackermann et al. pro-pose in [1] to utilize specification patterns for which OCL con-straints can be generated automatically, together with a collectionof OCL specification patterns formally defined. These techniques

F. Krikava and P. Collet, On the Use of an Internal DSL for Enriching EMF Models, OCL 2012 Workshop

Implementation (OCL)

Page 14: Enriching EMF Models with Scala (quick overview)

Approaches

• Low level

• Flexible

• State-of-the-art tooling

• Higher level

• Scalability problems

• language

• tools

Could we combine the two?

GPL DSL

Page 15: Enriching EMF Models with Scala (quick overview)

An Internal DSL in Scala

! def validateUniqueISBN(self: Book): Boolean = { ! self.library.books forall (e => e != self implies e.isbn != self.isbn)! }

Scala version

Page 16: Enriching EMF Models with Scala (quick overview)

An Internal DSL in Scala

! context Book! ! inv invariant_UniqueISBN:! ! self.library.books->forAll (e | e <> self implies e.isbn <> self.isbn)

OCL version

! def validateUniqueISBN(self: Book): Boolean = { ! self.library.books forall (e => e != self implies e.isbn != self.isbn)! }

Scala version

Page 17: Enriching EMF Models with Scala (quick overview)

An Internal DSL in Scala

• Bridging the gap between Scala and EMF

! def invokeGetBookByName(self: Library, name: String): Option [Book] = { ! self.books find (name == _.name)! }

Library

+ getBookByName(EString) : Book [0..1]

Page 18: Enriching EMF Models with Scala (quick overview)

Library

+ getBookByName ( EString ) : Book [0..1]

• Bridging the gap between Scala and EMF

! def invoke GetBookByName(self: Library, name: String): Option [Book] = { ! self.books find (name == _.name)! }

An Internal DSL in Scala

Page 19: Enriching EMF Models with Scala (quick overview)

An Example

Constraint: A writer’s name must be capitalized.

Geral Jay Sussman geral Jay SussmanGeral jay SussmanGeral Jay sussman

...

OK ERROR

Page 20: Enriching EMF Models with Scala (quick overview)

An Example

! def validateCapitalizedName(self: Writer) = ! self.name.split(“ ”) forall (_(0).isUpper)

Basics

Page 21: Enriching EMF Models with Scala (quick overview)

An Example

! def validateCapitalizedName(self: Writer) = ! self.name.split(“ ”) forall (_(0).isUpper)

Basics

@Satisfies(“nonEmptyName”) def validateCapitalizedName(self: Writer) = ! self.name.split(“ ”) forall (_(0).isUpper)

def validateNonEmpryName(self: Writer) = ! self.name != null && !self.name.isEmpty

Dependencies

Page 22: Enriching EMF Models with Scala (quick overview)

⌘-1 Quick Fix

@Satisfies(“nonEmptyName”) def validateCapitalizedName(self: Writer) = { if (self.name.split(“ ”) forall (_(0).isUpper)) { OK() } else { Error(“Name is not capitalized“, QuickFix(“Capitalize”) { w: Writer => w.setName(self.name.split(“ ”) map (_.capitalize) mkString (” ”)) }) } }

Page 28: Enriching EMF Models with Scala (quick overview)

EMF

Sigma Scala

Sigma Core

EMF DelegatesCodegen Dynamic

Templates

• Implemented as a layer on top of EMF

• Language agnostic

• special support for Scala

•Integrated with EMF models

Page 29: Enriching EMF Models with Scala (quick overview)

It’s just a beginning

• Internal DSL for

• M2M

• M2T

• programming Ecore models

• Ecore model testing

• ...

Page 30: Enriching EMF Models with Scala (quick overview)

Thank You

Questions?

Filip Krikava@fikovnik

Soon to appear at github.com/fikovnik/Sigma

Page 31: Enriching EMF Models with Scala (quick overview)

Drawbacks

• Expressions can contain an arbitrary code

• Harder to make formal analysis

• Side-effect free with external checking

• No pre/post conditions at the moment

Page 32: Enriching EMF Models with Scala (quick overview)

Among others

• Support for

• generic type parameters

• multiple models

• inheritance

• constraint reuse

• better NPE handling

• ...