SysML v2 Deep Dive 8 min read

Lesson 19: Packages, namespaces and imports; dependencies you can actually see

Packages, namespaces and imports: dependencies you can actually see

SysML v1 packages were not just folders. They were real UML namespaces. You could import a whole package or a single element, mark an import public or private, rename what you imported, and use qualified names. OMG's v1-to-v2 transformation spec maps each of those straight across to v2. Nothing was missing.

Most teams just didn't use them. In a graphical tool, an import was a dialog box and a dashed arrow on a package diagram someone had to draw and keep current. Dragging the element you needed into the model tree was easier.

SysML v2 puts every import at the top of the file. The team can read it, review it in a pull request and see it change in a diff. Same idea as v1, much better odds that anyone maintains it. That matters on a program with a shared physics library, a domain library and a model-year project, where one import decision either keeps those layers apart or quietly ties them together.

The concept: who can see what?

A package is a namespace. Imports decide which names from other packages are visible inside it, and to whom.

  • private import makes names visible inside this package only.
  • public import makes them visible here and re-exports them to anyone who imports this package.
  • ::* imports the direct members of a package. ::** imports the package and everything nested inside it.
  • alias X for Y; gives an element a second name in this package.
  • library package marks a package as a reusable library, a real declaration and not a comment.

The code: a three-tier vehicle model

Here is a physics library, an automotive library built on it, and a project built on that.

library package PhysicsLib {
    attribute def Stiffness :> ISQ::ForceValue;

    part def Spring {
        attribute rate : Stiffness;
    }
}

package AutomotiveLib {
    private import PhysicsLib::*;

    part def Suspension {
        part spring : Spring;
    }
}

package ModelYear2027 {
    private import AutomotiveLib::*;

    part def Vehicle {
        part frontSuspension : Suspension;
    }
}

Stiffness is simplified to a force value here to keep the example short.

ISQ::ForceValue resolves without any import at all. Every model sits inside one global namespace, which always contains the KerML and SysML model libraries, plus any domain libraries your tool provides. ISQ comes from the Quantities and Units domain library. When it's available, its top-level packages can be named from anywhere by qualified name.

AutomotiveLib imports PhysicsLib privately. That lets Suspension use Spring, but it doesn't pass Spring on to anyone who imports AutomotiveLib.

So ModelYear2027 sees Suspension, the concept the automotive library means to offer. It doesn't see Spring. If the project really needs the physics definition, it imports PhysicsLib itself, and that extra line makes the dependency visible.

Now change one word in AutomotiveLib:

public import PhysicsLib::*;

The automotive library now re-exports everything it imported. ModelYear2027 can use Spring, Stiffness and every other public PhysicsLib member without ever asking for them.

Here's why that matters. Two years in, the program swaps PhysicsLib for a supplier library. With a private import, only AutomotiveLib changes, and Suspension stays the same for everyone downstream. With a public import, any project that quietly came to depend on Spring breaks, and you find out which ones by breaking them.

There's a middle path between re-exporting everything and re-exporting nothing. Import single elements, and make public only the ones downstream projects are meant to use:

package AutomotiveLib {
    private import PhysicsLib::Spring;
    public import PhysicsLib::Stiffness;

    part def Suspension {
        part spring : Spring;
    }
}

A public membership import re-exports exactly one name. ModelYear2027 can now type an attribute with Stiffness, but it still can't see Spring. An alias does the same job under a name you choose. alias SpringRate for PhysicsLib::Stiffness; is public by default, so importers of AutomotiveLib see SpringRate. The library's public surface is now something you wrote down on purpose.

You can still write PhysicsLib::Spring if you need it. A private import doesn't hide a top-level package. The difference is that the dependency is now a line in your file that a reviewer can question.

Member visibility is a separate lever. Declare private attribute def DampingTable; inside PhysicsLib, and no qualified name from outside reaches it. PhysicsLib::DampingTable simply doesn't resolve, because qualified names only see public members.

There is one escape hatch, import all, which imports memberships regardless of their declared visibility. It's in the grammar, but use it knowingly. It undoes exactly the encapsulation described above.

Write private or public on every import. The grammar requires a visibility keyword, and the standard libraries spell it out every time.

One more visibility rule is worth knowing. Inside a definition or usage there's a third option, protected. A protected import is private to the outside world, but every specialization inherits it:

part def Suspension {
    protected import PhysicsLib::*;
    part spring : Spring;
}

part def SportSuspension :> Suspension {
    attribute stiffRate : Stiffness;
}

SportSuspension can use Stiffness without importing PhysicsLib itself. Code that only uses Suspension from outside sees none of those names. In a package, protected means the same as private.

Deep dive: four things worth knowing

A local name silently beats an imported one

Say a team imports a standard Temperature with a wildcard, then someone defines a local Temperature for sensor data. Which one does Temperature mean?

The language has a defined answer, and it's the local one. KerML says an imported name that clashes with an owned name is hidden. It isn't an error, and nothing gets reported. A sensor reading you meant to type with the standard quantity ends up typed by a homemade structure holding a Real. Your unit checks stop applying, and nothing tells you.

The fix is at the import line. Drop the wildcard import, and add an alias if you want a readable name for the standard type:

package Standards {
    attribute def Temperature :> ISQ::TemperatureValue;
}

package SensorPackage {
    private import ScalarValues::*;
    // no wildcard import of Standards - that is what would create the collision

    attribute def Temperature {
        attribute celsius : Real;
        attribute sensorId : String;
    }

    alias StdTemperature for Standards::Temperature;

    part def Sensor {
        attribute reading : StdTemperature;
    }
}

Here Temperature means the local sensor-data definition, and StdTemperature names the standard one. Nothing is hidden, and a reader can see which is which.

Two imports can collide as well. KerML hides an imported name if it clashes with an owned name, or with a visible name from another import. So if two wildcard imports both bring in a Temperature, the rule hides both. That isn't an error either. An unqualified Temperature then resolves in an enclosing namespace, if one has it, or fails to resolve. Either way, the wildcards made the decision, not you.

Definitions add one more layer. Inside a part def, an inherited feature hides an imported name in the same way an owned member does.

::, ::** and ::::**

::* imports only the direct members of a package. ::** imports the package itself plus everything nested inside it. ::*::** is also recursive but leaves out the package itself.

UML had no recursive import, so you imported package by package. The recursive forms are what make whole-model filters practical, as the last post showed. For ordinary dependencies they're usually too broad. Import what you need.

Recursion follows ownership, not imports. ::** descends into the packages and other namespaces that the imported package owns. It doesn't follow into a package that one of them merely imports, and it skips memberships inherited through implicit specialization.

library package is a real declaration

The library keyword tells tools that everything inside the package, at any depth, is a library element. The ISQ and SI packages use the stricter form, standard library package, which marks them as part of the standard. Your own libraries use library package.

SysML v1 had the «ModelLibrary» stereotype for the same purpose. In v2 it's part of the declaration, so it can't be forgotten or detached.

Package structure decides who can work at once

Many teams keep one package per file. That's a convention, not a language rule, but it pays off. Two engineers editing two package files rarely conflict. Two engineers editing the same file might.

So draw package boundaries along the lines your teams actually own, such as subsystem, discipline or supplier. Put a whole vehicle in one package and you've rebuilt the check-out queue you were trying to escape.

When an outside system hands you awkward identifiers, use unrestricted names. In part def <'FT-100'> 'Fuel Tank Assembly';, the short name is the stable ID your PLM tool knows, and the quoted name is what engineers read.

The short name is a real name for resolution, not just a label. A reference can use either one: 'FT-100' and 'Fuel Tank Assembly' resolve to the same element.

V1 vs. v2 packages and imports cheat sheet

ConceptSysML v1SysML v2
Package importPackageImport, created in a dialogprivate import P::*; at the top of the file
VisibilityPublic and private imports, also existedSame, written on every import
Recursive importNone, import package by package::** and ::*::**
Single-element importElementImport, with rename on importMembership import, rename with alias X for Y;
Name clashesUML rule, an owned name hides a clashing importSame rule, defined in KerML
Library marking«ModelLibrary» stereotypelibrary package
Reviewing dependenciesClick through packages one at a timeRead the import block, diff it

Next Lesson

Occurrences and the 4D model. Parts, actions and states turn out to be the same kind of thing underneath, something that exists in space and time. That idea explains why a state can be an action and why an action has a duration you can constrain.

SysML v2

SysModeler Team

Engineering & Product