% FROMSMODEL Construct a AO from an smodel.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    FROMSMODEL Construct a AO from an smodel
%
% DESCRIPTION: FROMSMODEL Construct a AO from an smodel
%
% CALL:        a = fromSModel(a, pl)
%
% PARAMETER:   pl: plist containing 'model'
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function a = fromSModel(a, pli, callerIsMethod)
  
  VERSION = '$Id: fromSModel.m,v 1.9 2011/08/15 06:13:07 hewitson Exp $';
  
  if callerIsMethod
    % do nothing
  else
    % get AO info
    ii = ao.getInfo('ao', 'From Smodel');
    
    % Set the method version string in the minfo object
    ii.setMversion([VERSION '-->' ii.mversion]);
  end
  
  if callerIsMethod
    pl = pli;
  else
    % Combine input plist with default values
    pl = applyDefaults(ii.plists, pli);
  end
  
  pl.getSetRandState();
  
  mdl = find(pl, 'model');
  
  % Build the plist needed for the smodel/eval call 
  pl_eval = plist(...
    'output x', find(pl, 'x'), ...
    'output xunits', find(pl, 'xunits'), ...
    'output type', find(pl, 'type'), ...
    't0', 0 ... 
    );
  
  % Build the object by calling smodel/eval
  a = mdl.eval(pl_eval);
  
  if isempty(pl.find('name'))
    pl.pset('name', sprintf('eval(%s)', mdl.name));
  end
  if isempty(pl.find('description'))
    pl.pset('description', mdl.description);
  end
  
  if callerIsMethod
    % do nothing
  else
    % Add history
    a.addHistory(ii, pl, [], mdl.hist);
  end
  
  % Set object properties from the plist
  a.setObjectProperties(pl);
  
end


