Ran Wei / SysML v2 系列 / 模块 6
EN
SysML v2 — Ran Wei

模块 6:用例

如何用用例描述系统场景、用分析用例评估设计备选方案、用验证用例校验需求 — SysML v2 用例建模的三大支柱。

KerML → SysML:用例概念

SysML v2 中的用例本质上是特化的行为。每个用例定义最终都继承自 KerML 的 CaseDefinition,而后者又特化自出现层(Occurrence layer)的 Behavior。用例在模块 4 介绍的通用行为机制之上,补充了结构化的语义 — 参与者、主题与目标。

SysML v2 概念(L2)底层 KerML 构造(L1)SysML 增加了什么
use case defUseCaseDefinition,特化自 CaseDefinitionBehavior围绕用户目标组织系统行为;补充参与者与主题语义
use case(使用)UseCaseDefinition 定型的 Feature系统或包上下文中的一个具体场景实例
actorActorMembershipPartDefinition把某个零件标记为与所考察系统交互的外部实体
subjectCaseDefinition 上的 SubjectMembership标识该用例所描述的系统或组件
objective用例通过 ObjectiveMembership 拥有的 RequirementUsage陈述该用例意图达成或验证的目标;是对结果的一条需求
include use caseIncludeUseCaseUsage (a PerformActionUsage)通过把另一个用例作为子步骤调用来组合用例
analysis defAnalysisCaseDefinition,特化自 CaseDefinition为分析评估增加返回属性;组织权衡研究的输入
analysis(使用)AnalysisCaseDefinition 定型的 Feature模型上下文中的一次具体分析评估
verification defVerificationCaseDefinition,特化自 CaseDefinition通过 verify requirement 连到需求;组织 V&V 活动
verification(使用)VerificationCaseDefinition 定型的 Feature绑定到某条特定需求的具体验证流程
verify requirementRequirementVerificationMembership声明某个验证用例验证某条特定需求
表 0 — 模块 6 的 SysML v2(L2)概念到 KerML(L1)起源的映射
说明

三类用例 — 用例、分析用例、验证用例 — 有共同的祖先 CaseDefinition,因此都支持 subjectobjectiveactor 关键字。它们的差别在于用途,以及各自额外补充的语义(例如分析用例的 return、验证用例的 verify requirement)。

1

用例定义

KerML 起源:use case defUseCaseDefinition,特化自 CaseDefinitionBehavior

用例描述系统为参与者交付某种有价值结果所执行的一系列动作。在 SysML v2 中,用例是一等的行为:它可以有子动作、参数和控制流,与模块 4 中的动作完全一样。区别在于用例额外引入了目标(objective)的概念 — 一条描述该用例意图达成什么的需求。

类比

可以把用例想成一份菜谱:它有目标(一道好菜)、主题(厨房)、参与者(厨师、供货商)和一连串步骤。菜谱本身不是那道菜 — 它描述的是这道菜如何被做出来。

定义用例

use case def 关键字声明一个可复用的用例模板,其主体包含描述场景流程的动作:

1use case def DriveToDestination {
2 doc /* The driver requests a route and the vehicle
3 navigates autonomously to the destination. */
4
5 objective {
6 doc /* Vehicle arrives at the requested destination
7 safely and within the estimated time. */
8 }
9
10 action enterDestination; // step 1
11 action computeRoute; // step 2
12 action navigateRoute; // step 3
13 action arriveAtDestination; // step 4
14
15 first enterDestination then computeRoute;
16 first computeRoute then navigateRoute;
17 first navigateRoute then arriveAtDestination;
18}

用例的使用

use case 使用把用例定义实例化到具体上下文中,将其绑定到某个特定的系统或包:

1package AutonomousVehicleUseCases {
2 use case driveCommute : DriveToDestination {
3 doc /* Daily commute scenario with familiar route. */
4 }
5
6 use case driveEmergency : DriveToDestination {
7 doc /* Emergency routing to nearest hospital. */
8 }
9}
说明

由于 use case def 特化自 CaseDefinition,而后者又特化自 Behavior,用例主体支持全部动作构造:first/thenif/elsefork/join、参数以及条目流。用例不只是一段文字描述 — 它是可执行的行为规约。

2

参与者与主题

KerML 起源:actor → 带 ActorMembershipPartDefinition; subjectSubjectMembership

每个用例都有一个主题(subject) — 被描述的系统或组件 — 以及一个或多个参与者(actor) — 与主题交互的外部实体。subject 关键字声明该用例所讲述的那个零件;actor 关键字声明从外部参与其中的零件。

声明参与者与主题

1part def Driver; // external human actor
2part def CloudService; // external system actor
3part def Vehicle; // the system under design
4
5use case def DriveToDestination {
6 subject vehicle : Vehicle; // the system being described
7 actor driver : Driver; // human interacting with the vehicle
8 actor cloud : CloudService; // external service providing maps
9
10 objective {
11 doc /* Vehicle arrives safely at destination. */
12 }
13
14 perform driver.enterDestination; // actor initiates the flow
15 perform cloud.computeRoute; // actor provides route data
16 perform vehicle.navigate; // subject performs navigation
17}
提示

subjectactor 关键字在全部三类用例上都可用 — 用例、分析用例和验证用例。它们划定了被建模系统与其环境之间的边界。

参与者就是零件定义

参与者并不是某种特殊元素 — 它们就是普通的 PartDefinition,只是恰好通过 ActorMembership 被引用而已。因此参与者同样可以拥有自己的属性、端口和行为:

1part def Driver {
2 attribute name : String;
3 attribute licenseLevel : LicenseType;
4 port driverInterface : DriverHMI;
5}
常见误区

不要把 actor 当成 SysML v1 里的构造型。在 SysML v2 中,actor 是一种成员关系角色,而非分类器种类。同一个 PartDefinition 可以在这个用例里是参与者、在另一个用例里是主题 — 角色取决于用例上下文,而不取决于定义本身。

3

包含用例

KerML 起源:include use caseIncludeUseCaseUsage(一种 PerformActionUsage

用例可以通过把其他用例作为子步骤纳入而组合起来。include use case 语句在一个用例的主体内调用另一个用例定义,就像 perform action 调用动作定义那样。

组合用例

1use case def Authenticate {
2 subject vehicle : Vehicle;
3 actor driver : Driver;
4
5 action scanBiometrics;
6 action validateIdentity;
7 first scanBiometrics then validateIdentity;
8}
9
10use case def DriveToDestination {
11 subject vehicle : Vehicle;
12 actor driver : Driver;
13
14 // Include authentication as a prerequisite step
15 include use case auth : Authenticate;
16
17 action computeRoute;
18 action navigateRoute;
19
20 first auth then computeRoute;
21 first computeRoute then navigateRoute;
22}

特化用例

用例定义可以用 specializes 特化其他用例定义,继承全部参与者、主题和步骤,同时追加或覆盖具体内容:

1use case def DriveInBadWeather specializes DriveToDestination {
2 doc /* Extends base driving use case with weather-specific
3 precautions: reduced speed limits and sensor recalibration. */
4
5 action recalibrateSensors; // additional step
6 first auth then recalibrateSensors;
7 first recalibrateSensors then computeRoute;
8}
说明

include use case 表达的是组合(被包含的用例作为包含方的一部分执行);specializes 表达的是继承(子用例本身就是父用例的一种)。两者都可用于构建复杂的用例层次,但语义不同。

4

分析用例

KerML 起源:analysis defAnalysisCaseDefinition,特化自 CaseDefinition

分析用例是为评估备选方案、计算结果而设计的用例。与描述场景的用例不同,分析用例会产出一个返回值 — 即分析评估的结果。分析用例是模块 7 中权衡研究的基础。

定义分析用例

1analysis def EvaluateSensorConfig {
2 subject vehicle : Vehicle;
3
4 objective {
5 doc /* Determine which sensor configuration provides
6 the best detection range at acceptable cost. */
7 }
8
9 in attribute sensorOptions : SensorConfig[1..*];
10 in attribute costLimit : Real;
11
12 // The return attribute holds the analysis result
13 return attribute bestConfig : SensorConfig;
14}

带评估逻辑的分析用例

分析用例的主体可以包含执行计算、筛选备选方案并产出返回值的动作:

1analysis def CompareBrakingSystems {
2 subject vehicle : Vehicle;
3
4 objective {
5 doc /* Select braking system with shortest stopping
6 distance under standard test conditions. */
7 }
8
9 in attribute candidates : BrakingSystem[1..*];
10 return attribute selectedBrake : BrakingSystem;
11
12 action computeStoppingDistance {
13 in attribute brake : BrakingSystem;
14 out attribute distance : ISQ::LengthValue;
15 }
16
17 action rankCandidates {
18 in attribute results : ISQ::LengthValue[1..*];
19 out attribute winner : BrakingSystem;
20 }
21}
类比

分析用例就像一次科学实验:有假说(目标)、受控的输入(参数)、实验步骤(动作)和结论(返回值)。用例把这些打包成一个可复用的元素,供权衡研究调用。

提示

分析用例参数上的 return 关键字,正是它区别于普通用例之处。返回属性是计算得到的结果,供下游的权衡研究或决策使用。没有 return,你写的就是一个用例;有了它,才是分析用例。

5

验证用例

KerML 起源:verification defVerificationCaseDefinition,特化自 CaseDefinition

验证用例描述一套用于检查系统是否满足某条特定需求的过程。它通过 verify requirement 语句,把行为世界(测试流程、仿真)与需求世界(模块 5)连接起来。

定义验证用例

1requirement def BrakingDistanceReq {
2 doc /* The vehicle shall stop within 40 metres
3 from 100 km/h on dry pavement. */
4 subject vehicle : Vehicle;
5 require constraint {
6 vehicle.stoppingDistance <= 40 [m]
7 }
8}
9
10verification def VerifyBrakingDistance {
11 subject vehicle : Vehicle;
12
13 objective {
14 verify requirement brakingReq : BrakingDistanceReq;
15 }
16
17 action accelerateTo100;
18 action applyFullBrake;
19 action measureDistance;
20
21 first accelerateTo100 then applyFullBrake;
22 first applyFullBrake then measureDistance;
23}

verify requirement 语句

写在验证用例 objective 内的 verify requirement 语句,在验证过程与被测需求之间建立形式化链接。这正是 SysML v2 用于把 V&V 活动追溯回需求的机制:

1requirement def MaxLatencyReq {
2 subject sys : AutonomousSystem;
3 require constraint { sys.responseTime <= 0.1 [s] }
4}
5
6verification def VerifyLatency {
7 subject sys : AutonomousSystem;
8
9 objective {
10 verify requirement latencyReq : MaxLatencyReq;
11 }
12
13 action injectStimulus;
14 action measureResponse;
15 action compareToThreshold;
16
17 first injectStimulus then measureResponse;
18 first measureResponse then compareToThreshold;
19}
常见误区

验证用例并不会自动运行测试。它只是描述测试流程并把它连到需求上。执行由工具负责(仿真、测试台架)。模型刻画的是「做什么」和「为什么」,工具提供的是「怎么做」。

说明

一个验证用例可以在其目标中列出多条 verify requirement 语句,从而验证多条需求。反过来,同一条需求也可以被多个验证用例引用,使同一需求可以采用不同的验证方法(测试、分析、检查、演示)。

6

完整示例

下面的模型为一个自动驾驶车辆系统综合运用了三类用例:描述驾驶场景的用例、评估传感器的分析用例,以及校验制动合规性的验证用例。

1package AutonomousVehicleCases {
2 private import ISQ::*;
3 private import SI::*;
4 private import ScalarValues::*;
5
6 // ── Part definitions ─────────────────────────────────────
7 part def Vehicle {
8 attribute stoppingDistance : ISQ::LengthValue;
9 attribute responseTime : ISQ::TimeValue;
10 part sensorSuite : SensorSuite;
11 part brakingSystem : BrakingSystem;
12 }
13
14 part def Driver;
15 part def TrafficAuthority;
16 part def SensorSuite {
17 attribute detectionRange : ISQ::LengthValue;
18 attribute cost : Real;
19 }
20 part def BrakingSystem {
21 attribute maxDeceleration : ISQ::AccelerationValue;
22 }
23 part def SensorConfig {
24 attribute lidarCount : Integer;
25 attribute cameraCount : Integer;
26 attribute totalCost : Real;
27 attribute effectiveRange : ISQ::LengthValue;
28 }
29
30 // ── Requirements ─────────────────────────────────────────
31 requirement def BrakingDistanceReq {
32 doc /* Vehicle shall stop within 40 m from 100 km/h
33 on dry pavement. */
34 subject vehicle : Vehicle;
35 require constraint {
36 vehicle.stoppingDistance <= 40 [m]
37 }
38 }
39
40 requirement def SensorRangeReq {
41 doc /* Sensor suite shall detect obstacles at 200 m. */
42 subject vehicle : Vehicle;
43 require constraint {
44 vehicle.sensorSuite.detectionRange >= 200 [m]
45 }
46 }
47
48 // ── Use Case: Drive to Destination ───────────────────────
49 use case def DriveToDestination {
50 subject vehicle : Vehicle;
51 actor driver : Driver;
52 actor traffic : TrafficAuthority;
53
54 objective {
55 doc /* Vehicle arrives at destination safely
56 and within estimated time. */
57 }
58
59 action enterDestination;
60 action planRoute;
61 action engageAutopilot;
62 action monitorTraffic;
63 action arriveAtDestination;
64
65 first enterDestination then planRoute;
66 first planRoute then engageAutopilot;
67 first engageAutopilot then monitorTraffic;
68 first monitorTraffic then arriveAtDestination;
69 }
70
71 // ── Use Case: Emergency Stop ─────────────────────────────
72 use case def EmergencyStop {
73 subject vehicle : Vehicle;
74 actor driver : Driver;
75
76 objective {
77 doc /* Vehicle comes to a complete stop as
78 quickly as safely possible. */
79 }
80
81 action detectObstacle;
82 action activateEmergencyBrake;
83 action notifyDriver;
84 action recordIncident;
85
86 first detectObstacle then activateEmergencyBrake;
87 first activateEmergencyBrake then notifyDriver;
88 first notifyDriver then recordIncident;
89 }
90
91 // ── Analysis Case: Evaluate Sensor Configurations ────────
92 analysis def EvaluateSensorConfig {
93 subject vehicle : Vehicle;
94
95 objective {
96 doc /* Determine the sensor configuration that
97 maximises detection range within budget. */
98 }
99
100 in attribute configs : SensorConfig[1..*];
101 in attribute budget : Real;
102 return attribute bestConfig : SensorConfig;
103
104 action filterByBudget {
105 in attribute allConfigs : SensorConfig[1..*];
106 in attribute maxCost : Real;
107 out attribute affordable : SensorConfig[1..*];
108 }
109
110 action rankByRange {
111 in attribute candidates : SensorConfig[1..*];
112 out attribute ranked : SensorConfig;
113 }
114
115 flow filterByBudget.affordable to rankByRange.candidates;
116 first filterByBudget then rankByRange;
117 }
118
119 // ── Verification Case: Braking Distance ──────────────────
120 verification def VerifyBrakingDistance {
121 subject vehicle : Vehicle;
122
123 objective {
124 verify requirement : BrakingDistanceReq;
125 }
126
127 action accelerateTo100 {
128 doc /* Bring vehicle to 100 km/h on dry test track. */
129 }
130
131 action applyFullBrake {
132 doc /* Apply maximum braking force. */
133 }
134
135 action measureStoppingDistance {
136 out attribute measuredDistance : ISQ::LengthValue;
137 }
138
139 action evaluateResult {
140 in attribute distance : ISQ::LengthValue;
141 doc /* Compare measured distance against 40 m limit. */
142 }
143
144 flow measureStoppingDistance.measuredDistance
145 to evaluateResult.distance;
146
147 first accelerateTo100 then applyFullBrake;
148 first applyFullBrake then measureStoppingDistance;
149 first measureStoppingDistance then evaluateResult;
150 }
151
152 // ── Verification Case: Sensor Range ──────────────────────
153 verification def VerifySensorRange {
154 subject vehicle : Vehicle;
155
156 objective {
157 verify requirement : SensorRangeReq;
158 }
159
160 action placeTargetAt200m;
161 action runDetectionSweep;
162 action confirmDetection;
163
164 first placeTargetAt200m then runDetectionSweep;
165 first runDetectionSweep then confirmDetection;
166 }
167}

该模型演示了:带参与者、主题与目标的用例定义(驾驶场景);带输入参数、返回值与评估动作的分析用例(传感器选型);以及用 verify requirement 把测试流程连到形式化需求的验证用例。三类用例共享同一 CaseDefinition 血统,并都用带后继关系的动作来描述流程。

7

模块总结

SysML v2 概念KerML 起源关键规则
use case defUseCaseDefinitionCaseDefinitionBehavior围绕用户目标描述系统场景;具有参与者、主题与目标
actorActorMembershipPartDefinition与主题交互的外部实体;基于角色,而非某种特殊分类器
subjectSubjectMembership标识该用例所描述的系统或组件;每个用例一个
objective通过 ObjectiveMembershipRequirementUsage陈述该用例意图达成的目标;是对结果的一条需求
include use caseIncludeUseCaseUsage通过把另一个用例作为子步骤调用来组合用例
analysis defAnalysisCaseDefinitionCaseDefinition评估备选方案;用 return 属性承载计算结果
return 属性AnalysisCaseDefinition 上的有向 Feature分析评估的输出;供权衡研究使用
verification defVerificationCaseDefinitionCaseDefinition描述一套 V&V 流程;通过 verify requirement 连到需求
verify requirementRequirementVerificationMembership验证用例与其所测需求之间的形式化链接
表 — 模块 6 的概念及其 KerML 起源
下一模块

模块 7 — 分析与权衡 — 如何组织权衡研究、用分析用例评估设计备选方案,并借助目标函数选出最优配置。