主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

systemcomposer.arch.Architecture

模型中的架构

    说明

    Architecture 对象表示 System Composer™ 模型中的架构。该类派生自 systemcomposer.arch.Element

    创建对象

    使用 systemcomposer.createModel 函数创建模型,并使用 systemcomposer.arch.Model 对象上的 Architecture 属性获取根架构。

    model = systemcomposer.createModel('archModel');
    arch = get(model,'Architecture')

    属性

    全部展开

    架构名称,指定为字符向量。架构名称源自架构所属的父组件或模型名称。

    示例: 'archModel'

    数据类型: char

    定义架构类型,指定为 compositionbehaviorview

    数据类型: enum

    架构所属父组件,指定为 systemcomposer.arch.Component 对象。

    架构的子组件,指定为 systemcomposer.arch.Component 对象数组。

    指定为 systemcomposer.arch.ArchitecturePort 对象数组的架构端口。

    连接架构子组件的连接器,指定为 systemcomposer.arch.Connectorsystemcomposer.arch.PhysicalConnector 对象数组。

    组件参数,指定为 systemcomposer.arch.Parameter 对象数组。

    架构的父 System Composer 模型,指定为 systemcomposer.arch.Model 对象。

    Simulink® 句柄,指定为 double

    此属性对于多个 Simulink 相关工作流以及使用 Requirements Toolbox™ 编程接口是必需的。

    示例: handle = get(object,'SimulinkHandle')

    数据类型: double

    父级 System Composer 模型的 Simulink 句柄,指定为 double

    此属性对于多个 Simulink 相关工作流以及使用 Requirements Toolbox 编程接口是必需的。

    示例: handle = get(object,'SimulinkModelHandle')

    数据类型: double

    唯一外部标识符,指定为字符向量。外部 ID 在元素的整个生命周期以及所有保留 UUID 的操作中都会被保留。

    数据类型: char

    统一唯一标识符,指定为字符向量

    示例: '91d5de2c-b14c-4c76-a5d6-5dd0037c52df'

    数据类型: char

    软件架构的函数,指定为 systemcomposer.arch.Function 对象数组。

    依赖项

    只有软件架构才具有这种属性。

    自 R2024b 起

    软件架构的初始化函数,指定为 systemcomposer.arch.Function 对象数组。

    依赖项

    只有软件架构才具有这种属性。

    对象函数

    addComponent为架构添加组件
    addVariantComponent为架构添加变体组件
    addPort为架构添加端口
    addFunction为软件组件的架构添加函数
    addParameter为架构添加参数
    getParameter从架构或组件获取参数
    connect创建架构模型连接
    smartConnect使用端口名称或自定义策略自动连接模型中的选定组件
    getUnconnectedPorts获取未连接的端口
    getSmartConnectPolicy获取智能连接政策
    setSmartConnectPolicy设置智能连接策略
    setMaskImage将封装图像应用到组件
    applyStereotype为架构模型元素应用构造型
    getStereotypes获取应用于架构模型元素的构造型
    changeStereotype将当前应用的构造型更改为其构造型层次结构中的新构造型
    removeStereotype从模型元素中移除构造型
    batchApplyStereotype将构造型应用于架构中的所有元素
    iterate遍历模型元素
    instantiate根据规范创建分析实例
    setProperty设置与应用于元素的构造型相对应的属性值
    getProperty获取与应用于元素的构造型相对应的属性值
    getPropertyValue获取架构属性值
    getEvaluatedPropertyValue从元素中获取属性的计算值
    getStereotypeProperties获取元素上的构造型属性名称
    removeProfile从模型中移除配置文件
    applyProfile将配置文件应用到模型
    hasStereotype查找元素是否已应用构造型
    hasProperty查找元素是否具有属性
    getEvaluatedParameterValue从元素中获取参数的计算值
    getParameterNames获取元素上的参数名
    getParameterValue获取参数值
    setParameterValue设置参数值
    setUnit设置参数值的单位
    resetParameterToDefault将组件上的参数重置为默认值

    示例

    全部折叠

    使用 System Composer™ 以编程方式构建架构模型。

    要构建模型,需添加包含数据接口、数据元素、值类型和物理接口的数据字典,然后添加组件、端口和连接。创建带有构造型和属性的配置文件,然后将这些构造型应用于模型元素。为端口分配专属接口。模型构建完成后,您可以创建自定义视图来关注特定的考虑因素。您还可以查询模型,根据指定的准则收集不同的模型元素。

    添加组件、端口、连接和接口

    创建一个模型并提取其架构。

    model = systemcomposer.createModel("mobileRobotAPI");
    arch = model.Architecture;

    创建一个数据字典并添加一个数据接口。向该数据接口添加一个数据元素。向数据字典中添加一个值类型。将数据元素的类型指定为该值类型。添加一个物理接口和带有物理域类型的物理元素。将数据字典链接到模型。

    dictionary = systemcomposer.createDictionary("SensorInterfaces.sldd");
    interface = dictionary.addInterface("GPSInterface");
    element = interface.addElement("SignalStrength");
    valueType = dictionary.addValueType("SignalStrengthType",Units="dB", ...
        Description="GPS Signal Strength");
    element.setType(valueType);
    physicalInterface = dictionary.addPhysicalInterface("PhysicalInterface");
    physicalElement = addElement(physicalInterface,"ElectricalElement", ...
        Type="electrical.electrical");
    linkDictionary(model,"SensorInterfaces.sldd");

    保存对数据字典的更改。

    dictionary.save

    保存模型。

    model.save

    打开模型。

    systemcomposer.openModel("mobileRobotAPI");

    接口编辑器中查看接口。

    Interface Editor with new data interfaces, data elements, value type, and physical interface

    添加组件、端口和连接。将物理接口设置为稍后要连接的物理端口。

    componentSensor = addComponent(arch,"Sensor");
    sensorPorts = addPort(componentSensor.Architecture,{'MotionData','SensorPower'}, ...
        {'in','physical'});
    sensorPorts(2).setInterface(physicalInterface)
    
    componentPlanning = addComponent(arch,"Planning");
    planningPorts = addPort(componentPlanning.Architecture, ...
        {'Command','SensorPower1','MotionCommand'}, ...
        {'in','physical','out'});
    planningPorts(2).setInterface(physicalInterface)
    
    componentMotion = addComponent(arch,"Motion");
    motionPorts = addPort(componentMotion.Architecture,{'MotionCommand','MotionData'}, ...
        {'in','out'});

    MotionData 端口上创建一个专属接口。在专属数据接口下添加一个专属数据元素。为数据元素 Rotation 指定一个单位设置为 degrees 的值类型。

    ownedInterface = motionPorts(2).createInterface("DataInterface");
    ownedElement = ownedInterface.addElement("Rotation");
    subInterface = ownedElement.createOwnedType(Units="degrees");

    接口编辑器中查看接口。选择 Motion 组件上的 MotionData 端口。在接口编辑器中,将视图从字典视图切换为端口接口视图

    Port interface view in the Interface Editor with the MotionData port and its elements

    使用接口规则和默认名称规则连接组件。接口规则会将共享同一接口的组件端口连接起来。默认情况下,名称规则会将共享相同名称的组件端口连接起来。

    c_sensorData = connect(arch,componentSensor,componentPlanning,Rule="interface");
    c_motionData = connect(arch,componentMotion,componentSensor);
    c_motionCommand = connect(arch,componentPlanning,componentMotion);

    添加和连接架构端口

    在架构上添加一个架构端口。

    archPort = addPort(arch,"Command","in");

    connect 命令需要一个组件端口作为参量。先获取组件端口,然后进行连接。

    compPort = getPort(componentPlanning,"Command");
    c_Command = connect(archPort,compPort);

    保存模型。

    model.save

    Ctrl+Shift+A 或使用以下命令整理布局。

    Simulink.BlockDiagram.arrangeSystem("mobileRobotAPI"); 

    The mobileRobotAPI architecture model after running the script

    创建和应用带有构造型的配置文件

    配置文件是可以应用于任何模型的 XML 文件。您可以在配置文件中添加具有属性的构造型,然后在配置文件编辑器中用特定值来为这些属性赋值。除了 System Composer 的内置分析功能之外,构造型还可以帮助您优化系统的性能、成本和可靠性。

    创建配置文件并添加构造型

    创建一个配置文件。

    profile = systemcomposer.createProfile("GeneralProfile");

    创建一个适用于所有元素类型的构造型。

    elemSType = addStereotype(profile,"projectElement");

    为不同类型的组件创建构造型。您可以根据自己的设计需求选择适当的类型。

    pCompSType = addStereotype(profile,"physicalComponent",AppliesTo="Component");
    sCompSType = addStereotype(profile,"softwareComponent",AppliesTo="Component");

    为连接创建一个构造型。

    sConnSType = addStereotype(profile,"standardConn",AppliesTo="Connector");

    添加属性

    为构造型添加属性。您可以使用属性来捕获模型元素的元数据,并分析非功能性需求。在导入了该配置文件的任何模型中,这些属性都会被添加到应用了该构造型的所有元素中。

    addProperty(elemSType,'ID',Type="uint8");
    addProperty(elemSType,'Description',Type="string");
    addProperty(pCompSType,'Cost',Type="double",Units="USD");
    addProperty(pCompSType,'Weight',Type="double",Units="g");
    addProperty(sCompSType,'develCost',Type="double",Units="USD");
    addProperty(sCompSType,'develTime',Type="double",Units="hour");
    addProperty(sConnSType,'unitCost',Type="double"',Units="USD");
    addProperty(sConnSType,'unitWeight',Type="double",Units="g");
    addProperty(sConnSType,'length',Type="double",Units="m");

    保存配置文件

    profile.save;

    将配置文件应用到模型

    将配置文件应用到模型。

    applyProfile(model,"GeneralProfile");

    将构造型应用于组件。有些组件是物理组件,有些组件是软件组件。

    applyStereotype(componentPlanning,"GeneralProfile.softwareComponent")
    applyStereotype(componentSensor,"GeneralProfile.physicalComponent")
    applyStereotype(componentMotion,"GeneralProfile.physicalComponent")

    将连接器构造型应用于所有连接。

    batchApplyStereotype(arch,'Connector',"GeneralProfile.standardConn");

    将通用元素构造型应用于所有连接器和端口。

    batchApplyStereotype(arch,'Component',"GeneralProfile.projectElement");
    batchApplyStereotype(arch,'Connector',"GeneralProfile.projectElement");

    为每个组件设置属性。

    setProperty(componentSensor,'GeneralProfile.projectElement.ID','001');
    setProperty(componentSensor,'GeneralProfile.projectElement.Description', ...
        'Central unit for all sensors');
    setProperty(componentSensor,'GeneralProfile.physicalComponent.Cost','200');
    setProperty(componentSensor,'GeneralProfile.physicalComponent.Weight','450');
    setProperty(componentPlanning,'GeneralProfile.projectElement.ID','002');
    setProperty(componentPlanning,'GeneralProfile.projectElement.Description', ...
        'Planning computer');
    setProperty(componentPlanning,'GeneralProfile.softwareComponent.develCost','20000');
    setProperty(componentPlanning,'GeneralProfile.softwareComponent.develTime','300');
    setProperty(componentMotion,'GeneralProfile.projectElement.ID','003');
    setProperty(componentMotion,'GeneralProfile.projectElement.Description', ...
        'Motor and motor controller');
    setProperty(componentMotion,'GeneralProfile.physicalComponent.Cost','4500');
    setProperty(componentMotion,'GeneralProfile.physicalComponent.Weight','2500');

    将所有连接的属性设置为相同值。

    connections = [c_sensorData c_motionData c_motionCommand c_Command];
    for k = 1:length(connections)
        setProperty(connections(k),'GeneralProfile.standardConn.unitCost','0.2');
        setProperty(connections(k),'GeneralProfile.standardConn.unitWeight','100');
        setProperty(connections(k),'GeneralProfile.standardConn.length','0.3');
    end

    添加层次结构

    Motion 组件内添加两个名为 ControllerScope 的组件。定义端口。将组件连接到架构以及彼此之间,并应用连接器构造型。架构图中的层次结构可以为组件的内部行为提供更详细的描述。

    motionArch = componentMotion.Architecture;
    
    motionController = motionArch.addComponent('Controller');
    controllerPorts = addPort(motionController.Architecture,{'controlIn','controlOut'}, ...
        {'in','out'});
    controllerCompPortIn = motionController.getPort('controlIn');
    controllerCompPortOut = motionController.getPort('controlOut');
    
    motionScope = motionArch.addComponent('Scope');
    scopePorts = addPort(motionScope.Architecture,{'scopeIn','scopeOut'},{'in','out'});
    scopeCompPortIn = motionScope.getPort('scopeIn');
    scopeCompPortOut = motionScope.getPort('scopeOut');
    
    c_planningController = connect(motionPorts(1),controllerCompPortIn);

    对于输出端口连接,可以指定目标数据元素。

    c_planningScope = connect(scopeCompPortOut,motionPorts(2),DestinationElement="Rotation");
    c_planningConnect = connect(controllerCompPortOut,scopeCompPortIn, ...
        "GeneralProfile.standardConn");

    保存模型。

    model.save

    Ctrl+Shift+A 或使用以下命令整理布局。

    Simulink.BlockDiagram.arrangeSystem("mobileRobotAPI/Motion");

    Inside the Motion component architecture model

    创建模型引用

    模型引用可以帮助您按层次结构组织大型模型,并可实现对其架构和行为的一次定义,多次重用。当一个组件引用另一个模型时,该组件上的任何现有端口都会被删除,而引用模型上的端口会出现在该组件上。

    创建一个新的 System Composer 模型。将 Controller 组件转换为引用组件,以引用新模型。要在 Controller 组件上添加端口,必须更新引用模型 mobileMotion

    referenceModel = systemcomposer.createModel("mobileMotion");
    referenceArch = referenceModel.Architecture;
    newComponents = addComponent(referenceArch,"Gyroscope");
    referenceModel.save
    
    linkToModel(motionController,"mobileMotion");

    Controller component linked to the referenced model mobileMotion

    保存模型。

    referenceModel.save
    model.save

    创建变体组件

    您可以使用 makeVariant 函数将 Motion 组件转换为变体组件。原始组件会作为可选的变体选择项之一嵌入到变体组件中。您可以在变体组件中设计其他变体选择项,并切换活动选择项。变体组件允许您在架构模型中以编程方式选择不同的行为设计,从而进行权衡研究和分析。

    [variantComp,choice1] = makeVariant(componentMotion);

    添加一个名为 MotionAlt 的变体选择项。第二个参量定义名称,第三个参量定义标签。标签用于标识选择项。活动选择项由标签控制。

    choice2 = addChoice(variantComp,{'MotionAlt'},{'MotionAlt'});

    在 MotionAlt 上创建所需的端口。

    motionAltPorts = addPort(choice2.Architecture,{'MotionCommand','MotionData'},{'in','out'});

    将 MotionAlt 设置为活动变体。

    setActiveChoice(variantComp,"MotionAlt")

    Ctrl+Shift+A 或使用以下命令整理布局。

    Simulink.BlockDiagram.arrangeSystem("mobileRobotAPI/Motion");

    The Motion variant component with two variants

    保存模型。

    model.save

    清理

    在再次运行此示例之前,请运行此脚本以删除生成的工件。

    cleanUpArtifacts
    

    详细信息

    全部展开

    版本历史记录

    在 R2019a 中推出