$30 off During Our Annual Pro Sale. View Details »

CSE460 Lecture 35

CSE460 Lecture 35

Software Analysis and Design
Eclipse Modeling Framework
(202011)

Javier Gonzalez-Sanchez
PRO

August 04, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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

    View Slide

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

    View Slide

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

    View Slide

  4. 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”

    View Slide

  5. 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

    View Slide

  6. jgs
    2. Code Generation

    View Slide

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

    View Slide

  8. 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.

    View Slide

  9. 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

    View Slide

  10. 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”.

    View Slide

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

    View Slide

  12. jgs
    3. Code

    View Slide

  13. 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

    View Slide

  14. 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();

    View Slide

  15. 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);
    }

    View Slide

  16. 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 ...
    }

    View Slide

  17. 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 ...

    View Slide

  18. 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();
    }
    }

    View Slide

  19. 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;
    }
    }

    View Slide

  20. 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

    View Slide

  21. jgs
    To be continued…

    View Slide

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

    View Slide

  23. 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.

    View Slide