Changes in Practice

Here we demonstrate how syside.preview functionalities could be used in practice. This is meant to give an idea of what was the goal behind the changes as well as show some practical applications.

Loading a package

The proposed changes allow for a more straightforward way of loading a package that does not involve manual locking of the document for multi-threading. It also allows for a slightly more intuitive way of checking for errors in the model by hiding the diagnostic messages.

import pathlib

import syside

EXAMPLE_DIR = pathlib.Path(__file__).parent
MODEL_FILE_PATH = EXAMPLE_DIR / "example_model.sysml"


def main() -> None:
    (model, diagnostics) = syside.load_model(paths=[MODEL_FILE_PATH])

    # Only errors cause an exception. Syside may also report warnings and
    # informational messages, but not for this example. As a result, manually
    # checking for warnings and informational messages is required.
    assert list(diagnostics.all) == []

    for doc in model.user_docs:
        # Since Syside is a multi-threaded application, we need to lock the
        # document to ensure that the document is not modified from another
        # thread while we are accessing it.
        with doc.lock() as locked:
            print("Model sexp:")
            print(syside.sexp(locked.root_node))


if __name__ == "__main__":
    main()
import pathlib

import syside.preview as syside

EXAMPLE_DIR = pathlib.Path(__file__).parent
MODEL_FILE_PATH = EXAMPLE_DIR / "example_model.sysml"


def main() -> None:
    with syside.open_model(
        paths=[MODEL_FILE_PATH], warnings_as_errors=True
    ) as model:
        # Either get by package name
        variant_1 = model.lookup("Car")
        assert variant_1 is not None
        # Or fetch by filename
        variant_2 = list(model.top_elements_from(MODEL_FILE_PATH))

        # Check that both variants are the same
        assert [variant_1] == variant_2

        print(syside.sexp(variant_1))


if __name__ == "__main__":
    main()

Creating a package

Currently, creating a new SysML v2 package is more complicated than it should be. The following example demonstrates how to create a new package called “Requirements” and how easier it is to do so in syside.preview.

import syside


def main() -> None:
    # Create an empty model (containing only standard library)
    model_document = syside.Document.create_st(
        syside.DocumentOptions(
            url=syside.Url("memory://requirements.sysml"), language="sysml"
        )
    )

    # Since Syside is a multi-threaded application, we need to lock the
    # document to ensure that the document is not modified from another
    # thread while we are accessing or modifying it.
    with model_document.lock() as model:
        # The document corresponds to a single root namespace.
        root_namespace = model.root_node

        # Add an owned requirements package to the root namespace. The
        # returned pair holds the membership and the package it owns.
        requirements_package = root_namespace.children.append(
            syside.OwningMembership, syside.Package
        )[1]
        requirements_package.declared_name = "Requirements"

        print(requirements_package.declared_name)


if __name__ == "__main__":
    main()
import syside.preview as syside


def main() -> None:
    # Create an empty model (containing only standard library)
    with syside.empty_model() as model:
        # Add an owned requirements package to the root namespace.
        requirements_package = model.new_top_level_package("Requirements")

        print(requirements_package.declared_name)


if __name__ == "__main__":
    main()