www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cageview/+project/StartActions.m

    classdef StartActions < handle
    %StartActions CAGE project start actions
    
    %  Copyright 2016-2016 The MathWorks, Inc. 

    properties (SetAccess=private)
        %Models Import Models
        Models
        %Tradeoff create tables and tradeoff from model
        Tradeoff
        %Optimization create optimization from model
        Optimization
        %Dataset import data for data set
        Dataset
        %Feature import Simulink model for feature
        Feature
        %ExportTables export tables to a calibration tpp;
        ExportTables
    end
    
    properties (SetAccess=private)
        %Browser CAGE Browser
        Browser
        IsRunning = false;
    end
    
    properties (Dependent,SetAccess=private)
        %ActionList list of all actions
        ActionList
    end
    
    methods
        
        function obj = StartActions(Browser)
        %StartActions constructor
        
        obj.Browser = Browser;
        
        obj.Models = mbcgui.actions.StatefulAction(@obj.onImportModels,'Import Models',...
            'Import models to generate calibrations.',cgrespath('mbcmodel.png'));
        obj.Tradeoff = mbcgui.actions.StatefulAction(@obj.onCreateTradeoff,'Tables and Tradeoff',...
            'Create tables and tradeoff for a model.',cgrespath('mbctradeoff.png'));
        obj.Tradeoff.ActiveLabel = 'Create Tables and Tradeoff';
        obj.Optimization = mbcgui.actions.StatefulAction(@obj.onCreateOptim,'Optimization',...
            'Create an optimization for a model and fill tables with the result.',cgrespath('mbcoptim.png'));
        obj.Optimization.ActiveLabel = 'Create Optimization';
        obj.Feature = mbcgui.actions.StatefulAction(@obj.onImportStrategy,'Feature',...
            'Import strategy from Simulink to fill tables using a model.',cgrespath('mbcsimulink.png'));
        obj.Feature.ActiveLabel = 'Import Strategy';
        obj.Dataset = mbcgui.actions.StatefulAction(@obj.onImportData,'Data set',...
            'Import data to evaluate models and calibrations.',cgrespath('mbcdataset.png'));
        obj.Dataset.ActiveLabel = 'Import Data';
        obj.ExportTables = mbcgui.actions.StatefulAction(@obj.onExportTables,'Export Tables',...
            'Export tables to a calibration tool.',cgrespath('mbctable.png'));
        
        end
        
        function a = get.ActionList(obj)
        
        a = [obj.Models obj.Optimization obj.Tradeoff obj.Feature obj.Dataset obj.ExportTables];
        
        end
        
        function reset(obj)
        obj.IsRunning = false;
        end
    end
    
    methods (Access=private)
        
        function onImportModels(obj,~,~)
        %onImportModels import models 
        if ~obj.IsRunning
            obj.IsRunning = true;      
            restore = onCleanup(@obj.reset);
            
            cgimporttool();
        end
        end
        

        function onCreateTradeoff(obj,~,~)
        %onCreateTradeoff create tables and tradeoff wizard
        if ~obj.IsRunning
            obj.IsRunning = true;            
            restore = onCleanup(@obj.reset);
            if canCreateOptimOrTradeoff(obj)
                h = obj.Browser;
                PR=xregGui.PointerRepository;
                ptrID=PR.stackSetPointer(h.Figure,'watch');
                C = cgcalsetup.TableData(h.CurrentNode, h.CurrentType);
                C.wizard('Tables')
                PR.stackRemovePointer(h.Figure,ptrID);
            else
                errordlg('You must import models before creating tables and a tradeoff.','Create Tables','modal');
            end
        end
        end
       function onCreateOptim(obj,~,~)
       %onCreateOptim create optimization from model
       
        if ~obj.IsRunning
            obj.IsRunning = true;
            restore = onCleanup(@obj.reset);
            
            if canCreateOptimOrTradeoff(obj)
                h = obj.Browser;
                PR=xregGui.PointerRepository;
                ptrID=PR.stackSetPointer(h.Figure,'watch');
                C = cgcalsetup.OptimData(h.CurrentNode, h.CurrentType);
                C.wizard('Optimization')
                PR.stackRemovePointer(h.Figure,ptrID);
            else
                errordlg('You must import models before creating an optimization.','Create Optimization','modal');
            end
        end
        end
        
        function onImportData(obj,~,~)
        %onImportData import data to data set, creating a data set if necessary
        
        if ~obj.IsRunning
            obj.IsRunning = true;       
            restore = onCleanup(@obj.reset);
            d = cgtypes.cgdatasettype;
            % should ask for MBrowser,File,Workspace
            nd = get(obj.Browser,'CurrentNode');
            
            src = importDataSource;
            switch src
                case 'File'
                    % 3 is file
                    d.Actions.import(3).fcn(nd);
                case 'Workspace'
                    d.Actions.import(4).fcn(nd);
                case 'Model Browser'
                    d.Actions.import(2).fcn(nd);
            end
        end
        end
        
        function onImportStrategy(obj,~,~)
        %onImportStrategy import strategy to feature, creating a feature if necessary
        
        if ~obj.IsRunning
            obj.IsRunning = true;       
            restore = onCleanup(@obj.reset);
            d = cgtypes.cgfeaturetype;
            nd = get(obj.Browser,'CurrentNode');
            d.Actions.import(1).fcn(nd);
        end 
        end
        
        function en = canCreateOptimOrTradeoff(obj)
        %canCreateOptimOrTradeoff check whether models exist to create
        %optimization or tradeoff
        
        h = obj.Browser;
        % enable create optim and tables
        en = ~(isempty(h.RootNode)|| isnull(h.RootNode) || isempty(h.RootNode.getmodels));

        end
        
        function onExportTables(obj,~,~)
        %onExportTables  export tables to cal tool
        
        if ~obj.IsRunning
            obj.IsRunning = true;       
            restore = onCleanup(@obj.reset);
            % whole project
            CAL = cgcaloutput(obj.Browser.RootNode);
            gui_export(CAL);
        end
        end
        
    end
    
end


function src = importDataSource
%importDataSource determine source for data import 
vars = evalin('base','whos');
hasTable = strcmp('table',{vars.class});
hasStruct = strcmp('struct',{vars.class});
for i=find(hasStruct)
   hasStruct(i) = isSweepsetStruct(sweepset, evalin('base',vars(i).name));
end
hasMatrix = strcmp('double',{vars.class});
for i=find(hasStruct)
   hasMatrix(i) = ismatrix(evalin('base',vars(i).name));
end

opts = {'File';'Workspace';'Model Browser'};
% check whether any data in workspace
hasWkSpace = any(hasTable) || any(hasStruct) || any(hasMatrix);

% check whether data is available in model browser
mbh = MBrowser;
hasBrowser = mbh.GUIExists && ~isempty(mbh.RootNode.dataptrs);

% create a dialog to ask for data source
dlg = mbcgui.container.Dialog('Name','Import Data',...
    'Buttons','OK_CANCEL',...
    'Size',[250 200]);
pnl = mbcgui.container.layoutpanel('Parent',dlg.Figure,...
    'BorderType','etchedin',...
    'Title','Data Source');
rb = xregGui.rbgroup('Parent',pnl,...
    'nx', 1, 'ny', length(opts),...
    'EnableArray',[true hasWkSpace hasBrowser],...
    'String',opts);
lyt = xreggridbaglayout(pnl,...
    'dimension',[2,1],...
    'rowsizes',[20*length(opts),-1],...
    'elements',{rb});
pnl.LayoutComponent = lyt;
dlg.Content = pnl;
cls = dlg.showDialog();
if strcmpi(cls,'OK')
    src = opts{rb.Selected};
else
    src = '';
end
delete(dlg)

end