主要内容

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

addMessage

为生命线之间的交互添加消息

自 R2024b 起

    说明

    message = addMessage(operand,sourceLifeline,sourcePort,destLifeline,destPort,label)sourceLifelinedestinationLifeline 指定的生命线之间添加一个消息,该生命线位于 operand 指定的操作数内,其中 sourcePortdestPort 是相应组件上的端口。message 消息也被分配给 label 消息标签。

    示例

    message = addMessage(___,After=existingElement) 用于在上述语法中的输入参量之外另使用一个或多个名称-值参量指定选项。例如,要在现有片段后添加 fragment 片段,可将 existingElement 设置为有效的片段对象。

    示例

    示例

    全部折叠

    以编程方式创建一个序列图,描述行人在有交通信号灯的十字路口过马路的情景。

    要了解有关序列图的更多信息,请参阅以交互方式编写序列图

    打开交通灯示例

    加载 TLExample 架构模型。

    model = systemcomposer.openModel('TLExample');

    TLExample architecture model with various components.

    创建一个名为 'InhibitCopy' 的新序列图。

    diagram = model.addInteraction('InhibitCopy');

    添加生命线

    为架构中的每个组件添加生命线。

    sourceLifeline = diagram.addLifeline('TLExample/source');
    pollerLifeline = diagram.addLifeline('TLExample/poller');
    switchLifeline = diagram.addLifeline('TLExample/switch');
    lampLifeline = diagram.addLifeline('TLExample/lampController');
    controllerLifeline = diagram.addLifeline('TLExample/controller');

    获取交互的根片段和根操作数。

    rootFragment = diagram.RootFragment;
    rootOperand = rootFragment.Operands(1);

    添加消息和片段

    向根操作数添加两条消息。

    sourceToPollerMsg = rootOperand.addMessage(sourceLifeline, "switchout", pollerLifeline, "sw", "rising(sw-1){sw==1}");
    pollerToSwitchMsg = rootOperand.addMessage(pollerLifeline, "switchEvent", switchLifeline, "switchEvent", "switchEvent{switchEvent==1}");

    向根操作数添加一个条件片段。

    altFrag = rootOperand.addFragment('Alt');

    为条件片段的每个操作数指定保护表达式。

    op1 = altFrag.Operands(1);
    op1.Guard = "switch/inhibit==0";
    op2 = altFrag.addOperand("switch/inhibit==1");

    向条件片段的第一个操作数添加两条消息。

    switchToLampControllerMsg1 = op1.addMessage(switchLifeline, "lampColor", lampLifeline, "switchPed", "switchPed{switchPed==1}");
    switchToControllerMsg = op1.addMessage(switchLifeline, "switchPush", controllerLifeline, "pedRequest", "pedRequest");

    在条件片段的第二个操作数中添加一条消息。

    switchToLampControllerMsg2 = op2.addMessage(switchLifeline, "lampColor", lampLifeline, "switchPed", "switchPed{switchPed==2}");

    添加持续时间约束

    在条件片段第一个操作数中的两个消息之间添加一个持续时间约束。

    startEvent = switchToLampControllerMsg1.End;
    endEvent = switchToControllerMsg.End;
    expression = "t > 12msec";
    durationConstraint = diagram.addDurationConstraint(startEvent, endEvent, expression);

    打开序列图

    打开新创建的序列图。

    open(diagram);

    Newly created sequence diagram called 'InhibitCopy'.

    输入参数

    全部折叠

    操作数,指定为 systemcomposer.interaction.Operand 对象。

    源生命线,指定为 systemcomposer.interaction.Lifeline 对象。

    示例: "source"

    源端口,指定为字符向量或字符串。

    数据类型: char | string

    目标生命线,指定为 systemcomposer.interaction.Lifeline 对象。

    示例: "dest"

    目标端口,指定为字符向量或字符串。

    数据类型: char | string

    消息标签,指定为字符向量或字符串。

    一个消息标签有一个触发条件 (trigger)、一个可选的保护条件 (guard) 和一个可选的约束条件 (constraint),其形式为 trigger[guard]{constraint}trigger 表示此消息的标识事件。guard 表示确定消息是否发生的附加条件。constraint 是当此消息发生时预期为 true 的表达式。

    • 在信号事件中,触发器遵循以下格式:direction(signal [+|-] value),它指定了带有方向和表达式的触发边沿。方向可以是:

      • 上升 - 边沿表达式从严格低于零上升到等于或大于零的值。

      • 下降 - 边沿表达式从严格高于零开始下降。

      • 交叉 - 上升或下降过零的边沿表达式。

    • 在消息事件中,触发器的格式为 port,指定输入消息端口的名称,代表消息的到达。

    • 消息标签上方括号内的保护条件是一个 MATLAB® 布尔表达式,是确定消息是否发生的附加条件。只有在软件检测到有效触发条件时,才会计算保护条件。在执行过程中,序列图会等待进入下一条消息,直到保护表达式计算结果为 true。

    • 消息标签上大括号中的约束条件是一个 MATLAB 布尔表达式,指定了输入目标生命线的预期值。在执行过程中,对约束条件的计算决定了序列图对该消息显示通过还是失败。

    示例: "rising(sw-1)"

    数据类型: char | string

    输出参量

    全部折叠

    已添加的消息,以 systemcomposer.interaction.Message 对象的形式返回。

    详细信息

    全部折叠

    版本历史记录

    在 R2024b 中推出