JEMRIS  2.8.1
open-source MRI simulations
Attribute.h
Go to the documentation of this file.
1 
8 /*
9  * JEMRIS Copyright (C)
10  * 2006-2015 Tony Stoecker
11  * 2007-2015 Kaveh Vahedipour
12  * 2009-2015 Daniel Pflugfelder
13  *
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 
31 #ifndef ATTRIBUTE_H_
32 #define ATTRIBUTE_H_
33 
34 #include "Declarations.h"
35 
36 #include <stdexcept>
37 #include <map>
38 #include <vector>
39 #include <typeinfo>
40 #include <cmath>
41 
42 #include <iostream>
43 #include <iomanip>
44 
45 #include "StrX.h"
46 #include <ginac/ginac.h>
47 #include <xercesc/dom/DOM.hpp>
48 
49 using namespace std;
50 
51 XERCES_CPP_NAMESPACE_USE
52 
53 //declaration of the Prototype
54 class Prototype;
55 
56 typedef double (*FUNCP_4P) (double, double, double, double);
57 
62 class Attribute {
63 
64  public:
65 
70  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const double& val) { Initialize(name,proto,pub,obs,val); };
71  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const int& val) { Initialize(name,proto,pub,obs,val); };
72  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const long& val) { Initialize(name,proto,pub,obs,val); };
73  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const unsigned& val) { Initialize(name,proto,pub,obs,val); };
74  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const bool& val) { Initialize(name,proto,pub,obs,val); };
75  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const std::string& val) { Initialize(name,proto,pub,obs,val); };
76  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const PulseAxis& val) { Initialize(name,proto,pub,obs,val); };
77 
82  Attribute (const std::string& name, Prototype* proto, const bool& pub, const bool& obs) {
83  m_public = true;
84  m_observable = false;
85  m_dynamic = false;
86  m_name = name;
87  m_prototype = proto;
88  m_symbol_name = "NA";
89  m_formula = "NA";
90  m_ginac_excomp = false;
91  m_num_fp = -1;
92  m_cur_fp = -1;
93  m_nlgfp = NULL;
94  m_diff = -1;
95  m_sym_diff = "NA";
96  m_complex = false;
97  m_imaginary = 0.0;
98  m_address = NULL;
99  m_datatype = "";
100  m_backup = NULL;
101 
102  }
103 
108  virtual ~Attribute ();
109 
115  bool IsPublic() const { return m_public; }
116 
122  void SetPublic(bool val) { m_public = val; }
123 
129  bool IsObservable() const { return m_observable; }
130 
136  void SetObservable(bool val) { m_observable = val; }
137 
144  bool IsDynamic() const { return m_dynamic; }
145 
151  void SetDynamic(bool val) { m_dynamic = val; }
152 
158  int GetDiff() { return m_diff; }
159 
165  double GetImaginary() { return m_imaginary; }
166 
172  bool IsComplex() const { return m_complex; }
173 
180  void SetDiff(int val=0, std::string sym="diff") { m_diff = val; m_sym_diff = sym; }
181 
187  std::string GetName() const { return m_name; }
188 
194  std::string GetTypeID() const { return m_datatype; }
195 
201  void* GetAddress() const { return m_address; }
202 
208  Prototype* GetPrototype() const { return m_prototype; }
209 
215  std::string GetSymbol() { return m_symbol_name; };
216 
222  std::string GetFormula() { return m_formula; };
223 
237  bool SetMember (std::string expr, const std::vector<Attribute*>& obs_attribs, const std::vector<std::string>& obs_attrib_keyword, bool verbose = false);
238 
246  void EvalExpression ();
247 
251  std::vector<Attribute*> GetObservers (){ return m_observers; };
252 
256  std::vector<Attribute*> GetSubjects (){ return m_subjects; };
257 
276  double EvalCompiledExpression (double const val, std::string const attrib );
277 
289  double EvalCompiledNLGExpression (double const x, double const y, double const z, double const g );
290 
294  bool HasGinacExCompiler(){return m_ginac_excomp;};
295 
299  int GetNumberFunctionPointers(){return m_num_fp;};
300 
304  int GetCurrentFunctionPointer (){ return m_cur_fp; };
305 
309  void ResetCurrentFunctionPointer(){ m_cur_fp=0; };
310 
314  void StepCurrentFunctionPointer (){ m_cur_fp = (m_cur_fp+1>m_num_fp)?m_num_fp:m_cur_fp+1; };
315 
316 
317  /* pure template Functions need header implementation! */
318 
324  template <typename T> bool Notify (const T& val) {
325 
326  if ( !NewState(val) ) return false;
327 
328  //initiate re-evaluation and preparation of the observers
329  for (unsigned int i=0; i<m_observers.size(); i++) {
330 
331  //increase the counter for the observer's function pointer
332  if (m_observers.at(i)->GetNumberFunctionPointers()>0) {
333  m_observers.at(i)->StepCurrentFunctionPointer( );
334  }
335  //cout << "DEBUG " << GetName() << " notified " << m_observers.at(i)->GetName() << endl;
336  m_observers.at(i)->EvalExpression();
337  m_observers.at(i)->UpdatePrototype();
338  }
339 
340  return true;
341  }
342 
349  template <typename T> bool NewState(const T& val) {
350  if ( *((T*) m_backup) == val ) return false;
351  *((T*) m_backup) = val;
352  return true;
353  };
354 
360  void AttachObserver (Attribute* attrib);
361 
367  void AttachSubject (Attribute* attrib);
368 
369 
370  private:
371 
377  void UpdatePrototype ();
378 
379 
380  /* pure template Functions need header implementation */
381 
387  template <typename T> void WriteMember (const T& val) {
388  *((T*) m_address) = val;
389  //Notify(val);
390  }
391 
397  template <typename T> T GetMember (){
398  T d = *((T*) m_address);
399  if (std::isnan(d)) return 0;
400  return d;
401  }
402 
412  template <typename T> void Initialize(const std::string& name, Prototype* proto, const bool& pub, const bool& obs, const T& val){
413  m_public = pub;
414  m_observable = obs;
415  m_dynamic = false;
416  m_name = name;
417  m_prototype = proto;
418  m_symbol_name = "x";
419  m_formula = "";
420  m_ginac_excomp = true;
421  m_compiled.push_back(false);
422  m_num_fp = 0;
423  m_cur_fp = 0;
424  m_nlgfp = NULL;
425  m_diff = 0;
426  m_sym_diff = "diff";
427  m_complex = false;
428  m_imaginary = 0.0;
429  m_address = ((void*) &val);
430  m_datatype = typeid(T*).name();
431  T* p = new T(val);
432  m_backup = (void*) p;
433  }
434 
435  bool m_public;
437  bool m_dynamic;
438  std::string m_name;
439  void* m_address;
440  std::string m_datatype;
441  void* m_backup;
443  std::string m_symbol_name;
444  std::string m_sym_diff;
445  std::string m_formula;
446  GiNaC::ex m_expression;
447  GiNaC::lst m_symlist;
449  unsigned int m_num_fp;
450  unsigned int m_cur_fp;
451  std::vector<bool> m_compiled;
452  std::vector<GiNaC::FUNCP_1P> m_fp;
453  std::vector<GiNaC::FUNCP_1P> m_fpi;
454  FUNCP_4P m_nlgfp;
455  int m_diff;
456  bool m_complex;
457  double m_imaginary;
458  std::vector<Attribute*> m_subjects;
459  std::vector<Attribute*> m_observers;
460 };
461 
462 #endif /* ATTRIBUTE_H_ */
Implementation of JEMRIS Declarations.
void StepCurrentFunctionPointer()
Increase the counter to the current function pointer by one.
Definition: Attribute.h:314
T GetMember()
get the member variable of this attribute
Definition: Attribute.h:397
std::string m_formula
Mathematical formula of the attribute (in XML) for GiNaC evaluation.
Definition: Attribute.h:445
std::string m_name
Name of the attribute (in XML).
Definition: Attribute.h:438
Prototype * m_prototype
Pointer to the Prototype object, which instantiated this attribute.
Definition: Attribute.h:442
std::string GetFormula()
Get the GiNaC formula of this attribute.
Definition: Attribute.h:222
bool m_public
Indicating whether the attribute is accessible through XML.
Definition: Attribute.h:435
Attribute class. Attributes are private member variables of a Prototype which are accessible through ...
Definition: Attribute.h:62
void SetObservable(bool val)
Set this attribute to be observable by other attributes.
Definition: Attribute.h:136
int GetCurrentFunctionPointer()
Return the counter to the current function pointer.
Definition: Attribute.h:304
bool NewState(const T &val)
Check the state of the value represented by this attribute.
Definition: Attribute.h:349
int GetNumberFunctionPointers()
Return the total number of function pointers.
Definition: Attribute.h:299
void ResetCurrentFunctionPointer()
Set the counter to the current function pointer to zero.
Definition: Attribute.h:309
GiNaC::ex m_expression
GiNaC Mathematical expression of the attribute.
Definition: Attribute.h:446
unsigned int m_num_fp
Number of GiNaC expression function pointers owned by this attribute.
Definition: Attribute.h:449
std::vector< Attribute * > m_subjects
Vector of attributes under observation by this attribute.
Definition: Attribute.h:458
Attribute(const std::string &name, Prototype *proto, const bool &pub, const bool &obs, const double &val)
Constructors: must provide name (XML), Prototype, public/observable status, and the member variable...
Definition: Attribute.h:70
bool m_observable
Indicating whether the attribute is observable.
Definition: Attribute.h:436
double m_imaginary
The imaginary part of complex expression evaluation.
Definition: Attribute.h:457
std::vector< GiNaC::FUNCP_1P > m_fp
Function pointers to GiNaC expression evaluation.
Definition: Attribute.h:452
std::vector< Attribute * > m_observers
Vector of attributes observing this attribute.
Definition: Attribute.h:459
std::string m_sym_diff
GiNaC symbol name for symbolic derivative.
Definition: Attribute.h:444
GiNaC::lst m_symlist
GiNaC list of all symbols involved in the calculation.
Definition: Attribute.h:447
std::vector< Attribute * > GetSubjects()
Return the subjects.
Definition: Attribute.h:256
std::string m_datatype
Type of the Prototype member variable, which is represented by this attribute.
Definition: Attribute.h:440
void * GetAddress() const
Get the pointer to the value represented by this attribute.
Definition: Attribute.h:201
Attribute(const std::string &name, Prototype *proto, const bool &pub, const bool &obs)
Constructor for an unobservable Attribute which does not represent a member variable.
Definition: Attribute.h:82
bool m_dynamic
Indicating whether the attribute dynamically changes its value in runtime.
Definition: Attribute.h:437
bool m_complex
If symbolic expressions are complex, the imaginary part is considered.
Definition: Attribute.h:456
Implementation of JEMRIS StrX.
bool HasGinacExCompiler()
True, if GiNaC external compiler is available on this system.
Definition: Attribute.h:294
Prototype super class.
Definition: Prototype.h:71
double GetImaginary()
Get the imaginary part the attribute&#39;s evaluation.
Definition: Attribute.h:165
std::string m_symbol_name
GiNaC symbol name of the attribute.
Definition: Attribute.h:443
bool IsPublic() const
Check, if this attribute is public for manipulation through XML.
Definition: Attribute.h:115
void SetDiff(int val=0, std::string sym="diff")
Set the number of symbolic differentiations of the attribute&#39;s expression.
Definition: Attribute.h:180
std::vector< GiNaC::FUNCP_1P > m_fpi
Function pointers to GiNaC expression evaluation of imaginary part.
Definition: Attribute.h:453
std::string GetName() const
Get the name of the attribute.
Definition: Attribute.h:187
void WriteMember(const T &val)
write the member variable of this attribute
Definition: Attribute.h:387
std::vector< bool > m_compiled
True, if GiNaC expression is compiled successfully in run time.
Definition: Attribute.h:451
Prototype * GetPrototype() const
Get the pointer to the Prototype to which this attribute belongs.
Definition: Attribute.h:208
void Initialize(const std::string &name, Prototype *proto, const bool &pub, const bool &obs, const T &val)
Initialize private members.
Definition: Attribute.h:412
int GetDiff()
Get the number of symbolic differentiations of the attribute&#39;s expression.
Definition: Attribute.h:158
void * m_backup
Backup value of the Prototype member variable, which is represented by this attribute.
Definition: Attribute.h:441
bool Notify(const T &val)
Notify all observers.
Definition: Attribute.h:324
bool m_ginac_excomp
True, if GiNaC external compiler is available on this system.
Definition: Attribute.h:448
std::string GetTypeID() const
Get the type ID of the attribute.
Definition: Attribute.h:194
bool IsObservable() const
Check, if this attribute is observable by other attributes.
Definition: Attribute.h:129
void SetPublic(bool val)
Set this attribute to be public for manipulation through XML.
Definition: Attribute.h:122
std::vector< Attribute * > GetObservers()
Return the observers.
Definition: Attribute.h:251
int m_diff
Number of symbolic differentiations of the attribute&#39;s expression.
Definition: Attribute.h:455
void SetDynamic(bool val)
Set this attribute to be dynamic.
Definition: Attribute.h:151
bool IsDynamic() const
Check, if this attribute is dynamic. This is needed for exporting static XML Files of the Sequence...
Definition: Attribute.h:144
unsigned int m_cur_fp
Current GiNaC expression function pointer.
Definition: Attribute.h:450
std::string GetSymbol()
Get the GiNaC symbol of this attribute.
Definition: Attribute.h:215
FUNCP_4P m_nlgfp
Function pointer to GiNaC expression evaluation of nonlinear gradients.
Definition: Attribute.h:454
void * m_address
Pointer to the Prototype member variable, which is represented by this attribute. ...
Definition: Attribute.h:439
bool IsComplex() const
Check, if this attribute&#39;s evaluation is complex.
Definition: Attribute.h:172

-- last change 17.06.2016 | Tony Stoecker | Imprint --