%% Import and Export Architectures 
% This example shows how to import and export architectures. 
% In System Composer, an architecture is fully defined by three sets of information:
%
          % * Component information
          % * Port information
          % * Connection information
%
% You can import an architecture into System Composer when this information
% is defined in, or converted into, MATLAB tables.
%%
% In this example, the architecture information of a simple UAV system is 
% defined in an Excel spreadsheet and is used to create a 
% System Composer architecture model. It also links elements to
% the specified system level requirement. You can modify the files in this
% example to import architectures defined in external tools, when the data
% includes the required information. The example also shows how to export
% this architecture information from System Composer architecture model to
% an Excel spreadsheet.


%% Architecture Definition Data
% You can characterize the architecture as a network of components and
% import by defining components, ports, connections, interfaces and
% requirement links in MATLAB tables. 
% The component table must include name, unique ID, and 
% parent component ID for each component. It can also include other relevant 
% information required to construct the architecture hierarchy for referenced
% model, and stereotype qualifier names.
% The port table must include port name, direction, component, and
% port ID information. Port interface information may also be required 
% to assign ports to components.
% The connection table includes information to connect ports. 
% At a minimum, this table must include the
% connection ID, source port ID, and destination port ID.

%%
% The systemcomposer.importModel(importModelName) API :
%
% * Reads stereotype names from Component table and load the profiles
%
% * Creates components and attaches ports
% 
% * Creates connections using the connection map
%
% * Sets interfaces on ports
%
% * Links elements to specified requirements
%
% * Saves referenced models
%
% * Saves the architecture model
%
%%
% Creating a temporary folder because this example will
% create files.
newDir = fullfile(tempdir, 'importedModelDir');
% Check if the folder exists
if exist(newDir,'dir')
    count = 1;
    uniqueName = newDir;
    while exist(uniqueName,'dir')
        uniqueName = [newDir num2str(count)];
        count = count + 1;
    end
    newDir = uniqueName;
end
% Creating a new directory.
mkdir(newDir);
% Instantiate adapter class to read from Excel.
modelName = 'simpleUAVArchitecture';
% importModelFromExcel function reads the Excel file and creates the MATLAB
% tables.
importAdapter = ImportModelFromExcel('SmallUAVModel.xls','Components','Ports','Connections','PortInterfaces','RequirementLinks');
importAdapter.readTableFromExcel();
% Changing the current path to the new created folder.
cd(newDir);
%% Import an Architecture
model = systemcomposer.importModel(modelName,importAdapter.Components,importAdapter.Ports,importAdapter.Connections,importAdapter.Interfaces,importAdapter.RequirementLinks);
% Auto-arrange blocks in the generated model
Simulink.BlockDiagram.arrangeSystem(modelName);
    
%% Export an Architecture
% You can export an architecture to MATLAB tables and then convert to
% an external file
exportedSet = systemcomposer.exportModel(modelName);
% The output of the function is a structure that contains the component table, port table,
% connection table, the interface table, and the requirement links table.
% Save the above structure to excel file.
SaveToExcel('ExportedUAVModel',exportedSet);

%% Close Model
bdclose(modelName);

 