COPASI API  4.16.103
CEvaluationNode.h
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #ifndef COPASI_CEvaluationNode
16 #define COPASI_CEvaluationNode
17 
18 #include <iostream>
19 #include <vector>
20 
21 #include "copasi.h"
22 #include "utilities/CCopasiNode.h"
23 #include "CFunctionAnalyzer.h"
24 
25 class CEvaluationTree;
26 class ASTNode;
27 class ConverterASTNode;
28 class CCopasiDataModel;
29 
30 /**
31  * This is the base class for nodes used in an evaluation trees
32  */
33 class CEvaluationNode : public CCopasiNode< std::string >
34 {
35 public:
36  /**
37  * Enumeration of possible node types.
38  */
39  enum Type
40  {
41  INVALID = 0xFF000000,
42  NUMBER = 0x01000000,
43  CONSTANT = 0x02000000,
44  OPERATOR = 0x03000000,
45  OBJECT = 0x04000000,
46  FUNCTION = 0x05000000,
47  CALL = 0x06000000,
48  STRUCTURE = 0x07000000,
49  CHOICE = 0x08000000,
50  VARIABLE = 0x09000000,
51  WHITESPACE = 0x0a000000,
52  LOGICAL = 0x0b000000,
53  MV_FUNCTION = 0x0c000000, // This not yet implemented
54  VECTOR = 0x0d000000,
55  DELAY = 0x0e000000
56  };
57 
58  // Methods
59  /**
60  * Creates an evaluation node of type with the given data
61  * @param const Type & type
62  * @param const Data & data
63  * @return CEvaluationNode * evaluationNode
64  */
65  static CEvaluationNode * create(const Type & type,
66  const Data & data);
67 
68  /**
69  * Retrieve the subtype part of type
70  * @param const Type & type
71  * @return Type subType
72  */
73  static Type subType(const Type & type);
74 
75  /**
76  * Retrieve the type part of type
77  * @param const Type & type
78  * @return Type type
79  */
80  static Type type(const Type & type);
81 
82 private:
83  /**
84  * A list of reserved key words
85  */
86  static const char * Keywords[];
87 
88 public:
89  /**
90  * Check whether the string is a keyword
91  * @param const std::string & str
92  * @return bool isKeyword
93  */
94  static bool isKeyword(const std::string & str);
95 
96  /**
97  * Default constructor
98  */
100 
101  /**
102  * Unequal operator, compares two CEvaluationNode objects and return true if
103  * they are equal.
104  */
105  virtual bool operator!=(const CEvaluationNode& right) const;
106 
107  /**
108  * Equals operator, compares two CEvaluationNode objects and return true if
109  * they are equal.
110  */
111  bool operator==(const CEvaluationNode& right) const;
112 
113  /**
114  * Less operator, compares two CEvaluationNode objects and return true if
115  * the first operand is smaller than the second.
116  */
117  bool operator<(const CEvaluationNode& right) const;
118 
119 protected:
120  /**
121  * Specific constructor
122  * @param const Type & type
123  * @param const Data & data
124  */
125  CEvaluationNode(const Type & type,
126  const Data & data);
127 
128 public:
129  /**
130  * Copy constructor
131  * @param const CEvaluationNode & src
132  */
133  CEvaluationNode(const CEvaluationNode & src);
134 
135  /**
136  * Destructor
137  */
138  virtual ~CEvaluationNode();
139 
140  /**
141  * Retrieve the value of the node
142  * @return const C_FLOAT64 & value
143  */
144  inline const C_FLOAT64 & getValue() const {return *mpValue;}
145 
146  /**
147  * Calculate the numerical result of the node. It is assumed that
148  * all child nodes are up to date.
149  */
150  virtual inline void calculate() {};
151 
152  /**
153  * Compile a node;
154  * @param const CEvaluationTree * pTree
155  * @return bool success;
156  */
157  virtual bool compile(const CEvaluationTree * pTree);
158 
159  /**
160  * Retrieve the infix value of the node and its eventual child nodes.
161  * @return std::string Infix
162  */
163  virtual std::string getInfix(const std::vector< std::string > & children) const;
164 
165  /**
166  * Build the infix string.
167  * @return std::string Infix
168  */
169  std::string buildInfix() const;
170 
171  /**
172  * Retrieve the display string of the node and its eventual child nodes.
173  * @return std::string DisplayString
174  */
175  virtual std::string getDisplayString(const std::vector< std::string > & children) const;
176 
177  /**
178  * Build the human readable display string.
179  * @return std::string DisplayString
180  */
181  std::string buildDisplayString() const;
182 
183  /**
184  * Retrieve the display string of the node and its eventual child nodes in C.
185  * @return std::string CCodeString
186  */
187  virtual std::string getCCodeString(const std::vector< std::string > & children) const;
188 
189  /**
190  * Build the C-code string.
191  * @return std::string CCodeString
192  */
193  std::string buildCCodeString() const;
194 
195  /**
196  * Retrieve the display string of the node and its eventual child nodes
197  * in Berkeley Madonna format.
198  * @return std::string BerkeleyMadonnaString
199  */
200  virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > & children) const;
201 
202  /**
203  * Build the Berkeley Madonna string.
204  * @return std::string BerkeleyMadonnaString
205  */
206  std::string buildBerkeleyMadonnaString() const;
207 
208  /**
209  * Retrieve the display string of the node and its eventual child nodes
210  * in XPPAUT format.
211  * @return std::string XPPString
212  */
213  virtual std::string getXPPString(const std::vector< std::string > & children) const;
214 
215  /**
216  * Build the XPPAUT string.
217  * @return std::string XPPString
218  */
219  std::string buildXPPString() const;
220 
221  /**
222  * Retrieve the type of the node.
223  * Note the type combines type and subType information. To
224  * Retrieve the type use type(getType()) and to retrieve the
225  * subType use subType(getType()).
226  * @return const Type & type.
227  */
228  const Type & getType() const;
229 
230  /**
231  * Check whether the result is Boolean
232  * @return bool isBoolean
233  */
234  virtual bool isBoolean() const;
235 
236  /**
237  * Add the children to the node
238  * @param const std::vector< CEvaluationNode * > & children
239  */
240  void addChildren(const std::vector< CEvaluationNode * > & children);
241 
242  /**
243  * Create a new invalid ASTNode.
244  * @return ASTNode* return a pointer to the newly created node;
245  */
246  virtual ASTNode* toAST(const CCopasiDataModel* pDataModel) const;
247 
248  /**
249  * Copy a node and assign new children child1 and child2
250  * @return CEvaluationNode* return a pointer to the new node
251  */
252  CEvaluationNode* copyNode(CEvaluationNode* child1, CEvaluationNode* child2) const;
253 
254  /**
255  * Copy a node and assign new children in the vector
256  * @return CEvaluationNode* return a pointer to the new node
257  */
258  CEvaluationNode* copyNode(const std::vector<CEvaluationNode*>& children) const;
259 
260  /**
261  * Copy the whole branch with this node as root.
262  * @return CEvaluationNode*, return a pointer to the root of the newly copied branch
263  */
264  CEvaluationNode* copyBranch() const;
265 
266  /**
267  * Create a simplified node from the original node with children child1 and child2 (if not exist, = NULL)
268  * @return CEvaluationNode* return a pointer to the simplified node;
269  */
270  virtual CEvaluationNode* simplifyNode(const std::vector<CEvaluationNode*>& children) const;
271 
272  /**
273  * Comparison operator used to evaluate the precedence of the node.
274  * it compares the right precedence of the left node with the left
275  * precedence of the right node.
276  * @param const CEvaluationNode & rhs;
277  * @return bool isLess
278  */
279  bool operator < (const CEvaluationNode & rhs);
280 
281  /**
282  * Retrieve the pointer to the value of the node
283  * @return const C_FLOAT64 * pValue
284  */
285  const C_FLOAT64 * getValuePointer() const;
286 
287  /**
288  * Build the MathML string
289  * @param const std::vector< std::string > & children
290  * @param bool expand = true
291  * @param const std::vector< std::vector< std::string > > & variables
292  * @return std::string MMLString
293  */
294  virtual std::string getMMLString(const std::vector< std::string > & children,
295  bool expand,
296  const std::vector< std::vector< std::string > > & variables) const;
297 
298  /**
299  * Build the MathML string
300  * @param bool expand = true
301  * @param const std::vector< std::vector< std::string > > & variables
302  * @return std::string MMLString
303  */
304  std::string buildMMLString(bool expand, const std::vector< std::vector< std::string > > & variables) const;
305 
306  void printRecursively(std::ostream & os, int indent = 0) const;
307 
308  void printRecursively() const;
309 
310  /**
311  * Split the tree in two parts at the given splitnode, starting from *this.
312  * Returned is the root node of a copy of the tree, including everything above
313  * the split node and either the branch below the left child of the split node
314  * or the branch below the right child of the split node. The split node itself
315  * is not included either case.
316  */
317  CEvaluationNode* splitBranch(const CEvaluationNode* splitnode, bool left) const;
318 
319  /**
320  * Find a minus operator in the tree that is suitable for splitting with splitBranch().
321  * Specifically it is a minus operator that is connected with the root node by
322  * multiplication or division nodes only. For the division nodes only the
323  * left child is considered.
324  */
325  const CEvaluationNode* findTopMinus(const std::vector<CFunctionAnalyzer::CValue> & callParameters) const;
326 
327  // Attributes
328 protected:
329  /**
330  * The type the node
331  */
333 
334  /**
335  * The numerical value of the node
336  */
338 
339  /**
340  * A pointer to the numerical value of the node
341  */
343 
344  /**
345  * Structure holding the precedence information
346  * The Precedence Table
347  * ====================
348  *
349  * Nodes of type WHITESPACE are ignored.
350  * Higher right than left precedence means evaluation
351  * proceeds form left to right. Higher left precedence
352  * as can be found for the operator '^' means evaluation
353  * proceeds from right to left.
354  *
355  * left precedence | | right precedence
356  * -----------------+--------------+------------------
357  * | LOGIC: |
358  * 3 | .not. | 2
359  * 4 | .or. | 5
360  * 6 | .xor. | 7
361  * 8 | .and. | 9
362  * 10 | .eq. | 11
363  * 12 | .ne. | 13
364  * 14 | .gt. | 15
365  * 16 | .ge. | 17
366  * 18 | .lt. | 19
367  * 20 | .le. | 21
368  * -----------------+--------------+------------------
369  * | OPERATOR: |
370  * 22 | - | 23
371  * 22 | + | 23
372  * 26 | % | 27
373  * 28 | / | 29
374  * 28 | * | 29
375  * 33 | ^ | 32
376  * -----------------+--------------+------------------
377  * 35 | FUNCTION | 34
378  * 35 | MV_FUNCTION | 34
379  * 35 | CALL | 34
380  * 35 | CHOICE | 34
381  * -----------------+--------------+------------------
382  * 36 | NUMBER | 37
383  * 36 | CONSTANT | 37
384  * 36 | OBJECT | 37
385  * 36 | VARIABLE | 37
386  * -----------------+--------------+------------------
387  * | STRUCTURE: |
388  * ULONG_MAX | (| 1
389  * 0 | , | 1
390  * 0 |) | ULONG_MAX
391  */
393  {
394  public:
395  /**
396  * Default constructor
397  * @param const size_t & left (default: 0)
398  * @param const size_t & right (default: 0)
399  */
400  CPrecedence(const size_t & left = 0,
401  const size_t & right = 0);
402 
403  /**
404  * Copy constructor
405  * @param const CPrecedence & src
406  */
407  CPrecedence(const CPrecedence & src);
408 
409  /**
410  * Destructor
411  */
412  ~CPrecedence();
413 
414  /**
415  * The left precedence
416  */
417  size_t left;
418 
419  /**
420  * The left precedence
421  */
422  size_t right;
423  }
424  mPrecedence;
425 };
426 
427 #define PRECEDENCE_DEFAULT CPrecedence(0, 0)
428 
429 #define PRECEDENCE_LOGIG_NOT CPrecedence(3, 2)
430 #define PRECEDENCE_LOGIG_OR CPrecedence(4, 5)
431 #define PRECEDENCE_LOGIG_XOR CPrecedence(6, 7)
432 #define PRECEDENCE_LOGIG_AND CPrecedence(8, 9)
433 #define PRECEDENCE_LOGIG_EQ CPrecedence(10, 11)
434 #define PRECEDENCE_LOGIG_NE CPrecedence(12, 13)
435 #define PRECEDENCE_LOGIG_GT CPrecedence(14, 15)
436 #define PRECEDENCE_LOGIG_GE CPrecedence(16, 17)
437 #define PRECEDENCE_LOGIG_LT CPrecedence(18, 19)
438 #define PRECEDENCE_LOGIG_LE CPrecedence(20, 21)
439 
440 #define PRECEDENCE_OPERATOR_MINUS CPrecedence(22, 23)
441 #define PRECEDENCE_OPERATOR_PLUS CPrecedence(22, 23)
442 #define PRECEDENCE_OPERATOR_MODULUS CPrecedence(26, 27)
443 #define PRECEDENCE_OPERATOR_DIVIDE CPrecedence(28, 29)
444 #define PRECEDENCE_OPERATOR_MULTIPLY CPrecedence(28, 29)
445 #define PRECEDENCE_OPERATOR_POWER CPrecedence(33, 32)
446 
447 #define PRECEDENCE_FUNCTION CPrecedence(35, 34)
448 
449 #define PRECEDENCE_NUMBER CPrecedence(36, 37)
450 
451 #define PRECEDENCE_STRUCTURE_OPEN CPrecedence(ULONG_MAX, 1)
452 #define PRECEDENCE_STRUCTURE_COMMA CPrecedence(0, 1)
453 #define PRECEDENCE_STRUCTURE_CLOSE CPrecedence(0, ULONG_MAX)
454 
455 #include "CEvaluationNodeCall.h"
456 #include "CEvaluationNodeChoice.h"
457 #include "CEvaluationNodeConstant.h"
458 #include "CEvaluationNodeDelay.h"
459 #include "CEvaluationNodeFunction.h"
460 #include "CEvaluationNodeLogical.h"
461 #include "CEvaluationNodeNumber.h"
462 #include "CEvaluationNodeObject.h"
463 #include "CEvaluationNodeOperator.h"
465 #include "CEvaluationNodeVariable.h"
466 #include "CEvaluationNodeVector.h"
468 
469 #endif // COPASI_CEvaluationNode
bool operator<(const CEvaluationNode &right) const
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
const CEvaluationNode * findTopMinus(const std::vector< CFunctionAnalyzer::CValue > &callParameters) const
CEvaluationNode * copyBranch() const
virtual std::string getDisplayString(const std::vector< std::string > &children) const
virtual std::string getXPPString(const std::vector< std::string > &children) const
CPrecedence(const size_t &left=0, const size_t &right=0)
const C_FLOAT64 * getValuePointer() const
CEvaluationNode * copyNode(CEvaluationNode *child1, CEvaluationNode *child2) const
const C_FLOAT64 & getValue() const
virtual bool operator!=(const CEvaluationNode &right) const
const Type & getType() const
static CEvaluationNode * create(const Type &type, const Data &data)
std::string buildInfix() const
virtual ~CEvaluationNode()
std::string buildCCodeString() const
virtual std::string getInfix(const std::vector< std::string > &children) const
virtual std::string getMMLString(const std::vector< std::string > &children, bool expand, const std::vector< std::vector< std::string > > &variables) const
static bool isKeyword(const std::string &str)
void addChildren(const std::vector< CEvaluationNode * > &children)
virtual void calculate()
static Type type(const Type &type)
std::string buildXPPString() const
std::string buildDisplayString() const
virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > &children) const
void printRecursively() const
std::string buildMMLString(bool expand, const std::vector< std::vector< std::string > > &variables) const
static Type subType(const Type &type)
class CEvaluationNode::CPrecedence mPrecedence
#define C_FLOAT64
Definition: copasi.h:92
virtual bool isBoolean() const
bool operator==(const CEvaluationNode &right) const
virtual bool compile(const CEvaluationTree *pTree)
std::string buildBerkeleyMadonnaString() const
virtual CEvaluationNode * simplifyNode(const std::vector< CEvaluationNode * > &children) const
const C_FLOAT64 * mpValue
virtual std::string getCCodeString(const std::vector< std::string > &children) const
CEvaluationNode * splitBranch(const CEvaluationNode *splitnode, bool left) const
static const char * Keywords[]