Main Content

Smart Connect Ports by Custom Policy

This example shows how to connect ports in the architecture model mSmartConnect.slx by using a custom port connection policy. First, smart connect uses the default port connection policy. Then, after some debugging, transitions to the custom port connection policy.

Connect Ports Using Default Port Connection Policy

Open the mSmartConnect.slx architecture model.

systemcomposer.openModel("mSmartConnect");

mSmartConnect architecture model with no connections between ports.

From the toolstrip, on the Connect tab, select Smart Connect. Connections are made using the default port connection policy, establishing connections between ports of the same name.

mSmartConnect architecture model with connections between the liked-named ports.

Debug Diagram and Highlight Ports and Connections to Consider

The default port connection policy did not complete all connections, so a custom port connection policy is required. First, clean up the diagram with the following methods.

From the toolstrip, on the Connect tab, select the drop down under Smart Connect. Select Highlight Unconnected Ports. Many of the ports still remain unconnected.

Highlight unconnected ports displaying ports that do not yet have a connection.

Next, clear Highlight Unconnected Ports and select Highlight Invalid Connectors. You can see that port names must match.

Highlighted invalid connectors on the mSmartConnect architecture model.

Now that the mismatched connections are highlighted, select Delete Invalid Connectors to delete them.

Create Custom Port Connection Policy

A custom port connection policy is required for this model so that smart connect can create all the correct connections.

To set up a custom port connection policy, under Smart Connect select the down arrow to access Smart Connect Settings, then select the radio button for Custom Match. To create a custom port connection policy, select Create Using Template. Choose a file name to save your template. A new systemcomposer.arch.SmartConnectPolicy class template opens in edit mode. Edit the template to customize it as shown.

classdef simpleCustomConnect < systemcomposer.arch.SmartConnectPolicy
% A simple smart connect policy that removes case sensitivity.

%   Copyright 2024 The MathWorks, Inc.

    properties
    end

    methods
        function obj = simpleCustomConnect()
            % Calling base class constructor
            obj@systemcomposer.arch.SmartConnectPolicy();
        end

        function initialize(obj)
            % Called at the beginning of smart connect 
        end

        function token = getPortToken(obj, port)
            % Called by smart connect to get port tokens
            token = lower(port.Name);
        end

        function terminate(obj)
            % Called at the end of smart connect
        end
    end

end

This new custom port connection policy makes the connections case insensitive using the lower() function.

Define the initialize function to open connections. Next, define the getPortToken function to customize port name matching using file imports, Excel® imports, databased connections, and tables in MATLAB®. The getPortToken function is called multiple times while forming the connections. Finally, use the terminate function to close out connections and files.

In Smart Connect Settings, insert the custom policy function in the Run custom policy as text box to use the function.

The custom policy function simpleCustomConnect() is in the Run custom policy as text box in the Smart Connect menu.

Then, click the Smart Connect button. The connections between the components complete in a case insensitive fashion creating more of the intended connections.

The mSmartConnect architecture model is fully connected due to the custom smart connect policy.

See Also

Functions

Topics