### Install PySysML2 from Source Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Clone the repository and install PySysML2 using pip. Ensure you are in the project's root directory. ```console git clone git@github.com:TrekkieByDay/PySysML2.git cd PySysML2/ pip install . ``` -------------------------------- ### Generate setup.py with poetry2setup Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Create a setup.py file from pyproject.toml for users installing from source without Poetry. Requires Poetry dev group installation. ```bash poetry run poetry2setup > setup.py ``` -------------------------------- ### Install PySysML2 with Pip or Poetry Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Clone the repository and install PySysML2 using pip or Poetry. Poetry installation also includes development dependencies. ```bash git clone git@github.com:TrekkieByDay/PySysML2.git cd PySysML2/ pip install . ``` ```bash poetry install poetry install --group=dev # includes pytest, etc. ``` -------------------------------- ### Install Poetry Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Install Poetry, a dependency management tool for Python, by running the provided curl command. This is a prerequisite for setting up the development environment. ```shell curl -sSL https://install.python-poetry.org | python3 - ``` -------------------------------- ### Install poetry-bumpversion Plugin Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Add the poetry-bumpversion plugin to manage project versioning across pyproject.toml and __init__.py. ```bash poetry self add poetry-bumpversion ``` -------------------------------- ### Install Development Dependencies with Poetry Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Install additional dependencies required for development and testing, such as pytest. Use the `--group=dev` flag with the `poetry install` command. ```shell poetry install --group=dev ``` -------------------------------- ### Install PySysML2 Dependencies with Poetry Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Install all required project dependencies using Poetry. Run this command in the root directory of the project repository. ```shell poetry install ``` -------------------------------- ### Batch Export Example Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Demonstrates parsing and exporting all .sysml2 model files in a directory to all supported formats using the Python API. ```APIDOC ## Batch Export with Python API Parse and export all `.sysml2` model files in a directory to all supported formats. ```python import os from pysysml2.modeling.model import Model in_dir = "examples/models" out_dir = "examples/output" sysml2_files = [f for f in os.listdir(in_dir) if f.endswith(".sysml2")] for filename in sysml2_files: filepath = os.path.join(in_dir, filename) model = Model() model.from_sysml2_file(filepath) model.to_csv(out_dir) model.to_JSON(out_dir) model.to_txt(out_dir) model.to_excel(out_dir) model.to_dot(out_dir) model.to_png(out_dir) print(f"Exported: {filename}") ``` ``` -------------------------------- ### SysML 2.0 Model File Syntax Example Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt This is an example of a SysML 2.0 model file written in the PySysML2 supported textual notation. It demonstrates packages, parts, attributes, and relationships. ```sysml2 // model_test_1.sysml2 — sample model showing supported constructs package Model { doc /* Package-level documentation */ comment Comment_1 /* Named comment */ package Structure { part def 'WiFi Card' {} part def 'Controller Board' { part 'wifi card' : 'WiFi Card' [1..2]; // specialization + multiplicity attribute 'RAM' : Integer; attribute 'WiFi Frequency' : Real; } part def 'Raspberry Pi Pico Wireless' specializes 'Controller Board' { attribute redefines 'RAM' = 264; // redefines relationship attribute redefines 'WiFi Frequency' = 2.4; } } package Behavior { part def 'User' {} use case def 'Change displayed image on eToken' { actor 'user' : 'User'; objective { doc /* The user changes the displayed image on the eToken */ } } } } ``` -------------------------------- ### SysML2 Port Definition Examples Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/Updated Workflow 20240805.md Demonstrates the basic syntax for defining a port in SysML2, which can appear in package or block scope. ```sysml2 port 'socket 1'; ``` ```sysml2 port 'USB-A-Socket' { attribute 'usb_type' : String; } ``` -------------------------------- ### Run Tests with Poetry Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Execute project tests within the Poetry-managed virtual environment. Ensure Poetry is installed and the project is set up. ```bash poetry run pytest ``` -------------------------------- ### Model.to_png Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Renders the model tree directly to a .png image file via Graphviz. Requires the 'dot' binary to be installed. ```APIDOC ## `Model.to_png(out_dir, file)` — Export Model to PNG Image Renders the model tree directly to a `.png` image file via Graphviz. Requires the `dot` binary to be installed on the system. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") try: model.to_png(out_dir="out/") # Creates: out/model_test_1.png except FileNotFoundError as e: if "No such file or directory: 'dot'" in str(e): print("Graphviz 'dot' not found. Install graphviz to use PNG export.") else: raise ``` ``` -------------------------------- ### Get Help for `pysysml2 export` Command Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md View the help message for the `pysysml2 export` command to understand its arguments and options. This includes supported formats and output directory configuration. ```console ❯ pysysml2 export --help ``` -------------------------------- ### Python Visitor Function for Port Definition Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/Updated Workflow 20240805.md Example of extending the ANTLR4-generated SysML2Visitor to handle port definitions. The function name must match the parser rule, and it sets the SysML2 type and calls the model table builder. ```python # Visit a parse tree produced by SysML2Parser#part_def. def visitPort_def(self, ctx: SysML2Parser.Port_defContext): setattr(ctx, _SYSML2_TYPE_NAME, _SML2_KWS.KW_PORT.value) self._model_table_builder(ctx) return self.visitChildren(ctx) ``` -------------------------------- ### Get Model as Python Dictionary Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Returns a flat dictionary of all model elements, keyed by element index (idx). Each value is a dictionary of element attributes. Useful for custom downstream processing. The model must be loaded first. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model_dict = model.to_dict() # Inspect a specific element by idx elem = model_dict[8] print(elem) # { # 'sysml2_layer': 'Systems Element', # 'archtype': 'element', # 'sysml2_type': 'part', # 'tree_level': 2, # 'name': 'Controller Board@8_4', # 'idx': 8, # 'uuid': '5549b2db-4af0-4d83-8b77-b9c19c54a323', # 'parent': 'Structure@4_0', # 'idx_parent': 4, # 'fully_qualified_name': 'Model::Structure::Controller Board', # 'keywords': ['def', 'part'], # 'value_types': None, # 'multiplicity': None, # ... # } # Filter elements by type parts = {k: v for k, v in model_dict.items() if v['sysml2_type'] == 'part'} attributes = {k: v for k, v in model_dict.items() if v['sysml2_type'] == 'attribute'} relationships = {k: v for k, v in model_dict.items() if v['archtype'] == 'relationship'} ``` -------------------------------- ### Generate Conda Environment with poetry2conda Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Create a conda environment.yaml file from pyproject.toml for Anaconda distribution support. Requires Poetry dev group installation. ```bash poetry run poetry2conda pyproject.toml environment.yaml ``` -------------------------------- ### Export Model to PNG Image Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Renders the model tree directly to a .png image file via Graphviz. Requires the 'dot' binary to be installed on the system. Handles FileNotFoundError if 'dot' is not found. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") try: model.to_png(out_dir="out/") # Creates: out/model_test_1.png except FileNotFoundError as e: if "No such file or directory: 'dot'" in str(e): print("Graphviz 'dot' not found. Install graphviz to use PNG export.") else: raise ``` -------------------------------- ### Export Model to Excel Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Exports the model element dictionary to an Excel .xlsx file via pandas. Requires the 'openpyxl' library to be installed. The model must be loaded prior to export. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_excel(out_dir="out/") # Creates: out/model_test_1.xlsx # Columns match to_dict() keys: sysml2_layer, archtype, sysml2_type, # tree_level, name, idx, uuid, parent, idx_parent, related_element_name, # idx_related_element, multiplicity, value_types, constants, keywords, # fully_qualified_name, fully_qualified_name_tagged, element_text ``` -------------------------------- ### Activate Poetry Virtual Environment Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Enter the project's isolated virtual environment managed by Poetry. Use 'exit' to leave. ```bash poetry shell ``` -------------------------------- ### Export Model to Plain Text Tree Format Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Write the AnytTree rendered text representation of the model to a `.txt` file using `Model.to_txt()`. This is useful for quick human-readable inspection. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_txt(out_dir="out/") # Content of out/model_test_1.txt: # [root]: # └── [0]: Model@0_None # ├── [4]: Structure@4_0 # │ ├── [8]: Controller Board@8_4 # │ │ ├── [9]: wifi card@9_8 # │ │ └── [11]: RAM@11_8 # │ └── [19]: Raspberry Pi Pico Wireless@19_4 # └── [34]: Behavior@34_0 # └── [36]: Change displayed image on eToken@36_34 ``` -------------------------------- ### Python Model Update for Port Creation Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/Updated Workflow 20240805.md Illustrates how to update the model.py file to create a Port instance when parsing SysML2 content. This involves checking the 'sysml2_type' and instantiating the Port class. ```python elif v["sysml2_type"] == smv._SML2_KWS.KW_PORT.value: Port(**v) ``` -------------------------------- ### `Model.from_sysml2_file(file)` Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Parses a `.sysml2` textual model file using ANTLR4, builds a typed AnytTree node hierarchy, and returns the populated `Model` instance. This is the primary entry point for all programmatic usage. ```APIDOC ## `Model.from_sysml2_file(file)` — Parse a SysML 2.0 File Pases a `.sysml2` textual model file using ANTLR4, builds a typed AnytTree node hierarchy, and returns the populated `Model` instance. This is the primary entry point for all programmatic usage. ```python from pysysml2.modeling.model import Model # Parse a SysML 2.0 file model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") # Print the tree structure to stdout print(model) # [root]: # └── [0]: Model@0_None # ├── [1]: PySysML2_GENERATED_NAME_1_0@1_0 (doc) # ├── [2]: PySysML2_GENERATED_NAME_2_0@2_0 (comment) # ├── [3]: Comment_1@3_0 # ├── [4]: Structure@4_0 # │ ├── [5]: WiFi Card@5_4 # │ ├── [8]: Controller Board@8_4 # │ │ ├── [11]: RAM@11_8 # │ │ └── [16]: Bluetooth Capable@16_8 # │ └── ... # └── [34]: Behavior@34_0 # ├── [35]: User@35_34 # └── [36]: Change displayed image on eToken@36_34 ``` ``` -------------------------------- ### Bump Project Version with Poetry Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Update the project's version using the poetry-bumpversion plugin. This command automatically updates both pyproject.toml and pysysml2/__init__.py. ```console ❯ poetry version patch Bumping version from 0.1.0 to 0.1.1 poetry-bumpversion: processed file: pysysml2/__init__.py ``` -------------------------------- ### `Model.to_txt(out_dir, file)` Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Writes the AnytTree rendered text representation of the model tree to a `.txt` file, useful for quick human-readable inspection. ```APIDOC ## `Model.to_txt(out_dir, file)` — Export Model to Plain Text Tree Writes the AnytTree rendered text representation of the model tree to a `.txt` file, useful for quick human-readable inspection. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_txt(out_dir="out/") # Content of out/model_test_1.txt: # [root]: # └── [0]: Model@0_None # ├── [4]: Structure@4_0 # │ ├── [8]: Controller Board@8_4 # │ │ ├── [9]: wifi card@9_8 # │ │ └── [11]: RAM@11_8 # │ └── [19]: Raspberry Pi Pico Wireless@19_4 # └── [34]: Behavior@34_0 # └── [36]: Change displayed image on eToken@36_34 ``` ``` -------------------------------- ### Batch Export SysML2 Files Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Parses and exports all .sysml2 model files in a directory to multiple supported formats (CSV, JSON, TXT, Excel, DOT, PNG). Iterates through files, loads each model, and applies export methods. ```python import os from pysysml2.modeling.model import Model in_dir = "examples/models" out_dir = "examples/output" sysml2_files = [f for f in os.listdir(in_dir) if f.endswith(".sysml2")] for filename in sysml2_files: filepath = os.path.join(in_dir, filename) model = Model() model.from_sysml2_file(filepath) model.to_csv(out_dir) model.to_JSON(out_dir) model.to_txt(out_dir) model.to_excel(out_dir) model.to_dot(out_dir) model.to_png(out_dir) print(f"Exported: {filename}") ``` -------------------------------- ### Export Model to Graphviz DOT Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Generates a Graphviz .dot file representing the model tree structure using AnytTree's DotExporter. The model must be loaded before generating the DOT file. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_dot(out_dir="out/") # Creates: out/model_test_1.dot # Can be rendered manually: dot -Tpng out/model_test_1.dot -o graph.png ``` -------------------------------- ### `Model.to_JSON(out_dir, file)` Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Serializes the full model tree to a structured JSON file using AnytTree's `JsonExporter`. Non-serializable objects (e.g., the ANTLR4 visitor stream) are represented as their Python type string. ```APIDOC ## `Model.to_JSON(out_dir, file)` — Export Model to JSON Serializes the full model tree to a structured JSON file using AnytTree's `JsonExporter`. Non-serializable objects (e.g., the ANTLR4 visitor stream) are represented as their Python type string. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") # Write JSON to ./out/model_test_1.json model.to_JSON(out_dir="out/") # Sample output structure (model_test_1.json): # { # "name": "root", # "input_file": "examples/models/model_test_1.sysml2", # "children": [ # { # "archtype": "element", # "sysml2_type": "package", # "sysml2_layer": "Kernel Element", # "fully_qualified_name": "Model", # "idx": 0, # "uuid": "6658a4e5-14b4-4ff2-bbc4-bb4cfb33810a", # "keywords": ["package"], # "tree_level": 0, # ... # } # ] # } ``` ``` -------------------------------- ### Model.to_dot Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Generates a Graphviz .dot file representing the model tree structure using AnytTree's DotExporter. ```APIDOC ## `Model.to_dot(out_dir, file)` — Export Model to Graphviz DOT Generates a Graphviz `.dot` file representing the model tree structure using AnytTree's `DotExporter`. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_dot(out_dir="out/") # Creates: out/model_test_1.dot # Can be rendered manually: dot -Tpng out/model_test_1.dot -o graph.png ``` ``` -------------------------------- ### Model.to_dict Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Returns a flat dictionary of all model elements, keyed by element index (idx). Each value is a dictionary of element attributes. ```APIDOC ## `Model.to_dict()` — Get Model as Python Dictionary Returns a flat dictionary of all model elements, keyed by element index (`idx`). Each value is itself a dictionary of element attributes. Useful for custom downstream processing. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model_dict = model.to_dict() # Inspect a specific element by idx elem = model_dict[8] print(elem) # { # 'sysml2_layer': 'Systems Element', # 'archtype': 'element', # 'sysml2_type': 'part', # 'tree_level': 2, # 'name': 'Controller Board@8_4', # 'idx': 8, # 'uuid': '5549b2db-4af0-4d83-8b77-b9c19c54a323', # 'parent': 'Structure@4_0', # 'idx_parent': 4, # 'fully_qualified_name': 'Model::Structure::Controller Board', # 'keywords': ['def', 'part'], # 'value_types': None, # 'multiplicity': None, # ... # } # Filter elements by type parts = {k: v for k, v in model_dict.items() if v['sysml2_type'] == 'part'} attributes = {k: v for k, v in model_dict.items() if v['sysml2_type'] == 'attribute'} relationships = {k: v for k, v in model_dict.items() if v['archtype'] == 'relationship'} ``` ``` -------------------------------- ### Parse SysML 2.0 File into Model Object Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Use `Model.from_sysml2_file()` to parse a SysML 2.0 file into a typed AnytTree node hierarchy. This is the primary method for programmatic access. ```python from pysysml2.modeling.model import Model # Parse a SysML 2.0 file model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") # Print the tree structure to stdout print(model) # [root]: # └── [0]: Model@0_None # ├── [1]: PySysML2_GENERATED_NAME_1_0@1_0 (doc) # ├── [2]: PySysML2_GENERATED_NAME_2_0@2_0 (comment) # ├── [3]: Comment_1@3_0 # ├── [4]: Structure@4_0 # │ ├── [5]: WiFi Card@5_4 # │ ├── [8]: Controller Board@8_4 # │ │ ├── [11]: RAM@11_8 # │ │ └── [16]: Bluetooth Capable@16_8 # │ └── ... # └── [34]: Behavior@34_0 # ├── [35]: User@35_34 # └── [36]: Change displayed image on eToken@36_34 ``` -------------------------------- ### Process SysML 2.0 Models and Generate Outputs Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/PySysML2_notebook.ipynb This snippet iterates through SysML 2.0 files in a directory, parses them into a Python Model object, and generates various output formats including .dot, .png, .xlsx, .csv, .JSON, and .txt. It's useful for model analysis and portability. ```python import os from pysysml2.modeling import Model in_dir = "./examples/models" out_dir = "./examples/output" in_sysml2 = [f for f in os.listdir(in_dir) if f.endswith(".sysml2")] # Looping through all model files in the input directory for in_f in in_sysml2: in_f = os.path.join(in_dir, in_f) # Set filename model = Model() # Create Model object model.from_sysml2_file(in_f) # Parse the textual model model.to_dot(out_dir) # Tranform model to a .dot file model.to_png(out_dir) # Export PNG of tree graph model.to_excel(out_dir) # Convert to tabular format model.to_csv(out_dir) # Export tabular view to CSV model.to_JSON(out_dir) # Serialize Model object to JSON model.to_txt(out_dir) # Output string representation ``` -------------------------------- ### Export SysML Model using CLI Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/README.md Use the `pysysml2 export` command to convert a SysML 2.0 textual model file to multiple output formats. Specify the input model file and the desired output directory. ```console ❯ pysysml2 export examples/models/model_test_1.sysml2 --output-dir out/ --format json,txt,csv,xlsx,dot,png ``` -------------------------------- ### Model.to_excel Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Exports the model element dictionary to an Excel .xlsx file via pandas. Requires openpyxl. ```APIDOC ## `Model.to_excel(out_dir, file)` — Export Model to Excel Exports the model element dictionary to an Excel `.xlsx` file via pandas. Requires `openpyxl`. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_excel(out_dir="out/") # Creates: out/model_test_1.xlsx # Columns match to_dict() keys: sysml2_layer, archtype, sysml2_type, # tree_level, name, idx, uuid, parent, idx_parent, related_element_name, # idx_related_element, multiplicity, value_types, constants, keywords, # fully_qualified_name, fully_qualified_name_tagged, element_text ``` ``` -------------------------------- ### Export Model to CSV Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Exports the flat model table to a CSV file. Each row represents a parsed SysML element with its metadata. Requires the model to be loaded first. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_csv(out_dir="out/") # out/model_test_1.csv columns: # name, sysml2_type, parent, idx, uuid, idx_parent, uuid_parent, # idx_related_element, related_element_name, value_types, constants, # multiplicity, context_type, keywords, fully_qualified_name, # fully_qualified_name_tagged, tree_level, element_text ``` -------------------------------- ### Traverse SysML 2.0 Model Tree in Python Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Use this snippet to parse a SysML 2.0 file and traverse the resulting model tree. Inspect elements like Parts and Attributes, and their properties. ```python from pysysml2.modeling.model import Model from pysysml2.modeling.element import ( Part, Attribute, Package, UseCase, Objective, Connection, RelationshipSpecializes, RelationshipRedefines, RelationshipConnect, RelationshipMessage, Comment, Doc ) from anytree import RenderTree model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") # Walk the tree and inspect typed nodes for pre, fill, node in RenderTree(model): if isinstance(node, Part): print(f"Part: {node.fully_qualified_name}") elif isinstance(node, Attribute): print(f" Attribute: {node.name}, type={node.value_types}, const={node.constants}") elif isinstance(node, RelationshipSpecializes): print(f" Specializes: {node.name} -> {node.related_element_name}") elif isinstance(node, RelationshipRedefines): print(f" Redefines: {node.name} = {node.constants}") # Each element exposes: # .name - element name (tagged with @idx_idxParent) # .sysml2_type - 'part', 'attribute', 'package', 'usecase', etc. # .sysml2_layer - 'Systems Element', 'Kernel Element', 'Root Syntactic Element' # .archtype - 'element' or 'relationship' # .fully_qualified_name - e.g. 'Model::Structure::Controller Board::RAM' # .idx / .uuid - integer index and UUID # .idx_parent - parent element index # .keywords - list of SysML2 keywords, e.g. ['def', 'part'] # .value_types - type annotation, e.g. 'Integer', 'String', 'Real' # .constants - assigned constant value # .multiplicity - e.g. '[1..2]' # .element_text - doc/comment body text ``` -------------------------------- ### Python Element Class for Port Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/Updated Workflow 20240805.md Defines a Python class for the 'Port' element, inheriting from a base Element class and NodeMixin, and setting its SysML2 layer. ```python class Port(Element, NodeMixin): def __init__(self, name=None, parent=None, **kwargs): super(Port, self).__init__(name=name, parent=parent, **kwargs) self.sysml2_layer = ArchitectureLayers.systems_element.value ``` -------------------------------- ### Export SysML2 Model to Multiple Formats via CLI Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Use the `pysysml2 export` command to convert a .sysml2 file to various formats like JSON, TXT, CSV, XLSX, DOT, and PNG. Specify an output directory and desired formats. ```bash pysysml2 export examples/models/model_test_1.sysml2 \ --output-dir out/ \ --format json,txt,csv,xlsx,dot,png ``` ```bash # Expected output: # Using output directory: /path/to/out # Exporting to JSON... # Exporting to txt... # Exporting to csv... # Exporting to xlsx... # Exporting to dot... # Exporting to png... ``` ```bash # Export only JSON (default) pysysml2 export examples/models/model_test_1.sysml2 ``` ```bash # Show full help pysysml2 export --help ``` -------------------------------- ### Parse a Single SysML 2.0 File Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/PySysML2_notebook.ipynb This snippet demonstrates how to parse a single SysML 2.0 file into a PySysML2 Model object. It is useful for loading and inspecting individual models. ```python import os from pysysml2.modeling import Model in_f = "./examples/updated_test/beagleplay_set1.sysml2" # in_f = "./examples/models/model_test_1.sysml2" model = Model() # Create Model object model.from_sysml2_file(in_f) # Parse the textual model ``` -------------------------------- ### Model.to_csv Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Exports the flat model table to a CSV file. Each row represents a parsed SysML element with its metadata. ```APIDOC ## `Model.to_csv(out_dir, file)` — Export Model to CSV Exports the flat model table (a pandas DataFrame built by the ANTLR4 visitor) to a CSV file. Each row represents one parsed SysML element with its metadata. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") model.to_csv(out_dir="out/") # out/model_test_1.csv columns: # name, sysml2_type, parent, idx, uuid, idx_parent, uuid_parent, # idx_related_element, related_element_name, value_types, constants, # multiplicity, context_type, keywords, fully_qualified_name, # fully_qualified_name_tagged, tree_level, element_text ``` ``` -------------------------------- ### Export Model to JSON Format Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Serialize the model tree to a JSON file using `Model.to_JSON()`. Non-serializable objects are represented as their Python type string. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") # Write JSON to ./out/model_test_1.json model.to_JSON(out_dir="out/") # Sample output structure (model_test_1.json): # { # "name": "root", # "input_file": "examples/models/model_test_1.sysml2", # "children": [ # { # "archtype": "element", # "sysml2_type": "package", # "sysml2_layer": "Kernel Element", # "fully_qualified_name": "Model", # "idx": 0, # "uuid": "6658a4e5-14b4-4ff2-bbc4-bb4cfb33810a", # "keywords": ["package"], # "tree_level": 0, # ... # } # ] # } ``` -------------------------------- ### CLI: pysysml2 export Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Export a .sysml2 model file to one or more output formats from the command line. Supported formats include json, txt, csv, xlsx, dot, and png. ```APIDOC ## CLI: `pysysml2 export` Export a `.sysml2` model file to one or more output formats from the command line. Supported formats: `json`, `txt`, `csv`, `xlsx`, `dot`, `png`. ```bash # Export to all supported formats into an output directory pysysml2 export examples/models/model_test_1.sysml2 \ --output-dir out/ \ --format json,txt,csv,xlsx,dot,png # Expected output: # Using output directory: /path/to/out # Exporting to JSON... # Exporting to txt... # Exporting to csv... # Exporting to xlsx... # Exporting to dot... # Exporting to png... # Export only JSON (default) pysysml2 export examples/models/model_test_1.sysml2 # Show full help pysysml2 export --help ``` ``` -------------------------------- ### Find Model Element by Index Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Traverses the model tree and returns the node with the given integer index. Returns None if the element is not found. The model must be loaded before searching. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") node = model.find_element_by_idx(8) print(node.name) # "Controller Board@8_4" print(node.sysml2_type) # "part" print(node.fully_qualified_name) # "Model::Structure::Controller Board" print([child.name for child in node.children]) # ['wifi card@9_8', 'bluetooth card@10_8', 'RAM@11_8', ...] ``` -------------------------------- ### Model.find_element_by_idx Source: https://context7.com/daf-digital-transformation-office/pysysml2/llms.txt Traverses the model tree and returns the node with the given integer index. Returns None if not found. ```APIDOC ## `Model.find_element_by_idx(idx)` — Find a Tree Node by Index Traverses the model tree and returns the node with the given integer index. Returns `None` if not found. ```python from pysysml2.modeling.model import Model model = Model() model.from_sysml2_file("examples/models/model_test_1.sysml2") node = model.find_element_by_idx(8) print(node.name) # "Controller Board@8_4" print(node.sysml2_type) # "part" print(node.fully_qualified_name) # "Model::Structure::Controller Board" print([child.name for child in node.children]) # ['wifi card@9_8', 'bluetooth card@10_8', 'RAM@11_8', ...] ``` ``` -------------------------------- ### ANTLR4 Rules for SysML2 Elements and Ports Source: https://github.com/daf-digital-transformation-office/pysysml2/blob/main/Updated Workflow 20240805.md Defines ANTLR4 grammar rules for various SysML2 elements, including a specific rule for ports. Keywords containing substrings of other keywords must be defined first due to ANTLR4's 'longest match' rule. ```antlr4 // An element is anything that can be a part of a model element : namespace | feature | comment | doc | statement | port ; // A namespace is an element with a scope defined by curly braces namespace : sysml2_package | part | use_case_def | comment | doc | port ; part_blk: (feature | comment | doc | part | port | connect | connection); // Ports port: (port_def | port_blk); // e.g. port 'socket 1'; port_def: KW_PORT ID ';' | (KW_PORT ID '{' port_blk* '}'); port_blk: (feature | comment | doc ); KW_PORT: 'port'; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.