COPASI API  4.16.103
CEvaluationTree.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_CEvaluationTree
16 #define COPASI_CEvaluationTree
17 
18 #include <vector>
19 #include <stack>
20 
24 
25 class ASTNode;
26 template <class CType> class CCopasiVectorN;
27 
29  public CCopasiContainer
30 {
31 public:
32  /**
33  * The valid types of a function
34  */
35  enum Type
36  {
37  Function = 0,
43  };
44 
45  /**
46  * The string representation of valid types of a function
47  */
48  static const std::string TypeName[];
49 
50  /**
51  * The string representation of valid types of a function
52  */
53  static const char * XMLType[];
54 
55  // Operations
56 public:
57  /**
58  * Create an EvaluationTree of the specified type.
59  * Note: the returned object has to be released after use with delete.
60  * @param CEvaluationTree::Type type
61  * @return EvaluationTree * pEvaluationTree
62  */
63  static CEvaluationTree *
65 
66  /**
67  * Copy an EvaluationTree of the specified type.
68  * Note: the returned object has to be released after use with delete.
69  * @param const EvaluationTree & src
70  * @return EvaluationTree * pEvaluationTree
71  */
72  static CEvaluationTree *
73  copy(const CEvaluationTree & src);
74 
75  /**
76  * Default constructor
77  * @param const std::string & name (default: "NoName")
78  * @param const CCopasiContainer * pParent (default: NULL)
79  * @param const CEvaluationTree::Type & type (default: Function)
80  */
81  CEvaluationTree(const std::string & name = "NoName",
82  const CCopasiContainer * pParent = NULL,
83  const Type & type = Function);
84 
85  /**
86  * Copy constructor
87  * @param const CEvaluationTree & src
88  * @param const CCopasiContainer * pParent (default: NULL)
89  */
90  CEvaluationTree(const CEvaluationTree & src,
91  const CCopasiContainer * pParent = NULL);
92 
93  /**
94  * Destructor
95  */
96  virtual ~CEvaluationTree();
97 
98  /**
99  * Retrieves the type of the function
100  * @return const CEvaluationTree::Type & type
101  */
102  const CEvaluationTree::Type & getType() const;
103 
104 protected:
105  /**
106  * Set the type of the function
107  * @param const CEvaluationTree::Type & type
108  */
109  void setType(const CEvaluationTree::Type & type);
110 
111 public:
112  /**
113  * Sets the root node of the tree.
114  * @param CEvaluationNode* root node of the tree
115  * @return bool success
116  */
117  virtual bool setRoot(CEvaluationNode* pRootNode);
118 
119  /**
120  * Sets the complete tree from an SBML ASTNode.
121  * @param const ASTNode& root node of the tree
122  * @return bool success
123  */
124  bool setTree(const ASTNode& pRootNode);
125 
126  /**
127  * Converts an SBML ASTNode to the corresponding
128  * CEvaluationNode tree.
129  * @param const ASTNode * pASTNode
130  * @return CEvaluationNode*
131  */
132  static CEvaluationNode* fromAST(const ASTNode * pASTNode);
133 
134  /**
135  * Converts a CEvaluationTree to an ASTNode based tree.
136  * @return ASTNode* root node of the tree.
137  */
138  virtual ASTNode* toAST(const CCopasiDataModel* pDataModel) const;
139 
140  /**
141  * Set the infix description of the tree and compile it.
142  * @param const std::string & infix
143  * @return bool success
144  */
145  virtual bool setInfix(const std::string & infix);
146 
147  /**
148  * Compile the evaluation tree.
149  */
150  virtual bool compile();
151 
152  /**
153  * Check whether this function is usable, i.e., properly defined
154  * @return bool isUsable
155  */
156  bool isUsable() const;
157 
158  /**
159  * Check whether this function evaluates to a boolean value.
160  * @return bool isBoolean
161  */
162  bool isBoolean() const;
163 
164  /**
165  * Retrieve the infix description of the tree
166  * @return const std::string & infix
167  */
168  const std::string & getInfix() const;
169 
170  /**
171  * Comparison operator
172  * @param const CEvaluationTree & rhs
173  * @return bool equal
174  */
175  bool operator == (const CEvaluationTree & rhs) const;
176 
177  /**
178  * Retrieve the position of the error in the string description.
179  * std::string::npos indicates no error.
180  * @return std::string::size_type errorPosition
181  */
182  std::string::size_type getErrorPosition() const;
183 
184  /**
185  * Retrieve the list of nodes
186  * @return const std::vector< CEvaluationNode * > & nodeList;
187  */
188  const std::vector< CEvaluationNode * > & getNodeList() const;
189 
190  /**
191  * Retrieve the index to the value of the named variable.
192  * @param const std::string & name
193  * @return size_t
194  */
195  virtual size_t getVariableIndex(const std::string & name) const;
196 
197  /**
198  * Retrieve the value of the indexed variable
199  * @param const size_t & index
200  * @return const C_FLOAT64 & variableValue
201  */
202  virtual const C_FLOAT64 & getVariableValue(const size_t & index) const;
203 
204  /**
205  * Returns the root node of the tree.
206  */
208 
209  /**
210  * Returns the root node of the tree.
211  */
212  const CEvaluationNode* getRoot() const;
213 
214  /**
215  * Updates the infix and the nodeList
216  */
217  bool updateTree();
218 
219  /**
220  * Check whether the the tree has circular dependencies.
221  * @return bool hasCircularDependency
222  */
223  bool hasCircularDependency() const;
224 
225  /**
226  * Checks whether the tree has a call node referring to the expression
227  * with the given name
228  * @param const std::string & name
229  * @return bool dependsOn
230  */
231  bool dependsOnTree(const std::string & name) const;
232 
233  /**
234  * Retrieve the list of deleted objects.
235  * @return CCopasiObject::DataObjectSet deletedObjects
236  */
238 
239  /**
240  * Check whether the evaluation tree calls any tree in the list
241  * @param std::set< std::string > & list
242  * @return bool calls
243  */
244  bool calls(std::set< std::string > & list) const;
245 
246  /**
247  * Check whether tree may introduce discontinuities.
248  * @return bool hasDiscontinity
249  */
250  bool hasDiscontinuity() const;
251 
252 protected:
253  /**
254  * Parse the description
255  * @return bool success
256  */
257  bool parse();
258 
259  /**
260  * Compile the individual nodes.
261  * @return bool success
262  */
263  bool compileNodes();
264 
265  /**
266  * Build the sequence of calculations required to determine
267  * the value of the expression.
268  */
270 
271  /**
272  * Calculate the value
273  */
274  void calculate();
275 
276 private:
277  /**
278  * Initialize the contained CCopasiObjects
279  */
280  void initObjects();
281 
282  // Attributes
283 protected:
284  /**
285  * The type of the function
286  */
288 
289 private:
290  /**
291  * The key of the function
292  */
293  // std::string mKey;
294 
295 protected:
296  /**
297  * The infix representation of the expression
298  */
299  std::string mInfix;
300 
301  /**
302  * Value indicating whether this functions might be used in a model
303  */
304  bool mUsable;
305 
306 private:
307  /**
308  * The error position in case the compilation fails.
309  * std::string::npos indicates no error.
310  */
311  std::string::size_type mErrorPosition;
312 
313 protected:
314  /**
315  * A vector of nodes containing the result of parse.
316  */
317  std::vector< CEvaluationNode * > * mpNodeList;
318 
319  /**
320  * The root node of the tree.
321  */
323 
324  /**
325  * The last computed value of the tree;
326  */
328 
329  /**
330  * A vector of nodes which need to be calculated in sequence
331  */
332  std::vector< CEvaluationNode * > mCalculationSequence;
333 };
334 
335 #endif // COPASI_CEvaluationTree
void setType(const CEvaluationTree::Type &type)
virtual bool setRoot(CEvaluationNode *pRootNode)
virtual ~CEvaluationTree()
std::vector< CEvaluationNode * > * mpNodeList
virtual size_t getVariableIndex(const std::string &name) const
bool hasDiscontinuity() const
const CEvaluationTree::Type & getType() const
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
std::string mInfix
static const std::string TypeName[]
std::vector< CEvaluationNode * > mCalculationSequence
static CEvaluationNode * fromAST(const ASTNode *pASTNode)
virtual const C_FLOAT64 & getVariableValue(const size_t &index) const
void buildCalculationSequence()
std::string::size_type getErrorPosition() const
bool dependsOnTree(const std::string &name) const
static CEvaluationTree * copy(const CEvaluationTree &src)
bool operator==(const CEvaluationTree &rhs) const
CCopasiObject::DataObjectSet getDeletedObjects() const
bool isUsable() const
bool setTree(const ASTNode &pRootNode)
Header file of class CCopasiContainer.
#define C_FLOAT64
Definition: copasi.h:92
virtual bool compile()
std::string::size_type mErrorPosition
static const char * XMLType[]
bool calls(std::set< std::string > &list) const
std::set< const CCopasiObject * > DataObjectSet
const std::vector< CEvaluationNode * > & getNodeList() const
CEvaluationTree(const std::string &name="NoName", const CCopasiContainer *pParent=NULL, const Type &type=Function)
bool isBoolean() const
bool hasCircularDependency() const
CEvaluationNode * getRoot()
const std::string & getInfix() const
virtual bool setInfix(const std::string &infix)
CEvaluationNode * mpRoot
CEvaluationTree::Type mType
static CEvaluationTree * create(CEvaluationTree::Type type)