COPASI API  4.16.103
CEvaluationNodeObject.cpp
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 #include "copasi.h"
16 #include "CEvaluationNode.h"
17 #include "CEvaluationTree.h"
18 #include "CExpression.h"
20 #include "report/CCopasiObject.h"
22 #include "model/CModel.h"
24 #include "math/CMathObject.h"
25 
26 #include "sbml/math/ASTNode.h"
27 #include "sbml/SBase.h"
28 #include "sbml/SBMLTypeCodes.h"
29 #include "sbml/Compartment.h"
30 #include "sbml/Species.h"
31 #include "sbml/Parameter.h"
32 #include "sbml/Reaction.h"
33 
35  CEvaluationNode(CEvaluationNode::INVALID, ""),
36  mpObject(NULL),
37  mRegisteredObjectCN("")
39 
41  const Data & data):
42  CEvaluationNode((Type)(CEvaluationNode::OBJECT | subType), data),
43  mpObject(NULL),
44  mRegisteredObjectCN(data.substr(1, data.length() - 2))
45 {
47 }
48 
50  CEvaluationNode((Type)(CEvaluationNode::OBJECT | POINTER), "pointer"),
51  mpObject(NULL),
52  mRegisteredObjectCN("")
53 {
55  mpValue = pValue;
56  mData = pointerToString(pValue);
57 }
58 
60  CEvaluationNode(src),
61  mpObject(src.mpObject),
62  mRegisteredObjectCN(src.mRegisteredObjectCN)
63 {
64  mpValue = src.mpValue;
65 }
66 
68 
70 {
71  mpObject = NULL;
72  mpValue = NULL;
73 
74  switch ((int) subType(mType))
75  {
76  case CN:
77  {
78  const CExpression * pExpression = dynamic_cast< const CExpression * >(pTree);
79 
80  if (!pExpression) return false;
81 
82  mpObject =
83  pExpression->getNodeObject(mRegisteredObjectCN);
84 
85  const CCopasiObject * pDataObject = dynamic_cast< const CCopasiObject * >(mpObject);
86 
87  if (pDataObject != NULL)
88  {
89  // We may have some container objects for which the value is an included
90  // reference. For the math model to work this needs to be corrected.
91  const CObjectInterface * pObject = pDataObject->getValueObject();
92 
93  if (mpObject != pObject && pObject != NULL)
94  {
95  mpObject = pObject;
97  mData = getData();
98  }
99 
100  if (pDataObject->isValueDbl())
101  {
103  }
104  }
105  else if (mpObject != NULL)
106  {
108  }
109 
110  if (mpValue == NULL)
111  {
112  mValue = std::numeric_limits<C_FLOAT64>::quiet_NaN();
113  mpValue = &mValue;
114  return false;
115  }
116 
117  mData = "<" + mRegisteredObjectCN + ">";
118  }
119  break;
120 
121  case POINTER:
122  // We need to convert the data into a pointer
123  {
124  mpValue = (const C_FLOAT64 *) stringToPointer(mData);
125  }
126  break;
127 
128  case INVALID:
129  break;
130  }
131 
132  return (getChild() == NULL); // We must not have any children.
133 }
134 
136 {
137  static std::string data;
138 
139  switch ((int) subType(mType))
140  {
141  case CN:
142  return data = "<" + mRegisteredObjectCN + ">";
143  break;
144 
145  case POINTER:
146  return mData;
147  break;
148  }
149 
150  return mData;
151 }
152 
154 {
155  mData = data;
156 
157  if ((int) subType(mType) == (int) CN)
158  mRegisteredObjectCN = data.substr(1, data.length() - 2);
159 
160  return true;
161 }
162 
163 // virtual
164 std::string CEvaluationNodeObject::getInfix(const std::vector< std::string > & /* children */) const
165 {
166  switch ((int) subType(mType))
167  {
168  case CN:
169  return "<" + mRegisteredObjectCN + ">";
170  break;
171 
172  case POINTER:
173  return mData;
174  break;
175  }
176 
177  return mData;
178 }
179 
180 #if 0
181 std::string CEvaluationNodeObject::getDisplayString(const CEvaluationTree * pTree) const
182 {
183  const CExpression * pExpression = dynamic_cast< const CExpression * >(pTree);
184 
185  if (!pExpression) return false;
186 
187  const CCopasiObject * pObject =
188  CCopasiContainer::ObjectFromName(mRegisteredObjectCN);
189 
190  if (pObject == NULL) return "<" + mRegisteredObjectCN + ">";
191 
192  return "<" + pObject->getObjectDisplayName() + ">";
193 }
194 #endif
195 
196 // virtual
197 std::string CEvaluationNodeObject::getDisplayString(const std::vector< std::string > & /* children */) const
198 {
199  const CCopasiObject* object = dynamic_cast<const CCopasiObject*>(mpObject);
200 
201  if (object != NULL)
202  return object->getObjectDisplayName();
203 
204  return "<" + mRegisteredObjectCN + ">";
205 }
206 
207 // virtual
208 std::string CEvaluationNodeObject::getCCodeString(const std::vector< std::string > & /* children */) const
209 {
210  return mData;
211 }
212 
213 // virtual
214 std::string CEvaluationNodeObject::getBerkeleyMadonnaString(const std::vector< std::string > & /* children */) const
215 {
216  return mData;
217 }
218 
219 // virtual
220 std::string CEvaluationNodeObject::getXPPString(const std::vector< std::string > & /* children */) const
221 {
222  return mData;
223 }
224 
225 // static
226 CEvaluationNode * CEvaluationNodeObject::fromAST(const ASTNode * pASTNode, const std::vector< CEvaluationNode * > & children)
227 {
228  assert(pASTNode->getNumChildren() == children.size());
229 
230  CEvaluationNodeObject* pNode = NULL;
231 
232  switch (pASTNode->getType())
233  {
234  case AST_NAME_AVOGADRO:
235  case AST_NAME_TIME:
236  case AST_NAME:
237  pNode = new CEvaluationNodeObject(CN, CCopasiObjectName(std::string("<") + pASTNode->getName() + std::string(">")));
238  break;
239 
240  default:
241  break;
242  }
243 
244  return pNode;
245 }
246 
247 ASTNode* CEvaluationNodeObject::toAST(const CCopasiDataModel* pDataModel) const
248 {
249  ASTNode* node = new ASTNode();
250  node->setType(AST_NAME);
251 
252  if (mRegisteredObjectCN == "rateOf")
253  {
254  node->setType(AST_FUNCTION);
255  const CEvaluationNode* child = dynamic_cast<const CEvaluationNode*>(this->getChild());
256 
257  if (child == NULL) fatalError();
258 
259  const CEvaluationNodeObject* sibling = dynamic_cast<const CEvaluationNodeObject*>(this->getChild()->getSibling());
260 
261  if (sibling == NULL) fatalError();
262 
263  node->setName(sibling->getObjectCN().c_str());
264  node->addChild(child->toAST(pDataModel));
265  return node;
266  }
267 
268  // since I can not get the model in which this node is located, I just
269  // assume that it will always be the current global model.
270  const CCopasiObject* pOrigObject = pDataModel->getDataObject(mRegisteredObjectCN);
271 
272  if (pOrigObject == NULL)
273  {
274  node->setName(mRegisteredObjectCN.c_str());
275  return node;
276  }
277 
278  const CCopasiObject* pObject = pOrigObject;
279 
280  // if it is a reference, we get the parent of the reference
281  if (pObject->isReference())
282  {
283  pObject = pObject->getObjectParent();
284  }
285 
286  const CModelEntity* pME = dynamic_cast<const CModelEntity*>(pObject);
287 
288  if (pME != NULL)
289  {
290  const CModel* pModel = dynamic_cast<const CModel*>(pME);
291 
292  if (pModel != NULL)
293  {
294 #if LIBSBML_VERSION >= 40100
295 
296  if (pOrigObject->getObjectName() == "Avogadro Constant")
297  {
298  node->setType(AST_NAME_AVOGADRO);
299  node->setName("avogadro");
300  }
301  else
302  {
303 #endif // LIBSBML_VERSION >= 40100
304  node->setType(AST_NAME_TIME);
305  node->setName("time");
306 
307  if (pModel->getInitialTime() != 0.0)
308  {
310  }
311 
312 #if LIBSBML_VERSION >= 40100
313  }
314 
315 #endif // LIBSBML_VERSION >= 40100
316  }
317  else
318  {
319  node->setName(pME->getSBMLId().c_str());
320  }
321  }
322  else
323  {
324  const CCopasiParameter* pPara = dynamic_cast<const CCopasiParameter*>(pObject);
325 
326  if (pPara != NULL)
327  {
328  // now we have to use the common name as the name for the
329  // node since we need to be able to identify local parameters
330  // in arbitrary expressions for the export
331  node->setName(pPara->getCN().c_str());
332  }
333  else
334  {
335  const CReaction* pReaction = dynamic_cast<const CReaction*>(pObject);
336 
337  if (pReaction)
338  {
339  node->setName(pReaction->getSBMLId().c_str());
340  }
341  else
342  {
343  fatalError();
344  }
345  }
346  }
347 
348  return node;
349 }
350 
352 {return mRegisteredObjectCN;}
353 
355 {
356  return mpObject;
357 }
358 
360 {
361  return mpValue;
362 }
363 
365 {
366  assert(pObjectValue);
367 
368  switch ((int) subType(mType))
369  {
370  case CN:
371  break;
372 
373  case POINTER:
374 
375  if (mpValue != pObjectValue)
376  {
377  mpValue = pObjectValue;
379  }
380 
381  break;
382  }
383 
384  return;
385 }
386 
387 #include "utilities/copasimathml.h"
388 
389 // virtual
390 std::string CEvaluationNodeObject::getMMLString(const std::vector< std::string > & /* children */ ,
391  bool /* expand */,
392  const std::vector< std::vector< std::string > > & /* variables */) const
393 {
394  std::ostringstream out;
395 
396  const CCopasiObject * pDataObject = dynamic_cast< const CCopasiObject * >(mpObject);
397 
398  if (pDataObject == NULL)
399  {
400  const CMathObject * pMathObject = dynamic_cast< const CMathObject * >(mpObject);
401 
402  if (pMathObject != NULL)
403  {
404  pDataObject = pMathObject->getDataObject();
405  }
406  }
407 
408  out << CMathMl::getMMLName(pDataObject) << std::endl;
409 
410  return out.str();
411 }
Header file of class CExpression.
static std::string getMMLName(const CCopasiObject *obj)
Definition: copasimathml.h:45
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
static CEvaluationNode * fromAST(const ASTNode *pASTNode, const std::vector< CEvaluationNode * > &children)
virtual std::string getObjectDisplayName(bool regular=true, bool richtext=false) const
virtual std::string getDisplayString(const std::vector< std::string > &children) const
const std::string & getSBMLId() const
const CObjectInterface * getObjectInterfacePtr() const
const std::string & getObjectName() const
const CObjectInterface * getNodeObject(const CCopasiObjectName &CN) const
virtual CCopasiObjectName getCN() const =0
const CRegisteredObjectName & getObjectCN() const
void setObjectValuePtr(C_FLOAT64 *pObjectValue)
#define fatalError()
std::string pointerToString(const void *pVoid)
Definition: utility.cpp:423
virtual std::string getInfix(const std::vector< std::string > &children) const
const CObjectInterface * mpObject
const std::string & getSBMLId() const
Definition: CReaction.cpp:1654
#define MCSBML
bool isReference() const
CRegisteredObjectName mRegisteredObjectCN
virtual std::string getBerkeleyMadonnaString(const std::vector< std::string > &children) const
virtual std::string getXPPString(const std::vector< std::string > &children) const
const C_FLOAT64 & getInitialTime() const
Definition: CModel.cpp:1184
Header file of class CCopasiContainer.
static Type subType(const Type &type)
const C_FLOAT64 * getObjectValuePtr() const
virtual CCopasiObjectName getCN() const
class CEvaluationNode::CPrecedence mPrecedence
#define C_FLOAT64
Definition: copasi.h:92
void * stringToPointer(const std::string str)
Definition: utility.cpp:414
virtual ASTNode * toAST(const CCopasiDataModel *pDataModel) const
virtual std::string getMMLString(const std::vector< std::string > &children, bool expand, const std::vector< std::vector< std::string > > &variables) const
bool isValueDbl() const
virtual bool setData(const Data &data)
virtual std::string getCCodeString(const std::vector< std::string > &children) const
virtual bool compile(const CEvaluationTree *pTree)
Definition: CModel.h:50
#define PRECEDENCE_NUMBER
virtual void * getValuePointer() const =0
const C_FLOAT64 * mpValue
virtual const Data & getData() const
virtual const CCopasiObject * getValueObject() const
CCopasiNode< Data > * getChild()
Definition: CCopasiNode.h:210
CCopasiContainer * getObjectParent() const
const CCopasiObject * getDataObject() const