課程章節
培訓 · 第 9 單元:SysML v2 基礎 · 第 9.1 章,共 8 章
9.1 定義與屬性 4:33
以 SysML v2 文字撰寫數值類型與元件,並為數值加上單位。
展示 Holarch 2.22。
您將學到
- 在編輯器中撰寫列舉、數值類型與抽象零件定義
- 以 `:>` 特化定義,並使用 `redefines` 為繼承的屬性指定帶單位的數值
- 找出並修正檢查器標上底線的單位錯誤
- 以「插入 ▾」與自動完成撰寫定義,並在標準庫中查詢類型
- 以 F2 在一個復原步驟中於每個地方重新命名元素
在 Holarch 的編輯器中撰寫 SysML v2 定義與屬性,依循 SysML v2 教學課程 Definitions and Attributes,以一個小型 Hybrid Car 專案進行:列舉、特化 ScalarValue 的數值類型、抽象元件及其帶單位數值的特化、檢查器找出的單位錯誤、自動完成與標準庫、插入,以及以 F2 重新命名。SysML 是 Object Management Group, Inc. 的註冊商標。
練習
在 Holarch 中於 Hybrid Car 示範專案(SysML v2)上親自操作。
- 建立 Hybrid Car 示範專案:管理專案 → 專案檔案 ▾ → 新增示範專案 → Hybrid Car (SysML v2)。開啟模型 → 編輯器以及套件
Vehicles。 - 在
enum def FuelKind下方新增enum def CoolingMode { enum passive; enum active; }。 - 在
part def Body下方選擇插入 ▾ → 零件定義,並以attribute redefines mass = 8 [kg];、attribute coolantVolume : VolumeValue;(輸入Volu,然後按 Enter 從自動完成清單中接受VolumeValue)與attribute mode : CoolingMode;完成part def Radiator :> Component。 - 將質量改為
8 [W],將指標移到底線上閱讀訊息,然後改回8 [kg]。 - 在
part def Powertrain中新增part radiator : Radiator;。然後將游標放在coolantVolume上,按 F2,將它重新命名為coolantCapacity。 - 若要進行有檢查的逐步練習,請執行 SysML v2 教學課程 Definitions and Attributes(說明 → 教學課程)。
當編輯器下方的狀態回報沒有問題,且「瀏覽器」在 Vehicles 下顯示 Radiator 及其 coolantCapacity 與 mode 時,即完成。
自我檢查
- 在
part def Battery :> Component { attribute redefines mass = 45 [kg]; }中,:>與redefines各自做什麼? - 為什麼 Component 宣告為
abstract part def? - 為什麼
attribute redefines power = 60 [kg];會產生錯誤? doc /* … */與//行有什麼不同?
顯示答案
在
part def Battery :> Component { attribute redefines mass = 45 [kg]; }中,:>與redefines各自做什麼?:>使 Battery 成為 Component 的特化,因此它繼承 mass、partNumber 與 power。redefines以 Battery 自己的特徵取代繼承的mass,此處繫結為 45 kg。為什麼 Component 宣告為
abstract part def?沒有任何東西單純是「元件」,只有各種元件。抽象定義收集每個元件共有的內容,只有其特化(例如 Battery 或 Engine)才描述實際的零件。
為什麼
attribute redefines power = 60 [kg];會產生錯誤?power的類型是PowerValue,而公斤是質量單位。檢查器會評估單位,因此會在數值下標上底線,直到單位改為功率單位,例如[W]。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.