%% 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. 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, and interfaces 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 such as referenced model, stereotype qualifier name and so on. 
% required to construct the architecture hierarchy. 
% 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. This includes, 
% at a minimum,
% 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 attach ports
% 
% * Creates connections using the connection map
%
% * Saves referenced models
%
% * Saves the architecture model
%
%%
% Make sure the current directory is writable because this example will be
% creating files.
[stat, fa] = fileattrib(pwd);
if ~fa.UserWrite
    disp('This script must be run in a writable directory');
    return;
end
% 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');
importAdapter.readTableFromExcel();

%% Import an Architecture
model = systemcomposer.importModel(modelName,importAdapter.Components,importAdapter.Ports,importAdapter.Connections,importAdapter.Interfaces);
% 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, and the interface table.
% Save the above structure to excel file.
SaveToExcel('ExportedUAVModel',exportedSet);

%% Close Model
bdclose(modelName);

 