Holarch
课程章节

培训 · 课程 9. SysML v2 基础 · 第 9.1 章,共 8 章

9.1 定义和属性 4:33

用 SysML v2 文本编写值类型和组件,并为数值附带单位。

演示 Holarch 2.22。

所有章节 · 9.2 端口和接口 →

您将学到

  • 在编辑器中编写枚举、值类型和抽象部件定义
  • 使用 `:>` 特化定义,并使用 `redefines` 为继承的属性赋予带单位的值
  • 找到并修正检查器标出下划线的单位错误
  • 使用 插入 ▾ 和自动补全编写定义,并在标准库中查找类型
  • 按 F2 在一个撤销步骤中全局重命名元素

在 Holarch 的编辑器中编写 SysML v2 定义和属性,基于一个小型 Hybrid Car 项目,依照 SysML v2 教程“定义和属性”:一个枚举、一个特化 ScalarValue 的值类型、一个抽象组件及其带单位数值的特化、检查器发现的单位错误、自动补全和标准库、插入,以及使用 F2 重命名。SysML 是 Object Management Group, Inc. 的注册商标。

项目文件

  • 完成项目 (.holarch.json;管理项目 → 项目文件 ▾ → 导入项目 (.json)…)

视频从 holarch.app 播放;参见隐私政策。

练习

在 Holarch 中基于 Hybrid Car 演示项目 (SysML v2) 亲自完成此操作。

  1. 创建 Hybrid Car 演示项目:管理项目 → 项目文件 ▾ → 新建演示项目 → Hybrid Car (SysML v2)。打开 模型 → 编辑器 和包 Vehicles。
  2. 在 enum def FuelKind 下方添加 enum def CoolingMode { enum passive; enum active; }。
  3. 在 part def Body 下方选择 插入 ▾ → 部件定义,并将 part def Radiator :> Component 补充完整,包含 attribute redefines mass = 8 [kg];、attribute coolantVolume : VolumeValue;(输入 Volu 并按 Enter 从补全列表中接受 VolumeValue)和 attribute mode : CoolingMode;。
  4. 将质量改为 8 [W],将指针指向下划线阅读消息,然后将其改回 8 [kg]。
  5. 在 part def Powertrain 中添加 part radiator : Radiator;。然后将光标放在 coolantVolume 上,按 F2 并将其重命名为 coolantCapacity。
  6. 如需带检查的分步演练,请运行 SysML v2 教程“定义和属性”(帮助 → 教程)。

当编辑器下方的状态报告没有问题,且浏览器在 Vehicles 下显示带有 coolantCapacity 和 mode 的 Radiator 时,即完成练习。

自测

  1. 在 part def Battery :> Component { attribute redefines mass = 45 [kg]; } 中,:> 和 redefines 各起什么作用?
  2. 为什么 Component 被声明为 abstract part def?
  3. 为什么 attribute redefines power = 60 [kg]; 会报错?
  4. doc /* … */ 与 // 行有什么区别?
显示答案
  1. 在 part def Battery :> Component { attribute redefines mass = 45 [kg]; } 中,:> 和 redefines 各起什么作用?

    :> 使 Battery 成为 Component 的特化,因此它继承 mass、partNumber 和 power。redefines 用 Battery 自己的特征替换继承的 mass,这里绑定为 45 kg。

  2. 为什么 Component 被声明为 abstract part def?

    不存在泛指的组件,只有各种具体的组件。抽象定义汇集了所有组件的共同之处,只有其特化(例如 Battery 或 Engine)才描述实际的部件。

  3. 为什么 attribute redefines power = 60 [kg]; 会报错?

    power 的类型为 PowerValue,而千克是质量单位。检查器会对单位进行求值,因此会在该数值下标出下划线,直到单位改为功率单位(例如 [W])为止。

  4. doc /* … */ 与 // 行有什么区别?

    doc 注释是属于该元素的文档,是模型的一部分。// 注释只保留在文本中,不属于模型。

延伸阅读

文字稿

显示文字稿 (旁白和屏幕内容)

Introduction

Welcome to SysML v2 Fundamentals. This track teaches the SysML v2 textual notation and views in Holarch, one task per chapter, on the starter projects of Holarch's SysML v2 tutorials, so you can follow along step by step.

In this chapter you write definitions and attributes: an enumeration, a value type, an abstract component and the components that specialize it, with values in units.

Start the tutorial

In Manage Projects, open Project Files and choose Start a Tutorial. Under the tutorials for SysML v2 projects, click Start on Definitions and Attributes. Holarch creates a small Hybrid Car project and opens the step panel, which checks each step as you go.

On screen: Project Files ▾ → Start a Tutorial… → SysML v2: Definitions and Attributes → Start; the step panel opens.

Enumerations and value types

Open Model, then Editor. The package Vehicles holds one part definition, Car, with two attributes. Every declaration is a keyword and a name, then a body in braces, or a semicolon when there is no body.

On screen: Model → Editor: Vehicles; outline part def Car.

An enumeration is a value type with a fixed set of values. Above the car, write the drive mode: economy, normal or sport.

On screen: Caret after the imports; type enum def DriveMode.

Next, a value type of your own. Tire pressure specializes Scalar Value from the standard library. The short symbol after the name means specializes.

On screen: Below DriveMode; type attribute def TirePressure :> ScalarValue;

Specialization and units

Now an abstract component. There is no component as such, only kinds of components, and every one has a mass, a part number and a power. The doc comment is part of the model. Two slashes start a note that stays in the text only.

On screen: Below TirePressure; type abstract part def Component.

The battery is a component, so it has a mass, a part number and a power of its own. Write it with a mistake on purpose: a power in kilograms.

On screen: Below Component; type part def Battery :> Component with power = 60 [kg].

The checker underlines the value, and the status under the editor counts the error. Point at the underline to read the message.

On screen: Problems count; the information card on 60 [kg].

Change kilograms to watts, and the error clears. Redefines gives each inherited attribute the battery's own value, with its unit in square brackets.

On screen: kg → W; No problems, Applied.

Completion and the Standard Library

Completion knows the model and the library. Inside the battery, type attribute capacity, a colon, and the start of Energy Value. Press Enter to take it from the list, and end the line with a semicolon.

On screen: Inside Battery: attribute capacity : Ener → completion → Enter → ;

Point at Energy Value. The card shows that it comes from the I S Q package of the standard library.

On screen: The information card on EnergyValue.

The Standard Library page lists every package and definition of that library. Open Settings, then Standard Library, and search for Energy Value.

On screen: Settings → Standard Library; Find EnergyValue.

Insert

Back in the Editor, Insert writes a construct for you. Place the cursor below the battery, open Insert, and choose Part Definition. Type Engine, and press Tab to move into its body.

On screen: Model → Editor; caret below Battery; Insert ▾ (open on camera) → Part Definition; Engine; Tab.

The engine is a component too. Give it its mass and a number of cylinders, and make it specialize Component.

On screen: Body: mass 110 kg, cylinders : Integer; then :> Component after the name.

Usages and rename

Now use the definitions. Inside the car, add a drive mode, a battery and an engine. Each usage is typed by a definition after the colon, the drive mode by the enumeration you wrote first.

On screen: In Car after curbMass: attribute mode : DriveMode; part battery : Battery; part engine : Engine;

To rename a name everywhere it is used, place the cursor on cylinders and press F2. Enter cylinder count and confirm. Every reference to it changes, in one undo step.

On screen: Caret in cylinders; F2; New name cylinderCount; OK.

Every step is ticked off. Click Mark as Done to finish the tutorial.

On screen: Step panel → Mark as Done.

Recap

Let's recap. You started a SysML v2 tutorial, wrote an enumeration and a value type, an abstract component with specializations and values in units, let the checker find a wrong unit, and used completion, the Standard Library, Insert and rename.

Next, ports and interfaces connect these components.