|
| 1 | +# FRET to Simulink Translator |
| 2 | + |
| 3 | +Translates [NASA FRET](https://github.com/NASA-SW-VnV/fret) temporal-logic requirements into Simulink® verification artifacts — Requirements Table blocks for formal analysis with Simulink® Design Verifier™, and Test Assessment blocks for simulation-based runtime verification with Simulink® Test™. |
| 4 | + |
| 5 | +Unlike [CoCoSim](https://github.com/NASA-SW-VnV/CoCoSim), which generates Verification Subsystem blocks with Proof Objectives for SLDV property proving, this pipeline targets Simulink's newer native artifacts — Requirements Table blocks (R2022a+) and Test Assessment blocks — enabling SLDV completeness/consistency analysis (Design Error Detection) and Simulink Test runtime verification workflows. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +| Dependency | Required | |
| 10 | +|---|---| |
| 11 | +| MATLAB R2023a or later | Yes | |
| 12 | +| Simulink | Yes | |
| 13 | +| Requirements Toolbox™ | Yes | |
| 14 | +| Simulink Test | Yes (for Test Assessment blocks) | |
| 15 | +| Simulink Design Verifier | Optional (for formal analysis of RT blocks) | |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +```matlab |
| 20 | +addpath("path/to/fret-to-simulink/helpers") |
| 21 | +
|
| 22 | +% Single-component project → one model with one RT block |
| 23 | +rtBlk = fretJsonToRT("fsm_reqts_and_vars.json") |
| 24 | +
|
| 25 | +% Multi-component project → one model per component (default) |
| 26 | +rtBlks = fretJsonToRT("LM_requirements.json", "LMCPS_RT") |
| 27 | +
|
| 28 | +% Multi-component project → merged into a single model |
| 29 | +rtBlk = fretJsonToRT("LM_requirements.json", "LMCPS_RT", PerComponent=false) |
| 30 | +
|
| 31 | +% Test Assessment block |
| 32 | +taBlk = fretJsonToTA("fsm_reqts_and_vars.json") |
| 33 | +``` |
| 34 | + |
| 35 | +When the FRET JSON contains multiple `component_name` values (common with multi-model benchmarks like LMCPS), `fretJsonToRT` creates a separate Simulink model per component by default. This avoids cross-component symbol conflicts and ensures each model can be independently analyzed with SLDV. Set `PerComponent=false` to merge all requirements into one model. |
| 36 | + |
| 37 | +The pipeline loads the FRET JSON, converts each requirement through `fretToSpec`, routes to RT (invariant patterns) or TA (temporal patterns) based on renderability, creates the Simulink model(s), and reports conversion statistics. |
| 38 | + |
| 39 | +For step-by-step examples, see **[GETTING-STARTED.md](GETTING-STARTED.md)**. |
| 40 | + |
| 41 | +AI coding agents with access to the [Simulink Agentic Toolkit](https://github.com/matlab/simulink-agentic-toolkit) MCP server can call the helpers directly via `evaluate_matlab_code`. |
| 42 | + |
| 43 | +## How It Works |
| 44 | + |
| 45 | +``` |
| 46 | +FRET JSON --> fretToSpec() --> reqCreateSpec struct --> reqCheckRenderability() |
| 47 | + |-- RT-renderable --> reqRenderToRT() --> RT block |
| 48 | + '-- TA-renderable --> reqRenderToTA() --> TA block |
| 49 | +``` |
| 50 | + |
| 51 | +The `fretToSpec` adapter handles: |
| 52 | +- SMV-to-MATLAB syntax conversion (`!` to `~`, `&` to `&&`, `=` to `==`) |
| 53 | +- Bi-implication expansion (`<=>` to conjunction of implications) |
| 54 | +- Implication splitting (`guard -> prop` to precondition/postcondition) |
| 55 | +- Constant substitution from FRET variable mapping (Internal variables) |
| 56 | +- FRET function mapping (`absReal` to `abs`, `preBool` to `prev`, `median` expansion) |
| 57 | +- FTP (First Time Point) sentinel handling |
| 58 | + |
| 59 | +The TA pipeline uses **if-guard semantics**: guarded requirements emit `if guard; verify(response); end` so that unexercised requirements show UNTESTED (not vacuous PASS), matching the structured assessment editor behavior. |
| 60 | + |
| 61 | +## Supported FRET Templates |
| 62 | + |
| 63 | +| Template Key | Pattern | RT | TA | Notes | |
| 64 | +|---|---|---|---|---| |
| 65 | +| `null,null,always` | Unconditional invariant | Y | Y | Implication splitting | |
| 66 | +| `in,null,always` | Scoped invariant | Y | Y | scope_mode as guard | |
| 67 | +| `null,regular,always` | Persistent obligation (edge) | - | Y | Temporal persistence | |
| 68 | +| `null,holding,always` | Persistent obligation (level) | - | Y | | |
| 69 | +| `null,regular,immediately` | Edge-triggered immediate | Y | Y | RT uses `prev()` pattern | |
| 70 | +| `in,null,immediately` | Scope entry immediate | Y | Y | | |
| 71 | +| `in,regular,immediately` | Scoped edge-triggered | Y | Y | | |
| 72 | +| `null,holding,immediately` | Level-triggered immediate | Y | Y | | |
| 73 | +| `null,regular,next` | Next-step response | - | Y | P at t+1 | |
| 74 | +| `null,regular,within` | Bounded response | - | Y | | |
| 75 | +| `null,null,within` | Unconditional bounded | - | Y | | |
| 76 | +| `null,null,for` | Duration constraint | - | Y | | |
| 77 | +| `null,regular,until` | Until response | - | Y | | |
| 78 | +| `null,null,eventually` | Eventual satisfaction | - | Y | | |
| 79 | +| `null,null,never` | Negated invariant | Y | Y | | |
| 80 | + |
| 81 | +## API |
| 82 | + |
| 83 | +| Function | Description | |
| 84 | +|---|---| |
| 85 | +| `fretJsonToRT(jsonFile, modelName, NV)` | One-call pipeline: FRET JSON to Requirements Table block(s) | |
| 86 | +| `fretJsonToTA(jsonFile, modelName, NV)` | One-call pipeline: FRET JSON to Test Assessment block | |
| 87 | +| `fretToSpec(id, nl, semantics, vars)` | Convert FRET semantics to reqCreateSpec struct | |
| 88 | +| `reqCreateSpec(id, nl, pattern, NV)` | Create a requirement specification struct | |
| 89 | +| `reqRenderToRT(spec)` | Render a spec as RT precondition/postcondition | |
| 90 | +| `reqRenderToTA(spec)` | Render a spec as TA trigger/response configuration | |
| 91 | +| `reqCheckRenderability(spec)` | Check which targets (RT, TA) a spec supports | |
| 92 | +| `reqRenderStructuredEnglish(spec)` | Render a spec as human-readable Structured English | |
| 93 | + |
| 94 | +### `fretJsonToRT` Name-Value Options |
| 95 | + |
| 96 | +| Option | Default | Description | |
| 97 | +|---|---|---| |
| 98 | +| `ReqFilter` | `{}` | Cell array of reqids to include (empty = all) | |
| 99 | +| `Tolerance` | `0` | Numeric tolerance for double equality | |
| 100 | +| `SLDVReady` | `true` | Configure model for SLDV (fixed-step discrete solver) | |
| 101 | +| `PerComponent` | `true` | Create one model per FRET component | |
| 102 | + |
| 103 | +## Validation |
| 104 | + |
| 105 | +Validated on case studies from the [NASA FRET repository](https://github.com/NASA-SW-VnV/fret/tree/master/caseStudies) and the [LMCPS benchmark](https://github.com/hbourbouh/lm_challenges): |
| 106 | + |
| 107 | +| Case Study | Total | RT Rendered | RT Compile | TA Rendered | TA Compile | |
| 108 | +|---|---|---|---|---|---| |
| 109 | +| FSM (Finite State Machine) | 13 | 11 | 1/1 | 13 | 1/1 | |
| 110 | +| Liquid Mixer | 12 | 9 | 1/1 | 12 | 1/1 | |
| 111 | +| LMCPS (all 10 challenges) | 97 | 71 | 13/13 | 74 | 13/13 | |
| 112 | +| **Total** | **122** | **91** | **15/15** | **99** | **15/15** | |
| 113 | + |
| 114 | +Skipped requirements use features not currently expressible: external function calls (`mag`, `dot`, `det_3x3`), `prev()` with complex expressions, or `persisted()` temporal operators. Each LMCPS component produces a separate model; all compile and pass validation (Update Diagram). See [SUPPORTED-PATTERNS.md](SUPPORTED-PATTERNS.md) for the full pattern coverage matrix. |
| 115 | + |
| 116 | +## References |
| 117 | + |
| 118 | +1. [NASA FRET](https://github.com/NASA-SW-VnV/fret) — Formal Requirements Elicitation Tool |
| 119 | +2. [LMCPS Benchmark](https://github.com/hbourbouh/lm_challenges) — Lockheed Martin Cyber-Physical Systems challenges |
| 120 | +3. [Simulink Agentic Toolkit](https://github.com/matlab/simulink-agentic-toolkit) — MCP server, tools, and skills for AI coding agents working with MATLAB and Simulink |
| 121 | +4. C. Menghi, E. Balai, D. Valovcin, C. Sticksel, A. Rajhans, "Completeness and Consistency of Tabular Requirements: an SMT-Based Verification Approach," *IEEE Transactions on Software Engineering*, vol. 51, no. 2, Feb. 2025. [[IEEE](https://ieeexplore.ieee.org/document/10844918)] |
| 122 | +5. A. Rajhans, A. Mavrommati, P. J. Mosterman, and R. G. Valenti, "Specification and Runtime Verification of Temporal Assessments in Simulink," *21st International Conference on Runtime Verification (RV)*, 2021. [[PDF](https://www.mathworks.com/content/dam/mathworks/conference-or-academic-paper/specification-and-runtime-verification-of-temporal-assessments-in-simulink.pdf)] |
0 commit comments