Skip to content

Latest commit

 

History

History
226 lines (193 loc) · 8.93 KB

running-external-programs.md

File metadata and controls

226 lines (193 loc) · 8.93 KB

Running external programs from a model

An analysis case's action can be performed by a program outside OpenSysML — a solver, a simulator wrapper, a script — rather than by a body written in SysML. This chapter walks the whole loop on the worked example in examples/external-tool-demo/: registering the program in a manifest, previewing what a run would give it, running the case, recording the run with its tool provenance, and tabulating the record in a document. The manifest members, the protocol the exchange speaks and every failure's shape are reference material in External tools; this is the story.

Declaring the tool

An action declares it is performed by a tool with the AnalysisTooling::ToolExecution metadata, its toolName and uri carried to the tool verbatim; each parameter taking part in the exchange carries @AnalysisTooling::ToolVariable naming it as the tool's manifest spells it:

action def Solve {
    metadata ToolExecution {
        toolName = "ThermalSolver";
        uri = "thermal://demo/solve";
    }
    in mass : MassValue           { @ToolVariable { name = "mass"; } }
    in power : PowerValue         { @ToolVariable { name = "power"; } }
    in ambient : TemperatureValue { @ToolVariable { name = "ambient"; } }
    out tMax : TemperatureValue   { @ToolVariable { name = "tMax"; } }
    out margin : TemperatureValue { @ToolVariable { name = "margin"; } }
}

The tools a sysml or sysml-grpc process may run are the entries of the directory OPENSYSML_TOOLS names, read once at startup — one JSON file per tool, in a directory outside every workspace, writable by its owner alone:

{
  "toolName": "ThermalSolver",
  "version": "1.0",
  "executable": "python3",
  "variables": ["mass", "power", "ambient", "tMax", "margin"],
  "invocation": {
    "args": ["thermal.py", "--mass", "{mass}", "--power", "{power}", "--ambient", "{ambient}"],
    "cwd": ".",
    "stdin": "none"
  },
  "reply": {
    "format": "csv",
    "source": "stdout",
    "outputs": {
      "tMax": {"column": "tmax", "type": "number", "unitColumn": "tmaxUnit"},
      "margin": {"column": "margin", "type": "number", "unit": "K"}
    }
  }
}

The invocation block composes the command: each args entry renders to exactly one argument with {mass} placeholders filled from the call's values, cwd confined to the manifest's directory (. — the script sits beside the entry), stdin saying what the process reads. The reply block says the answer is the CSV record on standard output and where each output lives in it. An entry with no block at all speaks the object protocol — one JSON object each way on stdin/stdout.

What a value looks like

A number, a truth and a string cross as the JSON scalar, a quantity with its unit, and a collection as an array of them. Two more kinds cross as objects.

An enumeration literal is sent as the object naming it, the same three members the gRPC wire contract's enumLiteral has:

{"value": {"literalId": "Rover::Mode::idle", "enumerationId": "Rover::Mode", "name": "idle"}}

literalId is the literal's identity. A tool answers an output typed by an enumeration either with that object (literalId alone is enough) or with the bare name of one of the enumeration's literals, {"value": "idle"}. A literal the model does not declare, or one of another enumeration, is a malformed output. An argument template or CSV cell spells a literal by its name. A literal of an enumeration that specializes a scalar type (enum def Level :> Integer) keeps crossing as its scalar value, as it always has: an answered string that is the value of one of the enumeration's literals is read as that value, not as the name of another literal. A mapped JSON reply reads the object, or an array of them, at an output declared "type": "string".

An object (a part, an item, any instance) is sent as an input only, as a snapshot of the feature values its model declares:

{"value": {"instanceId": "3", "typeId": "Fleet::car", "features": {
  "mass": {"value": 1200, "unit": "kg"},
  "hub": {"value": {"instanceId": "5", "typeId": "Fleet::Hub", "features": {"serial": {"value": "h1"}}}},
  "wheels": {"value": [{"value": {"instanceId": "7", "typeId": "Fleet::Car::wheels",
    "features": {"hub": {"value": {"instanceId": "5"}}}}}]}}}}
  • features holds each feature that has a value, by name; the standard library's own features are left out, and so is a feature with no value.
  • An object held by the object is nested the same way, to a depth of 8. One below that is sent with its typeId and "truncated": true and no features.
  • An object already sent in the same input is sent again as its instanceId alone: a reference to the first copy, which also ends a cycle.
  • omitted lists the features whose values the protocol does not carry, or that JSON cannot spell exactly, such as the Rational 1/3.
  • One input sends at most OPENSYSML_MAX_ELEMENTS objects, and a collection larger than that is not listed; an input past the budget is refused.
  • instanceId means something within the one call only; it is not stable across calls.

Only JSON standard input carries an object: an argument template, a CSV or an FMU input holding one is refused, and a tool cannot answer one.

-engines lists the tool as tool:ThermalSolver, its protocol column spelling the composition — here argv+none/csv:

tool:ThermalSolver  tool  argv+none/csv  observed  compute  ready (ThermalSolver 1.0 at /usr/bin/python3)

Previewing the call

-tool-dry-run at the CLI, %tool in the REPL, runs the case or action as -analysis would until the performance first reaches a tool — then prints what the call would have been given, without starting the process. What the preview performed is discarded: the session is as it found it.

The env block lists the manifest's own env entries with their rendered values; a variable taken from this process (PATH, HOME, TMPDIR, LANG and each OPENSYSML_TOOL_ENV_PASSTHROUGH names) is listed by name only, as NAME=<from this process>.

✓ ThermalDemo::heating: dry run of tool 'ThermalSolver' for ThermalDemo::Solve
  tool: ThermalSolver 1.0
  protocol: argv+none/csv
  manifest: /home/me/.opensysml-tools/thermal.json
  executable: /usr/bin/python3
  argv:
    "thermal.py"
    "--mass"
    "12.5"
    "--power"
    "250"
    "--ambient"
    "290"
  env:
    ...
  cwd: /home/me/.opensysml-tools
  stdin: none
  inputs:
    ambient = 290 [K]
    mass = 12.5 [kg]
    power = 250 [W]
  outputs:
    margin
    tMax
  reply: csv from stdout, header, delimiter ","
    margin: column "margin", row last, type number, unit K
    tMax: column "tmax", row last, unitColumn "tmaxUnit", type number
  the process was not started

A manifest fault, an unregistered tool or an input the call does not send reports the same typed error the real run would fail with — the preview is a diagnostic surface, not a guess. A run that reaches no tool says so.

Running and recording it

-analysis runs the case; the tool's process is started, the inputs bound, the reply read:

✓ ThermalDemo::heating
  tMax = 306.0 [SI::K]
  margin = 94.0 [SI::K]
  objective marginHeld: satisfied
  standing: value (observed: 1 run under reverse)

A tool's answer stands at strength observed: nothing in OpenSysML knows what the tool should have computed, so equal inputs answering differently are reported as a divergence in the run's notes.

-record-run records the run as an AnalysisRecords element — see Recording analysis runs — and its RecordedRun annotation's tools names every external tool call the run made, one element per call in call order (tool version from manifest: executable argv):

part heating_run1 : HeatingRun {
    @AnalysisRecords::RecordedRun {
        runAt = "2026-10-15T11:24:00Z";
        tool = "sysml dev";
        command = "-record-run \"ThermalDemo::heating\"";
        kind = "run";
        tools = ("ThermalSolver 1.0 from /home/me/.opensysml-tools/thermal.json: /usr/bin/python3 thermal.py --mass 12.5 --power 250 --ambient 290");
    }
    ...
}

Reporting it

Recording composes with document rendering — -record-run beside -render-document records first, so the document's queries see the record — and Project tabulates the record's own features. An annotation's attribute values are not projectable, so tools stays in the record's source rather than a table column:

./bin/sysml examples/external-tool-demo/thermal.sysml \
    -record-run ThermalDemo::heating \
    -render-document Reporting::HeatingReport -o report.md
| name | caseName | kind | mass | power | ambient | tMax | margin | objective |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| heating\_run1 | ThermalDemo::heating | run | 12.5 | 250 | 290 | 306 | 94 | satisfied |