Main Content

systemcomposer.arch.ComponentPort

Class that represents input and output ports of component

Description

The ComponentPort class represents the input and output ports of a component. This class inherits from systemcomposer.arch.BasePort. This class is derived from systemcomposer.arch.Element.

Creation

A component port is constructed by creating an architecture port on the architecture of the component.

addPort(compObj.Architecture,'portName','in');
compPortObj = getPort(compObj,'portName');

Properties

expand all

Name of port, specified as a character vector.

Example: 'portName'

Data Types: char

Port direction, specified as a character vector with values 'Input' and 'Output'.

Data Types: char

Name of interface associated with port, specified as a character vector.

Data Types: char

Interface associated with port, specified as a systemcomposer.interface.SignalInterface object.

Port connectors, specified as an array of systemcomposer.arch.Connector objects.

Whether port has connections, specified as a logical 1 (true) or 0 (false).

Data Types: logical

Component that owns port, specified as a systemcomposer.arch.Architecture object.

Architecture port within the component that maps to port, specified as a systemcomposer.arch.ArchitecturePort object.

Universal unique identifier for model component port, specified as a character vector.

Example: '91d5de2c-b14c-4c76-a5d6-5dd0037c52df'

Data Types: char

Unique external identifier, specified as a character vector. The external ID is preserved over the lifespan of the element and through all operations that preserve the UUID.

Data Types: char

Parent model of component port, specified as a systemcomposer.arch.Model object.

Simulink handle for component port, specified as a numeric value. This property is necessary for several Simulink related work flows and for using Simulink Requirement APIs.

Example: handle = get(object,'SimulinkHandle')

Data Types: double

Simulink handle to parent model of component port, specified as a numeric value. This property is necessary for several Simulink related work flows and for using Simulink Requirement APIs.

Example: handle = get(object,'SimulinkModelHandle')

Data Types: double

Object Functions

setNameSet name for port
setInterfaceSet interface for port
createAnonymousInterfaceCreate and set anonymous interface for port
applyStereotypeApply stereotype to architecture model element
getStereotypesGet stereotypes applied on element of architecture model
removeStereotypeRemove stereotype from model element
connectCreate architecture model connections
setPropertySet property value corresponding to stereotype applied to element
getPropertyGet property value corresponding to stereotype applied to element
getPropertyValueGet value of architecture property
getEvaluatedPropertyValueGet evaluated value of property from component
getStereotypePropertiesGet stereotype property names on element
destroyRemove model element

Examples

collapse all

This example shows how to build an architecture model using the System Composer™ API.

Prepare Workspace

Clear all profiles from the workspace.

systemcomposer.profile.Profile.closeAll;

Build a Model

To build a model, add a data dictionary with interfaces and interface elements, then add components, ports, and connections. After the model is built, you can create custom views to focus on a specific concern. You can also query the model to collect different model elements according to criteria you specify.

Add Components, Ports, and Connections

Create the model and extract its architecture.

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

Create data dictionary and add an interface. Link the interface to the model.

dictionary = systemcomposer.createDictionary('SensorInterfaces.sldd');
interface = addInterface(dictionary,'GPSInterface');
interface.addElement('Mass');
linkDictionary(model,'SensorInterfaces.sldd');

Add components, ports, and connections. Set the interface to ports, which you will connect later.

components = addComponent(arch,{'Sensor','Planning','Motion'});
sensorPorts = addPort(components(1).Architecture,{'MotionData','SensorData'},{'in','out'});
sensorPorts(2).setInterface(interface);

planningPorts = addPort(components(2).Architecture,{'Command','SensorData1','MotionCommand'},{'in','in','out'});
planningPorts(2).setInterface(interface);

motionPorts = addPort(components(3).Architecture,{'MotionCommand','MotionData'},{'in','out'});

Connect components with an interface rule. This rule connects ports on components that share the same interface.

c_sensorData = connect(arch,components(1),components(2),'Rule','interfaces');
c_motionData = connect(arch,components(3),components(1));
c_motionCommand = connect(arch,components(2),components(3));

Save Data Dictionary

Save the changes to the data dictionary.

dictionary.save(); 

Add and Connect an Architecture Port

Add an architecture port on the architecture.

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

The connect command requires a component port as argument. Obtain the component port and connect:

compPort = getPort(components(2),'Command');
c_Command = connect(archPort,compPort);

Save the model.

save(model)

Open the model

open_system(gcs);

Arrange the layout by pressıng Ctrl+Shift+A or using the following command:

Simulink.BlockDiagram.arrangeSystem('mobileRobotAPI'); 

Create and Apply Profile and Stereotypes

Profiles are xml files that can be applied to any model. You can add stereotypes with properties to profiles and then populate the properties with specific values. Along with System Composer’s built-in analysis capabilities, stereotypes can guide optimizations of your system for performance, cost, and reliability.

Create a Profile and Add Stereotypes

Create a profile.

profile = systemcomposer.createProfile('GeneralProfile');

Create a stereotype that applies to all element types:

elemSType = addStereotype(profile,'projectElement');

Create stereotypes for different types of components. These types are dictated by design needs and are up to your discretion:

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

Create a stereotype for connections:

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

Add Properties

Add properties to stereotypes. You can use properties to capture metadata for model elements and analyze non-functional requirements. These properties are added to all elements to which the stereotype is applied, in any model that imports the profile.

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');

Save the Profile

save(profile);

Apply Profile to Model

Apply the profile to the model:

applyProfile(model,'GeneralProfile');

Apply stereotypes to components. Some components are physical components, and others are software components.

applyStereotype(components(2),'GeneralProfile.softwareComponent')
applyStereotype(components(1),'GeneralProfile.physicalComponent')
applyStereotype(components(3),'GeneralProfile.physicalComponent')

Apply the connector stereotype to all connections:

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

Apply the general element stereotype to all connectors and ports:

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

Set properties for each component:

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

Set the properties of connections to be identical:

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

Add Hierarchy

Add two components named Controller and Scope inside the Motion component. Define the ports. Connect them to the architecture and to each other, applying a connector stereotype. Hierarchy in an architecture diagram creates an additional level of detail that specifies how components behave internally.

motionArch = components(3).Architecture;
motion = motionArch.addComponent({'Controller','Scope'});

controllerPorts = addPort(motion(1).Architecture,{'controlIn','controlOut'},{'in','out'});
controllerCompPortIn = motion(1).getPort('controlIn');
controllerCompPortOut = motion(1).getPort('controlOut');

scopePorts = addPort(motion(2).Architecture,{'scopeIn','scopeOut'},{'in','out'});
scopeCompPortIn = motion(2).getPort('scopeIn');
scopeCompPortOut = motion(2).getPort('scopeOut');

c_planningController = connect(motionPorts(1),controllerCompPortIn);
c_planningScope = connect(scopeCompPortOut,motionPorts(2));
c_planningConnect = connect(controllerCompPortOut,scopeCompPortIn,'GeneralProfile.standardConn');

Save the model.

save(model)

Arrange the layout by pressıng Ctrl+Shift+A or using the following command:

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

Create a Model Reference

Model references are useful to organize large models hierarchically and allow you to define architectures or behaviors once and reuse it. When a component references another model, any existing ports on the component are removed and ports that exist on the referenced model will appear on the component.

Create a new System Composer model. Convert the Sensor component into a reference component to reference the new model. To add additional ports on the Sensor component, you must update the referenced model mobileSensor.

newModel = systemcomposer.createModel('mobileSensor');
newArch = newModel.Architecture;
newComponents = addComponent(newArch,'ElectricSensor');
save(newModel);

linkToModel(components(1),'mobileSensor');

Apply a stereotype to the linked reference model's architecture and component.

referenceModel = get_param('mobileSensor','SystemComposerModel');
referenceModel.applyProfile('GeneralProfile');
referenceModel.Architecture.applyStereotype('GeneralProfile.softwareComponent');
batchApplyStereotype(referenceModel.Architecture,'Component','GeneralProfile.projectElement')

Add ports and connections to the reference component.

sensorPorts = addPort(components(1).Architecture,{'MotionData','SensorData'},{'in','out'});
sensorPorts(2).setInterface(interface)
connect(arch,components(1),components(2),'Rule','interfaces');
connect(arch,components(3),components(1));

Save the models.

save(referenceModel)
save(model)

Make a Variant Component

You can convert the Planning component into a variant component using the makeVariant function. The original component is embedded within a variant component as one of the available variant choices. You can design other variant choices within the variant component and toggle the active choice. Variant components allow you to choose behaviorial designs programmatically in an architecture model to perform trade studies and analysis.

[variantComp,choice1] = makeVariant(components(2));

Add an additional variant choice named PlanningAlt. The second argument defines the name, and the third argument defines the label. The label identifies the choice. The active choice is controlled by the label.

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

Create the necessary ports on PlanningAlt.

setActiveChoice(variantComp,choice2)
planningAltPorts = addPort(choice2.Architecture,{'Command','SensorData1','MotionCommand'},{'in','in','out'});
planningAltPorts(2).setInterface(interface);

Make PlanningAlt the active variant.

setActiveChoice(variantComp,'PlanningAlt')

Arrange the layout by pressıng Ctrl+Shift+A or using the following command:

Simulink.BlockDiagram.arrangeSystem('mobileRobotAPI/Planning');

Save the model.

save(model)

Clean Up

Uncomment the following code and run to clean up the artifacts created by this example:

% bdclose('mobileRobotAPI')
% bdclose('mobileSensor')
% Simulink.data.dictionary.closeAll
% systemcomposer.profile.Profile.closeAll
% delete('Profile.xml')
% delete('SensorInterfaces.sldd')

More About

expand all

Introduced in R2019a