主要内容

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

systemcomposer.arch.Component

System Composer 组件

    说明

    Component 对象代表 System Composer™ 模型中的一个组件。此类继承自 systemcomposer.arch.BaseComponent。该类派生自 systemcomposer.arch.Element

    创建对象

    使用 addComponent 函数在架构模型中创建一个组件。

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

    属性

    全部展开

    组件名称,指定为字符向量。

    示例: 'newComponent'

    数据类型: char

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

    定义组件结构的架构,指定为 systemcomposer.arch.Architecture 对象。对于引用不同架构模型或子系统的组件,该属性会返回一个指向该模型或子系统根架构的句柄。

    组件所属架构,指定为 systemcomposer.arch.Architecture 对象。对于引用架构模型的组件,此属性为空。对于引用架构子系统的组件,该属性是该组件所拥有的架构。

    指定为 systemcomposer.arch.ComponentPort 对象数组的组件端口数组。

    指定为 systemcomposer.arch.ComponentPort 对象数组的组件端口数组。对于引用组件,该属性为空。

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

    组件在画布上的位置,指定为坐标向量,单位为像素 [left top right bottom]

    如果是链接组件,组件引用的模型名称,指定为字符向量。

    数据类型: char

    组件是否为 Adapter 模块,指定为 1 (true) 或 0 (false)。

    数据类型: logical

    组件的父 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

    对象函数

    createArchitectureModel从组件创建架构模型
    createArchitectureSubsystem从组件创建架构子系统
    createSimulinkBehavior创建 Simulink 行为并链接到组件
    createStateflowChartBehavior为组件添加 Stateflow 图行为
    linkToModel将组件链接到模型
    linkToFMU将组件连接到调频股
    inlineComponent从组件中移除引用架构或行为
    makeVariant将组件转换为变体选择项
    isFMU确定组件与功能模型单元 (FMU) 的链接
    isProtected确定组件引用模型受到保护
    isReference确定组件对另一个模型的引用
    IsAdapterComponent确定组件为适配器
    connect创建架构模型连接
    getPort从组件获取端口
    applyStereotype为架构模型元素应用构造型
    getStereotypes获取应用于架构模型元素的构造型
    removeStereotype从模型元素中移除构造型
    setProperty设置与应用于元素的构造型相对应的属性值
    getProperty获取与应用于元素的构造型相对应的属性值
    getPropertyValue获取架构属性值
    getEvaluatedPropertyValue从元素中获取属性的计算值
    getStereotypeProperties获取元素上的构造型属性名称
    hasStereotype查找元素是否已应用构造型
    hasProperty查找元素是否具有属性
    getParameter从架构或组件获取参数
    getEvaluatedParameterValue从元素中获取参数的计算值
    getParameterNames获取元素上的参数名
    getParameterValue获取参数值
    setParameterValue设置参数值
    setUnit设置参数值的单位
    resetParameterToDefault将组件上的参数重置为默认值
    getQualifiedName获取模型元素的限定名称
    lookup搜索架构元素
    destroy移除模型元素

    示例

    全部折叠

    使用 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 中推出