主要内容

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

软件架构的函数编写与扩展

函数是可以在软件组件中定义的入口点。对于内嵌组件,您可以在架构级别使用函数编辑器addFunction 函数编写函数。然后,您就可以为自己编写的函数实现 Simulink® 行为。

对于引用组件,函数会自动从引用的行为 Simulink 模型创建。有关信息,请参阅仿真和部署软件架构

在本主题中,我们将介绍在架构级别创建函数的工作流,并描述如何:

  • 编写和可视化函数。

  • 实现函数的行为。

  • 导入和导出函数。

  • 使用构造型为函数添加自定义属性。

有关初始化函数的信息,请参阅在软件架构中调度组件初始化

使用函数编辑器编写和可视化函数

您可以使用函数编辑器将构造型应用于软件架构中的函数、编辑采样时间并指定函数周期。

函数编辑器仅在建模软件架构时可见。要打开函数编辑器,请在工具栏中导航至 建模 > 函数编辑器。模型会自动更新,表中会显示模型中的组件函数。

此示例显示了一个包含两个组件的软件架构,以及一个包含一个空表的函数编辑器

Software architecture with two components named Component1 and Component2. The Functions Editor is at the bottom of the canvas

编写函数并根据执行顺序对它们进行排序:

  1. 添加一个函数。选择 Component1 作为父级。使用相同的步骤为 Component2 添加一个函数。

    The Add a function to a software component button is highlighted in the Functions Editor. A function named Component1_Function displays for Component1. A function named Component2_Function displays for Component2.

  2. 使用向上和向下箭头或点击并拖动函数来排列函数的执行顺序。

    The up and down arrows in the Functions Editor are highlighted in a red box.

  3. 您可以通过修改表中的名称来更改这些函数的名称。将第一个函数的名称改为 myFunction

    The Function Name column in the Functions Editor shows that the first function name has been changed to myFunction.

  4. 您可以通过在表中指定这些函数的周期来编辑它们的采样时间。将第一个函数的周期改为 1

    A software architecture with three components: Component1, Component2, and Component3. Component3 is linked to a Simulink behavior. The Functions Editor shows the difference between three functions

  5. 您可以根据数据依赖关系自动对函数进行排序。此功能适用于行为模型中的函数。要启用自动排序,请选中架构模型上的按依赖关系对函数排序复选框或启用 OrderFunctionsByDependency

    set_param('MySoftwareArchitecture','OrderFunctionsByDependency','on')

    该参数的默认值为 off

函数编辑器可视化在架构级别创建的函数以及在组件引用的 Simulink 模型中实现的函数。

在此示例中,在 Simulink 行为模型中创建了第三个函数,并且该模型被第三个组件 Component3 引用。表中的软件组件列显示了在架构级别创建的函数与在 Simulink 行为中创建并被组件引用的函数之间的差异。

A software architecture with three components: Component1, Component2, and Component3. Component3 is linked to a Simulink behavior. The Functions Editor shows the difference between three functions.

以编程方式编写函数

您还可以使用 addFunction 函数为组件编写函数。

使用 addFunction 函数将一组函数添加到软件架构组件 architecture,并指定名称 functionNames

addFunction(architecture,functionNames)

有关详细信息,请参阅 addFunction

在架构级别实现函数的行为

您可以在架构级别创建函数,然后为函数实现行为。

  • 使用工具栏实现函数:

    1. 建模选项卡下,选择组件,然后选择创建 Simulink 行为

    2. 将 Simulink 行为的类型选为基于速率导出函数

      Create Simulink Behavior dialog box that displays the type of the behavior.

      或者,您可以右键点击一个组件,然后选择创建 Simulink 行为

  • 您还可以使用 createSimulinkBehavior 函数来编程实现函数。该函数创建一个基于速率或导出函数的新行为,并将软件组件链接到新模型。您只能为软件架构创建基于速率或导出函数的行为。

    createSimulinkBehavior(component,"mySoftwareModel",BehaviorType="RateBased")

将构造型应用于软件架构的函数

您可以通过添加包含自定义属性的构造型来扩展软件架构函数。这些步骤描述了如何向函数添加构造型,与向其他架构元素添加构造型的步骤非常相似。有关详细信息,请参阅应用构造型来扩展模型元素

  1. 使用配置文件编辑器定义函数构造型。

    The Profile Editor shows a list of stereotype properties

  2. 使用函数编辑器选择软件组件中的函数,应用构造型,查看应用于函数的构造型,以及编辑构造型的属性值。

    在此示例中,您可以使用属性检查器为名为 FunctionStereotype 的构造型指定 FunctionValue 属性的值。

    A Component is highlighted. The Functions Editor displays the selected function. The Property Inspector displays the value to be modified

软件架构的导入和导出函数

您可以导入和导出软件架构的函数。

  • 使用 systemcomposer.exportModel 函数输出一个 functions 字段,该字段包含一个表,其中包含函数的名称、执行顺序、父组件 ID、周期和构造型等信息。

    此示例展示了如何导出软件架构模型 mySoftwareArchitectureexportedSet 输出具有 functions 字段,该字段包含函数信息表。

    exportedSet = systemcomposer.exportModel('MySoftwareArchitecture')
    
    exportedSet = 
    
      struct with fields:
    
              components: [4×5 table]
                   ports: [6×4 table]
             connections: [3×5 table]
          portInterfaces: [0×9 table]
        requirementLinks: [0×15 table]
                  domain: 'Software'
               functions: [3×4 table]
    
    >> exportedSet.functions
    
    ans =
    
      3×4 table
    
                Name             ExecutionOrder    CompID    Period
        _____________________    ______________    ______    ______
    
        "myFunction"                  "1"           "1"      "1"   
        "Component2_Function"         "2"           "2"      "-1"  
        "Component3_D1"               "3"           "3"      "0.2" 
    
  • 使用 systemcomposer.importModel 函数导入包含函数的模型,其中 importStruct 参量可以包含包含函数信息的 functions 字段。

另请参阅

工具

对象

函数

主题