%% Testing AUTOSAR Compositions
%
% Run back-to-back tests on an AUTOSAR composition model.

%%
% This example demonstrates test harness features and back-to-back testing 
% workflows for an AUTOSAR composition model. Switch to a directory with write permissions.
%
% The example uses a model of a throttle position controller for an automobile. It is 
% based on an example from AUTOSAR Blockset. 
% For details, see <matlab:openExample('autosarblockset/ImportAUTOSARCompositionToSimulinkExample') Import AUTOSAR Composition to Simulink>.

%% Open the AUTOSAR Composition Model
% 
% AUTOSAR composition models contain a network of interconnected Model blocks, each of which represents an atomic
% AUTOSAR software component (ASWC). The throttle position controller composition was created by an 
% AUTOSAR authoring tool (AAT) and imported into Simulink using an ARXML file that describes the composition.
%
% The composition model contains six component models, one for each atomic 
% software component in the composition. Simulink inports and outports represent AUTOSAR ports and signal lines 
% represent AUTOSAR component connectors.

mdl = fullfile(matlabroot,'toolbox','simulinktest','simulinktestdemos', 'sltestThrottlePositionControlCompositionExample.slx');
open_system(mdl);

%% Open Test Harness
%
% A test harness for the model has been generated and can be opened using the perspective control in the lower
% right corner of the editor canvas. Alternately, use:
sltest.harness.open('sltestThrottlePositionControlCompositionExample','BasicSchedulerTest');

%%
% A Test Sequence block is used as the source. The component under test requires the accelerator pedal position sensor input 
% |APP_HwIO_Value|, which is modeled in the Test Sequence block using a simple three step sequence:

%%
% <<sltestBasicSchedulerTestTS.png>>

%%
% The |Initialize| step sets the input to a nominal value and the |Run| step models a steady acceleration command for
% 950 ms. The acceleration command is reset to the nominal value in the |Terminate| step. 
% The component under test requires two additional inputs that capture the primary and secondary throttle position sensor
% readouts. These inputs are modeled using an external timeseries input and are directly fed through the Test
% Sequence block without modification. This modeling style is useful when some stimulus inputs can be modeled and others are 
% only available as externally captured data.

%% Test Harnesses for Export Functions
%
% The component under test is the AUTOSAR composition model, which uses the export-function modeling style.  
% When you create a test harness for an export-function model, the harness will contain a 
% Test Sequence block configured to call each root-level Simulink Function block and send a
% trigger event to each function-call subsystem in the model. The generated Test Sequence block can be used as a 
% convenient starting point for modeling a scheduler.
%
% In this example, since the input signal data is also being  generated by a Test Sequence source block, 
% the code to send the trigger events has been consolidated into a single Test Sequence block and embedded in each step 
% after the stimulus waverforms have been generated. The call order of the trigger events are computed using 
% compiled information from the composition model.

%%
% # |send(TPS_Primary_Run_0005)|
% # |send(TPS_Secondary_Run_0005)|
% # |send(Monitor_Run_0005)|
% # |send(APPSnsr_Run_0005)|
% # |send(Controller_Run_0005)|
% # |send(Actuator_Run_0005)|
%
% Simulate the model to see the throttle command output from the component under test.
sim('BasicSchedulerTest');
open_system('BasicSchedulerTest/Scope');

%% Back-to-back Testing
%
% The test manager can be used to lock down simulation behavior and verify equivalence  in software-in-the-loop (SIL) mode. 
% Open the test file and run the equivalance test.
close_system(mdl,0);
filePath = fullfile(matlabroot,'toolbox','simulinktest','simulinktestdemos','sltestThrottlePositionControlTests.mldatx');
open(filePath);
sltest.testmanager.run;

%%
% The test case verifies the open-loop behavior of the Throttle Position Controller ASWC within the 
% composition model. The first part of the equivalence test case runs the test harness containing the composition in 
% normal simulation mode. The second part of the test uses the |Post-Load| callback to switch the Throttle Position Controller
% ASWC to software-in-the-loop (SIL) mode with |Top model| code interface. 
% The results of both simulations show that the behavior is equivalent. 

%%
% <<sltestBasicSchedulerTestSILEquivalence.png>>

%% Cleanup
clear sltestThrottlePositionControlData HBridgeCmd_LkupTbl SensorSelection SetpointPercent_LkupTbl TPSPrimaryPercent_LkupTbl TPSSecondaryPercent_LkupTbl TPSPercent_LkupTbl tout logsout mdl filePath;
sltest.testmanager.clear;
sltest.testmanager.clearResults;
sltest.testmanager.close;
displayEndOfDemoMessage(mfilename)

%%
%  Copyright 2017 The MathWorks, Inc.
