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

    classdef MessageService < mbcmodelview.MessageService
    %cageview.optim.MessageService class
    %   cageview.optim.MessageService extends mbcmodelview.MessageService.
    %
    %    cageview.optim.MessageService methods:
    %       getDataName - Get a name for the data being displayed
    %       getOptim - Get the optimisation object
    %       hasData - Check whether the MessageService contains viewable data
    %       sendEvent - Send the appropriate events for a change's root cause
    %       setOptim - Set a new optimization object
    
    %  Copyright 2005-2015 The MathWorks, Inc.
    
    properties (SetAccess=protected, AbortSet)
        %OptimObject Property is of type 'cgoptim'
        OptimObject = [];
        %pOptim pointer to cgoptin
        pOptim = xregpointer;
    end
    
    properties (Dependent,SetAccess=private)
        %ProjectPointer pointer to CAGE project
        ProjectPointer
    end
    
    properties(Dependent,SetAccess=private)
        %Title
        Title
    end

    events (NotifyAccess=private)
        ObjectChanged
        SetupChanged
        ConstraintChanged
        ObjectiveChanged
        DatasetChanged
        ParameterChanged
        FreeVariableChanged
        InputValueChanged
        ScaledChanged
    end  % events
    
    properties (SetObservable)
        %SelectedObjective currently selected objective
        SelectedObjective = 1;
        %SelectedConstraint currently selected constraint
        SelectedConstraint = 0;
    end
    
    methods  % constructor block
        function h = MessageService(Optim)
        %MessageService Create an instance of an MessageService object
        %  H = MessageService(OPTIM) constructs a new MessageService
        %  object.
        
        if nargin
            h.OptimObject = Optim;
        end
        h.Actions = cageview.optim.Actions(h);
        
        end  % MessageService
        
    end  % constructor block
    
    methods  % public methods
        
        function s = get.Title(obj)
        if obj.hasData
            s = name(obj.Node);
        else
            s = '';
        end        
        end
        
        function p = get.ProjectPointer(obj)
        
        if ~isnull(obj.Pointer)
            %CAGE project pointer
            p = obj.Pointer.root;
        else
            p = xregpointer;
        end
        
        end        
        
        function initialize(obj,pOptimNode)
        %initialize initialize MessageService with new cgoptimnode
        ptrOptim = getdata(info(pOptimNode));
        opt = info(ptrOptim);
        % select constraints and objective before setting up optimization
        % as there are listeners on SelectedConstraint and SelectedObjective
        if getNumConstraints(opt)>0
            obj.SelectedConstraint = 1;
        else
            obj.SelectedConstraint = 0;
        end            
        obj.SelectedObjective = 1;
        obj.Pointer = pOptimNode;
        obj.pOptim = ptrOptim;
        obj.setOptim(opt, 'all');

        end
        
        %----------------------------------------
        function nm = getDataName(obj)
        %GETDATANAME Get a name for the data being displayed
        %  NM = GETDATANAME(OBJ) returns a string that is the name of the data
        %  that is being encapsulated by the message service.  By default this
        %  returns the string '<Unknown>'.
        
        if ~isempty(obj.OptimObject)
            nm = getname(obj.OptimObject);
        else
            nm = '<Unknown>';
        end
        
        end  % getDataName
        
        %----------------------------------------
        function Optim = getOptim(obj)
        %GETOPTIM Get the optimisation object
        %  OPTIM = GETOPTIM(OBJ) returns the optimization object that is currently
        %  being viewed.
        
        Optim = obj.OptimObject;
        
        end  % getOptim
        
        function OK = leaveNode(obj)
        
        OK = leaveNode@mbcmodelview.MessageService(obj);
        if OK
            % clear data
            obj.OptimObject = [];
            obj.pOptim = xregpointer;
        end
        
        end
        
        %----------------------------------------
        function sendEvent(obj, Event)
        %SENDEVENT Send the appropriate events for a change's root cause
        %  SENDEVENT(OBJ, EVENT) sends the appropriate set of events given the root
        %  cause specified in EVENT.  Valid values for EVENT are:
        %
        %    'All'           : Every event is sent.
        %    'Setup'         : Every event associated with the setup is sent.
        %    'Constraint'    : The ConstraintChanged event is sent.
        %    'Objective'     : The ObjectiveChanged event is sent.
        %    'Dataset'       : The DatasetChanged event is sent.
        %    'Parameter'     : The ParametersChanged event is sent.
        %    'FreeVariable'  : The FreeVariablesChanged event is sent.
        %    'InputValue'    : The InputValuesChanged event is sent.
        %    'Scaled'        : The ScaledChanged event is sent.
        %
        %  Events are hierarchically ordered, so sending a 'ConstraintChanged'
        %  event also implies that 'SetupAltered' and 'ObjectAltered' events must
        %  be sent.
        
        % Always send object altered event
        ev = {'ObjectChanged'};
        
        if strcmpi(Event, 'all')
            ev = [i_GetEventSections, i_GetSetupEvents, ev];
        elseif i_isSetupEvent(Event)
            ev = [{'SetupChanged'}, ev];
            % Get specific events to add
            switch lower(Event)
                case 'setup'
                    ev = [i_GetSetupEvents, ev];
                case 'constraint'
                    ev = [{'ConstraintChanged'}, ev];
                case 'objective'
                    ev = [{'ObjectiveChanged'}, ev];
                case 'dataset'
                    ev = [{'DatasetChanged'}, ev];
                case 'parameter'
                    ev = [{'ParameterChanged'}, ev];
                case 'freevariable'
                    % Free variable changes can affect the state of constraints and
                    % objectives
                    ev = [{'FreeVariableChanged', 'ConstraintChanged', 'ObjectiveChanged'}, ev];
                case 'inputvalue'
                    % Input value changes can affect the state of constraints and
                    % objectives
                    ev = [{'InputValueChanged', 'ConstraintChanged', 'ObjectiveChanged'}, ev];
                case 'scaled'
                    ev = [{'ScaledChanged'}, ev];
            end
        end
        for n = 1:length(ev)
            obj.notify(ev{n});
        end
        end  % sendEvent
        
        %----------------------------------------
        function setOptim(obj, Optim, Event)
        %SETOPTIM Set a new optimization object
        %  SETOPTIM(OBJ, OPTIM, EVENT) sets an updated optimization object to be
        %  viewed.  The optional argument EVENT specifies which set of events
        %  should be sent to notify views of the changes.  See the SENDEVENT method
        %  for the valid EVENT settings.
        
        obj.OptimObject = Optim;
        if ~isnull(obj.pOptim)
            obj.pOptim.info = Optim;
        end
        if nargin>2
            obj.sendEvent(Event);
        end
        
        end  % setOptim
        
    end  % public methods
    
end  % classdef

function ev = i_GetEventSections
ev = {'SetupChanged'};
end  % i_GetEventSections


function ev = i_GetSetupEvents
ev = {'ConstraintChanged', ...
    'ObjectiveChanged', ...
    'DatasetChanged', ...
    'ParameterChanged', ...
    'FreeVariableChanged', ...
    'InputValueChanged', ...
    'ScaledChanged'};
end  % i_GetSetupEvents


function ret = i_isSetupEvent(Event)
ret = any(strcmpi(Event, {'setup', 'objective', 'constraint', 'dataset', ...
    'parameter', 'freevariable', 'inputvalue', 'scaled'}));
end  % i_isSetupEvent