CEN206 Object-Oriented Programming (formerly CE204)

CEN206 Object-Oriented Programming (formerly CE204)

Week-6 (UMPLE - Part 1)

Spring Semester, 2024-2025

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

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE

Common Scope

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

Common Scope

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

Common Scope

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

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Outline - UMPLE Part 1

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

Outline - UMPLE Part 1

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

Introduction to UMPLE

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE: Si&mple&, A&mple&, &UM&L &P&rogramming &L&anguag&e&

  • 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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE: Si&mple&, A&mple&, &UM&L &P&rogramming &L&anguag&e&

  • 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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Key Websites

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Motivation for developing UMPLE

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Some key UMPLE innovations

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Some key UMPLE innovations

  • Model is code

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

    • No round-trip engineering
RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Using UMPLE

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Using UMPLE

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Using UMPLE

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Philosophy

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Class Modeling

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Hello World Example 2 in the User Manual

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Key tools:

  • UMPLE Online
  • Command-Line
  • User Manual
RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Hello World example 2 in UMPLEOnline

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Attributes

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Attributes

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Attributes Exercise #1

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Code Generation (JavaDocs)

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Code Generation (Semantics)

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Lets explore attributes by example

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Multi-valued attributes

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

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

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Generalization and interfaces

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Avoiding unnecessary generalizations

Open in UMPLE

  • Inappropriate hierarchy of Classes

  • What should the model be?

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Methods

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

UMPLE Associations

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Associations

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Associations Exercise #1

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Associations Exercise #2

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Association Relationships

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Basic UML associations

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Many-to-one associations (1)

class Employee {
id;
firstName;
lastName;
}

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

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Typical erroneous use of one-to-one

center

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

Association classes (cont.)

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

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

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 CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

A final word on associations

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

References

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

References

RTEU CEN206 Week-6
CEN206 Object-Oriented Programming (formerly CE204)

EndOfWeek6End-Of-Week-6

RTEU CEN206 Week-6