Slide 1

Slide 1 text

jgs CSE 460 Software Analysis and Design Lecture 35: Eclipse Modeling Framework Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 2 jgs MDD Development Process

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 3 jgs Model UML Class Diagram Description

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 8 jgs Step 2 :: Model :: Relationships § EReference between Course and Student, New Child → EReference EType of the reference to “Student” upper bound to “-1”, the equivalent of “many”. set the property Containment to “true”

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 9 jgs Step 2 :: Model :: Relationships § EReference between Student and Computer, New Child → EReference EType of the reference to “Computer” upper bound to “1” (default) set the property Containment to “false” § Save all

Slide 6

Slide 6 text

jgs 2. Code Generation

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 11 jgs Eclipse Modeling Framework

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 12 jgs Generator Model § Create a generator model. This allows to configure properties for the code generation that are not part of the model itself. § Generate a maximum of four different plugins for a defined model: 1. Model plugin contains all entities, packages and factories to create instances of the model. 2. Edit plugin contains providers to display a model in a UI. For example, the providers offer a label for every model element, which can be used to display an entity showing an icon and a name. 3. Editor plugin is a generated example editor to create and modify instances of a model. 4. Test plugin contains templates to write tests cases for a model.

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 13 jgs Step 3 :: Generate Code § Right click the model folder in the project then § New → Other… → EMF Generator Model → Next § and enter myModel.genmodel as the file name. § Select Ecore model as the model importer. § select Browse Workspace… and select our previously created myModel.ecore

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 14 jgs Step 3 :: Generate Code § Based on the generator model, we can now generate the source code. § EMF allows you to generate 4 different plugins. § To generate the plugins, right-click on the root node of the genmodel and select the plugin. For our tutorial, please select “generate all”.

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 16 jgs Step 3 :: Generate Code

Slide 12

Slide 12 text

jgs 3. Code

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 18 jgs Code § There following is generated § Java interface (Package) § Java implementation class (Package.Impl) § Factory interface and implementation class (Package.Util) Also, § Edit (View) § Editor § Test

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 19 jgs Code § These are singletons, to access the instances use § XXXPackage.eINSTANCE § XXXFactory.eINSTANCE § Use the Factory to create instances of your model classes, e.g: TaskList t = ExampleFactory.eINSTANCE.createTaskList(); § Use the Package to access the meta-model definition, e.g: EClass c = ExamplePackage.eINSTANCE.getTaskList(); List attrs = c.getEAttributes();

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 20 jgs Interfaces package myModel; public interface Course extends EObject { String getName(); void setName(String value); EList getStudents(); } public interface Student extends EObject { String getName(); void setName(String value); int getGpa(); void setGpa(int value); boolean isHasScholarship(); void setHasScholarship(boolean value); Computer getComputer(); void setComputer(Computer value); } public interface Computer extends EObject { String getBrand(); void setBrand(String value); }

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 21 jgs Interfaces package myModel; public interface MyModelFactory extends EFactory { MyModelFactory eINSTANCE = myModel.impl.MyModelFactoryImpl.init(); Course createCourse(); Student createStudent(); Computer createComputer(); MyModelPackage getMyModelPackage(); } public interface MyModelPackage extends EPackage { String eNAME = "myModel"; String eNS_URI = "https://org/eclipse/example/myModel"; String eNS_PREFIX = "org.eclipse.example.myModel"; MyModelPackage eINSTANCE = myModel.impl.MyModelPackageImpl.init(); // more ... }

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 22 jgs Classes :: Course package myModel.impl; public class CourseImpl extends MinimalEObjectImpl.Container implements Course { protected static final String NAME_EDEFAULT = null; protected String name = NAME_EDEFAULT; protected EList students; protected CourseImpl() { super(); } @Override protected EClass eStaticClass() { return MyModelPackage.Literals.COURSE; } @Override public String getName() { return name; } @Override public void setName(String newName) { String oldName = name; name = newName; if (eNotificationRequired()) eNotify(new ENotificationImpl(this, Notification.SET, MyModelPackage.COURSE__NAME, oldName, name)); } // continue ...

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 23 jgs Classes :: Course @Override public EList getStudents() { if (students == null) { students = new EObjectContainmentEList (Student.class, this, MyModelPackage.COURSE__STUDENTS); } return students; } // more ... @Override public String toString() { if (eIsProxy()) return super.toString(); StringBuilder result = new StringBuilder(super.toString()); result.append(" (name: "); result.append(name); result.append(')’); return result.toString(); } }

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 24 jgs Classes :: Student package myModel.impl; public class StudentImpl extends MinimalEObjectImpl.Container implements Student { protected boolean hasScholarship = HAS_SCHOLARSHIP_EDEFAULT; protected Computer computer; @Override public void setComputer(Computer newComputer) { Computer oldComputer = computer; computer = newComputer; if (eNotificationRequired()) eNotify(new ENotificationImpl (this, Notification.SET, MyModelPackage.STUDENT__COMPUTER, oldComputer, computer)); } @Override public Computer getComputer() { if (computer != null && computer.eIsProxy()) { InternalEObject oldComputer = (InternalEObject)computer; computer = (Computer)eResolveProxy(oldComputer); if (computer != oldComputer) { if (eNotificationRequired()) eNotify(new ENotificationImpl (this, Notification.RESOLVE, MyModelPackage.STUDENT__COMPUTER, oldComputer, computer)); } } return computer; } }

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 25 jgs Why is This Better than Writing POJOs? § plain old Java object (POJO) § We have generated over 1,000 LOC, § Even very simple code is considered to be worth $1 per LOC. So, $1,000 just by clicking some buttons

Slide 21

Slide 21 text

jgs To be continued…

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 27 jgs Reference § Eclipse Modeling Framework by Frank Budinsky § EMF project on eclipse.org

Slide 23

Slide 23 text

jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez [email protected] Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.