Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/system/MarSystem.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_MARSYSTEM_H
00020 #define MARSYAS_MARSYSTEM_H
00021 
00022 #include <marsyas/common_header.h>
00023 #include <marsyas/system/MarControl.h>
00024 #include <marsyas/system/MarControlAccessor.h>
00025 #include <marsyas/realvec.h>
00026 #include <marsyas/marostring.h>
00027 #include <marsyas/Conversions.h>
00028 #include <marsyas/sched/EvEvent.h>
00029 #include <marsyas/sched/TmTime.h>
00030 #include <marsyas/sched/Scheduler.h>
00031 #include <marsyas/sched/TmControlValue.h>
00032 
00033 #include <vector>
00034 #include <iostream>
00035 #include <map>
00036 #include <string>
00037 #include <cassert>
00038 #include <utility>
00039 
00040 namespace Marsyas
00041 {
00084 class marsyas_EXPORT MarSystemObserver
00085 {
00086 public:
00087   virtual ~MarSystemObserver() {}
00088   virtual void preProcess( const realvec & input ) = 0;
00089   virtual void postProcess( const realvec & output ) = 0;
00090 };
00091 
00092 class marsyas_EXPORT MarSystem
00093 {
00094 
00095 //friend classes
00096   friend class MarSystemManager;
00097   friend class MarControl;
00098 
00099 public:
00100   typedef std::vector<MarSystem*>::size_type child_count_t;
00101   typedef std::vector<MarSystemObserver*>::size_type observer_count_t;
00102 
00103 private:
00104   void addControls();//add MarSystem default controls
00105   virtual void activate(bool state);
00106 
00107   std::vector<MarSystemObserver*> observers_;
00108 
00109   MarSystem * parent_scope_;
00110   std::map<std::string, MarSystem*> scope_;
00111 
00112 protected:
00113 
00114   // Parent MarSystem (if in a composite, otherwise it's NULL)
00115   MarSystem* parent_;
00116 
00117   // Type of MarSystem
00118   std::string type_;
00119 
00120   // Name of instance
00121   std::string name_;
00122 
00123   // /type_/name_/
00124   std::string prefix_;
00125 
00126   // /parent0Type/parent0Name/.../parentNType/parentNName/type_/name_/
00127   // in case this MarSystem is part of a composite
00128   // this is the absolute path to it
00129   std::string absPath_;
00130 
00131   bool isComposite_;
00132 
00133   // children
00134   std::vector<MarSystem*> marsystems_;
00135   std::vector<MarSystem*> attached_marsystems_;
00136 
00137   // Controls
00138   mutable std::map<std::string, MarControlPtr> controls_;
00139   typedef std::map<std::string, MarControlPtr>::iterator ControlItr;
00140 
00141   //in flow member vars
00142   mrs_natural inObservations_;
00143   mrs_natural inSamples_;
00144   mrs_real israte_;
00145   mrs_string inObsNames_;
00146   mrs_natural inStabilizingDelay_;
00147 
00148   //out flow member vars
00149   mrs_natural onObservations_;
00150   mrs_natural onSamples_;
00151   mrs_real osrate_;
00152   mrs_string onObsNames_;
00153   mrs_natural onStabilizingDelay_;
00154 
00155   //temporary in flow vars
00156   mrs_natural tinObservations_;
00157   mrs_natural tinSamples_;
00158   mrs_real      tisrate_;
00159   mrs_string    tinObsNames_;
00160   mrs_natural tinStabilizingDelay_;
00161 
00162   //temporary out flow vars
00163   mrs_natural tonObservations_;
00164   mrs_natural tonSamples_;
00165   mrs_real      tosrate_;
00166   mrs_string    tonObsNames_;
00167   mrs_natural tonStabilizingDelay_;
00168 
00169   mrs_natural irows_;
00170   mrs_natural icols_;
00171   mrs_natural orows_;
00172   mrs_natural ocols_;
00173 
00174   realvec inTick_;
00175   realvec outTick_;
00176 
00177   Scheduler scheduler_;
00178 
00179   // scheduling purposes
00180   mrs_natural count_;
00181 
00182   bool active_;
00183 
00184   mrs_natural addToStabilizingDelay_; // value which is added to inStabilizingDelay to produce onStabilizingDelay.  Defaults to 0.
00185 
00186   // is true while inside update() -> used for children to check if an update came from their parent
00187   bool isUpdating_;
00188 
00189   std::string MATLABscript_;
00190 
00191   // control paths
00192   std::string getControlRelativePath(std::string cname) const;
00193   std::string getControlLocalPath(std::string cname) const;
00194 
00204   virtual void myUpdate(MarControlPtr sender);
00205 
00206   virtual void localActivate(bool state);
00207 
00216   virtual void myProcess(realvec& in, realvec& out) = 0;
00217 
00218   // Assignment operator (should never be called!) [!]
00219   MarSystem& operator=(const MarSystem&)
00220   {
00221     assert(0);
00222     return *this;
00223   }
00224 
00225   virtual std::ostream& put_html_worker(std::ostream& o);
00226 
00227 public:
00228   MarSystem(std::string type, std::string name);
00229   MarSystem(const MarSystem& a);    // copy constructor
00230   virtual ~MarSystem();
00231 
00232   virtual MarSystem* clone() const = 0;
00233   void relinkControls(const MarSystem& a);
00234 
00235   // Naming methods
00236   virtual void setName(std::string name);
00237   virtual void setType(std::string type);
00238   const std::string & getType() const;
00239   const std::string & getName() const;
00240   const std::string & getPrefix() const;
00241   const std::string & getAbsPath() const;
00242   void updatePath();
00243 
00244   const std::map<std::string, MarControlPtr> & controls() { return controls_; }
00245 
00246   // Link controls
00247   bool linkControl(std::string cname1, std::string cname2, bool update = true);
00248   DEPRECATED(bool linkctrl(std::string cname1, std::string cname2, bool update = true))
00249   {
00250     return linkControl(cname1, cname2, update);
00251   }
00252 
00253   // Update controls
00254   bool updControl(MarControlPtr control, MarControlPtr newcontrol, bool upd = true);
00255   bool updControl(const char* cname, MarControlPtr newcontrol, bool upd = true);
00256   bool updControl(std::string cname, MarControlPtr newcontrol, bool upd = true);
00257   void updControl(EvEvent* me);
00258   void updControl(TmTime t, EvEvent* ev);
00259   void updControl(TmTime t, Repeat r, EvEvent* ev);
00260   void updControl(TmTime t, std::string cname, MarControlPtr control);
00261   void updControl(TmTime t, Repeat r, std::string cname, MarControlPtr control);
00262 
00263   DEPRECATED(bool updctrl(MarControlPtr control, MarControlPtr newcontrol, bool upd = true))
00264   {
00265     return updControl(control, newcontrol, upd);
00266   }
00267 
00268   DEPRECATED(bool updctrl(const char *cname, MarControlPtr newcontrol, bool upd = true))
00269   {
00270     return updControl(cname, newcontrol, upd);
00271   }
00272 
00273   DEPRECATED(bool updctrl(std::string cname, MarControlPtr newcontrol, bool upd = true))
00274   {
00275     return updControl(cname, newcontrol, upd);
00276   }
00277 
00278   // set controls (does not call update())
00279   bool setControl(std::string cname, MarControlPtr newcontrol)
00280   {
00281     return updControl(cname, newcontrol, NOUPDATE);
00282   }
00283   bool setctrl(const char *cname, MarControlPtr newcontrol)
00284   {
00285     return updControl(std::string(cname), newcontrol, NOUPDATE);
00286   }
00287   bool setctrl(std::string cname, MarControlPtr newcontrol)
00288   {
00289     return updControl(cname, newcontrol, NOUPDATE);
00290   }
00291   bool setctrl(MarControlPtr control, MarControlPtr newcontrol)
00292   {
00293     return updControl(control, newcontrol, NOUPDATE);
00294   }
00295 
00296   // query controls
00297   bool hasControl(MarControlPtr control, bool searchChildren = true);
00298   bool hasControlLocal(MarControlPtr control)
00299   {
00300     return hasControl(control, false);
00301   }
00302   bool hasControl(std::string cname, bool searchChildren = true);
00303   bool hasControlLocal(std::string cname)
00304   {
00305     return hasControl(cname, false);
00306   }
00307 
00308   bool isComposite()
00309   {
00310     return isComposite_;
00311   }
00312 
00321   MarControlPtr getControl(std::string path, bool searchParent = false, bool searchChildren = true);
00322   MarControlPtr getControlLocal(std::string cname)
00323   {
00324     return getControl(cname, false, false);
00325   }
00326   MarControlPtr getctrl(std::string cname)
00327   {
00328     return getControl(cname);
00329   }
00330 
00331   //add controls
00332   bool addControl(std::string cname, MarControlPtr v);
00333   bool addControl(std::string cname, MarControlPtr v, MarControlPtr& ptr);
00334   bool addctrl(std::string cname, MarControlPtr v)
00335   {
00336     return addControl(cname, v);
00337   }
00338   bool addctrl(std::string cname, MarControlPtr v, MarControlPtr& ptr)
00339   {
00340     return addControl(cname, v, ptr);
00341   }
00342 
00343   std::map<std::string, MarControlPtr> getControls(std::map<std::string, MarControlPtr>* cmap = NULL);
00344   const std::map<std::string, MarControlPtr>& getLocalControls();
00345 
00346   // set control state
00347   void setControlState(std::string cname, bool state);
00348   void setctrlState(std::string cname, bool state)
00349   {
00350     setControlState(cname, state);
00351   }
00352   void setctrlState(const char * cname, bool state)
00353   {
00354     setControlState(std::string(cname), state);
00355   }
00356   void setctrlState(MarControlPtr control, bool state)
00357   {
00358     control->setState(state);
00359   }
00360 
00361   // get control state
00362   bool hasControlState(std::string cname);
00363   bool hasctrlState(std::string cname)
00364   {
00365     return hasControlState(cname);
00366   }
00367   bool hasctrlState(char* cname)
00368   {
00369     return hasControlState(std::string(cname));
00370   }
00371   bool hasctrlState(MarControlPtr control)
00372   {
00373     return control->hasState();
00374   }
00375 
00376   // Composite interface
00377   virtual bool addMarSystem(MarSystem *marsystem);
00378 
00384   virtual MarSystem* getChildMarSystem(std::string path);
00385   virtual void setParent(const MarSystem* parent);
00386   MarSystem* getParent() const
00387   {
00388     return parent_;
00389   }
00390   virtual std::vector<MarSystem*> getChildren();
00391   bool isDescendentOf(MarSystem *parent);
00392 
00393   // Scope interface
00394 
00395   MarSystem * scope();
00396   MarSystem * parentScope() { return parent_scope_; }
00397   void addToScope( MarSystem * marsystem );
00398   void removeFromScope();
00399 
00400   // New composite interface //
00401 
00402   static std::string splitPathEnd( const std::string & path, std::string & remaining );
00403 
00404   std::string path() const;
00405 
00406   const std::vector<MarSystem*> & children() const { return marsystems_; }
00407 
00412   MarSystem *child( const std::string & name );
00413 
00418   MarControlPtr control( const std::string & name );
00419 
00424   MarSystem *subSystem( const std::string & name );
00425 
00436   MarSystem *remoteSystem( const std::string & path );
00437 
00447   MarControlPtr remoteControl( const std::string & path );
00448 
00449   // Processing and update methods
00450   bool isUpdating();
00451   void checkFlow(realvec&in, realvec& out);
00452   void update(MarControlPtr sender = MarControlPtr());
00453   void process(realvec& in, realvec& out);
00454   void tick();
00455 
00456   std::string toString();
00457   std::string toStringShort();
00458   std::string toStringGraphViz();
00459   void toStringGraphViz(std::ostringstream& os_defs, std::ostringstream& os_links);
00460 
00461   virtual marostring& toString(marostring& m);
00462 
00463   // Derived class such as Composite can override put,
00464   // essentially overriding operator<<
00465   virtual std::ostream& put(std::ostream& o, bool verbose);
00466 
00467   // The opposite of toString() and put() above, read in the parameters for a system.
00468   virtual std::istream& put(std::istream& is);
00469 
00470   // Output the MarSystem as an HTML document with nested lists
00471   virtual std::ostream& put_html(std::ostream& o);
00472 
00473   // the usual stream IO
00474   marsyas_EXPORT friend std::ostream& operator<<(std::ostream&, MarSystem&);
00475 
00476   // controls serialization methods
00477   marsyas_EXPORT friend std::istream& operator>>(std::istream&, MarSystem&); //[!]
00478   marsyas_EXPORT friend std::ostream& operator<<(std::ostream&, const std::map<std::string,MarControlPtr>&);
00479 
00480   // MATLAB scripting
00481   void setMATLABscript(std::string script);
00482   std::string getMATLABscript();
00483 
00485   // EvEvent methods
00487   mrs_natural getTime(std::string timer_name);
00488   void updtimer(std::string tmr_ctrl_path, TmControlValue value);
00489   void updtimer(std::string tmr_path, TmParam param);
00490   void updtimer(std::string tmr_path, std::vector<TmParam> params);
00491   void addTimer(std::string tmr_class, std::string tmr_ident);
00492   void addTimer(std::string tmr_class, std::string tmr_ident, std::vector<TmParam> params);
00493   //void addTimer(TmTimer* t);
00494   void removeTimer(std::string name);
00495 
00496   void updctrl(EvEvent* me);
00497   //void updctrl(std::string time, EvEvent* ev); //clashes with void upctrl(std::string cname, 0);
00498   //void updctrl(std::string time, Repeat rep, EvEvent* ev);
00499   //void updctrl(Repeat rep, EvEvent* ev);
00500   //void updctrl(std::string time, std::string cname, MarControlPtr control);
00501   //void updctrl(std::string time, Repeat rep, std::string cname, MarControlPtr control);
00502   //void updctrl(Repeat rep, std::string cname, MarControlPtr control);
00503   void updctrl(TmTime t, EvEvent* ev);
00504   void updctrl(TmTime t, Repeat rep, EvEvent* ev);
00505   void updctrl(TmTime t, std::string cname, MarControlPtr control);
00506   void updctrl(TmTime t, Repeat rep, std::string cname, MarControlPtr control);
00507 
00509   // Observing
00511 
00512   void addObserver( MarSystemObserver * observer );
00513   void removeObserver( MarSystemObserver * observer );
00514   bool hasObserver( MarSystemObserver * observer ) const;
00515 
00517   // Attaching
00519 
00520   void attachMarSystem( MarSystem * system );
00521   void detachMarSystem( MarSystem * system );
00522 
00524 
00525   //control pointers [!] should these be public?
00526   MarControlPtr ctrl_inSamples_;
00527   MarControlPtr ctrl_inObservations_;
00528   MarControlPtr ctrl_israte_;
00529   MarControlPtr ctrl_inObsNames_;
00530   MarControlPtr ctrl_inStabilizingDelay_;
00531   MarControlPtr ctrl_onSamples_;
00532   MarControlPtr ctrl_onObservations_;
00533   MarControlPtr ctrl_osrate_;
00534   MarControlPtr ctrl_onObsNames_;
00535   MarControlPtr ctrl_onStabilizingDelay_;
00536   MarControlPtr ctrl_debug_;
00537   MarControlPtr ctrl_verbose_;
00538   MarControlPtr ctrl_mute_;
00539   MarControlPtr ctrl_active_;
00540   MarControlPtr ctrl_processedData_;
00541 };
00542 
00543 // Helper function for adding a prefix to each of the observation names.
00544 mrs_string obsNamesAddPrefix(mrs_string observationNames, mrs_string prefix);
00545 
00546 // Helper function for splitting a string.
00547 std::vector<mrs_string> stringSplit(mrs_string input, mrs_string delimiter);
00548 
00549 // Helper function for splitting an observation name string.
00550 std::vector<mrs_string> obsNamesSplit(mrs_string observationNames);
00551 
00552 }//namespace Marsyas
00553 
00554 #endif