function plotfis(fis,varargin)
%PLOTFIS Display FIS input-output diagram.
%   PLOTFIS(FIS) creates an input-output display of the fuzzy
%   inference system FIS. Inputs and their membership functions appear to
%   the left of  the FIS structural characteristics, while outputs and
%   their membership functions appear on the right.
%
%   This FIS diagram is similar to the one shown at the top of the 
%   Fuzzy Logic Designer (invoked by the fuzzyLogicDesigner command).
%
%   PLOTFIS(FIST) Shows FIST, a fistree object, with the connections
%   between the networked fuzzy inference systems. The plot uses different
%   colors for open inputs, outputs, and connections.
%
%   PLOTFIS(FIST,NAME1,VALUE1) Shows FIST with the specified parameter
%   Name/Value pair. You can specify the following parameter Name/Value
%   pair:
%       Legend - A flag for showing a legend for different connections
%                between the networked fuzzy inference systems. PLOTFIS
%                shows the legend if flag is 'on'. The default is 'off'.
%
%   For example:
%
%           %% Plot a fuzzy inference system.
%           fis = mamfis;
%           fis = addInput(fis,[0 10],'Name','service');
%           fis = addMF(fis,'service','gaussmf',[1.5 0],'Name','poor');
%           fis = addMF(fis,'service','gaussmf',[1.5 10],'Name','excellent');
%           fis = addInput(fis,[0 10],'Name','food');
%           fis = addMF(fis,'food','trapmf',[-2 0 1 3],'Name','rancid');
%           fis = addMF(fis,'food','trapmf',[7 9 10 12],'Name','delicious');
%           fis = addOutput(fis,[0 30],'Name','tip');
%           fis = addMF(fis,'tip','trimf',[0 5 10],'Name','cheap');
%           fis = addMF(fis,'tip','trimf',[20 25 30],'Name','generous');
%           ruleList = [1 1 1 1 2; 2 2 2 1 2 ];
%           fis = addRule(fis,ruleList);
%           plotfis(fis)
%
%           %% Plot a tree of fuzzy inference systems.
%           fis1 = mamfis('Name','fis1','NumInputs',2,'NumOutputs',2);
%           fis2 = mamfis('Name','fis2','NumInputs',3,'NumOutputs',1);
%           con1 = ["fis1/output1" "fis2/input1"];
%           con2 = ["fis1/output2" "fis2/input2"];
%           fisT = fistree([fis1 fis2],[con1; con2]);
%           plotfis(fisT)
%
%           %% Plot a tree of fuzzy inference systems with legend.
%           fis1 = mamfis('Name','fis1','NumInputs',2,'NumOutputs',1);
%           fis2 = sugfis('Name','fis2','NumInputs',2,'NumOutputs',1);
%           con = ["fis1/input1" "fis1/input2";"fis2/input1" "fis1/input1"];
%           fisT = fistree([fis1 fis2],con);
%           plotfis(fisT,"Legend",'on')
%
%   See also
%       mamfis mamfistype2 sugfis sugfistype2 fistree plotmf

%   Copyright 1994-2021 The MathWorks, Inc.


if isa(fis,'fistree')
    p = inputParser;
    p.addParameter('Legend','off',@validateLegend)
    parse(p,varargin{:})
    
    showLegend = p.Results.Legend;
    if ischar(showLegend) || isstring(showLegend)
        if ischar(showLegend) || isstring(showLegend)
            showLegend = lower(showLegend);
        end
        if showLegend == "on"
            showLegend = true;
        elseif showLegend=="off"
           showLegend = false;
        end
    elseif isnumeric(showLegend)
        showLegend = logical(showLegend);
    end
    
    plot(fis,showLegend)
else
    fuzzy.internal.utility.plotfis(fis,varargin{:})
end

end

%% Local functions --------------------------------------------------------
function validateLegend(value)
%% Legend value must be on(true)/off(false)

% Char/string
if ischar(value) || isstring(value)
    value = (lower(value));
    if isequal(value,'on') || isequal(value,'off')
        return
    end
end

% Logical
if islogical(value) && isscalar(value)
    return
end

% Numeric
if isreal(value) && (isequal(value,0) || isequal(value,1))
    return
end

% Invalid value
error(message('fuzzy:general:errInvalidLegendParamValue'))
    
end