samer@0: % with_arrow - Instatiate processing network and run a command against it samer@0: % samer@0: % with_arrow :: samer@27: % arrow(T1@typelist(N),T2@typelist(M),S), samer@0: % Cmd:(unit(T1,T2,S) -> action R) ~'function to apply to live unit', samer@0: % {[N]->size} ~'sizes of inputs', samer@0: % options { samer@0: % pause :: boolean/false ~'pause after creation and before destruction of unit'; samer@0: % keyboard :: boolean/false ~'drop to debug command line instead of pausing'; samer@0: % gui :: boolean/false ~'create Java GUI for viewables' samer@0: % } samer@0: % -> action R ~'eventually returns any return values from Cmd. samer@0: % samer@0: % This command brings to life the processing network defined by the samer@0: % supplied arrow. The function Cmd is called passing in a structure representing samer@0: % the live system. After Cmd, the system is closed in a controlled fashion, samer@0: % releasing any system resources that were claimed during construction. samer@0: % This occures even if an exception is thrown. HOWEVER - if you press Ctrl-C, samer@0: % this generates an uncatchable exception and so resources will not be samer@0: % correctly released. samer@0: % samer@0: % If the gui option is supplied, a Java GUI is created for any viewable samer@0: % objects created by the system (Java class samer.core.Viewable). This samer@0: % requires that the Java AWT be available, ie usejava('awt')==1. samer@0: samer@0: samer@0: function varargout=with_arrow(sys,cmd,sizes_in,varargin) samer@37: opts=options('pause',0,'gui',0,'keyboard',0,varargin{:}); samer@0: ud.figs=[]; samer@0: ud.arrows={}; samer@0: set(0,'UserData',ud); samer@0: u=construct(sys,sizes_in); samer@0: if opts.gui, samer@0: if usejava('awt'), samer@0: if exist('expose_viewables','file') samer@0: frame=expose_viewables(u.viewables,'system'); samer@0: else samer@0: fprintf('WARNING: could not find expose_viewables function.\n'); samer@0: opts.gui=false; samer@0: end samer@0: else samer@0: fprintf('WARNING: cannot create GUI Java AWT is unavailable.\n'); samer@0: opts.gui=false; samer@0: end samer@0: end samer@0: if opts.keyboard samer@0: whos samer@0: fprintf('type "return" to when you have finished snooping around.\n'); samer@0: keyboard samer@0: elseif opts.pause>0, samer@0: msg_pause('press any key to start'); samer@0: fprintf('running...'); samer@0: end samer@0: try, [varargout{1:nargout}]=cmd(u); samer@0: catch ex samer@0: fprintf('\nwith_arrow: CAUGHT EXCEPTION\n\n'); samer@0: % fprintf(getReport(ex)); samer@0: if opts.keyboard, keyboard samer@0: elseif opts.pause>0, msg_pause('\npress any key to close'); end samer@0: if opts.gui, frame.dispose; end samer@0: u.dispose(); samer@0: rethrow(ex); samer@0: end samer@0: if opts.keyboard samer@0: fprintf('type "return" to when you have finished snooping around.\n'); samer@0: keyboard samer@0: elseif opts.pause>0, samer@0: msg_pause('\npress any key to close'); samer@0: end samer@0: if opts.gui, frame.close; end samer@0: u.dispose(); samer@0: set(0,'UserData',[]); samer@0: end samer@0: samer@0: function msg_pause(msg) samer@0: fprintf(msg); pause; samer@0: fprintf('\n'); samer@0: end