% FROMEXPRESSION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromExpression
%
% DESCRIPTION: Construct an smodel from an expression
%
% CALL:        mdl = fromExpression(a, pl)
%
% PARAMETER:   pl   - plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mdl = fromExpression(mdl, pli)
  
  VERSION = '$Id: fromExpression.m,v 1.11 2011/08/17 07:01:32 hewitson Exp $';
  
  % get smodel info
  ii = smodel.getInfo('smodel', 'From Expression');
  
  % Set the method version string in the minfo object
  ii.setMversion([VERSION '-->' ii.mversion]);
  
  % Combine input plist with default values
  pl = applyDefaults(ii.plists, pli);
  
  % Set fields
  expr   = pl.find('expression');
  params = pl.find('params');
  vals   = pl.find('values');
  xvar   = pl.find('xvar');
  xvals  = pl.find('xvals');
  
  % Check the contents of vals
  if iscell(vals)
    for ll=1:numel(vals)
      if isa(vals{ll}, 'ao')
        vals{ll} = vals{ll}.y;
      end
    end
  else
    if isa(vals, 'ao')
      vals = {vals.y};
    end
  end
  
  % Set params and default values
  if ~isempty(params) 
    mdl.params = params;
    if ~isempty(vals)
      mdl.values = vals;
    end
  end
  
  % Set x-variable
  if ~isempty(xvar)
    mdl.xvar = xvar;
  end
  
  % Set x-values
  if ~isempty(xvals)
    if isa(xvals, 'ao')
      mdl.xvals = xvals.x;
    else
      mdl.xvals = xvals;
    end
  end
    
  % Set expression
  mdl.expr = msym(expr);
  
  % Add history
  mdl.addHistory(ii, pl, [], []);
  
  % Set object properties
  mdl.setObjectProperties(pl, {'params', 'values', 'xvals', 'xval'});
  
end
