API Reference
The previous section showed examples of what can be achieved using SysIDE Automator. This section describes the Python API provided by SysIDE Automator that enables building such and more complicated workflows.
A typical workflow using SysIDE Automator will consist of some subset of the following steps, each of which is described in more detail in the following sections:
Loading a model from KerML and SysML v2 textual notation files and converting them into abstract syntax.
Querying the abstract syntax of the model.
Modifying the abstract syntax of the model.
Exporting the abstract syntax into textual notation or JSON format.
Loading a Model
In SysIDE, a model is represented using Model
class. It can
be loaded using try_load_model
or
load_model
functions, which take a list of KerML and
SysML v2 files as input and return a Model
instance. The
files can be collected by using collect_files_recursively
function, which is a convenience function that
collects all files in a directory recursively. The key difference between
load_model
and try_load_model
is that the former raises a ModelError
exception if the model contains errors while the latter produces
some model even for files with errors. Both functions return a diagnostics
object containing the errors, warnings, and informational messages
found when loading the model.
Table: Main Functions and Objects
The following table lists the functions and objects related to loading SysML models:
Load a SysMLv2 model. |
|
Load a SysMLv2 model. |
|
Recursively collect all |
|
A SysMLv2 model represented using abstract syntax. |
Table: Diagnostics
Loading a model can result in errors, warnings, and informational messages, which are reported using diagnostics. The following table lists the diagnostic-related objects:
An exception thrown when model contains errors. |
|
All model diagnostics. |
|
A diagnostic providing information about a model. |
|
Table: (Advanced) Pipeline Construction
If load_model
is not flexible enough for your use case,
the following table lists the lower level primitives that can be used to build a custom
model loading pipeline:
Get a default initialized |
|
Standard library environment for use with user models. |
|
Is this a model-created document? |
|
Build the AST for |
|
Collect and cache symbols exported by |
|
Reset semantic state of |
|
Semantic resolver for SysML. This is responsible for linking references and resolving semantic rules in the pipeline. |
|
Cache of standard library elements used by sema. |
|
Semantic resolution state of |
|
Querying the Abstract Syntax
When SysIDE loads a SysML model from textual notation, it converts it into abstract syntax as defined in the specification. SysML v2 abstract syntax is based on object-oriented principles and, therefore, can be modeled using Python classes. Page Metamodel (Abstract Syntax) shows a list of all element kinds defined in KerML and SysML v2 specifications and to which Python classes they are mapped. The Python classes were created based on the specification by following these principles:
The SysIDE Automator API uses Python convention for class and attribute names instead of the Java convention used in the specification. For example, attribute
assertedConstraint
onAssertConstraintUsage
is mapped toasserted_constraint
.According to the specification, a SysML v2 model is a set of root namespaces, which are namespaces that have no owner. Since root namespaces do not have names, there is no direct way of selecting a specific namespace. For this reason, SysIDE design exploits the KerML clause 10 that defines that each root namespace corresponds to a single file and shows a model as a collection of
documents
. The documents are divided into two groups: documents that are part of the standard library documents and documents that are not. The former can be accessed through fieldstdlib_docs
and the latter through fielduser_docs
.Since
try_load_model
can produce a model even if the model contains errors, most attributes are defined as potentially returningNone
even if according to the specification they are required.All Python classes modeling abstract syntax are subclasses of
AstNode
, which provides several methods and fields that are often useful for querying the abstract syntax:cast
andtry_cast
methods for casting the node to a specific type (discussed below).document
attribute for accessing the document the node belongs to.parent
attribute for accessing the parent node.isinstance
method for checking if the node is an instance of a specific type.owned_elements
attribute for accessing the owned elements of the node.cst_node
attribute for accessing the concrete syntax node corresponding to the abstract syntax node.
The specification uses multiple inheritance extensively, modelling which in Python is challenging. For this reason, a class corresponding to a SysML element (for example,
AssertConstraintUsage
) that according to the specification specializes two other elements (ConstraintUsage
andInvariant
) will show only one class as a base class (in this case,ConstraintUsage
).When using static type checkers such as mypy and Pyright, the behavior of the specification can be emulated by casting elements to standard conforming unions which are declared as
STD
class variables on every element, e.g.Connector.STD
. Also be aware thatmypy
uses joins to infer generic types, reducing them to the most-derived common base type, whilepyright
correctly infers the generic union types.Example of casting an
element
to a standard conforming type:element: syside.Element connector = element.cast(syside.Connector.STD) typing.reveal_type(connector) # pyright: syside.Connector | syside.ConnectorAsUsage
For type hints, a similar
Std
type alias can be used, however it is only defined during type checking so cannot be used at runtime. This distinction is required due to limitations of the Python type system which does not allow type aliases and variables to be bound to the same name. Example of using standard type aliases:def example(element: syside.Element) -> syside.Connector.Std: return element.cast(syside.Connector.STD)
Table: Main Helper Classes for Querying
The following classes are likely to be needed when working with querying models written in textual notation:
AstNode
is a base class for all classes representing abstract syntax.CstNode
is a class providing information about the text from which the node was parsed. An instance of this class can be obtained usingcst_node
attribute ofAstNode
class.Document
is a class representing a document in the model. A document of an abstract syntax node can be obtained usingdocument
attribute ofAstNode
class. An element representing the root namespace can be obtained by usingroot_node
attribute.Document
class also provides two methods for obtaining all elements of specific kind present in the document:all_elements
andall_nodes
. The former returns all elements of a given kind excluding subtypes, while the latter returns all elements of a given kind including subtypes. This functionality is also exposed onModel
class as methodall_elements
.Url
is a class representing an URL of a document, which typically corresponds to a file path. An URL of a document can be obtained usingurl
attribute ofDocument
class.Heritage
is a class containing type specializations and conjugations of atype
. It can be obtained usingheritage
attribute ofType
class.
Table: Abstract Syntax
The table with all classes modelling the abstract syntax can be found on page Metamodel (Abstract Syntax).
Table: Containers
The following table shows container classes that are used in Python classes modelling the abstract syntax:
An immutable view into a native random-access container. Implements Sequence protocol. |
|
A view to a container of children nodes. |
|
Container that stores a vector of children nodes. |
|
Container that stores a vector of potentially owned children nodes. |
|
Container that stores a vector of children nodes that may own feature chainings. |
|
A sequence of qualified name segments that stringifies with unrestricted names as needed. Unlike string, this allows querying segments in a qualified name without having to parse it again, and is cheaper to construct as string conversion is performed only when needed. |
|
The type used in a type check expression, e.g. |
Table: Compiler
The following table shows the classes related to the compiler:
Table: Urls
Internally, documents are referenced by their Urls. We provide Url bindings as well.
|
|
Construct a Url for a filesystem path with file:// scheme. This correctly handles Windows and Posix paths, and percent escapes Unicode characters. |
|
Decode a filesystem path from a |
|
Percent-encoding options |
|
Table: (Advanced) Documents
The following table shows additional classes related to documents, which may be needed for advanced use cases:
Table: (Advanced) Text Documents
Modifying the Abstract Syntax
For convenient modification of the abstract syntax, SysIDE provides a set of additional properties and methods on the abstract syntax classes. Most commonly used ones are:
children
is a property defined on classNamespace
that represents and allows modifying elements in the body of the element (in textual notation, between brackets{
and}
) and expression arguments.prefixes
is a property defined on classNamespace
that represents a group for metadata prefixes, prefixed with#
in textual notation.type_relationships
is a property defined on classType
that represents non-specialization type relationships appearing after specialization part, including feature chaining.declared_ends
anddeclared_messages
are end features and messages before the children block. In the textual syntax they appear in the same position, hence in contrast to similar groups there are additionaltry_append
andtry_insert
methods that returnNone
without throwing if modification failed because the slot is already occupied by block.Accessors are used to provide mutable access to specific attributes of an element. For example, attribute
receiver_member
onAcceptActionUsage
is an accessor of typeParameterAccessor
that allows modifying thereceiver
. The receivers can be seen in the list of SysIDE specific attributes on the page of the corresponding element. They are typically indicated by_member
or_target
suffixes.
When modifying the abstract syntax, the following constraints must be observed:
An element can have only a single owner. Violating this constraint raises
ValueError
exception. However, the same element can be referenced by multiple elements.Moving an element from one document to another is not supported and will raise
ValueError
exception.Stealing ownership of an element is not allowed because the desired semantics are unclear. Remove the element from the model first before re-parenting it.
Adding a new owned or referenced element must satisfy the typing constraints and will raise
TypeError
exception if violated.An element can be added only to an element that is not removed from the model. If this constraint is violated, a
RuntimeError
exception is raised. The problem can be fixed by adding the parent element back to the document as an owned element.
Table: Helper Classes
The following table shows the helper classes for modifying the abstract syntax:
Table: Member Accessors
The following table gives the list of accessor classes:
Table: Reference Accessors
Table: Containers Accessors
Container for relationship bodies. Works similarly to |
|
Exporting to Textual Notation or JSON
SysIDE provides two ways of exporting the modified abstract syntax:
pprint
function for pretty printing the abstract syntax in textual notation.json.dumps
function for serializing the abstract syntax into JSON format.
Table: Pretty Printing SysML and KerML
The following table shows the classes and functions related to pretty printing SysML and KerML in textual notation:
Prints model subtree starting at |
|
Table: JSON Serialization
Error serializing element to SysML v2 JSON. |
|
Error serializing element to SysML v2 JSON. |
|
Class for warnings from serialization and deserialization |
|
Serialize |
|
loads implementation |
Table: (Advanced) Formatting
Table: (Advanced) Serialization Low Level
The following table shows the classes and functions related to serialization:
Convenience function for serialization. Prefer using |
|
Convenience function for deserialization. Prefer using |
|
Abstract base class for serialization writer implementations. |
|
Serializer for SysML models. The actual serialization output depends on used |
|
Abstract base class for all deserialization readers. |
|
Deserializer for SysML models. The actual deserialization input depends on used |
|
Action taken when a serialization error is encountered. |
|
Message emitted during (de)serialization |
|
Options for SysML model serialization. Attribute options are ordered in descending precedence. |
|
(De)Serialization report containing emitted messages. |
|
The model as it was deserialized, with references potentially unresolved. |
|
Reference that has yet to be linked. |
|
|
Table: (Advanced) JSON Low Level
The following table shows the classes and functions related to JSON serialization:
Serialization writer that outputs JSON string |
|
Unbound reader for JSON deserialization |
|
Options for serialization writer to JSON strings |
|
Internal opaque type for deserialization attribute mapping |
|