classdef (StrictDefaults)slexLawOfLargeNumbersCalculateAndDisplaySysObj < matlab.System
    %slexLawOfLargeNumbersCalculateAndDisplaySysObj   Calculate and plot empirical probability.
    
    % Copyright 2015-2016 The MathWorks Inc
    
    properties(Nontunable)
        %TargetProbability   Target probability
        TargetProbability = 0
    end
    
    properties (Access = private)
        plotAttribs
        figHandle
    end    
    
    methods
        function this = slexLawOfLargeNumbersCalculateAndDisplaySysObj(varargin)
            % Support name-value pair arguments when constructing object
            setProperties(this, nargin, varargin{:})
        end        
    end
    
    methods(Access=protected)        
        function validatePropertiesImpl(obj)
            % Error handling for property values
            if ((numel(obj.TargetProbability) > 1) || ...
                    (obj.TargetProbability< 0)|| ...
                    (obj.TargetProbability > 1) )
                error(message('simdemos:MLSysBlockMsg:OutOfRangeProbability'))
            end
        end        
        
        function validateInputsImpl(~, successfulTrial, totalEvents)
            % Error handling for input values
            if (numel(successfulTrial)>1 || numel(totalEvents)>1)
                error(message('simdemos:MLSysBlockMsg:ScalarSignalOnly'))
            end
            if (totalEvents < successfulTrial)
                error(message('simdemos:MLSysBlockMsg:TotalTrialsMoreThanSuccessfulTrials'))
            end
        end
        
        function setupImpl(this)
            % Initialize figure and text values
            this.figHandle = figure;
            
            this.plotAttribs = {'Probability','Averaged results', ...
                'The cumulative probability comparison for %d experiments'};
            
            ylabel(this.plotAttribs{1})
            xlabel(this.plotAttribs{2})
            grid on         
        end
        
        function Pr = stepImpl(this,positiveCases,totalCases)
            
            % Calculate the probability with the cumulative data available
            Pr = positiveCases/totalCases;
            
            % Update Plots  with the latest simulation result
            figure(this.figHandle)
            hold on
            plot(totalCases, Pr,'.')
            plot(totalCases, this.TargetProbability,'.r')
            xlim([0 totalCases])
            title(sprintf(this.plotAttribs{3}, totalCases))
            hold off
        end
        
        function releaseImpl(this)
            figure(this.figHandle)
            legend('Average Probability','Expected Value')
            close(this.figHandle)
        end

        function s1 = getOutputSizeImpl(~)
            s1 = [1 1];
        end
        
        function d1 = getOutputDataTypeImpl(~)
            d1 = 'double';
        end
        
        function c1 = isOutputComplexImpl(~)
            c1 = false;
        end
        
        function c1 = isOutputFixedSizeImpl(~)
            c1 = true;
        end
        
        function s = saveObjectImpl(~) %#ok<STOUT>
            error(message('simdemos:MLSysBlockMsg:CannotSaveSimstate'))
        end
        
        function loadObjectImpl(~, ~, ~)
            error(message('simdemos:MLSysBlockMsg:CannotSaveSimstate'))
        end

        function icon = getIconImpl(~)
            % Define icon for System block
            icon = matlab.system.display.Icon(...
                'slexLawOfLargeNumbersIcon.jpg');
        end
    end    

    methods(Access = protected, Static)
        function header = getHeaderImpl
            % Define header panel for System block dialog
            header = matlab.system.display.Header(mfilename('class'), ...
                'Title', 'Calculate and Plot Probability');
        end
    end
end
