Skip to content

Commit

Permalink
Refactors eventManager and adds new useLivePlot class
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenNneji committed Jun 3, 2024
1 parent 5e2e53e commit aa985d9
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 165 deletions.
116 changes: 66 additions & 50 deletions API/events/eventManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function setEvents(value)
persistent events

if isempty(events)
events = {};
events = cell(0, 3);
end

switch op
Expand All @@ -36,62 +36,56 @@ function setEvents(value)
value = events;
end

function value = getHandlers()
% Gets the list of registered callbacks
function funcID = getCallbackID(callback)
% Generates a id for a callback handle function
%
% handlerList = eventManager.getHandlers();
value = eventManager.handlers('get');
% ID = getCallbackID(@disp)
funcID = func2str(callback);
funcDetails = functions(callback);
if strcmp(funcDetails.type, 'anonymous')
workspace = functions(callback).workspace{:};
if isfield(workspace, 'obj') && isprop(workspace.obj, 'callback') && isprop(workspace.obj, 'figureId')
funcID = sprintf('Function ''%s'' on figure %d', func2str(workspace.obj.callback), workspace.obj.figureId);
end
end
end

function setHandlers(value)
% Sets the list of registered callbacks
function callback = validateCallback(callback)
% Validate the given callback and return handle for the
% callback
%
% eventManager.setHandlers(handlerList);
eventManager.handlers('set', value);
end

function value=handlers(op, newValue)
% Helper to store the list of registered callbacks as static variable
%
% handlerList = eventManager.handlers('get');
% eventManager.handlers('set', handlerList);
persistent handlers

if isempty(handlers)
handlers = {};
% callback = validateCallback('disp');
if ~isa(callback, 'function_handle') && (~isText(callback) || isempty(char(callback)))
throw(exceptions.invalidType('Second value must be function name (text) or handle'));
end

switch op
case 'set'
handlers = newValue;

if isText(callback)
callback = str2func(callback);
end
value = handlers;
end

function register(eventType, functionName)
function register(eventType, callback)
% Register a callback function for the given eventType. eventType
% should be an eventTypes enum and functionName should be name of the
% should be an eventTypes enum and callback should be name or handle of the
% callback function.
%
% eventManager.register(eventTypes.Plot, 'plotRefSLDHelper');
eventType = validateOption(eventType, 'eventTypes', 'Event type must be an eventTypes enum').value;

if ~isText(functionName) || isempty(char(functionName))
throw(exceptions.invalidType('Second value must be function name (text)'));
end
callback = eventManager.validateCallback(callback);

events = eventManager.getEvents();
handlers = eventManager.getHandlers();
funcID = eventManager.getCallbackID(callback);

if isempty(events) || ~any([events{:}] == eventType)
eventManagerInterface('register', eventType);
if ~isempty(events)
for i=1:size(events, 1)
if strcmp(events{i, 1}, funcID) && events{i, 2} == eventType
return
end
end
end

events{end + 1} = eventType;
handlers{end + 1} = functionName;

eventManager.setEvents(events)
eventManager.setHandlers(handlers)
events(end + 1, :) = {funcID, eventType, callback};
eventManager.setEvents(events);
end

function notify(eventType, data)
Expand All @@ -101,28 +95,50 @@ function notify(eventType, data)
%
% eventManager.notify(eventTypes.Message, 'wow');
events = eventManager.getEvents();
handlers = eventManager.getHandlers();

eventType = validateOption(eventType, 'eventTypes', 'Event type must be an eventTypes enum').value;
for i=1:length(events)
if (eventType == events{i})
for i=1:size(events, 1)
if eventType == events{i, 2}
try
funcHandle = str2func(handlers{i});
funcHandle = events{i, 3};
funcHandle(data);
catch ME
fprintf('EVENTMANAGER: calling %s function failed on line %d because: \n\n %s\n', handlers{i}, ME.stack(1).line, ME.message);
fprintf('EVENTMANAGER: calling %s function failed on line %d because: \n\n %s\n', events{i, 1}, ME.stack(1).line, ME.message);
end
end
end
end

function clear()
% Clear all register event callbacks
function unregister(eventType, callback)
% Remove specific event callbacks
%
% eventManager.unregister(eventTypes.Plot, 'plotRefSLDHelper');
eventType = validateOption(eventType, 'eventTypes', 'Event type must be an eventTypes enum').value;
callback = eventManager.validateCallback(callback);
events = eventManager.getEvents();
for i=size(events, 1):-1:1
if (eventType == events{i, 2}) && strcmp(eventManager.getCallbackID(callback), events{i, 1})
events(i, :) = [];
end
end
eventManager.setEvents(events);
end

function clear(varargin)
% Clear all register event callbacks or specific types
%
% eventManager.clear();
eventManager.setEvents({})
eventManager.setHandlers({})
eventManagerInterface('clear');
% eventManager.clear(eventTypes.Plot);
events = eventManager.getEvents();
if isempty(events)
return
end
if nargin == 0
events = cell(0, 3);
else
eventType = validateOption(varargin{1}, 'eventTypes', 'Event type must be an eventTypes enum').value;
events(eventType==[events{:, 2}], :) = [];
end
eventManager.setEvents(events);
end

end
Expand Down
5 changes: 0 additions & 5 deletions buildScript.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
ratMainCompileScript;
ratMainCodeGen;

% Finally, change the paths back or the tests might fail...
compilePath = fullfile(thisPath, 'compile', 'events');
cd(compilePath);
eventCompileScript;

compilePath = fullfile(thisPath, 'compile', 'customWrapper');
cd(compilePath);
wrapperCompileScript;
Expand Down
2 changes: 1 addition & 1 deletion docs/source/livePlot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ reflectivity, SLD's and so on as the fit progresses. By writing functions that '
In this section, we'll use this capability to build a live, updating plot of reflectivity and SLD, similar to that of the main RasCAL GUI.

.. note::
The code in this section already exists in the repo (utilities / plotting), and you can activate the full updating plot at any time by just typing 'useUpdatePlot()' at the Matlab command window. But we detail it here to illustrate how to interact with events.
The code in this section already exists in the repo (utilities / plotting), and you can activate the full updating plot at any time by just typing 'useLivePlot()' at the Matlab command window. But we detail it here to illustrate how to interact with events.

**Registering Listeners**

Expand Down
2 changes: 1 addition & 1 deletion docs/source/plotFunsBayes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

==========================
Plotting Bayesian Analysis
===========================
==========================

A number of function exist for plotting the results of Bayesian analysis.

Expand Down
29 changes: 0 additions & 29 deletions examples/miscellaneous/alternativeLanguages/speedTest.m

This file was deleted.

Loading

0 comments on commit aa985d9

Please sign in to comment.