APIs cookbook

Each recipe aims for clarity, ease of use, and customizability for different use cases.

1. Setup instructions

To get started with the examples, Python is used to interact with SysON’s features and data. Python offers a powerful way to automate tasks, handle API calls, and process data, making it an ideal choice for this purpose. Follow the steps after to set up your environment and get everything ready to run the provided code snippets.

1.1. Step 1: Install required Python libraries

Confirm you have the following Python libraries installed before running the code snippets:

  • requests: For making HTTP requests to the SysML v2 API.

  • pandas: For organizing and manipulating data.

To install these libraries, run:

pip install requests pandas

1.2. Step 2: Configure SysML v2 API access

init_api.py
# These examples are adapted from the SysML v2 API Cookbook, available at
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
# Object Management Group (OMG).
# The original cookbook is designed for use with Jupyter Lab.
# These examples have been adapted to run as standalone Python scripts, making
# them suitable for use in various environments, including SysON.
# They showcase practical usage scenarios and may include additional functionality
# or modifications tailored to specific needs.
import argparse

def init_sysmlv2_api():
    host =  "http://localhost:8080/api/rest" # Replace with your actual API host URL (1)
    return host

def parse_arguments():
    # Parse command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "project_id",
        type=str,
        help="The project ID.",
    )
    parser.add_argument(
        "element_id",
        type=str,
        nargs="?",  # This makes element_id optional
        help="The element ID (optional).",
    )
    return parser.parse_args()
Replace http://localhost:8080 with the actual URL of your API server if hosted elsewhere.

What this code does:

1 Define the base host: Assigns the variable host with the base URL of the SysML v2 API server.

In the examples, the SysON server uses a randomization function to generate unique IDs, so outputs can differ when you run the examples on your project.

Other code snippets use this function. To run it, see the following section about recipes.

1.3. Step 3: Test code snippets

Once you have configured the SysML v2 API access, you can start testing the recipes.

1.4. Troubleshooting

  1. Error Handling:

    • Check endpoint URLs, project_id, commit_id, and authentication tokens if the API returns errors: for example, 400 or 500 status codes.

    • Confirm the API is running.

  2. Empty Responses:

    • Verify that queried elements exist within the specified project and commit.

    • Use the SysON web interface for visual inspection.

  3. Recursive Function Depth:

    • For large models, manage recursion depth appropriately.

    • Change the logic to handle large datasets by limiting recursion depth.

2. Recipes

2.1. Project and commit recipe (python script)

Learn how to manage projects and retrieve related commits programmatically. Each recipe includes a detailed explanation, step-by-step instructions, and code snippets.

Recipes covered:

2.1.1. Get projects

This code example demonstrates how to use Python’s requests library to interact with the SysON API and retrieve a list of projects. It sends a GET request to the API endpoint for projects, processes the response, and prints the name and ID of each project.

Example script to fetch projects:

fetch_projects.py
# These examples are adapted from the SysML v2 API Cookbook, available at
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
# Object Management Group (OMG).
# The original cookbook is designed for use with Jupyter Lab.
# These examples have been adapted to run as standalone Python scripts, making
# them suitable for use in various environments, including SysON.
# They showcase practical usage scenarios and may include additional functionality
# or modifications tailored to specific needs.

import requests (1)
from init_api import init_sysmlv2_api

def fetch_projects(host): (2)
    projects_url = f"{host}/projects" (3)
    response = requests.get(projects_url) (4)
    if response.status_code == 200: (5)
        projects = response.json()
        for project in projects:
            print(f"Project Name: {project['name']}, ID: {project['@id']}")
    else:
        print(f"Error fetching projects: {response.status_code} - {response.text}")

if __name__ == "__main__":
    host = init_sysmlv2_api()
    # Get the projects
    fetch_projects(host)

What this code does:

1 Import required libraries:
  • requests: Used for sending HTTP requests.

2 Define the fetch_projects function with one parameter:
  • host: The base API address.

3 Constructs the API endpoint address for fetching projects.
4 Sends a GET request to the API, passing the SysON address as a query parameter.
5 Handles the API response:
  • If the projects are successfully fetched - HTTP status 200, the function prints all the projects ID and name.

  • If an error occurs, the status code and error message are displayed.

Run the script:

$ python fetch_projects.py

Output:

Project Name: Batmobile, ID: 63a03bd8-a81a-4818-801a-01790ce8a086

2.1.2. Create a new project

This recipe demonstrates how to create a new project in SysON using Python. It sends a POST request to the /projects endpoint to create a new project with a unique name and description.

Example script to create a new project:

create_project.py
# These examples are adapted from the SysML v2 API Cookbook, available at
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
# Object Management Group (OMG).
# The original cookbook is designed for use with Jupyter Lab.
# These examples have been adapted to run as standalone Python scripts, making
# them suitable for use in various environments, including SysON.
# They showcase practical usage scenarios and may include additional functionality
# or modifications tailored to specific needs.

import requests (1)
from init_api import init_sysmlv2_api
from fetch_projects import fetch_projects
from datetime import datetime

def create_project(host, project_name): (2)
    # Define project data as query parameters
    project_params = {
        "name": project_name
    }

    # API endpoint to create a project
    url = f"{host}/projects"  (3)

    # Send POST request with query parameters
    response = requests.post(url, params=project_params)  (4)

    # Check if the project creation was successful
    if response.status_code == 201:  (5)
        response_json = response.json()
        print("Project created successfully:")
        print(f"Project ID: {response_json.get("@id", "Unknown ID")}")
        print(f"Project Name: {response_json.get('name', 'Unknown Name')}")
    else:
        print(f"Error creating project: {response.status_code} - {response.text}")

if __name__ == "__main__":
    host = init_sysmlv2_api()

    # Fetch and display the list of projects currently available on the server
    print("Fetching the list of projects currently available on the server:")
    fetch_projects(host)

    # Create a new project with a unique name by appending a timestamp
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    project_name = f"New Project - {timestamp}"
    create_project(host, project_name)

    # Fetch and display the updated list of projects after creating the new project
    print("Fetching the updated list of projects after creating a new project:")
    fetch_projects(host)

What this code does:

1 Import required libraries:
  • requests: Used for sending HTTP requests.

  • datetime: Used to generate a timestamp for unique project naming.

2 Define the create_project function with two parameters:
  • host: The base API URL.

  • project_name: The name of the project to be created.

3 Constructs the API endpoint URL of project creation.
4 Sends a POST request to the API, passing the project name as a query parameter.
5 Handles the API response:
  • If the project is successfully created - HTTP status 201, the function extracts and prints the project ID and name.

  • If an error occurs, the status code and error message are displayed.

Run the script:

$ python create_project.py

Output:

Fetching the list of projects currently available on the server:
Project Name: Batmobile, ID: 63a03bd8-a81a-4818-801a-01790ce8a086
Project created successfully:
Project ID: d967b937-304f-43a9-8af6-b1f3a9d6adbe
Project Name: New Project - 2024-12-31 10:52:56
Fetching the updated list of projects after creating a new project:
Project Name: Batmobile, ID: 63a03bd8-a81a-4818-801a-01790ce8a086
Project Name: New Project - 2024-12-31 10:52:56, ID: d967b937-304f-43a9-8af6-b1f3a9d6adbe

2.1.3. Get commits

SysON always returns a single branch with a single commit.

This example extends the functionality by fetching the commit associated with a specific project ID. It constructs an address based on the host and project ID, sends a GET request, and prints the ID of the retrieved commit.

Example script to fetch commits:

fetch_commits.py
# These examples are adapted from the SysML v2 API Cookbook, available at
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
# Object Management Group (OMG).
# The original cookbook is designed for use with Jupyter Lab.
# These examples have been adapted to run as standalone Python scripts, making
# them suitable for use in various environments, including SysON.
# They showcase practical usage scenarios and may include additional functionality
# or modifications tailored to specific needs.

import requests (1)
from init_api import parse_arguments
from init_api import init_sysmlv2_api

def fetch_commits(host, project_id):  (2)
    commits_url = f"{host}/projects/{project_id}/commits"  (3)
    response = requests.get(commits_url)  (4)
    if response.status_code == 200:  (5)
        commits = response.json()
        for commit in commits:
            print(f"Commit ID: {commit['@id']}")
        return commits
    else:
        print(f"Error fetching commits: {response.status_code} - {response.text}")
        return None

#  Retrieves the latest commit for a given project.
def get_last_commit_id(host, project_id):
    commits = fetch_commits(host, project_id)
    if commits:
        last_commit = commits[-1] if commits else None
        if last_commit:
            last_commit_id = last_commit['@id']
            print(f"Last Commit ID: {last_commit_id}")
        return last_commit_id
    else:
        print("No commits available.")
        return None

if __name__ == "__main__":
    args = parse_arguments()
    host = init_sysmlv2_api()
    project_id = args.project_id
    fetch_commits(host, project_id)

What this code does:

1 Import required libraries:
  • requests: Used for sending HTTP requests.

2 Define the fetch_commits Function with two parameters:
  • host: The base API URL.

  • project_id: The project ID.

3 Constructs the API endpoint URL for fetching commits.
4 Sends a GET request to the API, passing the SysON address and the project ID as a query parameter.
5 Handles the API response:
  • If the projects are successfully fetched - HTTP status 200, the function prints all the projects ID and name.

  • If an error occurs, the status code and error message are displayed.

Run the script:

$ python fetch_commits.py your-project-id

Output:

Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086

2.2. Element owned elements recipe (python script)

Learn how to retrieve owned elements programmatically. Each recipe includes a detailed explanation, step-by-step instructions, and sample code.

Recipes covered:

2.2.1. Get owned elements

This example demonstrates how to recursively navigate through the hierarchical structure of elements by using the SysON API. The script starts from a specific element identified by its ID and retrieves its details along with any "owned elements." The function calls itself recursively to explore all child elements in the hierarchy, printing their names, IDs, and types in an indented format to show the hierarchy visually.

Example script to get owned elements:

get_owned_element.py
# These examples are adapted from the SysML v2 API Cookbook, available at
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
# Object Management Group (OMG).
# The original cookbook is designed for use with Jupyter Lab.
# These examples have been adapted to run as standalone Python scripts, making
# them suitable for use in various environments, including SysON.
# They showcase practical usage scenarios and may include additional functionality
# or modifications tailored to specific needs.

import requests
from init_api import parse_arguments
from init_api import init_sysmlv2_api
from fetch_commits import get_last_commit_id

# Function to fetch an element and print its name and type
def get_element(host, project_id, commit_id, element_id, indent):
     # Fetch the element in the given commit of the given project
    element_url = f"{host}/projects/{project_id}/commits/{commit_id}/elements/{element_id}"  (3)
    response = requests.get(element_url)  (1)

    if response.status_code == 200:
        element_data = response.json()
        element_name_to_print = element_data['name'] if element_data['name'] else 'N/A'
        element_id = element_data ['@id']
        element_type = element_data ['@type']
        print(f"{indent} - {element_name_to_print} (id = {element_id} , type = {element_type})") (2)
        return element_data
    else:
        return None

# Fetches immediate owned elements for a given element
def get_owned_elements_immediate(host, project_id, commit_id, element_id, indent):
    element_data = get_element(host, project_id, commit_id, element_id, indent)
    if element_data:
        owned_elements = element_data['ownedElement']
        if len(owned_elements) > 0:
            for owned_element in owned_elements:
                get_element(host, project_id, commit_id, owned_element['@id'], indent + '  ')
    else:
        print(f"Unable to fetch element with id '{element_id}' in commit '{commit_id}' of project '{project_id}'")

# Fetches owned elements recursively for a given element
def get_owned_elements(host, project_id, commit_id, element_id, indent):
    element_data = get_element(host, project_id, commit_id, element_id, indent)
    if element_data: (3)
        owned_elements = element_data['ownedElement']
        if len(owned_elements) > 0:
            for owned_element in owned_elements:
                get_owned_elements(host, project_id, commit_id, owned_element['@id'], indent+' ')
    else:
        print(f"Unable to fetch element with id '{element_id}' in commit '{commit_id}' of project '{project_id}'")  (4)

if __name__ == "__main__":
    args = parse_arguments()
    host = init_sysmlv2_api()
    project_id = args.project_id
    commit_id = get_last_commit_id(host, project_id)
    element_id = args.element_id

    #Get owned elements (immediate) for the given element in the given commit of the given project
    print("Immediate Owned Elements:")
    get_owned_elements_immediate(host, project_id, commit_id, element_id, '')

    # Get owned elements (recursive) for the given element in the given commit of the given project
    print("\nRecursive Owned Elements:")
    get_owned_elements(host, project_id, commit_id, element_id, '')

What this code does:

1 Fetches an Element: Sends a GET request to the SysON API to retrieve details about an element specified by its ID.
2 Prints Element Details: Displays the element’s name, ID, and type in a formatted, indented way.
3 Processes Owned Elements: Checks for owned elements and calls itself recursively for each one.
4 Recursive Traversal: Continues recursing through hierarchical levels until it explores and prints all owned elements.

This approach helps visualize the structure of complex models or systems, where elements are organized hierarchically. It highlights how to navigate relationships between elements efficiently by using the SysON API.

Run the script:

$ python get_owned_elements.py your-project-id your-element-id

Output example:

Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086
Last Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086
Immediate Owned Elements:
 - Batmobile (Definition)
   - problemStatement (ReferenceUsage)
   - systemIdea (ReferenceUsage)
   - seat (PartUsage)
   - body (PartUsage)
   - wheels (PartUsage)
   - frontLeftWheel (PartUsage)
   - frontRightWheel (PartUsage)
   - rearLeftWheel (PartUsage)
   - rearRightWheel (PartUsage)
   - battery (PartUsage)
   - batmobileEngine (PartUsage)

Recursive Owned Elements:
 - Batmobile (Definition)
  - problemStatement (ReferenceUsage)
   - N/A (LiteralString)
  - systemIdea (ReferenceUsage)
   - N/A (LiteralString)
  - seat (PartUsage)
  - body (PartUsage)
  - wheels (PartUsage)
  - frontLeftWheel (PartUsage)
  - frontRightWheel (PartUsage)
  - rearLeftWheel (PartUsage)
  - rearRightWheel (PartUsage)
  - battery (PartUsage)
   - powerPort (PortUsage)
   - capacity (AttributeUsage)
  - batmobileEngine (PartUsage)
   - enginePort (PortUsage)

2.3. Create element recipe (swagger UI / REST client)

Learn how to create and element through REST API. Each recipe includes a detailed explanation, step-by-step instructions, and sample code.

Test on simple SysMLv2 project (with id = 44ba0783-8476-442a-9ab3-b6c26e251734) with only one element: a Package named "Package1" with elementId = b2426313-e795-47e8-a2b2-7e29adb12a56

1) Let’s retrieve the elements with /elements GET request

http://localhost:8080/api/rest/projects/44ba0783-8476-442a-9ab3-b6c26e251734/commits/44ba0783-8476-442a-9ab3-b6c26e251734/elements
Replace http://localhost:8080 with the actual URL of your API server if hosted elsewhere.

Use the swagger UI to execute the request (or your favorite REST client):

Swagger UI GET elements request

The JSON response is:

[
  {
    "@id": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    "@type": "Package",
    "declaredName": "Package1",
    "elementId": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    ...
  }
]

2) Let’s add a new PartDefinition under our Package "Package1".

As you may know, with SysML v2, an Element is contained in an another Element through an intermediate Element called a Membership. In our case, an OwningMembership is the appropriate intermediate Element. So we have to add two new Elements. The containment relation between an Element and a Membership is named "ownedRelationship". The containment relation between a Membership and an Element is named "ownedRelatedElement".

2.1) Execute the /commits POST request, allowing to create the OwningMembership:

http://localhost:8080/api/rest/projects/44ba0783-8476-442a-9ab3-b6c26e251734/commits

with the following raw JSON body:

{
	"@type": "Commit",
	"change": [
		{
			"@type": "DataVersion",
			"identity": null,
			"payload": {
				"@id": "8e57348e-338f-4272-9e01-bb896784d849", // a random ID
				"@type": "OwningMembership"
			}
		},
		{
			"@type": "DataVersion",
			"identity": {
				"@id":"b2426313-e795-47e8-a2b2-7e29adb12a56", // Package1 elementId
				"@type" : "DataIdentity"
			},
			"payload": {
				"@id": "f3d97864-09f0-4af7-95bd-44b1a3243fdb", // a random ID
				"@type": "OwningMembership",
				"ownedRelationship": [{
					"@id": "8e57348e-338f-4272-9e01-bb896784d849" // the random ID used for the creation of the OwningMembership. This will not be the final elementId of the OwningMembership.
				}]
			}
		}
	]
}

Use the swagger UI to execute the request (or your favorite REST client):

Swagger UI POST commit request

The JSON response:

{
	"@id": "44ba0783-8476-442a-9ab3-b6c26e251734",
	"@type": "Commit",
	"created": "1970-01-01T00:00:00Z",
	"description": "The one and only commit for this project",
	"owningProject": {
		"@id": "44ba0783-8476-442a-9ab3-b6c26e251734"
	},
	"previousCommits": []
}

2.2) Execute the /elements GET request, allowing to get the real "elementId" of the new OwningMembership that will be the container of our new PartDefinition:

http://localhost:8080/api/rest/projects/44ba0783-8476-442a-9ab3-b6c26e251734/commits/44ba0783-8476-442a-9ab3-b6c26e251734/elements

Use the swagger UI to execute the request (or your favorite REST client):

Swagger UI GET elements request

The JSON response is:

[
  {
    "@id": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    "@type": "Package",
    "declaredName": "Package1",
    "elementId": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    ...
  },
  {
    "@id": "61508abd-26a6-4b23-9bf5-1ab100f32a47",
    "@type": "OwningMembership",
    "elementId": "61508abd-26a6-4b23-9bf5-1ab100f32a47", // This is the real elementId of the OwningMembership we just created
    ...
  }
]

2.3) Now we have the "elementId" of our new OwningMembership, we can create a new PartDefinition inside it.

Execute the /commits POST request, allowing to create the PartDefinition:

http://localhost:8080/api/rest/projects/44ba0783-8476-442a-9ab3-b6c26e251734/commits

with the following raw JSON body:

{
	"@type": "Commit",
	"change": [
		{
			"@type": "DataVersion",
			"identity": null,
			"payload": {
				"@id": "4d4596e7-27a3-493a-8c6a-de63dddfca2b", // random ID
				"@type": "PartDefinition",
				"declaredName": "MyRESTNewPartDef"
			}
		},
		{
			"@type": "DataVersion",
			"identity": {
				"@id":"61508abd-26a6-4b23-9bf5-1ab100f32a47", //OwningMembership elementId
				"@type" : "DataIdentity"
			},
			"payload": {
				"@id": "aff1e118-af4b-465d-b9a9-8a407017d4d2", // random ID
				"@type": "PartDefinition",
				"ownedRelatedElement": [{
					"@id": "4d4596e7-27a3-493a-8c6a-de63dddfca2b" // the random ID used for the creation of the PartDefinition. This will not be the final elementId of the PartDefinition.
				}]
			}
		}
	]
}

Use the swagger UI to execute the request (or your favorite REST client):

Swagger UI POST commit request

The JSON response:

{
	"@id": "44ba0783-8476-442a-9ab3-b6c26e251734",
	"@type": "Commit",
	"created": "1970-01-01T00:00:00Z",
	"description": "The one and only commit for this project",
	"owningProject": {
		"@id": "44ba0783-8476-442a-9ab3-b6c26e251734"
	},
	"previousCommits": []
}

3) Finally, retrieve the elements one last with /elements GET request to confirm the new PartDefinition has been created by the POST requests.

http://localhost:8080/api/rest/projects/44ba0783-8476-442a-9ab3-b6c26e251734/commits/44ba0783-8476-442a-9ab3-b6c26e251734/elements

Use the swagger UI to execute the request (or your favorite REST client):

Swagger UI GET elements request

The JSON response is:

[
  {
    "@id": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    "@type": "Package",
    "declaredName": "Package1",
    "elementId": "b2426313-e795-47e8-a2b2-7e29adb12a56",
    ...
  },
  {
    "@id": "61508abd-26a6-4b23-9bf5-1ab100f32a47",
    "@type": "OwningMembership",
    "elementId": "61508abd-26a6-4b23-9bf5-1ab100f32a47",
    ...
  },
  {
    "@id": "b7403610-e104-4b8e-9eac-7d4ee5c41de0",
    "@type": "PartDefinition",
    "declaredName": "MyRESTNewPartDef",
    "elementId": "b7403610-e104-4b8e-9eac-7d4ee5c41de0",
    ...
  }
]

2.4. Create new objects from text recipe (python script)

Learn how to create and element through GraphQL API.

Each recipe includes a detailed explanation, step-by-step instructions, and sample code.

Recipes covered:

2.4.1. New objects from text

This example demonstrates how to create new objects from a textual SysMLv2 file by using the SysON GraphQL API.

The script reads a SysMLv2 file, extracts its content, and creates new elements in a specified project and namespace.

Example script to create new objects from text:

get_owned_element.py
###############################################################################
# Copyright (c) 2025 Obeo.
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#     Obeo - initial API and implementation
###############################################################################

import requests (1)
import uuid
import argparse
from pathlib import Path

headers = {
    "Content-Type": "application/json"
}

GRAPHQL_ENDPOINT =  "/api/graphql"

# GraphQL query to fetch editing context and stereotypes
fetch_editing_context_query = """
query FetchEditingContext($projectId: ID!) {
  viewer {
    project(projectId: $projectId) {
      currentEditingContext {
        id
        stereotypes {
          edges {
            node {
              id
              label
            }
          }
        }
      }
    }
  }
}
"""

# GraphQL mutation for importing SysML v2 content
import_sysml_mutation = """
mutation InsertTextualSysMLv2($input: InsertTextualSysMLv2Input!) {
  insertTextualSysMLv2(input: $input) {
    __typename
    ... on SuccessPayload {
      id
    }
    ... on ErrorPayload {
      message
    }
  }
}
"""

# Function to fetch the editing context
def fetch_editing_context(url, project_id): (2)
    graphql_url = f"{url}{GRAPHQL_ENDPOINT}"
    variables = {"projectId": project_id}
    response = requests.post(
        graphql_url,
        json={"query": fetch_editing_context_query, "variables": variables},
        headers=headers
    )

    if response.status_code != 200:
        print(f"Error fetching editing context: {response.status_code} - {response.text}")
        return None

    data = response.json()
    viewer = data.get("data", {}).get("viewer", {})
    project = viewer.get("project", {})
    editing_context = project.get("currentEditingContext", {})
    return editing_context

# Function to import SysML v2 content into a project
def import_sysml_to_project(url, file_path, editing_context_id, object_id): (3)
    graphql_url = f"{url}{GRAPHQL_ENDPOINT}"
    try:
        # Read the SysML v2 file
        with open(file_path, "r") as file:
            textual_content = file.read()

        # Generate a unique operation ID
        operation_id = str(uuid.uuid4())

        # Prepare mutation variables
        variables = {
            "input": {
                "id": operation_id,
                "editingContextId": editing_context_id,
                "objectId": object_id,
                "textualContent": textual_content
            }
        }

        # Send the mutation request (4)
        response = requests.post(
            graphql_url,
            json={"query": import_sysml_mutation, "variables": variables},
            headers= headers
        )

        if response.status_code != 200:
            print(f"Error importing SysML v2: {response.status_code} - {response.text}")
            return False

        data = response.json()
        result = data.get("data", {}).get("insertTextualSysMLv2", {})

        if result.get("__typename") == "SuccessPayload":
            return True
        elif result.get("__typename") == "ErrorPayload":
            print(f"Error: {result.get('message')}")
            return False
        else:
            print("Unexpected response:", data)
            return False

    except Exception as e:
        print(f"An error occurred while importing SysML v2: {e}")
        return False

if __name__ == "__main__":
    # Set up argument parsing
    parser = argparse.ArgumentParser(description="SysON new objects from text script")

    parser.add_argument(
        "url",
        nargs="?",  # Makes the argument optional
        default="http://localhost:8080",  # Default value
        help="The API base URL (e.g., http://yourSysonServerURL). Defaults to http://localhost:8080"
    )

    parser.add_argument(
        "file_path",
        type=Path,  # Ensure it's a valid path
        help="File path containing the SysML textual file to import (absolute path required)"
    )

    parser.add_argument(
        "project_id",
        type=str,
        help="The UUID of the project to import the SysML file into"
    )

    parser.add_argument(
        "namespace_elementId",
        type=str,
        help="The elementId of the SysMLv2 element under which the content of the SysMLv2 file will be imported"
    )

    args = parser.parse_args()

    # Get all function parameters (5)
    url = args.url
    file_path = args.file_path
    # Fetch editing context
    editing_context = fetch_editing_context(url, args.project_id)
    if editing_context:
        editing_context_id = editing_context["id"]
    else:
        print("Editing context not found.")
        exit()
    namespace_id = args.namespace_elementId

    # Import SysML file into the project, under the specified Namespace element
    if import_sysml_to_project(url, file_path, editing_context_id, namespace_id): (6)
        print(f"SysML v2 file imported successfully: {file_path}") (7)
    else:
        print(f"Failed to import SysML v2 file: {file_path}") (7)

What this code does:

1 Import required libraries:
  • requests: Used for sending HTTP requests.

2 Define the fetch_editing_context function with two parameters:
  • host: The base API address.

  • projectId: The UUID of the SysON project in which you want to create you new objects from text.

3 Define the import_sysml_to_project function with four parameters:
  • host: The base API address.

  • file_path: The absolute file path of the textual SysMLv2 file containing the new objects.

  • editing_context_id: The UUID of the SysON editing context in which you want to create your new objects.

  • object_id: The UUID of the SysMLv2 Element in which you want to create your new objects.

4 Sends a POST request to the API, passing all script parameters as a query parameters.
5 Constructs the API endpoint address for fetching projects.
6 Call the import_sysml_to_project function passing all script parameters as a function parameters.
7 Handles the API response:
  • If the mutation is successful, a message is displayed.

  • If an error occurs, an error message is displayed.

Run the script:

$ python new_objects_from_text.py url-of-syson-server file-path-of-sysmlv2-file your-project-id your-namespace-elementId

For example, to run the script, you would use the following command:

$ python new_objects_from_text.py http://localhost:8080 /Users/myUserFolder/myFolder/mySysMLv2File.sysml 76bb3f29-17d1-465a-a2ca-a331978c36f3 2c0ddeb4-b9fe-478e-951e-0ca312013a5b

Output example:

SysML v2 file imported successfully: /Users/myUserFolder/myFolder/mySysMLv2File.sysml

Then you can check the project in the SysON web application to see the newly created objects, or you can use the get_owned_element.py script to retrieve the owned elements of the project.