Main Content

Build an Architecture Model from Command Line

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

See Also

Functions

Blocks

Related Topics