27
- 1 Umple and Model-Oriented Programming Umple and Model-Oriented Programming: A Short Tutorial Version 1, January 2011 Timothy C. Lethbridge University of Ottawa, Canada [email protected]

Umple and Model-Oriented Programming: A Short Tutorial

  • Upload
    mercer

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Umple and Model-Oriented Programming: A Short Tutorial. Version 1, January 2011 Timothy C. Lethbridge University of Ottawa, Canada [email protected]. The philosophy of Model-Oriented Programming (MOP). 1. Modeling abstractions are embedded directly in programming languages - PowerPoint PPT Presentation

Citation preview

Page 1: Umple and Model-Oriented Programming: A Short Tutorial

- 1Umple and Model-Oriented Programming

Umple and Model-Oriented Programming:

A Short Tutorial

Version 1, January 2011

Timothy C. Lethbridge

University of Ottawa, [email protected]

Page 2: Umple and Model-Oriented Programming: A Short Tutorial

- 2Umple and Model-Oriented Programming

The philosophy ofModel-Oriented Programming (MOP)

1. Modeling abstractions are embedded directly in programming languages

– E.g. UML associations, attributes and state machines2. Programs and models are unified

– Traditional models can be expressed as program code– Traditional code is really just modeling at a more

detailed level (a lower level of abstraction)3. The programmer/modeler has a choice of workflow

– Model-first: Use just the modeling notations, then add detail

– Incremental re-engineering: Take existing code and incrementally convert it to use modeling abstractions

Page 3: Umple and Model-Oriented Programming: A Short Tutorial

- 3Umple and Model-Oriented Programming

The philosophy of MOP – continued3. Text-diagram duality

– The abstractions in the model-oriented programming language can be rendered directly as a diagram• Unambiguously, without reverse engineering

– The diagram can be edited to update the code (live)– The code can be edited to update the diagram (live)

4. The model/code can be compiled to build a complete system

– No editing needed of code generated from the model since all needed algorithms, methods, etc. are present in the model/code source

– No ‘round tripping’

Page 4: Umple and Model-Oriented Programming: A Short Tutorial

- 4Umple and Model-Oriented Programming

Umple: An MOP technology and language family

Adds associations, attributes and state machines to programming languages

– Java, PHP, Ruby– Others being developed: C++

Stand-along code-generator and diagram/text editor is online at– http://cruise.site.uottawa.ca/umpleonline/– Limited to a single file, but incorporates many examples

Works from the command line, with Eclipse and with other tools for diagram generation and code generation– Xtext– Papyrus open-source modelling

The following slides give an introduction to key concepts– Some details for more advanced users are not discussed

Page 5: Umple and Model-Oriented Programming: A Short Tutorial

- 5Umple and Model-Oriented Programming

Umple: What’s in the name?UML Programming Language

Ample– All you need to merge modelling and programming

Simple– Easy for programmers or modelers to adopt, without a

significant learning curve– Easy to convert existing code

• We did it with Umple itself• Umple is the only modelling tool we know of that is

developed in a fully model-driven manner!

Page 6: Umple and Model-Oriented Programming: A Short Tutorial

- 6Umple and Model-Oriented Programming

Basic declaration of Umple classes and attributes

class Student{ studentNumber; // defaults to String String grade; Integer entryAverage; // implemented as int}

A UML/Umple attribute is not the same as an instance variable (member variable)

– Not all instance variables are attributes• some model associations (discussed later)

– Attributes can have properties like immutability, being part of a key, having constraints, etc. (discussed later)

Page 7: Umple and Model-Oriented Programming: A Short Tutorial

- 7Umple and Model-Oriented Programming

Datatypes that can be used to declare attributes

Umple treats the following attribute types as specialString (always the default if unspecified)IntegerDoubleBooleanDate, Time

Code generation from the above will generate suitable types in the underlying language (Java, PHP, etc.)

Umple classes can be used as types, but consider declaring associations instead (discussed later)

Page 8: Umple and Model-Oriented Programming: A Short Tutorial

- 8Umple and Model-Oriented Programming

Additional options for attributesname = “Unknown”;

– Initial value set to default, not required in class constructor

immutable idNumber;– Cannot be changed after being set in constructor

lazy immutable name;– Can be set once, right after construction, and is

immutable after that– Useful for frameworks where objects are created

without initializing values

Page 9: Umple and Model-Oriented Programming: A Short Tutorial

- 9Umple and Model-Oriented Programming

Additional options for attributes - continued

unique String ipAddress;– Value must be different in each object

autounique Integer flightNumber;– Umple assigns the next available number

const MAX = 1000;– Constants

• In Java they become static

Page 10: Umple and Model-Oriented Programming: A Short Tutorial

- 10Umple and Model-Oriented Programming

Additional options for attributes - continued

defaulted type = “Long”;– If the value is reset, the default is re-established– Such attributes can never be ‘unspecified’

Integer length;Integer width;Integer perimeter = { 2*getLength() + 2*getWidth() }Integer area = { getLength() * getWidth() }

– Derived attributes

String[] names;String[0..3] addressLines;

– Multiplicities other than 1

Page 11: Umple and Model-Oriented Programming: A Short Tutorial

- 11Umple and Model-Oriented Programming

Code generation from attributesArbitrary methods written inline in the Umple must access the attributes using a defined APIAll attributes become private instance variables

– User-written code is not allowed to access these

Constructors arguments are generated where an initial value is needed

public getX()– Always call this to access the attribute

public setX()– available except for immutable, constant, autounique

and derived attributes

Page 12: Umple and Model-Oriented Programming: A Short Tutorial

- 12Umple and Model-Oriented Programming

Umple associationsclass Student { id; name; }

class Course { description; code; }

class CourseSection { sectionLetter; 1..* -- 1 Course; // association declared in a class}

association { * CourseSection -- * Student registrant;}

Try copying and pasting the above into http://cruise.site.uottawa.ca/umpleonline/

Page 13: Umple and Model-Oriented Programming: A Short Tutorial

- 13Umple and Model-Oriented Programming

Two ways of writing associationsclass A {1 -- * B;}class B {}

Is semantically identical to

class A{}class B{}association {1 A -- * B;}

Page 14: Umple and Model-Oriented Programming: A Short Tutorial

- 14Umple and Model-Oriented Programming

API for manipulating links of associationsAccessing the association end at class A

public B getB(int index)public List<B> getBs() /* unmodifiable */public int numberOfBs()public boolean hasBs()public int indexOfB(B aB)public B addB() /* creates new B */public boolean addB(B aB)public boolean removeB(B aB)

Acessing the association end at class Bpublic A getA()public boolean setA(A aA)public void delete()

Page 15: Umple and Model-Oriented Programming: A Short Tutorial

- 15Umple and Model-Oriented Programming

Some benefits of having associations at the programming level

Saves writing a large amount of ‘boilerplate’ code– Savings can be 10:1

Referential integrity– 1 X -- * Y

• An X points to some Y’s; a Y always points to an X• Bidirectionality of links managed

Page 16: Umple and Model-Oriented Programming: A Short Tutorial

- 16Umple and Model-Oriented Programming

Associations continuedUmple supports the full set of UML associations

– Directional Associations (m and n can be any number)• * -> 0..1, * -> 1, * -> *, * -> m..n, * - >n, *->m..* and *->0..n.

– Reflexive Associations• 0..1, 0..n, *, 1, n, m..n,m..*

– Bidirectional non-Reflexive Associations• The boxed ones are the common cases

Page 17: Umple and Model-Oriented Programming: A Short Tutorial

- 17Umple and Model-Oriented Programming

Qualifiers in UmpleA UML qualifier can be declared as follows

class RegularFlight {unique Integer flightNumber on airline;

}class Airline {}

Corresponding UML diagram

Page 18: Umple and Model-Oriented Programming: A Short Tutorial

- 18Umple and Model-Oriented Programming

Generalizationclass Shape2D {}class Circle { isA Shape2D;}

The isA keyword is used so Umple code is visually distinct from code in other languages

– Different languages use different notations

Alternative notationclass Shape2D { class Circle {}}

Page 19: Umple and Model-Oriented Programming: A Short Tutorial

- 19Umple and Model-Oriented Programming

State machines in Umple - syntax<state variable> { <initial state> { entry / {<action language to do on entry>} exit / {<action language to do on exit>} do {<action language to do while in state>} <event> [<guard condition>] / { <action language action>} -> <next state>; ... } <otherState> { … <nestedState { … } }}

Page 20: Umple and Model-Oriented Programming: A Short Tutorial

- 20Umple and Model-Oriented Programming

State machines in Umple - exampleclass A { sm { // A state machine is a special attribute S1 { // State e1 -> S2;} // Transition S2 { e2 -> S1;} }}

The API to be used by methods in Java to access this is:enum Sm { S1, S2 }setSm(Sm.s1)public boolean e1()public boolean e2()

Page 21: Umple and Model-Oriented Programming: A Short Tutorial

- 21Umple and Model-Oriented Programming

Selected patternsclass University { singleton; String name;}

Future work includes developing other pattern extensions for Umple

Page 22: Umple and Model-Oriented Programming: A Short Tutorial

- 22Umple and Model-Oriented Programming

Keysclass Flight { id; * -- 1 Airline; key { id, airline }}

– Generation of equals and hashcode methods relies on knowing which attribute(s) and associations should be considered the ‘key’

Page 23: Umple and Model-Oriented Programming: A Short Tutorial

- 23Umple and Model-Oriented Programming

Before and after code injectionProvides aspect-oriented capabilities

class Person { name; before setName { if (aName != null && aName.length() > 20) { return false; } } after setName { System.out.println( "Successfully set name to : " + aName); }}

Asterisks can be used for pattern matching

Page 24: Umple and Model-Oriented Programming: A Short Tutorial

- 24Umple and Model-Oriented Programming

Mix-in capabilityDefine features in separate files and merge those features by compiling the classes together

In one fileclass X { Integer a;}In another fileclass X { Integer b;}

Class X now has two attributes

Page 25: Umple and Model-Oriented Programming: A Short Tutorial

- 25Umple and Model-Oriented Programming

Key advantages of Umple and MOPProgrammers can use Umple as little or as much as they want

– Pure Java/PHP/Ruby is just ‘passed through’– Learning curve is low, and adoption can be gradual

Helps students and beginner modelers understand the benefits of modeling

Our code generation is state-of-the art– E.g. most commercial tools have weak or no code

generation for state machines and association multiplicity

Support for multiple programming languages

Page 26: Umple and Model-Oriented Programming: A Short Tutorial

- 26Umple and Model-Oriented Programming

Other Umple tutorial informationA complete example, including embedded java

– http://tims-ideas.blogspot.com/2010/11/umple-tutorial-1-basic-attributes-and.html

Andrew Forward’s PhD thesis, with extensive details:– http://www.site.uottawa.ca/~tcl/gradtheses/aforwardphd/

Umple home page– http://cruise.site.uottawa.ca/umple/

Page 27: Umple and Model-Oriented Programming: A Short Tutorial

- 27Umple and Model-Oriented Programming

Other tutorial information, continuedUmple is in the process of being made open source on Google Code:

– https://code.google.com/p/umple/– Please contribute

Sample student laboratory instructions to help students or others try out Umple:

– http://cruise.site.uottawa.ca/umple/UmpleLabInstructions.doc