Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC509 Lecture 06

CSC509 Lecture 06

Software Design
Eclipse Modeling Tool
(202310)

Javier Gonzalez-Sanchez

October 26, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 509
    Software Engineering II:
    Modeling and Design
    Lecture 06:
    Eclipse Modeling Tool
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View full-size slide

  2. jgs
    Previously…

    View full-size slide

  3. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 3
    § Gamma, E., Helm, R., Johnson, R. E. & Vlissides, J. (1993). Design
    Patterns: Abstraction and Reuse of Object-Oriented Design. Proceedings
    of the European Conference on Object-Oriented Programming, 707, 406–
    431.
    § Garlan, D. & Shaw, M. (1994). An Introduction to Software
    Architecture (CMU Software Engineering Institute). CMU Software
    Engineering Institute.
    Homework :: Read

    View full-size slide

  4. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 4
    GoF Patterns

    View full-size slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 5
    Eric Gamma

    View full-size slide

  6. jgs
    Next
    Model-Driven Development

    View full-size slide

  7. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 8
    Definition
    Model-driven software development (MDD)
    § Write and implement software quickly, effectively and at minimum cost.
    § The approach focuses on the construction of a software model.
    § The model (text or graphic) specifies how the software system should work
    before the code is generated.
    § The software is created automatically (translate the model to code)
    § Tested and then deployed.

    View full-size slide

  8. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 9
    MDD Development Process
    http://www.theenterprisearchitect.eu/blog/2009/08/05/a-metaphor-for-model-driven-engineering/

    View full-size slide

  9. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 10
    MDD Development Process

    View full-size slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 11
    MDD Development Process

    View full-size slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 12
    MDD Development Process

    View full-size slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 13
    MDD Development Process

    View full-size slide

  13. jgs
    0. The Tool: Eclipse Modeling Framework

    View full-size slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 15
    Eclipse
    § Eclipse is a universal platform for integrating development tools
    § Architecture based on plug-ins
    § SWT (Standard Widget Toolkit) is a generic graphics and GUI widget set
    § JFace is set of UI frameworks for common UI tasks
    § Workbench and Workspace are core – Editors, Views, and Perspectives

    View full-size slide

  15. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 16
    Eclipse Modeling Framework
    2. Model specification
    (it is stored as XMI
    but defined as text or
    diagram). A subset of
    UML class diagrams
    semantics
    1. EMF provides tools
    and runtime
    environment.
    3. Translate to Java
    Classes and Interfaces

    View full-size slide

  16. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 17
    Download Eclipse Modeling Tools

    View full-size slide

  17. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 18
    Eclipse Modeling Tools

    View full-size slide

  18. jgs
    1. Define the Model

    View full-size slide

  19. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 20
    Model
    UML Class Diagram
    Description

    View full-size slide

  20. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 21
    Step 1 :: Project
    § Create an empty modeling project in your workspace
    File → New → Other… and choose Empty EMF Project
    § Click Next, enter a name for the project, e.g., org.eclipse.example.mymodel, and hit
    Finish

    View full-size slide

  21. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 22
    Step 2 :: Model
    § Please right click the model folder in your new modeling project then
    New → Other… → Ecore Model, then click Next and give the ecore file the name
    myModel.ecore.
    § It will open in the default Ecore editor, which allows the definition of Ecore models in
    a tree-based view

    View full-size slide

  22. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 23
    Step 2 :: Model :: Package
    § Give the package of your new model a name and an URI. This will be done in the
    properties view. The URI is used to identify the model later.
    Name the package myModel,
    Set the Ns Prefix to org.eclipse.example.myModel
    and the Ns URI to https://org/eclipse/example/myModel.

    View full-size slide

  23. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 24
    Step 2 :: Model :: Classes and Attributes
    § Define model elements as children of the root package by right clicking on the
    myModel package and on the EClass respectively
    Add EClass as children, New Child → EClass
    Add EAttribute as children, New Child → EAttribute

    View full-size slide

  24. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 25
    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 full-size slide

  25. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 26
    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 full-size slide

  26. jgs
    2. Code Generation

    View full-size slide

  27. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 28
    Eclipse Modeling Framework

    View full-size slide

  28. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 29
    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 full-size slide

  29. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 30
    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 full-size slide

  30. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 31
    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 full-size slide

  31. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 32
    Step 3 :: Generate Code

    View full-size slide

  32. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 33
    Step 3 :: Generate Code
    Could you create a class
    diagram for the package
    myModel?

    View full-size slide

  33. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 36
    § For each class in your model, there is a corresponding generated
    § Java interface
    § Java implementation class
    § For each package, there is a
    § XXXPackage interface and implementation class
    § XXXFactory interface and implementation class

    View full-size slide

  34. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 37
    § 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 full-size slide

  35. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 38
    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 full-size slide

  36. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 39
    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 full-size slide

  37. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 40
    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 full-size slide

  38. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 41
    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 full-size slide

  39. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 42
    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 full-size slide

  40. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 43
    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 full-size slide

  41. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 44
    Let’s Work

    View full-size slide

  42. jgs
    Assignment 06

    View full-size slide

  43. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 46
    Assignment
    UML Class Diagram
    1. Define the Model in EMF (take a
    screen shoot)
    2. Generate Code (take a screen shoot)
    3. Use the Code to Create a Class
    Diagram (only the Model package
    but include both interfaces and
    implementations).
    It will be bigger and more complex
    that the one provided

    View full-size slide

  44. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 47
    Questions

    View full-size slide

  45. jgs
    Javier Gonzalez-Sanchez | CSC 509 | Fall 2023 | 48
    Reference
    § Eclipse Modeling Framework by Frank Budinsky
    § EMF project on eclipse.org

    View full-size slide

  46. jgs
    CSC 509 Software Design
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2023
    Copyright. These slides can only be used as study material for the class CSC509 at Cal Poly.
    They cannot be distributed or used for another purpose.

    View full-size slide