Skip to content

matlab/agent-skills-playground / demos/requirements-based-verification/skills/simulink-test-sequence-from-ears

simulink-test-sequence-from-ears

Verified

Generate Simulink Test Sequence blocks and harnesses from EARS requirements. Use when asked to create a test case from a requirement, generate tests from a .slreqx file, or verify a Simulink model with requirement-based tests.

scripts/readMappingReviewYaml.m

View on GitHub
function doc = readMappingReviewYaml(yamlPath)
%READMAPPINGREVIEWYAML Read the mapping review YAML generated by this skill.
%   DOC = readMappingReviewYaml(YAMLPATH) parses the restricted YAML subset
%   written by writeMappingReviewYaml.

    lines = splitlines(string(fileread(yamlPath)));
    lines = stripTrailingComments(lines);
    lines = lines(lines ~= "");

    [value, nextIdx] = parseNode(lines, 1, 0); %#ok<ASGLU>
    if isstruct(value)
        doc = value;
    else
        doc = struct('requirements', value);
    end
end

function [value, idx] = parseNode(lines, idx, indentLevel)
    if idx > numel(lines)
        value = [];
        return;
    end

    [indent, content] = splitIndent(lines(idx));
    if indent ~= indentLevel
        value = [];
        return;
    end

    if startsWith(content, "- ")
        [value, idx] = parseSequence(lines, idx, indentLevel);
    else
        [value, idx] = parseMapping(lines, idx, indentLevel);
    end
end

function [value, idx] = parseMapping(lines, idx, indentLevel)
    value = struct();

    while idx <= numel(lines)
        [indent, content] = splitIndent(lines(idx));
        if indent < indentLevel || startsWith(content, "- ")
            break;
        end
        if indent > indentLevel
            break;
        end

        sep = strfind(content, ":");
        key = matlab.lang.makeValidName(strtrim(extractBefore(content, sep(1))));
        rest = strtrim(extractAfter(content, sep(1)));

        if strlength(rest) == 0
            [child, idx] = parseNode(lines, idx + 1, indentLevel + 2);
            value.(key) = child;
        else
            [value.(key), idx] = parseValue(rest, lines, idx, indentLevel + 2);
        end
    end
end

function [value, idx] = parseSequence(lines, idx, indentLevel)
    items = {};

    while idx <= numel(lines)
        [indent, content] = splitIndent(lines(idx));
        if indent ~= indentLevel || ~startsWith(content, "- ")
            break;
        end

        itemContent = extractAfter(content, 2);
        itemContent = strtrim(itemContent);

        if strlength(itemContent) == 0
            [item, idx] = parseNode(lines, idx + 1, indentLevel + 2);
            items{end+1} = item; %#ok<AGROW>
            continue;
        end

        if contains(itemContent, ":")
            sep = strfind(itemContent, ":");
            key = matlab.lang.makeValidName(strtrim(extractBefore(itemContent, sep(1))));
            rest = strtrim(extractAfter(itemContent, sep(1)));
            item = struct();
            if strlength(rest) == 0
                [child, idx] = parseNode(lines, idx + 1, indentLevel + 2);
                item.(key) = child;
            else
                [item.(key), idx] = parseValue(rest, lines, idx, indentLevel + 4);
            end

            while idx <= numel(lines)
                [nextIndent, nextContent] = splitIndent(lines(idx));
                if nextIndent < indentLevel + 2 || startsWith(nextContent, "- ")
                    break;
                end
                if nextIndent > indentLevel + 2
                    break;
                end
                sep = strfind(nextContent, ":");
                nextKey = matlab.lang.makeValidName(strtrim(extractBefore(nextContent, sep(1))));
                rest = strtrim(extractAfter(nextContent, sep(1)));
                if strlength(rest) == 0
                    [child, idx] = parseNode(lines, idx + 1, indentLevel + 4);
                    item.(nextKey) = child;
                else
                    [item.(nextKey), idx] = parseValue(rest, lines, idx, indentLevel + 4);
                end
            end

            items{end+1} = item; %#ok<AGROW>
        else
            items{end+1} = parseScalar(itemContent); %#ok<AGROW>
            idx = idx + 1;
        end
    end

    value = normalizeSequence(items);
end

function [value, idx] = parseValue(text, lines, idx, blockIndentLevel)
    text = strtrim(string(text));

    if startsWith(text, "|") || startsWith(text, ">")
        [value, idx] = parseBlockScalar(lines, idx + 1, blockIndentLevel, text);
    else
        value = parseScalar(text);
        idx = idx + 1;
    end
end

function [value, idx] = parseBlockScalar(lines, idx, indentLevel, style)
    blockLines = strings(0,1);

    while idx <= numel(lines)
        rawLine = char(lines(idx));
        currentIndent = numel(regexp(rawLine, '^\s*', 'match', 'once'));
        if currentIndent < indentLevel
            break;
        end

        if length(rawLine) < indentLevel
            content = "";
        else
            content = string(rawLine(indentLevel+1:end));
        end
        blockLines(end+1,1) = content; %#ok<AGROW>
        idx = idx + 1;
    end

    if isempty(blockLines)
        value = "";
        return;
    end

    if startsWith(style, ">")
        value = strtrim(strjoin(cellstr(blockLines), " "));
    else
        value = strjoin(cellstr(blockLines), newline);
    end
end

function value = parseScalar(text)
    text = strtrim(string(text));

    if text == "{}"
        value = struct([]);
        return;
    end
    if text == "[]"
        value = strings(0,1);
        return;
    end
    if text == "~" || strcmpi(text, "null")
        value = [];
        return;
    end
    if text == "true"
        value = true;
        return;
    end
    if text == "false"
        value = false;
        return;
    end
    if startsWith(text, "'") && endsWith(text, "'")
        value = replace(extractBetween(text, 2, strlength(text)-1), "''", "'");
        return;
    end
    if startsWith(text, "[") && endsWith(text, "]")
        inner = strtrim(extractBetween(text, 2, strlength(text)-1));
        if strlength(inner) == 0
            value = strings(0,1);
        else
            parts = split(inner, ",");
            value = strtrim(parts(:));
        end
        return;
    end

    numericValue = str2double(text);
    if ~isnan(numericValue)
        value = numericValue;
    else
        value = text;
    end
end

function value = normalizeSequence(items)
    if isempty(items)
        value = strings(0,1);
        return;
    end

    if all(cellfun(@isstruct, items))
        value = vertcat(items{:});
    elseif all(cellfun(@(x) isstring(x) || ischar(x), items))
        value = string(items(:));
    else
        value = items;
    end
end

function lines = stripTrailingComments(lines)
    for i = 1:numel(lines)
        line = char(lines(i));
        if startsWith(strtrim(line), '#')
            lines(i) = "";
        else
            lines(i) = string(regexprep(line, '\s+$', ''));
        end
    end
end

function [indent, content] = splitIndent(line)
    line = char(line);
    indent = numel(regexp(line, '^\s*', 'match', 'once'));
    content = string(line(indent+1:end));
end
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall