Lesson 18: Semantic metadata, keywords and filter; your model starts answering questions
Semantic metadata, keywords and filter: your model starts answering questions
SysML v1 stereotypes were capable. They could carry properties, constraints and inheritance. The trouble was that one profile mechanism had to cover both simple labels and real domain concepts. And finding tagged elements across a model usually meant writing OCL or a tool-specific script.
SysML v2 splits those jobs. Plain metadata records a fact about an element. Semantic metadata gives a tag a modeling consequence, so the tagged element becomes a kind of something. A filter then builds a package from whatever the model holds right now.
Picture a launch-control model. The flight director needs commands like abort and hold to stand apart from every other part. Those commands need their own properties. And the safety team needs a list of critical items that nobody has to keep up to date by hand. This post covers all three.
The concept: how does a tag become a type?
Semantic metadata is metadata with a base type. Anything you tag with it specializes that base type.
SemanticMetadatais the library definition you specialize to make semantic metadata.baseTypenames what every tagged element should specialize.- A short name like
<cmd>turns the metadata into a user keyword,#cmd. annotatedElementlimits where the keyword is legal, as in the last post.filtergives a package a Boolean condition. Only imported members that pass it get in.
The code: a small command language
Here is a command language for the launch-control model, and two commands written in it.
package CommandDSL {
private import ScalarValues::*;
private import Metaobjects::SemanticMetadata;
part def Command {
attribute issuedBy : String;
attribute priority : Integer;
}
part commands : Command[*] nonunique;
metadata def <cmd> CommandMetadata :> SemanticMetadata {
:> annotatedElement : SysML::PartUsage;
:>> baseType = commands meta SysML::Usage;
}
}
package MissionControl {
private import CommandDSL::*;
#cmd part abortSequence {
:>> issuedBy = "Flight Director";
:>> priority = 1;
}
#cmd part holdCountdown {
:>> issuedBy = "Launch Conductor";
:>> priority = 3;
}
}Command defines what every command has, an issuer and a priority. commands is a usage typed by Command, so it carries both.
CommandMetadata specializes SemanticMetadata. Its short name, cmd, creates the #cmd keyword. The annotatedElement line makes the keyword legal only on part usages. Change it to SysML::ActionUsage and #cmd becomes a keyword for actions instead.
The baseType line is the one everyone squints at. meta SysML::Usage is a cast. It turns the commands usage into the kind of value baseType expects. That's all it does.
Here is the payoff. #cmd part abortSequence doesn't just label the part as a command. Because both are usages, abortSequence subsets commands. It inherits issuedBy and priority. Try to redefine an attribute that Command doesn't have, and the tool rejects it.
What baseType implies depends on what you tag. The specification spells out the cases:
- Tag a usage, with a usage as
baseType, and the tagged usage subsets it. That'sabortSequenceandcommands. - Tag a definition, with a usage as
baseType, and the tagged definition subclassifies the definitions of that usage. Here, that meansCommand. - Tag a definition, with a definition as
baseType, and the tagged definition subclassifies it directly. - Any other combination adds no implicit specialization.
Right now #cmd only works on part usages, so it can't mark a whole family of commands. annotatedElement can be subset more than once, and an element is legal if it matches any one of them. So allow part definitions as well:
metadata def <cmd> CommandMetadata :> SemanticMetadata {
:> annotatedElement : SysML::PartDefinition;
:> annotatedElement : SysML::PartUsage;
:>> baseType = commands meta SysML::Usage;
}
// Subclassifies Command, so it has issuedBy and priority
#cmd part def EmergencyCommand;It's the same keyword with a different relationship, picked by the rules above. abortSequence subsets commands. EmergencyCommand subclassifies Command.
A declaration can also carry more than one keyword, and a keyword can be a qualified name. Plain and semantic metadata mix freely:
package VehicleModel {
private import Metaobjects::SemanticMetadata;
part def Check;
part checks : Check[*] nonunique;
metadata def Verified :> SemanticMetadata {
:> annotatedElement : SysML::PartDefinition;
:> annotatedElement : SysML::PartUsage;
:>> baseType = checks meta SysML::Usage;
}
}
#VehicleModel::Verified #cmd part abortSequence {
:>> issuedBy = "Flight Director";
:>> priority = 1;
}The # form has no body, so it can't fill in attribute values. It suits flags like Verified, and keywords like #cmd whose baseType is already bound in the definition.
One more form comes from the specification's own examples. A semantic keyword can replace the language keyword entirely, so #situation def Failure; declares a definition that subclassifies Situation. That form declares a generic definition or usage, not a part. So #cmd as written would reject #cmd abortSequence. If you want the short form, leave annotatedElement unrestricted or allow SysML::Usage.
You just built a small domain-specific language. There's no profile and no metamodel extension, just a package your team imports. After that, they write #cmd as if it shipped with SysML.
This isn't a side feature. In the Requirement Derivation library, #derivation marks a connection as a derivation, and #original and #derive mark its ends. #causation in the Cause and Effect library works the same way. Here's the derivation example from the specification:
private import RequirementDerivation::*;
#derivation connection {
end #original ::> vehicleSpecification.vehicleMassRequirement;
end #derive ::> engineSpecification.engineMassRequirement;
}Your #cmd is built exactly like those.
Packages that fill themselves
Now the safety team wants every safety-critical element in the vehicle model, in one place.
package SafetyCriticalItems {
private import VehicleModel::**;
filter @VehicleModel::SafetyCritical;
}The import pulls in the whole model. The filter keeps only the members tagged SafetyCritical. When an engineer tags a new part, it shows up in the package. When someone removes the tag, it drops out. Nobody maintains a list.
A filter is a Boolean expression, so it can combine conditions:
package UnverifiedCriticalItems {
private import VehicleModel::**;
filter @VehicleModel::SafetyCritical and not @VehicleModel::Verified;
}This package answers a real question. Which critical items haven't been verified yet? The answer lives in the model, not in a report that goes stale after the next change.
A filter can also test the values a tag holds, not just whether the tag is there. If your safety metadata records a severity, one filter can pick out only the catastrophic items. Write the rule once, and every package that uses it agrees.
Here's what that looks like. Give the metadata an attribute, set it where you tag, and read it in the filter:
package VehicleModel {
enum def Severity { catastrophic; hazardous; major; minor; }
metadata def SafetyCritical {
attribute severity : Severity;
}
part brakeController {
@SafetyCritical { severity = Severity::catastrophic; }
}
}
package CatastrophicItems {
private import VehicleModel::**;
filter @VehicleModel::SafetyCritical and
VehicleModel::SafetyCritical::severity == VehicleModel::Severity::catastrophic;
}The first condition checks that the tag is there. The second reads the value that tag holds on each candidate element. The specification uses the same pattern to select designs approved above a given level.
A filter can also test what kind of element it's looking at. Any element can be tested against its SysML abstract-syntax metaclass, as if it carried that as metadata:
package CriticalParts {
private import VehicleModel::**;
filter @SysML::PartUsage and @VehicleModel::SafetyCritical;
}Critical parts get in. Critical requirements, ports and actions don't.
There's a limit on what a filter can say. It must be a model-level evaluable expression: literals, feature references and a fixed set of library functions, such as the comparisons, and, or, not, @ and meta. It can't range over instances with all, and it can't call arbitrary functions. The restriction exists so the expression can be evaluated on the model itself.
The same filter keyword works in views. Put the condition on a view definition, and every view of that kind applies it to whatever it exposes:
view def SafetyView {
filter @VehicleModel::SafetyCritical;
}
view criticalItems : SafetyView {
expose VehicleModel::**;
}The package gives you a named subset of the model. The view gives you something to render from it.
There's a bigger idea underneath. A filter is a query that lives in the model. The Systems Modeling API, the standard that ships with SysML v2, exposes model queries over HTTP. So the same question can feed a dashboard, a CI check or generated safety evidence. We'll build that out in a later post.
Deep dive: three things worth getting right
Filters apply to imports, not owned members
A filter decides what an import brings in. It doesn't remove anything the package owns directly. So keep filtered packages thin. They should own an import and a filter, not a second copy of the model.
A package filter also applies to every import in that package. That's why the examples write VehicleModel::SafetyCritical in full. A short name brought in by its own import would have to pass the same filter, and it wouldn't.
::, ::** and ::::** bring in different things
Take a small package with one level of nesting:
package VehicleModel {
part def Vehicle;
package Braking {
part brakeController;
}
}import VehicleModel::*; brings in only the direct members, Vehicle and Braking. brakeController sits inside Braking, so it doesn't come across.
import VehicleModel::**; is recursive. It brings in VehicleModel itself, plus Vehicle, Braking and brakeController.
import VehicleModel::*::**; is also recursive, but it leaves out the package itself. You get Vehicle, Braking and brakeController.
That's why the filter examples use ::**. With a single star, a safety-critical part inside Braking never reaches the filter, and the package quietly misses it. Plenty of people have wondered why their safety package found only four elements. This is usually why.
Semantic and plain metadata do different jobs
Use semantic metadata when the tag should create a domain category, with a shared type and shared properties. A command is a kind of thing in the launch-control domain. It earns a Command definition and a keyword.
Use plain metadata when you're recording a fact, such as a review state or an export designation. SafetyCritical says something about an element. It doesn't turn the element into a different kind of part.
A quick test helps. If the tagged elements should inherit features from a common parent, use semantic metadata. If you only want to label them and find them later, use plain metadata. A filter can find both.
V1 vs. v2 semantic metadata cheat sheet
| Concept | SysML v1 | SysML v2 |
|---|---|---|
| Domain concept | Stereotype | metadata def :> SemanticMetadata |
| Showing it | «guillemets» on a diagram | #keyword in text and diagrams |
| Typing | Applying a stereotype doesn't make the element specialize anything | baseType makes tagged elements specialize or subset it |
| Finding tagged elements | OCL or a tool script | filter @Tag in a package |
| Keeping the list current | Rerun the query after changes | The package is derived from the current model |
| A domain language | A profile per domain | A package you write and import |
Next Lesson
Packages, namespaces and imports. We've been importing things for three posts and taking it for granted. Next we look at public versus private imports, aliases, and how package structure decides whether two engineers can work on the same model at once.