import system composer architecture using modelbuilder class -凯发k8网页登录
import architecture specifications into system composer™ using the systemcomposer.io.modelbuilder
utility class. these architecture specifications can be defined in an external source, such as an excel® file.
in system composer, an architecture is fully defined by four sets of information:
components and their position in the architecture hierarchy.
ports and their mapping to components.
connections among components through ports. in this example, we also import interface data definitions from an external source.
interfaces in architecture models and their mapping to ports.
this example uses the systemcomposer.io.modelbuilder
class to pass all of the above architecture information and import a system composer model.
in this example, architecture information of a small uav system is defined in an excel spreadsheet and is used to create a system composer architecture model.
external source files
architecture.xlsx
— this excel file contains hierarchical information of the architecture model. this example maps the external source data to system composer model elements. this information maps in column names to system composer model elements.
# element : name of the element. either can be component or port name. # parent : name of the parent element. # class : can be either component or port(input/output direction of the port). # domain : mapped as component property. property "manufacturer" defined in the profile uavcomponent under stereotype partdescriptor maps to domain values in excel source file. # kind : mapped as component property. property "modelname" defined in the profile uavcomponent under stereotype partdescriptor maps to kind values in excel source file. # interfacename : if class is of port type. interfacename maps to name of the interface linked to port. # connectedto : in case of port type, it specifies the connection to other port defined in format "componentname::portname".
datadefinitions.xlsx
— this excel file contains interface data definitions of the model. this example assumes this mapping between the data definitions in the excel source file and interfaces hierarchy in system composer.
# name : name of the interface or element. # parent : name of the parent interface name(applicable only for elements) . # datatype : datatype of element. can be another interface in format bus: interfacename # dimensions : dimensions of the element. # units : unit property of the element. # minimum : minimum value of the element. # maximum : maximum value of the element.
step 1. instantiate the modelbuilder
class
you can instantiate the modelbuilder
class with a profile name.
[stat,fa] = fileattrib(pwd); if ~fa.userwrite disp('this script must be run in a writable directory'); return; end
specify the name of the model to build.
modelname = 'scexamplemodelbuilder';
specify the name of the profile.
profile = 'uavcomponent';
specify the name of the source file to read architecture information.
architecturefilename = 'architecture.xlsx';
instantiate the modelbuilder
.
builder = systemcomposer.io.modelbuilder(profile);
step 2. build interface data definitions
reading the information in the external source file datadefinitions.xlsx
to build the interface data model.
create matlab® tables from the excel source file.
opts = detectimportoptions('datadefinitions.xlsx'); opts.datarange = 'a2';
force readtable
to start reading from the second row.
definitioncontents = readtable('datadefinitions.xlsx',opts);
the systemcomposer.io.idservice
class generates unique id
for a given key.
idservice = systemcomposer.io.idservice(); for rowitr =1:numel(definitioncontents(:,1)) parentinterface = definitioncontents.parent{rowitr}; if isempty(parentinterface)
in the case of interfaces, add the interface name to the model builder.
interfacename = definitioncontents.name{rowitr};
get the unique interface id
.
getid(container,key)
generates or returns (if key is already present) same value for input key within the container.
interfaceid = idservice.getid('interfaces',interfacename);
use builder.addinterface
to add the interface to the data dictionary.
builder.addinterface(interfacename,interfaceid);
else
in the case of an element, read the element properties and add the element to the parent interface.
elementname = definitioncontents.name{rowitr};
interfaceid = idservice.getid('interfaces',parentinterface);
the elementid
is unique within a interface. append e
at the start of an id
for uniformity. the generated id
for an input element is unique within parent interface name as a container.
elemid = idservice.getid(parentinterface,elementname,'e');
set the data type, dimensions, units, minimum, and maximum properties of the element.
datatype = definitioncontents.datatype{rowitr}; dimensions = string(definitioncontents.dimensions(rowitr)); units = definitioncontents.units(rowitr);
make sure that input to builder utility function is always a string.
if ~ischar(units) units = ''; end minimum = definitioncontents.minimum{rowitr}; maximum = definitioncontents.maximum{rowitr};
use builder.addelementininterface
to add an element with properties in the interface.
builder.addelementininterface(elementname,elemid,interfaceid,datatype,dimensions,units,'real',maximum,minimum); end end
step 3. build architecture specifications
architecture specifications are created by matlab tables from the excel source file.
excelcontents = readtable(architecturefilename);
iterate over each row in the table.
for rowitr =1:numel(excelcontents(:,1))
read each row of the excel file and columns.
class = excelcontents.class(rowitr); parent = excelcontents.parent(rowitr); name = excelcontents.element{rowitr};
populate the contents of the table.
if strcmp(class,'component') id = idservice.getid('comp',name);
the root id
is by default set as zero.
if strcmp(parent,'scexamplesmalluav') parentid = "0"; else parentid = idservice.getid('comp',parent); end
use builder.addcomponent
to add a component.
builder.addcomponent(name,id,parentid);
read the property values.
kind = excelcontents.kind{rowitr}; domain = excelcontents.domain{rowitr};
use builder.setcomponentproperty
to set stereotype and property values.
builder.setcomponentproperty(id,'stereotypename','uavcomponent.partdescriptor','modelname',kind,'manufacturer',domain); else
in this example, concatenation of the port name and parent component name is used as key to generate unique ids for ports.
portid = idservice.getid('port',strcat(name,parent));
for ports on root architecture, the compid
is assumed as 0
.
if strcmp(parent,'scexamplesmalluav') compid = "0"; else compid = idservice.getid('comp',parent); end
use builder.addport
to add a port.
builder.addport(name,class,portid,compid );
the interfacename
specifies the name of the interface linked to the port.
interfacename = excelcontents.interfacename{rowitr};
get the interface id.
getid
will return the same ids already generated while adding interface in step 2.
interfaceid = idservice.getid('interfaces',interfacename);
use builder.addinterfacetoport
to map interface to port.
builder.addinterfacetoport(interfaceid,portid);
read the connectedto
information to build connections between components.
connectedto = excelcontents.connectedto{rowitr};
connectedto
is in the format:
(destinationcomponentname::destinationportname)
for this example, consider the current port as source of the connection.
if ~isempty(connectedto) connid = idservice.getid('connection',connectedto); splits = split(connectedto,'::');
get the port id of the connected port.
in this example, port id is generated by concatenating the port name and the parent component name. if the port id is already generated, the getid
function returns the same id for the input key.
connectedportid = idservice.getid('port',strcat(splits(2),splits(1)));
populate the connection table.
sourceportid = portid; destportid = connectedportid;
use builder.addconnection
to add connections.
builder.addconnection(connectedto,connid,sourceportid,destportid); end end end
step 3. import model from populated tables with builder.build
function
[model,importreport] = builder.build(modelname);
clean up artifacts.
cleanup
凯发官网入口首页 copyright 2020 the mathworks, inc.
see also
| |