概述
UML(Unified Modeling Language),统一建模语言,是一种标准可视化建模语言,通过业务建模来分析,设计,实现一个软件系统.
本文主要参考:https://www.uml-diagrams.org/,基于uml 2.4,2.5规范.
UML核心元素
图
UML分为结构图和行为图
结构图:显示了系统及其部件在不同抽象和实现级别上的静态结构以及它们如何相互关联。结构图中的元素表示系统的有意义的概念,并且可以包括抽象的,现实的和实现的概念。
行为图:显示了系统中对象的动态行为,可以将其描述为系统随时间的一系列更改。
蓝色部分为对应的扩展图。

Element
泛指所有的uml元素.
uml element定义:

Frame
框架,一般用于diagram元素需要在边框上添加一些自有元素的情况,比如class diagram的port.
frame需要heading.
frame-heading ::= [ element-kind ] element-name [ parameters ]
element-kind ::= short-element-kind | long-element-kind
| Short kind | Long kind - owning element or enclosing namespace |
|---|---|
| act | activity |
| class | class |
| cmp | component |
| dep | deployment |
| sd | interaction |
| pkg | package |
| stm | state machine |
| uc | use case |
框架类型并不是里面元素的类型:

namespace
命名空间是一个抽象的概念,没有专门的识别符号,需要使用他的实现类,常见的namespace:
- package
- classifier
分类器
具有属性和操作的UML元素。表现形式一般就是不同的矩形图形,且最上面的名称就是该分类器的名称。
分类器:

可以看到分类器继承了namespace,所以可以作为命名空间使用(嵌套类,结构化分类器等等)
最重要的分类器: class,interface,datatype,component,collaboration usercase
类
class也是分类器。就是面向对象中的类。
class是最常用的分类器,所以可以不需要额外的标注来表示class.


下划线表示静态方法。
结构化class
class分类器可以放在namespace,frame或其他class中

(结构化分类器:网上购物,具有购物端口(正方形)和内部结构(其他分类器)。)
class stereotypes
class使用stereotypes进一步标识class.
Standard UML class stereotypes:
| Name | Description |
|---|---|
| «Auxiliary» | Auxiliary is class that supports another more central or fundamental class, typically by implementing secondary logic or control flow. The class that the auxiliary supports may be defined either explicitly using a focus class or implicitly by a dependency relationship.Auxiliary classes are typically used for specifying the secondary business logic or control flow of components during design phase. |
| «Focus» | Focus is class that defines the core logic or control flow for one or more supporting classes. The supporting classes may be defined either explicitly using auxiliary classes or implicitly by dependency relationships.Focus classes are typically used for specifying the core business logic or control flow of components during design phase. |
| «ImplementationClass» | The implementation of a class in some programming language (e.g., C++, Smalltalk, Java) in which an instance may not have more than one class.This is in contrast to UML class, for which an instance may have multiple classes at one time and may gain or lose classes over time, and an object may dynamically have multiple classes.An implementation class may realize a number of different types. The physical attributes and associations of the implementation class do not have to be the same as those of any classifier it realizes and the implementation class may provide methods for its operations in terms of its physical attributes and associations. |
| «Metaclass» | A class whose instances are also classes. |
| «Type» | Type is class that specifies a domain of objects together with the operations applicable to the objects, without defining the physical implementation of those objects.Type may have attributes and associations. Behavioral specifications for type operations may be expressed using, for example, activity diagrams. An object may have at most one implementation class, however it may conform to multiple different types. |
| «Utility» | Utility is class that has only class scoped static attributes and operations. As such, utility class usually has no instances. Math is utility class - having static attributes and operations (underlined) |
Nonstandard UML class stereotypes:
| Name | Description |
|---|---|
| «Boundary» | Boundary is a stereotyped class or object that represents some system boundary, e.g. a user interface screen, system interface or device interface object. It could be used in the analysis or conceptual phase of development to capture users or external systems interacting with the system under development. It is often used in sequence diagrams which demonstrate user interactions with the system.Boundary is drawn as a circle connected with a short line to a vertical line to the left It could be also shown as a class with the «Boundary» stereotype. |
| «Control» | Control is a stereotyped class or object that is used to model flow of control or some coordination in behavior. One or several control classes could describe use case realization. System controls represent the dynamics of the designed system and usually describe some “business logic”.Control is drawn as a circle with embedded arrow on the top. It could be also shown as a class with the «Control» stereotype.Note, that UML has standard «Focus» stereotype applicable to classes which could be used for specifying the core business logic or control flow of components during design. |
| «Entity» | Entity is a stereotyped class or object that represents some information or data, usually but not necessarily persistent.Entity is drawn as a circle with line attached to the bottom of the circle. It could be also shown as a class with the «Entity» stereotype.Business entities represent some “things”, items, documents, or information handled, used or processed by business workers while they do business. Examples of business entities are Prescription at the doctor’s office, Menu at the restaurant, Ticket at the airport.System entities represent some information or data, usually but not necessarily persistent, which is processed by the system.Note, that in UML 1.4.2 stereotype «entity» represented a passive class, i.e. class whose objects do not initiate interactions on their own. UML 2.0 updated «Entity» standard stereotype to be applicable to components and representing a business concept of a persistent information. |
抽象类
抽象类的名称以斜体显示。

抽象分类器也可以使用名称后面或下面的关键字abstract来显示。
接口
接口是一个分类器,它声明一组一致的公共特性和义务。就是面向对象中的接口。

分类器实现了某个接口:
分类器实现的接口是其提供的接口。

(SiteSearch 接口由SearchService 实现(实现)。)
分类器依赖某个接口:

(SearchController 使用(必需) SiteSearch 接口。)
接口可以用作其他分类器的命名空间。
数据类型
数据类型是一个分类器—类似于一个类—其实例“仅通过它们的值来识别”。理解为常规意义上的数据类型即可。


被类引用:

原始类型:
- Boolean,
- Integer,
- UnlimitedNatural,
- String,
- Real.
关键字“primitive”表示原始类型:

(原始数据类型Weight)
枚举类型:

Collaboration
同时继承了行为分类器和结构分类器.
协作,主要用来表明一个system是如何工作的.
核心为多个角色共同协作完成某个任务.

use case
行为分类器的子类
基本表示
用例,一般用来描述一个功能单元,用例需要参与者(actor)协作来完成某个主题(subject),并产生可观察,有价值的结果(相对actor).
用例命名,一般表示某个动作.

用例可以有属性,以及自定义的stereotype:

用例扩展
用例可以有扩展点(extension points):

扩展点可以被其他用例扩展:
(可以使用可选的扩展条件,以及指定扩展点.一般注明在注释中)
注意: 扩展点所在use case本身就是完整的,这不同于include,include依赖方use case本身并不完整.

也可以使用矩形表示:

注意: 扩展用例本身没有意义,他是用来增强基本用例的(可以在某些”条件”下),所以扩展用例不能直接使用,他只是base use case的附属
抽象用例
用例也是分类器,分类器可以抽象,被泛化.和其他抽象分类器使用相同(uml2.5貌似不建议使用抽象用例)

如果不希望使用抽象用例,还可以使用<<include>>的包含关系(extend 和 mixed的区别):

用例之间的关系
| Generalization | Extend | Include |
|---|---|---|
![]() | ![]() | ![]() |
| Base use case(Bank ATM Transaction) could be abstract use case (incomplete) or concrete (complete). | Base use case(Bank ATM Transaction) is complete (concrete) by itself, defined independently. | Base use case(Bank ATM Transaction) is incomplete (abstract use case). |
| Specialized use case is required, not optional, if base use case is abstract. | Extending use case is optional, supplementary. | Included use case required, not optional. |
| No explicit location to use specialization. | Has at least one explicit extension location. | No explicit inclusion location but is included at some location. |
| No explicit condition to use specialization. | Could have optional extension condition. | No explicit inclusion condition. |
business use case
(区别上面的system use case,系统用例.注意,业务用例不属于标准的uml规范)
业务用例,是从业务角度出发,中间可能包含系统用例.
业务角度: 我需要什么
系统角度: 我提供什么

actor
actor,行为分类器的子类.常用于用例图中.
一般用火柴人表示:

可以自定义图标:


也可以用标准分类器表示,需要使用<<actor>> keyword.

business actor:
(对应business use case)

结构化分类器
拥有内部接口的分类器,他的行为能够被内部实例协作进行描述.


嵌套分类器
class和interface可以作为namespace来包含其他分类器,比如其他class,interface,user case等等,这些内部的分类器作用域就被限制在外部的分类器容器中.一般用作作用域限制(有点像内部类)
借用了package的包含表现形式.


Feature
feature是分类器的结构和行为表现.
结构: 即属性
行为: 即操作
实际上就是field 和 method

属性
应用场景:
- classfier 属性
- 关联端(Classifier attribute may represent an association end when this association end is owned by the classifier.)
- 结构化分类器的part
property ::= [ visibility ] [’/’ | ’^’] property-name [ ’:’ property-type ] [ ’[’ multiplicity ’]’ ] [ ’=’ default-value ] [ property-modifiers ]
”/“表示派生属性,也就是所谓的计算属性(birthDate派生出age).
”^“表示继承属性(uml2.5新增特性,可以使用浅色区分).
visibility ::= ’+’ | ’~’ | ’#’ | ’-‘
property-modifiers ::= ’{’ property-modifier [ ’,’ property-modifier ] * ’}’
property-modifier ::= ‘id’ | ‘readOnly’ | ‘ordered’ | ( ‘seq’ | ‘sequence’ ) | ‘unique’ | ‘nonunique’ | ‘union’ |
‘redefines’ property-name | ‘subsets’ property-name | property-constraint
| Modifier | Description |
|---|---|
| id | Property is part of the identifier for the class which owns the property. |
| readOnly | Property is read only (isReadOnly = true). |
| ordered | Property is ordered (isOrdered = true). |
| unique | Multi-valued property has no duplicate values (isUnique = true). |
| nonunique | Multi-valued property may have duplicate values (isUnique = false). |
| sequence (or seq) | Property is an ordered bag (isUnique = false and isOrdered = true). |
| union | Property is a derived union of its subsets. |
| redefines property-name | Property redefines an inherited property named property-name. |
| subsets property-name | Property is a subset of the property named property-name. |
| property-constraint | A constraint that applies to the property |
例:
(患者的属性)

分类器实例的非静态属性,可以分配值。

使用关联符号表示属性:
即关联端是属性,related to the other end of association.
(关联端,所有权等概念详见下方关联部分)

操作

多重性
UML中的多重性允许指定一些元素集合的基数(即元素的数量)。
multiplicity-range :: = [ lower-bound ’..’] upper-bound
lower-bound :: = natural-value-specification upper-bound :: = natural-value-specification | ’*’
* 表示不限。
比如:0..0,0..1,0..*,m..n

如果没有和文本字符串关联,可以省略[],比如关联关系中的应用。
多重性可以有属性:
collection-type ::= multiplicity-range [ ’{’ collection-options ’}’ ]
collection-options ::= order-designator [ ’,’ uniqueness-designator ] | uniqueness-designator [ ’,’ order-designator ] order-designator ::= ‘ordered’ | ‘unordered’ uniqueness-designator ::= ‘unique’ | ‘nonunique’


可见性
-
+: public
-
#: protected
-
~: package
-
-: private
约束
constraint ::= ’{’ [ name ’:’ ] boolean-expression ’}’


xor为预定义UML约束标记,表示或。
约束字符串可以放置在注释符号中,并通过虚线附着到约束元素的每个符号上。
对象
匿名对象:

未知类型对象:

带包名:
object:package::class

对象有具体值:

对象有结构特征:

关联
关联不只是类图上的概念,整个UML中都会用到。
组合,聚合等都属于关联,可以使用关联的一些特性,比如关联端,关联端所有权.

关联端

author和textbook为关联端的名称,关联端名称可以理解为关联端所在侧分类器所扮演的role。
实心三角形定义了关联端的顺序。
关联端可以被分类器和关联本身拥有。

黑色原点,表示对所在关联端的所有权是另一侧的分类器,也就是这个关联端表现为另一侧分类器的属性,且类型为黑色原点所在分类器.没有黑色原点,表示该关联端被关联本身拥有,上面的例子中qbuilder关联端被Builds关联拥有.
属性例子:

(类拥有的关联端是属性,>表示到航性,见下)
导航性
一般应用于instance(object),也就是Link associate上使用.

关联的两端都具有未指定的可航性。

A2具有未指定的可导航性,而B2 可从A2进行导航。

A3 无法从B3 导航,而B3具有未指定的导航能力。

A4 不可从B4 导航,而B4 可从A4 导航。

A5 可从B5 导航,而B5 可从A5 导航。

A6 不可从B6 导航,而B6 不可从A6 导航。
多元关联
二元关联,简单的用实现连接,多元关联需要用菱形。

Link
link就是association的实例,分类器,关联本身都需要实例化.
(Link Wrote between instance p of Professor playing author role and instance b of Book in the textbook role.)

聚合

(三角形有三条线段的“边”集合。每条线段可以是无三角形、一个三角形或多个三角形的一部分,所以用*表示。)
聚合可以和关联一起装饰:

(三角形具有三个独特线段的“边”集合。线段可从Triangle导航。关联端“边”由Triangle拥有,而不是关联本身。)
组合
聚合的部分可脱离整体独立存在。(可以理解为引用,即部分的源在其他地方)
组合的部分不能脱离整体独立存在。
最简单的就是看删除整体,部分还存不存在来判断。

文件必须放在文件夹中,如果删除文件夹,文件也会删除。
组合,即可以own被组合元素.
泛化
UML中用于表示通用分类器和具体分类器之间的定向关系。类似面向对象中的继承。(概念的扩展,泛化)。


(2种写法都可以)
多重继承

泛化集
针对通用分类器泛化后的集合,也就是再次分类。
每个set有2个属性:是否覆盖、是否相交,1个powertype(超类)。
isCovering ::= complete | incomplete
- complete: 通用分类器的实例,也会是set种至少某个分类器的实例
- incomplete: 通用分类器的实例,不是set中任何一个分类器的实例
isDisjoint ::= disjoint | overlapping
- disjoint: set中任何一个分类器的实例,不能是其他分类器的实例
- overlapping: set中可以共享实例
属性 ::= ’{’ isCovering, isDisjoint ’}’
powertype ::= ’:‘type
type实际上就是set的进一步抽象。
比如下面的例子:
- 工作基本保险、人身保险、福利保险的power type是 Coverage Type(覆盖类型)
- HMO、POS、PPO、FFS保险的power type是Insurance Plan(保险计划)

依赖
和常规的依赖不同,uml中概念更广泛。
可以直接以语义化的形式理解依赖,即我需要你,才能做后面的事情,也就是client和supplier的关系,且指向supplier.
比如,UserService <<create>> User,这也是依赖关系,UserService需要User的定义才能create User。
依赖有3大类形式,use(使用),abstraction(抽象),deployment(部署)
use的实现有:
use的表现像传统的依赖,意味着需要你来补全我.

-
create

-
instanticate(和create区别不大,实际上概念上2者的区分也不明显)
-
call,一个操作调用另一个操作,当然会依赖另一个
-
send,一般用作一个操作发送了一个信号
-
required interface,依赖接口来提供服务
非矩形接口形式:

矩形接口形式:

abstraction的实现有:
抽象,总是指向更抽象的那一方(依赖更抽象者)

-
realization,实现
(具体见实现)
-
manifest,外在表现,这个一般只用在部署图中,artifacts就是components的外在表现.这可以理解为artifacts是对components的实现(components为抽象对象)
(具体见部署图)
-
trace,跟踪,2个元素的历史或过程关系,指向更早的元素,表示从过去可以追踪到当前

-
refine,重新定义,指向被重新定义者(更抽象)

-
derive,派生,指向派生源(更抽象)

deployment: 一般就是指manifestation
(具体见部署图)
依赖关系结构图:

实现
接口和实现。
非矩形表示接口时:

矩形表示接口:

模板
就是常规意义上的模板.实际上就是带参数的各种分类器.
右上角的为模板签名(类似函数签名),里面的为模板参数:
template-parameter ::= template-param-name [ ’:’ parameter-kind ] [ ’=’ default ]
可以指定类型和默认值.
模板通常需要bind,实际上就是传参数:
template-param-substitition ::= template-param-name '→' actual-template-parameter
使用<<bind>> stereotypes,和<template-param-substitition>进行bind.
-
类模板
(有点类似泛型)

-
collaboration模板

-
package模板

注释

UML复合结构
类图
类图是结构化图的一种,在classes和interfaces层面显示system的结构,显示他们的features(结构,行为),约束,关系.
包括:
- domain model diagram
- diagram of implementation classes
Object class diagram是class diagram的instance level,实例层面的类图.
概念
相关概念见UML核心元素章节.
domain model diagram
领域模型图,常规意义上的类图,classes,interfaces,relationships,constraints,multiplicity等等.

diagram of implementation classes
实现类图没有明确的定义,感觉就像是领域图的细化.

object diagram
object即instance,本质上是class diagram的实例层面的展示.

包图
包本质上就是提供了一个命名空间,用于对建模元素进行分组,以更高的层次(即package level)来展示系统的结构。
概念
包是一个命名空间,包含子元素(类似Java中package和class的概念).
元素在包内表示:

元素在包外:

类似组合。”+“符号表示组合。
包导入:
虽然看起来是依赖关系,但是和依赖关系不大,是完全独立的概念.

包导入后的可见性有public和private.
import导入后,被导入的元素在导入的命名空间外部是可以见的.
access则不可见.
包合并:

(Kernel and Profile packages merge Constructs package)
类似继承。源包元素继承了目标包同名元素的特性,一般用在概念增量的设计模式下。
例子

模型图
概念
model
model的概念不是很清晰,可以大致理解为一个指定角度的view(对system的某种抽象),用来描述system.


还可以使用 <<model>> keyworkd:

模型图
系统分层模型中,包括视图层,业务层,数据层

组合结构图
作用:
- 展示分类器的内部结构
- 分类器通过Ports和环境交互
- 协作行为
本质上就是一组互联的元素,实现共同目标的运行时实例.
主要有2个实现 internal structure diagrams 和 collaboration use diagrams
基本概念
part
分类器的属性.实际上就是某些class或interface的运行时实例.
分类器使用composition持有该属性:

不使用compositon持有该属性(可以理解为引用过来的):

connector
类似association.
connector-label ::= [ connector-name ] [ ’:’ ( association-name | association-class-name ) ]
-
delegation connector
连接到的Port没有在part上,并且这个port不是behavior port 那这个连接就是delegation,实际上相当于多了一层代理.



-
assembly connector
没有中间代理,委托的



port
分类器用来和外部环境沟通的通道.



(Library Services class has 1 to 6 searchPort ports.)

(Port searchPort provides SearchBooks and SearchVideo,interfaces and requires Inventory interface.)

(合并provide接口)
simple port:
只有一个provided or required



服务端口和行为端口:
带小长方形的端口表示行为端口,意味者通过该端口发送到分类器的请求会被转发到该分类器的behavior,而不是包含的某些实例.
端口默认为服务端口.

internal structure diagrams

collaboration use diagrams
collaboration:

collaboration use:

组件图
用来描述组件,接口,ports之间的关系.

概念
component
系统模块化的模块,它可以被artifacts(见部署图)来表现。模块封装了内部细节,通过ports来实现provided interfaces和required interfaces.
通用组件的表示:


其他组件,使用特定的stereotypes:
| 名称 | 说明 |
|---|---|
| <<BuildComponent>> | 为系统级开发活动定义的元素集合,例如编译和版本控制。 |
| <<Entity>> | 表示业务概念的持久信息组件。 实体组件客户 |
| <<Implement>> | 实现是一个组件定义,它本身并不打算有一个规范。相反,它是一个独立规范的实现,它依赖于该规范。 |
| <<Process>> | 流程是基于事务的组件。 |
| <<Realization>> | 实现是一个分类器,它指定了对象的域,也定义了这些对象的物理实现。 例如,由实现模式化的组件将只具有实现由单独的规范组件指定的行为的实现分类器。这不同于实现类,因为实现类是一个类的实现,它可以具有对系统设计者有用的特性,例如属性和方法。 |
| <<Service>> | 服务是一个无状态的功能组件。 服务组件WeatherServices |
| <<Specification>> | 规范是一个分类器,它指定对象的一个域,而不定义这些对象的物理实现。 例如,由< |
| <<Subsystem>> | 子系统是代表大系统分层分解单元的组件,用于对大规模组件建模。子系统的定义可能在不同的领域和软件方法之间有所不同。预计域和方法概要文件将专门化这个元素。 子系统通常是间接实例化的。子系统可以有规范和实现元素。 |
provided接口:

required接口:

provided和required的另一种表现形式:

组件实现
servelt stereotype和dao stereotype继承了component stereotype.

还可以使用”white-box”模式:

例子
例子中的其他概念参考《组合结构图》

manifestation图
组件图的外在(物理表现),表现形式为artifacts,manifestation diagram是组件图和部署图的中间部分。
详见部署图。
部署图
系统架构到artifact映射,然后部署到deployment target.
概念
artifact
开发结果产物,物理呈现,它是组件(并不仅仅是components,包括所有packageable elements)的外部表现(可以理解为实现).一般指可执行文件,Libs,配置文件,database schemas等等,还包括:
- text document
- source file
- script
- binary executable file
- archive file
- database table
表现为:

或使用特殊的图标,比如:

可以使用标准的**«artifact»**
还可以使用:
| «file» | A physical file in the context of the system developed. |
|---|---|
和它的子类:
| «document» | A generic file that is not a «source» file or «executable». |
|---|---|
| «source» | A source file that can be compiled into an executable file. |
| «library» | A static or dynamic library file. |
| «executable» | A program file that can be executed on a computer system. |
| «script» | A script file that can be interpreted by a computer system. |
(EJB component UserService and skeleton of web services are manifested (implemented) by EJB module user-service.jar artifact)

deployment target
uml中用node表示,一般指设备或运行环境,用来放置artifacts的地方.
node
一般就是指设备和运行环境.

node可以嵌套(上面的组合,本质也是嵌套):

设备
设备的标准stereotype: «device»
非标准stereotypes:
- «application server»
- «client workstation»
- «mobile device»
- «embedded device»
运行环境
运行环境的标准stereotype: «executionEnvironment»
非标准stereotypes:
- «OS»
- «workflow engine»
- «database system»
- «J2EE container»
- «web server»
- «web browser»
communication path
node之间的通信关联.表现形式也和association一样.

通信双方如果为设备,关联一般表现为”物理连接”:

(Gigabit Ethernet as communication path between application and database servers.)
如果通信双方为执行环境,关联通常表现为某种“通信协议”:

(TCP/IP protocol as communication path between J2EE server and database system.)
deployment
artifacts和deployment的关联,使用依赖来描述.使用**<<deploy>>** stereotypes.

实例化部署:
(artifact实例可以省略名称和:)

更常见的部署表现形式为嵌套:

对于部署图来说,更好的是放在一个frame里:

deployment specification
部署规范本质上是一个配置文件,用来定义artifacts是怎么部署在node上的.

规范实例:

artifact指定规范:
(The ejb-jar.xml deployment specification for user-service.ejb artifact.)

在deployment时指定规范:
(The ejb-jar.xml deployment specification attached to deployment.)
注意,这里的关联使用的是虚线.

manifestation diagram
组件图和部署图中间还有一个 manifestation diagram过渡:

deployment diagram
部署图有2种,规范和实例,就是通用和具体.
规范图:

具体:
和上面的区别就是deployment target node都是具名(比如: wsrv-01)或匿名(比如: :Oracle 10g)的

网络结构图
uml规范中并没有网络图,也没有对应的表示符号。
一般用部署图和特定的stereotype结合表示。
网络图包含nodes和communication paths(见部署图)
常用的devices(nodes)有:
- switch: 交换机

- router: 路由器

- load balance: 负载均衡器

- firewall:防火墙


Profile图
配置图是uml的一种扩展机制,用于自定义stereotyps之类.
概念
profile
profile本质是对metamodel扩展的package,来对某些特定的领域和平台进行更加细化的语义表达.
profile不能独立存在,它必须扩展自某个metamodel(比如UML)
profile是通过extension metaclass来实现,extension的目标就是stereotype,profile本质上就是这些stereotypes的集合.
比如SysML 是统一建模语言的对象管理组 (OMG) 标准化配置文件,用于系统工程应用程序。参考
表现形式为带有keyword «profile» 的package:

例子:

profile有一个可选的URI属性来进行唯一标识这个package.
uri ::= http://qualified-profile-parent /profile-version /profile-name.xmi

Metaclass
(<<MetaClass>>可以省略)

extensions
扩展分为non-required extension和required extension.
TODO: 不是很理解,见官方文档
non-required,和metaclass instance之间的关系比较随意,不需要随时连接.
required要求stereotype的实例必须一直和metaclass的实例连接.
默认为non-required.
stereotype扩展metaclass:
(黑色实心箭头,目前只发现在profile的extension中使用)


被多个扩展:

stereotype
用来标识uml element的目的.
uml内置了很多stereotypes,可以直接使用(更合理的说法是包含这些stereotypes的profile自动应用到了每个uml element)
stereotype一定是在profile中定义,一定是从metaclass extension,stereotype本身不能被其他stereotype扩展.
stereotype使用class来表示,使用关键字**«stereotype»** :

stereotype可以自定义图标:
使用组合符号.


stereotype为class,可以有属性:

应用
stereotype必须被应用到各种uml elment上才有意义.
(Stereotype «Servlet» applied to the model element SearchServlet)
一个uml element可以应用多个stereotypes ::= ’<<‘stereotypeName [,stereotypeName]*’>>’
如果还有keywords,2者需要并排: <<stereotype>><<keyword>>

如果stereotype有自定义图标,在uml element上还可以直接用图标代替,前提是这个uml element有图形化外观(分类器box),并且只有一个stereotype:

甚至可以用放大的icon直接代替uml element的box:

对于那些已经使用特殊图标代替box的element:
- 可以直接用stereotype的icon代替element的图标,但是要提供stereotype name:

- 以注释的形式提供stereotype:

stereotype relationships
stereotype不能extension其他stereotype,但是可以泛化(继承):

tag definition
由于stereotype本质上是class,可以有properties,tag实际就是properties.

tag通常为string和boolean,虽然也允许使用其他类型的值.
当stereotype应用到element时需要给tag绑定值:
- 直接赋值:

- 分割框中赋值:

- 注释赋值:

reference
即导入.
(Metaclass Component is referenced (imported) by profile Servlets)

application
profile一定要应用到element上,profile中的stereotype才能被使用(内置的profile默认被应用).
(Profiles Java and Servlets applied to package WebApplication.)

例子


用例图
用例图,通常用来表示actors和系统(主体)协作来执行一组操作,其中的每个用例都应该给actors提供可观察,有价值的value.
概念
- use case,见UML核心元素 ⇒ use case
- subject,也叫system boundary.可以理解为一个子系统,模块等所表示的行为,是use case的集合.
- actor,见UML核心元素 ⇒ actor.注意actor对于subject一定是external user,他是外部的,不属于业务或系统内部
其他概念见UML核心元素-分类器-use case
基本表示

(Books Online (subject) with applicable use cases and Web Customer actor.)
business 表示
同样的不属于uml标准规范,可以自定义stereotypes

system 表示
虽然也没有标准的stereotypes,但是常用的有:
- Subsystem
- Process
- Service
- Component

拥有use case
前面的只是用例和subject的关联,一个用例可以在多个subject中.
subject也是可以拥有use case:
(Retail Website subject owns use cases)

business use case diagram

例子

system use case diagram

例子


information flow图
information flow图,用于展示在很高抽象层次的系统实体之间的信息交换.
uml标准中,information flow图很弱,基本只能表达出一个很粗糙的信息流.

information flow: 基本只表示流的方向(这里的虚线箭头不是依赖关系,是完全独立的概念),并使用关键字«flow».information flow的source和target只能是actor,use case,node,artifacts,class,component,port,property,interface,package,activity node,activity partition,instance specification.
information item: 表示传递的信息,是一个classfier,但是这个classfier没有feature,无法显示更多的信息细节.
更复杂的例子:

活动图
activity图本身是流程图的一种,用来对动作流建模,重点是流的顺序和条件.
activity diagram通常包括: activity,partition,action,object,control,activity edge.
概念
Activity
管理一组动作流.
主要包括的nodes有: action,object,control
活动的表现形式:

包含参数的活动:
(参数列在边框上,并且在activity name下以name: type形式列出)

使用frame表示:
(使用frame语法,type可以是activity or act)

Activity Partition
类似流程图中的泳道,表示了一组具有共同特征的actions.
最常见的分区为组织单元和业务角色.

分区可以有子分区,分类抽象到具体.
Activity Edge
简单理解就是带箭头实线,表现形式和association类似,但是这里是完全独立的概念.
有出和入edge.
用于连接actions,objects,controls.
可以给edge指定名称,名称靠近箭头:
设置条件:
(Fill Order when priority is 1)

edge的另一种表现形式:
(这么做的唯一目的,是为了避免过长的连接,对结果表达没有任何影响.)

object flow edge
actions之间可以传递object(data).

object可以指定weight,表示数量达到这个weight后,一次性的传递给target action:
(Send Notification when number of Warnings reaches 6.)
interrupting edge
中断逻辑.
(cancel request事件导致中断,造成cancel order)

另一种表现:

Actions
表示一种原子化的行为.

设置前置和后置条件,表示在开始和结束这个action时进行约束:
(条件已备注的形式注明)

call activity action
表示调用另一个activity:

send signal action
表明这个action会发送一个同名signal:

(After order is shipped, Notify Customer send signal action creates and sends Notify Customer signal)
accept event action
表明这个action在等待接收一个同名event:
(Acceptance of the Accept Order event by Accept Order action causes an invocation of a Process Order action. The accept event action is enabled upon entry to the activity containing it)

accept event action
对应send signal action.表示这个action在等待接收一个同名signal,表现形式和accept event action相同,实际上此时的event就是signal event.
(Payment Requested signal is sent. The activity then waits to receive Payment Confirmed signal. Acceptance of the Payment Confirmed signal is enabled only after the request for payment is sent.)

wait time action
又名 accept time event action.也就是根据时间来接收event:
(每小时触发一次)

Controls
control node主要作用是用来协调flow.

flow final node

这代表flow的结束.而activity final node,表示这个activity中的所有flow结束.
decision node
分支,一定会有条件.
![For decision points, a predefined guard [else] may be defined.](https://s2.loli.net/2024/05/14/VimNM2QSywX14oT.png)
对条件进行转化:
使用keyword: <<decisionInput>>,表达式应该是一个纯函数(无副作用)

merge node
merge node可以和decision node同时使用:
合并后再fork.

fork node
1变多,通常用于并发场景.

可以和join node同时使用:
先join,再fork.
join node
通常用于同步并发flow.

Objects
object node用来表示object/token/data.
表示这个object从from流向to.

还可以使用pin来表示:

data store
可以理解为Collection<Object>.
(Incoming Patient token is stored by the Patients data store.)

例子




状态机图
用来描述一个对象在他的生命周期中响应事件时所经历的状态变化.
主要包含的元素有:state,transition,pseudostates(伪状态,可以理解为非直接状态,用来辅助协调state)
可以在frame中定义,使用label: state machine or stm
state machine meta-model

behavioral state machine
带有触发器,是某一个具体情况下的状态转换.

behavioral state
simple state

compartments表示方法:

entry: 进入时执行
do: 在这个状态时执行
exit: 退出时执行
transition见后续相关语法.
composite state
拥有子状态的状态.子状态可以是顺序的,也可以是并行的(多个region的情况).composite state包含一个或多个region.
region可以在state machine和composite state中存在.
只有一个region:

多个region:
多个region中的活动是并发(正交orthogonal,对应顺序执行的不相交disjoint)的,所有region都完成,这个composite state才算完成.

同样可以使用compartments表示,比simple的compartments多了一个decomposition compartment.这个区域也就是嵌套的子状态图表示,为了便于图形化展示,一般不会直接在composite state中直接展示decomposition compartment,会使用指定的符号(composite icon)代替,然后在外部画出这部分.

submachine state
实际上就是封装一个state machine然后进行引用,引用方式为:
state name: reference state name
封装的目的就是为reuse.

pseudostate
“伪状态”用于连接transitions,还可以用作source,target.
| Initial Pseudostate | |
|---|---|
Initial pseudostate transitions to Waiting for User Input state. | 默认初始状态,一个region最多有一个 |
| Terminate Pseudostate | |
Transition to terminate pseudostate. | 这个state的context object(载体)终止了. |
| Entry Point | |
Entry point user entry. | Entry point pseudostate is an entry point of a state machine or composite state. In each region of the state machine or composite state it has at most a single transition to a vertex within the same region.An entry point is shown as a small circle on the border of the state machine diagram or composite state, with the name associated with it. |
| Exit Point | |
Exit point user exit. | Exit point pseudostate is an exit point of a state machine or composite state. Entering an exit point within any region of the composite state or state machine referenced by a submachine state implies the exit of this composite state or submachine state and the triggering of the transition that has this exit point as source in the state machine enclosing the submachine or composite state.An exit point is shown as a small circle with a cross on the border of the state machine diagram or composite state, with the name associated with it. |
| Choice | |
Select outgoing transition based on condition. | Choice pseudostate realizes a dynamic conditional branch. It evaluates the guards of the triggers of its outgoing transitions to select only one outgoing transition.A choice pseudostate is shown as a diamond-shaped symbol. |
Choice based on guards applied to the value inside diamond. | 如果有多个true,会任意选一条,如果都为false,则模型配置错误,此时可以设置一个else.可以将表达式左边放在choice符号内,其他的放在transition的guard中. |
| Fork | |
Fork splits transition into two transitions. | 类似activity中的fork,一分多,多的部分并行,一般连接多个region.fork出的transition不能使用triggers和guards. |
| Join | |
Join merges transitions into single transition. | 和fork相反,从多个regions中出,join到一个. |
| Final State | |
Transition to final state. | 表示region complete.所有的regions complete,则state machine complete. |
junction:
用来关联多个transitions,一个分多,多和一,多对多.

history:
记录了中断时的状态,可以直接连接到这个状态,而不是从头开始.
分为:
-
shallow history pseudostate: 表现为带圆圈的H
浅历史状态,history pseudostate符号同层的最近激活的一个substate(不会再进入查找)
-
deep history pseudostate: 表现为带圆圈的H^*^
深历史状态,最近激活的一个状态(不限制层次)

transition
用来关联各种state.
transition ::= [ triggers ] [ guard ] [ ’/’ behavior-expression ] triggers ::= trigger [ ’,’ trigger ]* guard ::= ’[’ constraint ’]’
触发器一般为各种事件.
guard为条件.
behavior-expression,可以是一个action列表,当开始转换时执行.
例子:
left-mouse-down(coordinates) [coordinates in active_window] / link:=select-link(coordinates);link.follow()trigger: left-mouse-down
guard: coordinates in active_window
expression: link:=select-link;link.follow()
当trigger,并且满足gurad时,执行expression,并开始transition.
protocol state machine
没有触发器,是某一类情况,指明了在什么时候,什么条件下可以转换状态.
所以可以把行为状态看作协议状态的实现.
行为状态机说明如何到达这个状态,而协议状态机则只说明满足某些条件时会处于这个状态.

frame表现形式对比behavioral state machine多了一个{protocol}标记.

也有simple state和composite state.

协议state中不能出现entry,exit,history,可以有submachine states,composite states,regions.
transition
这是和behavioral state machine最大的区别.
protocol-transition ::= [ pre-condition ] trigger ’/’ [ post-condition ] pre-condition ::= ’[’ constraint ’]’ post-condition ::= ’[’ constraint ’]’
根据协议状态机的特点可以知道,他的transition实际上只做了一件事,那就是满足这些条件时,我会transition到这个状态.
即,在满足pre-condition且进行了trigger时,如果在transition结束时还满足了post-condition,则会转换到目标状态.

例子



交互图
用于描绘一个system的动态行为,聚焦于一个system不同部分是如何交互联系的.
交互图使用frame表达,label为:interaction或sd
通常分为下面四个类型.
时序图
也叫序列图(sequence diagrams).
最常用的交互图,用于描述system多条lifelines上随时间变化进行信息交换,重点在信息的顺序上以及信息对应的执行规范.

概念
lifeline
交互的参与者(participant),包括2部分,head和垂直的生命周期线.
head的表现形式为这个参与者的classifier的形状,通常为矩形框.
参与者的标识语法如下:
lifeline-ident ::= [ connectable-element-name [ ’[’ selector ’]’ ]] [ ’:’ class-name ] [ decomposition ] | ‘self’ selector ::= expression decomposition ::= ‘ref’ interaction-ident [ ‘strict’ ]
selector为表达式,如果参与者有多个part,需要selector来决定代表哪个,否则即使任意一个.
self表示这个参与者就是整个sequence diagram.
lifeline-ident不能省略.
![Lifeline x selected with [k] of class X.](https://s2.loli.net/2024/05/16/a9AGJjDUKwQcmBR.png)
message
用于lifeline之间沟通,包括内容(信息类型),发送者,接受者.
消息反映了操作的调用,执行的开始,信号的发送和接收.
语法规范:
message ::= [ attribute ’=’ ] signal-or-operation-name [ arguments ] [ ’:’ return-value ] | '' arguments ::= ’(’ [argument [ ’,’ argument] ’)’ argument ::= [ parameter-name ’=’] argument-value | attribute ’=’ out-parameter-name [ ’:’ argument-value ] | ’ -’
如果message为动作,arguments就是这个动作的参数,如果message为signal,arguments就是这个signal的属性.
根据action的类型,message有如下类型:
-
synchronous call
同步调用通常表现为操作的调用,暂停执行,等待响应.

-
asynchronous call
不需要等待,线程继续执行.

-
asynchronous signal
对应活动图中的发送信息号,此时是异步的.

-
create
用来创建指向的lifeline,箭头指向lifeline的head.
(Online Bookshop creates Account.)

-
delete
指向被destroy的lifeline,用来终止这条lifeline.
当前规范没有提供专用的符号和stereotype,所以通常使用自定义的<<destory>>,和destruction occurrence符号来表示:

-
replay
一般用于操作调用的response.

message还可能对应send event和receive event,针对这2个事件,有如下可能性:
-
complete message
send event和receive event同时存在
-
unkown message
send event和receive event都不存在
-
lost message
只存在send event.
就好像message无法到达目的地(未知目的地).
(Web Client sent search message which was lost.)

-
found message
和lost相反,只有receive event.
就像消息的源头在当前作用域之外,无法trace(或者不想trace).
(Online Bookshop gets search message of unknown origin.)

gate
作为一个message端,用于连接内外部消息的点.
通常从diagram的边框连接到lifeline.
interaction fragment
交互片段.
很宽泛的概念,交互图中最通用的交互单元,以下都是交互片段:
- occurrence
- execution
- state invariant
- combined fragment(组合片段,也就是狭义上的fragment)
- interaction use
具体定义见相关章节.
execution
是一个交互片段,表示参与者生命周期线上的一段时期,可以是:
- 执行一个行为单元
- 给其他参与者发送signal
- 等待其他参与者的响应

另一种表现形式:
矩形中的标识为被执行的action.

overlappingd execution:
在同一lifeline上重叠execution,比如调用自己:

又比如目标参与者回调源参与者:

occurrence
用来描述message和execution的开始或结束的时刻(也可以理解为事件).
实际上occurrence的顺序就是交互的含义.
message occurrence:
比较特殊的一个message occurrence就是destruction occurrence:
用于终止lifeline,以及销毁lifeline对应的object,destruction occurrence后该lifeline后面不能再有任何occurrence.

execution occurrence:
一个execution会有一个开始execution occurrence和结束execution occurrence.

state invariant
就是交互的约束条件,可以使用属性/变量值或状态来表示.
属性变量一般指所属参与者,用{}包裹:

状态(也可以是外部状态)表示所属参与者的状态:

interaction use
就是引用其他interaction,避免过于庞大和复杂的diagram,同时也能剥离出可复用的interaction.
引用语法:
interaction-use ::= [ attribute-name ’=’ ] [ collaboration-use ’.’ ] interaction-name [ io-arguments ] [ ’:’ return-value ] io-arguments ::= ’(’ io-argument [ ’,’ io-argument ]* ’)’ io-argument ::= in-argument | ‘out’ out-argument
可以看到还能传入参数.
(Web customer and Bookshop use (reference) interaction Checkout.)

(Use Login interaction to authenticate user and assign result back to the user attribute of Site Controller.)

combined fragment
狭义上的fragment.
本质上就是组合广义上的interaction fragments.
combined fragment使用操作符(类型)和操作数(具体内容)来定义,能够进行更复杂的表达.操作符有如下:
- alt - alternatives
- opt - option
- loop - iteration
- break - break
- par - parallel
- strict - strict sequencing
- seq - weak sequencing
- critical - critical region
- ignore - ignore
- consider - consider
- assert - assertion
- neg - negative
combined fragment通常存在约束(也就是guard,condition),用来约束操作数,具体见对应的操作符说明.
alternatives
if..else..模式选择操作数(比如下图中的accept,reject).
最多只有一个操作数的guard为true.
(Call accept() if balance > 0, call reject() otherwise.)

option
if..then..
(Post comments if there were no errors.)

loop
while..,do..while..
循环边界定义如下:
loop-operand ::= loop [ ’(’ min-int [ ’,’ max-int ] ’)’ ] min-int ::= non-negative-integer max-int ::= positive-integer | ’*’
没有提供边界,则无限循环(比如web服务的监听):

只提供min-int,此时max-int == min-int,表示循环指定次数:

同时提供min-int和max-int,这一般会配合gurad使用,但是在理解上有混乱:
-
循环5次后,再判断guard是否为false,是或者达到上限则跳出循环.
-
不管是否达到最小次数,只要gurad为false或达到上限则跳出循环.
哪个是对的?

break
中断外层loop,break一定要有guard.

parallel
并发.
2种表现形式:

strict sequencing
严格按顺序执行交互.

weak sequencing
严格模式下,不管是否同一lifeline都要严格顺序,但是弱顺序模式下,不同操作数,不同lifeline下的顺序可以是任意的,但是不同操作数,相同lifeline下的顺序是固定的.
比如: google和bing,yahoo没有严格顺序,可能是并行的,但是bing一定在yahoo之前.

实际上就是同一参与者上的顺序是严格的,不同参与者上的顺序是任意的.
critical region
临界区域,表示不能被其他occurrence交互,被外层fragment原子化对待.

ignore
相关message不应该被响应(忽略掉了)

consider
只有指定的消息应该被响应,其他都ignore.

assertion
断言这个操作数一定是下一个执行.

nagative
当系统出现故障时执行.
(Should we receive back timeout message, it means the system has failed.)

例子




mermaid表示
sequenceDiagram autonumber participant A as objectA participant B as objectB participant C as objectC participant D as objectD participant E as objectE A ->>+ B:[connect=true]notice %% this is a comment,will be ignored by the parser B -->>- A:get Note right of A:note right of A Note over A,B:note over A,B C ->> C:check selft B ->>+ C:send message every minute loop Every Minute C -->>- B:send message end C ->+ D:async operator D -->>- C:return par threadpool E ->> E:thread 1 E ->> E:thread 2 end alt verification == true D ->>+ E:request:http://localhost/profile E -->>- D:index page else D ->>+ E:request:http://localhost/profile E -->>- D:error page end
mermaid语法
- sequenceDiagram表示序列图
- participant 参与者,对象
- autonumber表示自动按顺序表示消息order,从上到下,从左到右的顺序
- →实线,异步消息;⇒虚线;→>带箭头实线,同步消息;⇒>虚线箭头,返回消息;
- A 箭头类型 B,表示A 到 B的消息,可以箭头类型后面跟“+”,同时B 箭头类型 A,箭头类型后面带”-“表示一组消息,并激活控制焦点。
- note语法,note right/left of object:note content;note over …objects:note content
- 消息语法,A 箭头类型[+/-] B:[boolean expression]message
- 组合片段语法:alt/opt/loop… [boolean expression] 包裹消息序列 end。alt还需要跟一个else。
communication diagram
定义: 协作图/通信图,交互图的一种.objects和parts之间使用顺序messages来交互,可以看作简单的时序图,但是没有interaction use,combined fragments.
主要使用的元素: lifeline,message
使用frame表达时,communication diagram没有专用的label,使用交互图的标准label: interaction或sd

概念
lifeline
和序列图的lifeline定义基本相同,命名语法相同,表现形式同序列图的head,因为这里的lifeline没有”line”.
![Lifeline x selected with [k] of class X.](https://s2.loli.net/2024/05/20/2YnZoBUCeLuVvlx.png)
message
和序列图的message不同.
由3部分构成,line(就像associate),arrow,sequence expression.
(Instance of class A sends remove() message to instance of B if s1 is equal to s2)

重点是sequence exression:
sequence-expression ::= sequence-term ’.’ … ’:’ message-name
sequence-term ::= [ integer [ name ] ] [ recurrence ]
recurrence ::= branch | loop branch ::= ’[’ guard ’]’ loop ::= ’*’ [ ’||’ ] [ ’[‘iteration-clause ’]’ ]
关于interger指定的顺序:
- message with sequence 2 follows message with sequence 1,
- 2.1 follows 2
- 5.3 follows 5.2 within activation 5
- 1.2.4 follows message 1.2.3 within activation 1.2.d
name可以理解为并发线程,也就是同一级别上不同的线程.
例:
- messages 2.3a and 2.3b are concurrent within activation 2.3,
- 1.1 follows 1a and 1b,
- 3a.2.1 and 3b.2.1 follow 3.2.

gurad:
也就是message发送(执行)的条件,没有明确语法定义,可以是伪代码,特定的语言语法或者其他有意义的布尔表达式
loop:
重复执行逻辑.
* 表示重复执行是按顺序的.
*|| 表示重复执行是并发的.
iteration-clause没有明确语法定义,就像guard一样.
例:
- 4.2c *[i=1..12]: search(t[i]) - search() will be executed 12 times, one after another
- 4.2c *||[i=1..12]: search(t[i]) - 12 search() messages will be sent concurrently,
- 2.2 *: notify() - message notify() will be repeated some unspecified number of times.
(Instance of class A will send search() message to instance of B n times, one by one)

(Instance of class A will send n concurrent search() messages to instance of B)

例子

timing diagram
定义: 时间图/计时图,交互图的一种.用来描述对象随时间,状态的变化,以及造成这些变化的messages.
主要使用的元素: lifeline,timeline,state/condition,message,constraint,time ruler.
相关概念:
- timeline: 时间线,state/condition持续的时长
- message: 参考时序图
- time ruler: 时间坐标

概念
lifeline
参与者对象,参考时序图概念,一般以name的形式出现,多个lifeline时,diagram结构类似横向泳道图.

state or condition
就是lifeline的一些离散状态.

constraint
2类约束(注意,在时序图中也有用到)
-
时间长度约束
比如下图,Melting的状态应该只有1min 至 6min,因为冰会在1min 至 6min内融化为水.

-
时刻约束
在所指定时间范围内,状态会改变.
这个图形表达,可以看成,状态转换的”转折点”上标注时间范围,表示这个时刻的约束条件.
(Person should wake up between 5:40 am and 6 am)

desctruction occurrence
(Virus lifeline is terminated)

例子


interaction overview diagram
定义: 交互概要图,本质上是活动图和交互图的结合,使用了活动图的控制流,但是参与的action是interactions,interactions use.
也就是图中会出现activity diagram的control nodes,和interaction diagram的interaction,interaction use,以及duration constraint,time constraint.
frame表现形式的label使用标准的交互图label: interaction or sd

例子


Math is utility class - having static attributes and operations (underlined)


实体组件客户
服务组件WeatherServices
Initial pseudostate transitions to Waiting for User Input state.
Transition to terminate pseudostate.
Entry point user entry.
Exit point user exit.
Select outgoing transition based on condition.
Choice based on guards applied to the value inside diamond.
Fork splits transition into two transitions.
Join merges transitions into single transition.
Transition to final state.