CE204 Object-Oriented Programming

CE204 Object-Oriented Programming

Week-6 (UMPLE - Part 1)

Spring Semester, 2021-2022

Download DOC-PDF, DOC-DOCX, SLIDE, PPTX,

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE

Common Scope

  • What is UMPLE?
  • What is its purpose?
  • How to create a UML model with UMPLE?
  • What is philosophy of UMPLE?
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Common Scope

  • How to use UMPLE?
    • UMPLE Online
    • Command-Line
    • Eclipse Plugin
    • Visual Studio Code Plugin
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Common Scope

  • How to learn UMPLE?
    • Online Documentations
    • Video Tutorials
    • UMPLE Community
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Common Scope

  • Overview of the basics of UMPLE
  • Associations in UMPLE
  • State machines in UMPLE
  • Product lines in UMPLE: Mixins and Mixsets
  • Other separation of concerns mechanisms: (Aspects and traits) and their code generation
  • Other advanced features of UMPLE
  • Hands-on exercise developing versions of a concurrent system using state machines and product lines.
  • UMPLE as written in itself: A case study.
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Common Scope

  • Introduction:
  • Overview of Model-Driven Development
    • Languages / Tools / Motivation for UMPLE
  • Class Modeling
    • Tools / Attributes / Methods / Associations / Exercises /
      Patterns
  • Modeling with State Machines
    • Basics / Concurrency / Case study and exercises
  • Separation of Concerns in Models
    • Mixins / Aspects / Traits
  • More Case Studies and Hands-on Exercises
    • UMPLE in itself / Real-Time / Data Oriented
  • Conclusion
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Outline - UMPLE Part 1

  • Introduction to UMPLE
  • Motivation for developing UMPLE
  • Some key UMPLE innovations
  • Using UMPLE
  • UMPLE Philosophy
  • UMPLE Class Modeling
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Outline - UMPLE Part 1

  • UMPLE Online Usage
  • UMPLE Attributes
  • UMPLE Generalization and interfaces
  • UMPLE Methods
  • UMPLE Associations
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Introduction to UMPLE

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE: Simple, Ample, UML Programming Language

  • Open source textual modelling tool set for 3 platforms

    • Command line compiler

    • Web-based tool (UMPLEOnline) for demos and education

    • Eclipse plugin

  • Code generator for UML ++

    • Infinitely nested state machines, with concurrency

    • Proper referential integrity and multiplicity constraints on associations

    • Traits, mixins, aspects for modularity

    • Text generation templates, patterns, traits

  • Pre-processor to add UML, patterns and other features on top of
    Java, PhP, C++ and other languages

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE: Simple, Ample, UML Programming Language

  • Open source textual modeling tool and code generator
    • Adds modeling to Java,. C++, PHP
    • A sample of features
      • Referential integrity on associations
      • Code generation for patterns
      • Blending of conventional code with models
      • Infinitely nested state machines, with concurrency
      • Separation of concerns for models: mixins, traits, mixsets, aspects
  • Tools
    • Command line compiler
    • Web-based tool (UMPLEOnline) for demos and education
    • Plugins for Eclipse and other tools
RTEU CE204 Week-6
CE204 Object-Oriented Programming

What Are we Going to Learn About in This Tutorial? What Will You Be Able To Do?

  • Modeling using class diagrams
    • Attributes, Associations, Methods, Patterns, Constraints
  • Modeling using state diagrams
    • States, Events, Transitions, Guards, Nesting, Actions, Activities
    • Concurrency
  • Separation of Concerns in Models
    • Mixins, Traits, Aspects, Mixsets
  • Practice with a examples focusing on state machines and product lines
  • Building a complete system in UMPLE
RTEU CE204 Week-6
CE204 Object-Oriented Programming

What Technology Will You Need?

  • As a minimum: Any web browser.
  • For a richer command-line experience
    • A computer (laptop) with Java 8-14 JDK
    • Mac and Linux are the easiest platforms, but Windows also will work
    • Download UMPLE Jar at http://dl.UMPLE.org
  • You can also run UMPLE in Docker: http://docker.UMPLE.org
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Key Websites

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Key Websites (Another way)

The UMPLEOnline web interface is at try.UMPLE.org

The user manual is at manual.UMPLE.org

The UMPLE home page is at www.UMPLE.org

UMPLE download page: dl.UMPLE.org

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Motivation for developing UMPLE

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Motivation for developing UMPLE (1)

Designers want the best combination of features:

  • Textual editing and blending with other languages

  • Ability to use in an agile process

    • Write tests, continuous integration, versioning

    • Combine the best of agility and modeling

  • Excellent code generation

    • A complete generation of real systems (including itself)
  • Multi-platform (command line, Eclipse, VsCode, Web)

  • Practical and easy to use for developers

    • Including great documentation
  • Open source

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Motivation for developing UMPLE (2)

Many existing tools:

  • Lacked in usability

    • Awkward to edit diagrams

    • Many steps to do a task

    • Lengthy learning process

  • Lack in ongoing support

  • Could be enhanced by us perhaps, but we would be tied to key decisions (e.g. Eclipse-only)

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Some key UMPLE innovations

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Some key UMPLE innovations

  • Model is code

    • Traditional code is embedded in model
  • No need to edit generated code

    • No round-trip engineering
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Using UMPLE

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Using UMPLE

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Docker Container Experimental

mkdir ~/src && cd ~/src &&  git clone git@github.com:UMPLE/UMPLE.git
docker run -i -t -v `pwd`:/src UMPLE/UMPLE:0.4.0 bash
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Using UMPLE

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Philosophy

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Philosophy 1-4

  • P1. Modeling is programming and vice versa

  • P2. An UMPLE programmer should never need to edit generated code to accomplish any task.

  • P3. The UMPLE compiler can accept and generate code that uses nothing but UML abstractions.

    • The above is the inverse of the following
  • P4. A program without UMPLE features can be compiled by an UMPLE compiler.

    • e.g. input Java results in the same as output
RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Philosophy 5-8

  • P5. A programmer can incrementally add UMPLE features to an existing program

    • Umplification
  • P6. UMPLE extends the base language in a minimally invasive and safe way.

  • P7. UMPLE features can be created and viewed diagrammatically or textually

  • P8. UMPLE goes beyond UML

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Class Modeling

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Class Models - Quick Overview

  • Key elements:

    • Classes

    • Attributes

    • Associations

    • Generalizations

    • Methods

  • We will look at all these using examples via UMPLE ONLINE

  • UMPLE code/models are stored in files with suffix .ump

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Exercise: Compiling and changing a model

  • Look at the example at the bottom of

  • Click on Load the above code into UMPLEOnline

    • Observe and modify the diagram
    • Add an attribute
    • Make a multiplicity error, then undo
    • Generate code and take a look
    • Download, compile and run if you want
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Hello World Example 2 in the User Manual

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Key tools:

  • UMPLE Online
  • Command-Line
  • User Manual
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Hello World example 2 in UMPLEOnline

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Exploration of UMPLEOnline

  • Explore class diagram examples
  • Options
    • T or Control-t (hide and show text)
    • D or Control-d (hide and show diagram)
    • A, M to hide and show attributes, methods
    • Default diagram types
      • G/Control-g (Graphviz), S/Control-s (State Diagram)
      • E/Control-e (Editable class diagram)
  • Generate code and look at the results
    • In UMPLE you never should modify generated code
    • It is designed to be readable for educational purposes
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Use of the UMPLEOnline Docker image

  • UMPLE’s server can handle 80,000 transactions per hour

    • Code generations, edits
  • But needs a good Internet connection
    (sometimes hundreds of students have assignments due)

  • To maximize speed of UMPLEOnline run it in your local machine:

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Demo of compiling on the command line

  • To compile on the command line you will need Java 8
  • Download UMPLE from http://dl.UMPLE.org
  • Basic compilation
java -jar UMPLE.jar model.ump
  • Help for features and commands
java -jar UMPLE.jar --help
  • To generate and compile the java to a final system
java –jar UMPLE.jar model.ump -c -
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Quick walkthrough of the user manual

Note in particular

  • Key sections:
    • attributes,
    • associations,
    • state machines
  • Grammar
  • Generated API
  • Errors and warnings
  • Editing pages in github
RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Attributes

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attributes

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attributes Exercise #1

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attributes

  • "Instance variables"

    • Part of the state of an object
    • Simple data that will always be present in each instance
  • Specified like a Java or C++ field or member variable

  • But, intended to be more abstract!

    • Example, with an initial value
a = "init value";
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attributes

  • As in UML, more abstract than instance variables

    • Always private by default

    • Should only be accessed get, set methods

    • Can be stereotyped (upcoming slides) to affect code generation

    • Can have aspects applied (discussed later)

    • Can be constrained (discussed later)

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Code generation from attributes

  • Default code generation
    • Generates a getName() and setName() method for name
      • public
  • Creates an arguments in the class constructor by default
  • An attribute is private to the class by default
    • Should only be accessed get, set methods
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Code Generation (JavaDocs)

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Code Generation Patterns

  • Attributes

    • Set/Get (UB = 1)
    • Add/Remove/NumberOf/IndexOf/Get (UB > 1)
    • Lazy immutability
    • Default values
    • Constants
    • Before / After cod

    UB = upper bound

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Code Generation Patterns

  • Associations

    • Set/Get (UB = 1)
    • Add/Remove/NumberOf/IndexOf/Get (UB > 1)
    • Referential Integrity
    • Multiplicity Constraints
    • 42 different cases

    UB = upper bound

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Code Generation (Semantics)

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE builtin datatypes

String // (default if none specified)
Integer
Float
Double
Boolean
Time
Date
  • The above will generate appropriate code in Java, C++ etc.
    • e.g. Integer becomes int
  • Other (native) types can be used but without guaranteed correctness
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attribute stereotypes (1)

  • Code generation can be controlled through stereotypes:
    • lazy - don’t add a constructor argument
lazy b; // sets it to null, 0, “” depending on type
  • Defaulted – can be reset
defaulted s = "def"; // resettable to the default
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Attribute stereotypes (2)

  • autounique – provide a unique value to each instance
autounique x; // sets attribute to 1, 2, 3 …
  • internal – don’t generate any methods
internal i; // doesn’t generate any get/set either
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Immutability

  • Useful for objects where you want to guarantee no possible change once created

    • e.g. a geometric point
  • Generate a constructor argument and get method but no set method

immutable String str;
  • No constructor argument, but allows setting just once.
lazy immutable z;
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Lets explore attributes by example

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Derived attributes

  • These generate a get method that is calculated.
class Point
{
// Cartesian coordinates
Float x;
Float y;

// Polar coordinates
Float rho =
{Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2))}
Float theta =
{Math.toDegrees(Math.atan2(getY(),getX()))}

}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Multi-valued attributes

  • Limit their use. Associations are generally better.
class Office {
Integer number;
Phone[] installedTelephones;
}

class Phone {
String digits;
String callerID;
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Keys

  • Enable UMPLE to generate an equals() and a hashcode() method
class Student {
Integer id;
name;
key { id }
}
  • The user manual has a sports team example showing keys on associations too
  • Note how this feature is not inherited from UML
RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Generalization and interfaces

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Generalization in UMPLE

  • UMPLE uses the isA keyword to indicate generalization
  • Used to indicate superclass, used trait, implemented interface
class Shape {
colour;
}
class Rectangle {
isA Shape;
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Avoiding unnecessary generalizations

Open in UMPLE

  • Inappropriate hierarchy of Classes

  • What should the model be?

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Interfaces

  • Declare signatures of a group of methods that must be implemented by various classes

  • Also declared using the keyword isA

  • Essentially the same concept as in Java

  • Let’s explore examples in the user manual …

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Methods

RTEU CE204 Week-6
CE204 Object-Oriented Programming

User-written Methods in UMPLE

  • Methods can be added to any UMPLE code.

  • UMPLE parses the signature only; the rest is passed to the generated code.

  • You can specify different bodies in different languages

  • We will look at examples in the user manual …

RTEU CE204 Week-6
CE204 Object-Oriented Programming

UMPLE Associations

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Associations

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Associations Exercise #1

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Associations Exercise #2

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Associations

  • Describe how instances of classes are linked at runtime
    • Bidirectional -- or
    • Unidirectional ->
  • Multiplicity:
    • Bounds on the number of linked instances
  • * Or 0..* \longrightarrow 0 or more
  • 1..* \longrightarrow 1 or more
  • 1 \longrightarrow Exactly 1
  • 2 \longrightarrow Exactly 2
  • 1..3 \longrightarrow Between 1 and 3
  • 0..2 \longrightarrow Up to 2
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Association Relationships

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Association Relationships

  • Directional Associations
* -> 0..1, * -> 1, * -> *, * -> m..n, * - >n, *->m..* and*->0..n.
  • Symmetric Reflexive
0..1, 0..n, *, 1, n, m..n,m..*
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Basic UML associations

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Many-to-one associations (1)

class Employee {
id;
firstName;
lastName;
}

class Company {
name;
1 -- * Employee;
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Many-to-one associations (2)

  • A company has many employees,
  • An employee can only work for one company.
    • This company will not store data about the moonlighting activities of employees!
  • A company can have zero employees
    • E.g. a ‘shell’ company
  • It is not possible to be an employee unless you work for a company
  • Let’s draw and write this in UMPLEOnline:

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Role names (optional, in most cases)

  • Allow you to better label either end of an association
class Person{
id;
firstName;
lastName;
}

class Company {
name;
1 employer -- * Person employee;
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Referential Integrity

  • When an instance on one side of the association changes

    • The linked instances on the other side know …

    • And vice-versa

  • This is standard in UMPLE associations, which are
    bidirectional

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Many-to-Many Associations

  • An assistant can work for many managers
  • A manager can have many assistants
  • Assistants can work in pools working for several managers
  • Managers can have a group of assistants
  • Some managers might have zero assistants.
  • Is it possible for an assistant to have, perhaps temporarily, zero managers?

Open in UMPLE

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

One-to-One Associations (Use cautiously)

  • For each company, there is exactly one board of directors
  • A board is the board of only one company
  • A company must always have a board
  • A board must always be of some company

Open in UMPLE

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Typical erroneous use of one-to-one

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Unidirectional Associations

  • Associations are by default bi-directional
  • It is possible to limit the direction of an association by adding an arrow at one end
  • In the following unidirectional association
    • A Day knows about its notes, but a Note does not know which Day is belongs to
    • Note remains ‘uncoupled’ and can be used in other contexts
class Day {
* -> 1 Note;
}
class Note {}

Open in UMPLE

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Association Classes

  • Sometimes, an attribute that concerns two associated classes cannot be placed in either of the classes

Open in UMPLE

and Extended Example

center

  • The following are nearly equivalent
    • The only difference:
      • in the association class there can be only a single registration of a given Student in a CourseSection

center

RTEU CE204 Week-6
CE204 Object-Oriented Programming

Association classes (cont.)

  • UMPLE code
class Student {}
class CourseSection {}
associationClass Registration {
*  Student;
*  CourseSection;
}
  • Open in UMPLEOnline, and then generate code
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Reflexive Associations

  • An association that connects a class to itself

center

Open in UMPLE

class Course {
* self isMutuallyExclusiveWith; // Symmetric
}

association {
* Course successor -- * Course prerequisite;
} 
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Inline vs. Standalone associations

  • The following are equivalent to allow flexibility:
class X {}
class Y {
1 -- * X;

}
class X {}
class Y {}
association {
1 Y -- * X;

}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Aggregation

  • Aggregations are ordinary associations that represent part-whole relationships.
    • The 'whole' side is often called the assembly or the aggregate
    • This is a shorthand for association named isPartOf
    • UMPLE has no special syntax currently

center

class Vehicle {
1 whole -- * VehiclePart part;
}
class VehiclePart{
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Composition

  • A composition is a strong kind of aggregation
    • If the aggregate is destroyed, then the parts are destroyed as well

class Building {
1 <@>- * Room;
}
class Room{
}
RTEU CE204 Week-6
CE204 Object-Oriented Programming

Sorted Associations

  • Order objects in the association according to a specific key
class Academy {
1 -- * Student registrants sorted {id};
}

class Student {
Integer id;
name;
}
  • We will look at a more complete example in the User Manual
RTEU CE204 Week-6
CE204 Object-Oriented Programming

A final word on associations

RTEU CE204 Week-6
CE204 Object-Oriented Programming

References

RTEU CE204 Week-6
CE204 Object-Oriented Programming

References

RTEU CE204 Week-6
CE204 Object-Oriented Programming

EndOfWeek6End-Of-Week-6

RTEU CE204 Week-6