SDM Lab 6: OCL in Eclipse, and ecore (Updated 5/11/19)

This page gives brief notes and instructions on the guided lab session. It will have omissions and bugs - you need to listen as well as read!

Introduction

The primary aim of this lab is to let you try using OCL in Eclipse, and thereby gain familiarity with OCL. A secondary aim is to start experiencing the fringes of what's possible in a modern model-driven development environment. Some of the tooling we'll use is very bleeding edge, and not very documented, however! I hope I've designed this lab so that you won't cut yourselves. Do feel free to explore off the path, but don't say I didn't warn you... Don't worry if you find much of the tooling confusing; so do I. You're trying to get a flavour of it. When you have six months of experience, or after ten years of progress, it will probably all look much easier...

You'll need the documentation here. (Warning: you may find parts of this document (though I hope not the parts you'll use in this lab!) out of date. I mention this not just to complain, but to draw to your attention something that is quite an important factor relevant to this course: although we have the understanding to do some quite magical things with models these days, the effort required to keep the magic working and its documentation up to date is considerable, and not always available...)

You are welcome to work individually or in small groups, as you prefer - but as always, if you work in a group, make sure you all understand everything done.

Note: You can either use the papyrus version of Eclipse, or the modelling edition: it shouldn't matter much today. I was using the modelling edition as I wrote the lab.

Adding OCL constraints to Papyrus models

  1. Read the instructions in the OCL documentation on pages 56-58 (PDF numbering) to see how to add class invariants and operation pre- and post-conditions to Papyrus models.
  2. Create a new Papyrus model (or load one, if you know where to find one you made earlier) with the Paleo class diagram - like this one (but do fix the bug that attribute text probably should not be private - do you see why?): paleo metamodel
  3. Add OCL class invariants to specify:
    1. the length of the text string of any Node is between 3 and 20 characters inclusive;
    2. any Question with isInitial set to true has text 'Does it have a face?' (use single quotes for the string literal!);
    3. (harder - probably several ways to do it) if this Question is reached by following a yes link from another question, then this Question does not have isInitial set to true.
  4. Add an operation getYes() to Question, taking no arguments and returning a Node. Add pre- and post-conditions to specify that this operation is always applicable, and that the Node it returns is the one linked to this question by the association yes.
Note If you get a red cross in your constraint, in principle something has gone wrong: hovering should tell you what the problem is. However, 2019 note it is distressingly easy to tickle some bug that gives you mysterious "No pivot for..." errors even when your OCL really is correct. My possibly superstitious suggestion is: Compare with people around you to see if you've got the OCL correct. Once you're sure you have, visit Piazza and, if you are the first, post your solution so that there will be correct answers (and evidence that it's possible to avoid red crosses!) there. (You might also like to look at the corresponding 2016 post and corresponding 2018 post.) If you're not the first, see if you're happy with the solutions there. There is more than one way to get these questions right!

Doing more with OCL

In the first part, we didn't have any way to do more than check whether an OCL expression was legal - we used OCL syntax, but the only relevance of OCL semantics was in our heads. (In principle we can go further using OCL in Papyrus. Exercise for the keen, later: investigate how. But for now we will use a standard tutorial, which also allows me to introduce ecore.)

Getting started: installing necessary software

Install the OCL Examples and Editors SDK following the instructions here.

Follow the OclInEcore tutorial from page 97 to page 114

i.e. section 4.1 of the same PDF you were using a moment ago.

Think of ecore as a metamodel for a very simplified fragment of UML. It corresponds roughly to (part of) MOF which has been mentioned. Don't worry about the tutorial assuming you are familiar with EMF models! You may find the copy/paste links in the PDF don't work; I include the text here for convenience, and include a few notes, so follow both here and in the tutorial document.

Say Yes if you're asked whether you want to convert your Tutorial project into an OCL project.

Have a look at the various editors available for your Tutorial.ecore model! You'll probably find the syntax provided by the OCLinEcore editor the most usable, but do understand the interchangeability...

First part:

import ecore : 'http://www.eclipse.org/emf/2002/Ecore#/' ;

package tutorial : tut = 'http://www.eclipse.org/mdt/ocl/oclinecore/tutorial'
{
class Library
{
  attribute name : String;
  property books#library : Book[*] { composes };
  property loans : Loan[*] { composes };
  property members#library : Member[*] { composes };
}
class Book
{
  attribute name : String;
  attribute copies : Integer;
  property library#books : Library[?];
}
class Member
{
  attribute name : String;
  property library#members : Library[?];
}
class Loan
{
  property book : Book[?];
  property member : Member[?];
  attribute date : ecore::EDate[?];
}
}
Remark: it's off the path of this lab, but you might like to experiment with the ecore tools and get them to generate a diagrammatic presentation of tutorial.ecore for you. (I have several diagram generation options on my right click menu, but I also have a lot of modelling software installed in my Eclipse; you may need to try installing ecore tools.)

4.1.6 Create Dynamic Instance I found I didn't, in fact, have that option on the context sensitive menu from the OCLinEcore editor - but I did from the other other editors. (There's a lesson about tooling there, too...)

Next bit:

invariant SufficientCopies:
library.loans->select(book=self)->size() <= copies;
(though you might find it instructive to type that one, and experiment with how far the content assist (that you get with Control-Space) helps. You'll find it sometimes gives up with an error message beginning "no viable alternative"...) NB if you select and then Control-Shift-F, it will tidy up formatting.

When you get to the OCL Console section, I found that only the instructions starting "Alternatively" at the bottom of p106 work: use those!

property loans : Loan[*] {derived, volatile}
{
  derivation : library.loans->select(book=self);
}
operation isAvailable() : Boolean[?]
{
  body: loans->size() < copies;
}
And finally:
invariant AtMostTwoLoans : loans->size() <= 2;
invariant UniqueLoans : loans->isUnique(book);
property loans : Loan[*] { derived, volatile } 
{
  derivation : library.loans->select(member=self);
}
property books : Book[*] { derived, volatile, !unique} 
{
  derivation : loans->collect(book);
}
Before you start the section Generating Java Code, fix the validation errors you just discovered, and revalidate - it should, and probably does, make you very uncomfortable to go on while Eclipse is showing red crosses!

Do explore the code you get when you generate model code, and feel free to play with nearby capabilities - the tutorial itself is only scratching the surface of what this tooling can do.


This page is maintained by Perdita Stevens (perdita@inf.ed.ac.uk)


Home : Teaching : Courses : Sdm 

Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail: school-office@inf.ed.ac.uk
Please contact our webadmin with any comments or corrections. Logging and Cookies
Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh