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
assertedConstraintonAssertConstraintUsageis 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_docsand the latter through fielduser_docs.Since
try_load_modelcan produce a model even if the model contains errors, most attributes are defined as potentially returningNoneeven 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:castandtry_castmethods for casting the node to a specific type (discussed below).documentattribute for accessing the document the node belongs to.parentattribute for accessing the parent node.isinstancemethod for checking if the node is an instance of a specific type.owned_elementsattribute for accessing the owned elements of the node.cst_nodeattribute 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 (ConstraintUsageandInvariant) 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
STDclass variables on every element, e.g.Connector.STD. Also be aware thatmypyuses joins to infer generic types, reducing them to the most-derived common base type, whilepyrightcorrectly infers the generic union types.Example of casting an
elementto 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
Stdtype 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:
AstNodeis a base class for all classes representing abstract syntax.CstNodeis a class providing information about the text from which the node was parsed. An instance of this class can be obtained usingcst_nodeattribute ofAstNodeclass.Documentis a class representing a document in the model. A document of an abstract syntax node can be obtained usingdocumentattribute ofAstNodeclass. An element representing the root namespace can be obtained by usingroot_nodeattribute.Documentclass also provides two methods for obtaining all elements of specific kind present in the document:all_elementsandall_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 onModelclass as methodall_elements.Urlis a class representing an URL of a document, which typically corresponds to a file path. An URL of a document can be obtained usingurlattribute ofDocumentclass.Heritageis a class containing type specializations and conjugations of atype. It can be obtained usingheritageattribute ofTypeclass.
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:
childrenis a property defined on classNamespacethat represents and allows modifying elements in the body of the element (in textual notation, between brackets{and}) and expression arguments.prefixesis a property defined on classNamespacethat represents a group for metadata prefixes, prefixed with#in textual notation.type_relationshipsis a property defined on classTypethat represents non-specialization type relationships appearing after specialization part, including feature chaining.declared_endsanddeclared_messagesare 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_appendandtry_insertmethods that returnNonewithout 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_memberonAcceptActionUsageis an accessor of typeParameterAccessorthat 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_memberor_targetsuffixes.
When modifying the abstract syntax, the following constraints must be observed:
An element can have only a single owner. Violating this constraint raises
ValueErrorexception. However, the same element can be referenced by multiple elements.Moving an element from one document to another is not supported and will raise
ValueErrorexception.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
TypeErrorexception if violated.An element can be added only to an element that is not removed from the model. If this constraint is violated, a
RuntimeErrorexception 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:
pprintfunction for pretty printing the abstract syntax in textual notation.json.dumpsfunction 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 |
|