Skip to content

matlab/agent-skills-playground / demos/mbse-with-agentic-ai/skills/system-composer

system-composer

Verified

Use this skill when authoring reusable, idempotent MATLAB scripts that build System Composer architecture models via the architecture-modeling API — `systemcomposer.createModel`, `addComponent`, `addPort`, `setInterface`, `connect(srcPort, dstPort)`, interface dictionaries (.sldd) with `addInterface`/`addElement`, profiles/stereotypes with `Profile.createProfile` and `addStereotype`, or `systemcomposer.allocation.createAllocationSet`. Also trigger when debugging these APIs (connections that don't appear, interfaces that don't resolve, profile save errors, `createAllocationSet` signature-mismatch errors). Do NOT trigger for ad-hoc structural edits to an already-built model (adding one SubSystem, rewiring a port) — use `building-simulink-models` with `model_edit` for that.

code/buildMyVariantComposite.m

View on GitHub
function buildMyVariantComposite(modelName, wrapperName, archDir)
% BUILDMYVARIANTCOMPOSITE Template for a System Composer variant component.
%   Demonstrates the canonical build sequence for converting a composite
%   into a variant wrapper with multiple architectural alternatives. Wire
%   the wrapper from the outer architecture as if it were a regular
%   composite; under the hood, only the active choice's sub-structure is
%   visible to `instantiate` at analysis time.
%
%   Reference the skill doc's "Variant Components" section for the list of
%   gotchas this template encodes. The short version:
%     - After makeVariant the original Component ref is STALE; re-fetch.
%     - Rename choice AND call setCondition to keep them in sync.
%     - applyStereotype on the wrapper errors -- apply on each choice.
%     - Numeric stereotype properties propagate choice -> wrapper instance;
%       STRING properties do not (encode variant flags as numbers).
%
%   Inputs:
%     modelName   - Name of the SC model (string)
%     wrapperName - Name of the variant component to create (string)
%     archDir     - Folder to save the .slx and profile .xml (string)

    slxFile     = fullfile(archDir, char(modelName) + ".slx");
    profileName = "MyVariantProfile";
    profileFile = fullfile(archDir, char(profileName) + ".xml");

    if bdIsLoaded(char(modelName)), close_system(char(modelName), 0); end
    systemcomposer.profile.Profile.closeAll();
    if isfile(slxFile),     delete(slxFile);     end
    if isfile(profileFile), delete(profileFile); end

    % ── 1. Model + baseline composite content ───────────────────────────────
    model = systemcomposer.createModel(char(modelName));
    arch  = model.Architecture;

    comp = addComponent(arch, char(wrapperName));
    % Add boundary ports + baseline sub-components on the COMPOSITE first.
    % After makeVariant this content becomes the first auto-created choice.
    in  = addPort(comp.Architecture, 'Input',  'in');
    out = addPort(comp.Architecture, 'Output', 'out');
    baselineSub = addComponent(comp.Architecture, 'BaselineImpl');
    addPort(baselineSub.Architecture, 'In',  'in');
    addPort(baselineSub.Architecture, 'Out', 'out');
    connect(in,  baselineSub.getPort('In'));
    connect(baselineSub.getPort('Out'), out);

    % ── 2. Convert to variant wrapper ───────────────────────────────────────
    vc = comp.makeVariant();

    % ── 3. Re-fetch -- original `comp` is stale in the new variant scope ────
    wrapper = arch.getComponent(char(wrapperName)); %#ok<NASGU>

    % ── 4. Rename the auto-choice + sync its condition ──────────────────────
    choices = vc.getChoices();
    choices(1).Name = 'V0_Baseline';
    vc.setCondition(choices(1), 'V0_Baseline');

    % ── 5. Add alternative choices -- each is a regular Component ───────────
    v1 = vc.addChoice({'V1_Alternative'});
    % Boundary ports on this choice (match the wrapper's boundary shape)
    v1In  = addPort(v1.Architecture, 'Input',  'in');
    v1Out = addPort(v1.Architecture, 'Output', 'out');
    % Internal content -- different from V0 to justify the variant existing
    altA = addComponent(v1.Architecture, 'AltA');
    altB = addComponent(v1.Architecture, 'AltB');
    addPort(altA.Architecture, 'In',  'in');
    addPort(altA.Architecture, 'Out', 'out');
    addPort(altB.Architecture, 'In',  'in');
    addPort(altB.Architecture, 'Out', 'out');
    connect(v1In, altA.getPort('In'));
    connect(altA.getPort('Out'), altB.getPort('In'));
    connect(altB.getPort('Out'), v1Out);

    % ── 6. Stereotype profile + per-variant properties ──────────────────────
    profile = systemcomposer.profile.Profile.createProfile(char(profileName));
    st = addStereotype(profile, "Props", AppliesTo="Component");
    addProperty(st, "Cost",                   Type="double", DefaultValue="0");
    addProperty(st, "Throughput",             Type="double", DefaultValue="0");
    % Numeric flag for topology-dependent rollup: 0 = MIN/serial, 1 = SUM/parallel.
    % Must be numeric -- string stereotype properties do not propagate from a
    % variant choice to the wrapper's instance at instantiate time.
    addProperty(st, "UseParallelThroughput", Type="double", DefaultValue="0");
    profile.save(char(archDir));

    applyProfile(model, char(profileName));

    stPath = char(profileName) + ".Props";

    % Apply stereotype + values to EACH choice (not the variant wrapper).
    % V0 serial aggregation; V1 parallel aggregation.
    applyStereotype(choices(1), stPath);
    setProperty(choices(1), stPath + ".Cost",                   "0");
    setProperty(choices(1), stPath + ".Throughput",             "0");
    setProperty(choices(1), stPath + ".UseParallelThroughput",  "0");   % MIN

    applyStereotype(v1, stPath);
    setProperty(v1, stPath + ".Cost",                   "0");
    setProperty(v1, stPath + ".Throughput",             "0");
    setProperty(v1, stPath + ".UseParallelThroughput",  "1");   % SUM

    % Sub-components also get stereotyped with their own values.
    applyStereotype(baselineSub, stPath);
    setProperty(baselineSub, stPath + ".Cost",       "100");
    setProperty(baselineSub, stPath + ".Throughput", "50");

    applyStereotype(altA, stPath);
    setProperty(altA, stPath + ".Cost",       "40");
    setProperty(altA, stPath + ".Throughput", "30");
    applyStereotype(altB, stPath);
    setProperty(altB, stPath + ".Cost",       "40");
    setProperty(altB, stPath + ".Throughput", "30");

    % ── 7. Default active variant ──────────────────────────────────────────
    vc.setActiveChoice('V0_Baseline');

    % ── 8. Layout -- reach inactive choices explicitly ─────────────────────
    Simulink.BlockDiagram.arrangeSystem(char(modelName));
    arrangeCompositesIncludingVariants(arch, char(modelName));

    save_system(char(modelName), char(slxFile));
    fprintf("Variant composite %s (choices: V0_Baseline, V1_Alternative) saved.\n", wrapperName);
end

function arrangeCompositesIncludingVariants(arch, pathPrefix)
% .Architecture on a VariantComponent returns only the ACTIVE choice, so a
% walker that follows .Architecture never reaches inactive variants. Iterate
% getChoices() explicitly so every choice's canvas gets laid out.
    for c = arch.Components
        subPath = [pathPrefix, '/', c.Name];
        if isa(c, 'systemcomposer.arch.VariantComponent')
            try, Simulink.BlockDiagram.arrangeSystem(subPath); catch, end
            for ch = c.getChoices()
                choicePath = [subPath, '/', ch.Name];
                try, Simulink.BlockDiagram.arrangeSystem(choicePath); catch, end
                arrangeCompositesIncludingVariants(ch.Architecture, choicePath);
            end
        elseif ~isempty(c.Architecture) && ~isempty(c.Architecture.Components)
            Simulink.BlockDiagram.arrangeSystem(subPath);
            arrangeCompositesIncludingVariants(c.Architecture, subPath);
        end
    end
end
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall