Executable local examples

These examples are checked with Sphinx doctest during CI. They run without a Studio account, network requests or the reference compiler. They use SysML sources checked against the executable profile. Reference validation and typed Python examples require the additional compiler setup in Local engine reference.

Run a scoped action

>>> from sysml2py import SimConfig
>>> from sysml2py.modelgraph import ConnectedModel
>>> source = """package Example {
...     part counter {
...         attribute value = 0;
...         attribute rate = 2;
...         action increment { assign value := value + rate; }
...     }
... }"""
>>> model = ConnectedModel.from_documents([
...     {"project_id": "counter", "source": source}
... ])
>>> result = model.simulate(SimConfig(end_time=3, step_size=1))
>>> column = result["attributes"].index("Example.counter.value") + 1
>>> [(row[0], row[column]) for row in result["rows"]]
[(0, 0.0), (1, 2.0), (2, 4.0), (3, 6.0)]

Time occupies the first value in each row; attribute and unit lists describe the remaining columns. Each simulation starts from the model’s declared initial state rather than continuing the preceding run.

Supply a quantity and inspect required inputs

>>> from sysml2py import SimConfig
>>> from sysml2py.modelgraph import ConnectedModel
>>> model = ConnectedModel.from_documents([{
...     "project_id": "travel",
...     "source": """package Travel {
...         part cart {
...             in attribute speed : ISQ::SpeedValue;
...             in attribute dt : ISQ::DurationValue;
...             attribute distance : ISQ::LengthValue = 0 [SI::m];
...             action move { assign distance := distance + speed * dt; }
...         }
...     }""",
... }])
>>> sorted(model.input_schema())
['Travel.cart.dt', 'Travel.cart.speed']
>>> result = model.simulate(
...     SimConfig(end_time=1, step_size=0.3, step_attribute="Travel.cart.dt"),
...     inputs={"Travel.cart.speed": {"value": 200, "unit": "cm/s"}},
... )
>>> distance = result["attributes"].index("Travel.cart.distance") + 1
>>> round(result["rows"][-1][distance], 6)
2.0
>>> result["rows"][-1][0] == 1
True

The last step lasts 0.1 seconds and the clock binding supplies that duration. The engine converts the input from centimetres per second to the declared unit.

Resolve two source documents

>>> from sysml2py.modelgraph import ConnectedModel
>>> documents = [
...     {"project_id": "library", "source":
...      "package Cells { part def Cell { attribute charge = 10; } }"},
...     {"project_id": "assembly", "source":
...      "package Assembly { part cell : Cells::Cell; }"},
... ]
>>> model = ConnectedModel.from_documents(documents)
>>> model.graph["attributes"]["Assembly.cell.charge"]["value"]
10.0
>>> "package Cells" in model.to_sysml() and "package Assembly" in model.to_sysml()
True

Use compose_subsystems to generate a composition with checked port connections from reusable part definitions. It requests reference validation by default.

Exercise a client without a server

Use HTTPX’s explicit mock transport when testing an integration. This example does not make network requests or need a real key:

>>> import httpx
>>> from sysml_studio import Client, Model
>>> model_id = "00000000-0000-0000-0000-000000000001"
>>> def respond(request):
...     assert request.method == "GET"
...     assert request.url.path == "/api/projects/" + model_id
...     return httpx.Response(200, json={
...         "id": model_id, "name": "Example", "language": "sysml",
...         "source": "package Example { attribute value = 2; }", "version": 1,
...     })
>>> with Client("https://studio.example", "test-only-key",
...             transport=httpx.MockTransport(respond)) as studio:
...     saved = studio.get_model(model_id)
>>> isinstance(saved, Model), saved.version
(True, 1)
>>> saved.to_connected_model().graph["attributes"]["Example.value"]["value"]
2.0