link to homepage

Institute for Neuroscience and Medicine

Navigation and service


API documentation

Attribute.h

Go to the documentation of this file.
00001 
00008 /*
00009  *  JEMRIS Copyright (C) 2007-2010  Tony Stöcker, Kaveh Vahedipour
00010  *                                  Forschungszentrum Jülich, Germany
00011  *
00012  *  This program is free software; you can redistribute it and/or modify
00013  *  it under the terms of the GNU General Public License as published by
00014  *  the Free Software Foundation; either version 2 of the License, or
00015  *  (at your option) any later version.
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU General Public License for more details.
00021  *
00022  *  You should have received a copy of the GNU General Public License
00023  *  along with this program; if not, write to the Free Software
00024  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 
00027 
00028 #ifndef ATTRIBUTE_H_
00029 #define ATTRIBUTE_H_
00030 
00031 #include "Declarations.h"
00032 
00033 #include     <stdexcept>
00034 #include     <map>
00035 #include     <vector>
00036 #include     <typeinfo>
00037 #include     <cmath>
00038 
00039 #include     "StrX.h"
00040 #include     <ginac/ginac.h>
00041 #include     <xercesc/dom/DOM.hpp>
00042 
00043 XERCES_CPP_NAMESPACE_USE
00044 
00045 //declaration of the Prototype
00046 class Prototype;
00047 
00048 typedef double (*FUNCP_4P) (double, double, double, double);
00049 
00054 class Attribute {
00055 
00056  public:
00057 
00062     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const    double& val) { Initialize(name,proto,pub,obs,val); };
00063     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const       int& val) { Initialize(name,proto,pub,obs,val); };
00064     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const      long& val) { Initialize(name,proto,pub,obs,val); };
00065     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const  unsigned& val) { Initialize(name,proto,pub,obs,val); };
00066     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const      bool& val) { Initialize(name,proto,pub,obs,val); };
00067     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const    string& val) { Initialize(name,proto,pub,obs,val); };
00068     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs, const PulseAxis& val) { Initialize(name,proto,pub,obs,val); };
00069 
00074     Attribute (const string& name, Prototype* proto, const bool& pub, const bool& obs) {
00075         m_public        = true;
00076         m_observable    = false;
00077         m_dynamic       = false;
00078         m_name                  = name;
00079         m_prototype     = proto;
00080         m_symbol_name   = "NA";
00081         m_formula           = "NA";
00082         m_ginac_excomp  = false;
00083         m_num_fp                = -1;
00084         m_cur_fp                = -1;
00085         m_nlgfp                 = NULL;
00086         m_diff                  = -1;
00087         m_sym_diff      = "NA";
00088         m_complex       = false;
00089         m_imaginary             = 0.0;
00090         m_address       = NULL;
00091         m_datatype      = "";
00092         m_backup        = NULL;
00093 
00094     }
00095 
00100     virtual ~Attribute ();
00101 
00107     bool IsPublic() const { return m_public;  }
00108 
00114     void SetPublic(bool val)    { m_public = val; }
00115 
00121     bool IsObservable() const    { return m_observable; }
00122 
00128     void SetObservable(bool val)    { m_observable = val; }
00129 
00136     bool IsDynamic() const    { return m_dynamic; }
00137 
00143     void SetDynamic(bool val)    { m_dynamic = val; }
00144 
00150     int GetDiff()    { return m_diff; }
00151 
00157     double GetImaginary()    { return m_imaginary; }
00158 
00164     bool IsComplex() const    { return m_complex; }
00165 
00171     void SetDiff(int val=0, string sym="diff")    { m_diff = val; m_sym_diff = sym; }
00172 
00178     string GetName() const    { return m_name; }
00179 
00185     string GetTypeID() const   { return m_datatype; }
00186 
00192     void* GetAddress() const    { return m_address; }
00193 
00199     Prototype*          GetPrototype() const    { return m_prototype; }
00200 
00206     string      GetSymbol() { return m_symbol_name; };
00207 
00221     bool SetMember (string expr, const vector<Attribute*>& obs_attribs, bool verbose = false);
00222 
00230     void EvalExpression ();
00231 
00250     double EvalCompiledExpression (double const val, string const attrib );
00251 
00262     double EvalCompiledNLGExpression (double const x, double const y, double const z, double const g );
00263 
00267     bool        HasGinacExCompiler(){return     m_ginac_excomp;};
00268 
00272     int         GetNumberFunctionPointers(){return m_num_fp;};
00273 
00277     int         GetCurrentFunctionPointer  (){  return m_cur_fp; };
00278 
00282     void        ResetCurrentFunctionPointer(){  m_cur_fp=0; };
00283 
00287     void        StepCurrentFunctionPointer (){  m_cur_fp = (m_cur_fp+1>m_num_fp)?m_num_fp:m_cur_fp+1; };
00288 
00289 
00290     /* pure template Functions need header implementation! */
00291 
00297     template <typename T> bool Notify (const T& val) {
00298 
00299         if ( !NewState(val) ) return false;
00300 
00301         //initiate re-evaluation and preparation of the observers
00302         for (unsigned int i=0; i<m_observers.size(); i++) {
00303 
00304                 //increase the counter for the observer's function pointer
00305                 if (m_observers.at(i)->GetNumberFunctionPointers()>0) {
00306                         m_observers.at(i)->StepCurrentFunctionPointer(  );
00307                 }
00308 
00309                 m_observers.at(i)->EvalExpression();
00310             UpdatePrototype( m_observers.at(i)->GetPrototype() );
00311             //cout << "DEBUG " << GetName() << " notified " << m_observers.at(i)->GetName() << endl;
00312         }
00313 
00314         return true;
00315     };
00316     
00323     template <typename T> bool NewState(const T& val) {
00324         if ( *((T*) m_backup) == val ) return false;
00325         *((T*) m_backup) = val;
00326         return true;
00327     };
00328 
00334     void AttachObserver (Attribute* attrib);
00335 
00341     void AttachSubject (Attribute* attrib);
00342 
00343 
00344  private:
00345 
00351     void UpdatePrototype (Prototype* prot);
00352 
00353 
00354     /* pure template Functions need header implementation */
00355 
00361     template <typename T> void WriteMember (const T& val) {
00362         *((T*) m_address) = val;
00363         Notify(val);
00364     };
00365 
00371     template <typename T> T GetMember (){
00372         T d = *((T*) m_address);
00373         if (isnan(d)) return 0;
00374         return d;
00375     };
00376 
00386         template <typename T> void Initialize(const string& name, Prototype* proto, const bool& pub, const bool& obs, const T& val){
00387         m_public        = pub;
00388         m_observable    = obs;
00389         m_dynamic       = false;
00390         m_name                  = name;
00391         m_prototype     = proto;
00392         m_symbol_name   = "x";
00393         m_formula           = "";
00394         m_ginac_excomp  = true;
00395         m_compiled.push_back(false);
00396         m_num_fp                = 0;
00397         m_cur_fp                = 0;
00398         m_nlgfp                 = NULL;
00399         m_diff                  = 0;
00400         m_sym_diff      = "diff";
00401         m_complex       = false;
00402         m_imaginary             = 0.0;
00403         m_address       = ((void*) &val);
00404         m_datatype      = typeid(T*).name();
00405         T* p            = new T(val);
00406         m_backup        = (void*) p;
00407 };
00408 
00409         bool                    m_public;               
00410         bool                    m_observable;   
00411         bool                    m_dynamic;              
00412         string                  m_name;                 
00413         void*                   m_address;              
00414         string                  m_datatype;             
00415         void*                   m_backup;               
00416     Prototype*          m_prototype;    
00417     string                      m_symbol_name;  
00418     string                      m_sym_diff;             
00419         string                  m_formula;              
00420         GiNaC::ex       m_expression;   
00421         GiNaC::lst              m_symlist;              
00422         bool                    m_ginac_excomp; 
00423         unsigned int    m_num_fp;               
00424         unsigned int    m_cur_fp;               
00425         vector<bool>    m_compiled;             
00426         vector<GiNaC::FUNCP_1P> m_fp;   
00427         vector<GiNaC::FUNCP_1P> m_fpi;  
00428         FUNCP_4P                m_nlgfp;                
00429         int                             m_diff;                 
00430         bool            m_complex;      
00431     double          m_imaginary;    
00432     vector<Attribute*> m_subjects;  
00433     vector<Attribute*> m_observers; 
00434 };
00435 
00436 #endif /* ATTRIBUTE_H_ */

Servicemeu

institutes

Scientific Technical Facilities