COPASI API  4.16.103
CExpression.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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 /*!
16  \file CExpression.cpp
17  \brief Implementation file of class CExpression
18  */
19 
20 #include "copasi.h"
21 
22 #include "CExpression.h"
23 
25 
26 CExpression::CExpression(const std::string & name,
27  const CCopasiContainer * pParent):
28  CEvaluationTree(name, pParent, CEvaluationTree::Expression),
29  mpListOfContainer(NULL),
30  mDisplayString(""),
31  mIsBoolean(false)
32 {
33  initObjects();
34 }
35 
37  const CCopasiContainer * pParent):
38  CEvaluationTree(src, pParent),
39  mpListOfContainer(NULL),
40  mDisplayString(src.mDisplayString),
41  mIsBoolean(src.mIsBoolean)
42 {
43  initObjects();
44  compile();
45 }
46 
48 
50 {
51  CObjectInterface * pObject =
52  const_cast< CObjectInterface * >(getObject(CCopasiObjectName("Reference=Value")));
53  assert(pObject != NULL);
54 
55  static_cast< CCopasiObject * >(pObject)->setRefresh(this, &CExpression::refresh);
56 }
57 
58 void CExpression::setIsBoolean(const bool & isBoolean)
59 {
61 }
62 
63 bool CExpression::setInfix(const std::string & infix)
64 {
65  if (!CEvaluationTree::setInfix(infix)) return false;
66 
67  if (mpNodeList == NULL) return true;
68 
69  // Check whether the expression has the expected type.
70  if (mpRoot != NULL)
71  {
72  if (mIsBoolean && !mpRoot->isBoolean())
73  {
74  return false;
75  }
76 
77  // We wrap a boolean expression in an if construct for
78  // non-boolean expressions
79  if (!mIsBoolean && mpRoot->isBoolean())
80  {
81  std::string Infix = "if(" + infix + ", 1, 0)";
83  }
84  }
85 
86  // We need to check that the expression does not contain any variables
87  std::vector< CEvaluationNode * >::const_iterator it = mpNodeList->begin();
88  std::vector< CEvaluationNode * >::const_iterator end = mpNodeList->end();
89 
90  for (; it != end; ++it)
91  if (((*it)->getType() & 0xFF000000) == CEvaluationNode::VARIABLE)
92  return false;
93 
94  return true;
95 }
96 
97 bool CExpression::compile(std::vector< CCopasiContainer * > listOfContainer)
98 {
99  if (getObjectParent() != NULL)
100  listOfContainer.push_back(getObjectParent());
101 
102  mpListOfContainer = & listOfContainer;
103 
104  bool success = compileNodes();
105 
106  if (mpRoot)
107  {
109  mInfix = mpRoot->buildInfix();
110  }
111  else
112  {
113  mDisplayString = "";
114  mInfix = "";
115  }
116 
117  mpListOfContainer = NULL;
118 
119  return success;
120 }
121 
123 {
124  calculate();
125 
126  return mValue;
127 }
128 
130 {calcValue();}
131 
133 {
134  const CCopasiDataModel* pDataModel = getObjectDataModel();
135 
136  if (pDataModel == NULL) return NULL;
137 
138  if (mpListOfContainer != NULL)
139  {
140  return pDataModel->ObjectFromCN(*mpListOfContainer, CN);
141  }
142  else
143  {
144  return pDataModel->getDataObject(CN);
145  }
146 }
147 
148 const std::vector< CCopasiContainer * > & CExpression::getListOfContainer() const
149 {return *mpListOfContainer;}
150 
152 {
153  if (mpNodeList == NULL) return false;
154 
155  mInfix = mpRoot->buildInfix();
156 
157  return true;
158 }
159 
160 const std::string & CExpression::getDisplayString() const
161 {return mDisplayString;}
162 
163 std::string CExpression::getCCodeString() const
164 {
165  std::string str1;
166 
167  if (mpRoot)
168  str1 = mpRoot->buildCCodeString();
169  else
170  str1 = "";
171 
172  return str1;
173 }
174 
176 {
177  std::string str1;
178 
179  if (mpRoot)
181  else
182  str1 = "";
183 
184  return str1;
185 }
186 
187 std::string CExpression::getXPPString() const
188 {
189  std::string str1;
190 
191  if (mpRoot)
192  str1 = mpRoot->buildXPPString();
193  else
194  str1 = "";
195 
196  return str1;
197 }
198 
199 #include "utilities/copasimathml.h"
200 
201 void CExpression::writeMathML(std::ostream & out, bool fullExpand, size_t l) const
202 {
203  if (mpRoot)
204  {
205  //create empty environment. Variable nodes should not occur in an expression
206  std::vector<std::vector<std::string> > env;
207 
208  bool flag = false; //TODO include check if parantheses are necessary
209 
210  if (flag) out << SPC(l) << "<mfenced>" << std::endl;
211 
212  out << mpRoot->buildMMLString(fullExpand, env);
213 
214  if (flag) out << SPC(l) << "</mfenced>" << std::endl;
215  }
216 }
217 
218 // static
220 {
221  size_t Size = CCopasiMessage::size();
222  CExpression * pInitialExpression = new CExpression(expression, expression.getObjectParent());
223 
224  std::vector< CEvaluationNode * > * pNodeList =
225  const_cast< std::vector< CEvaluationNode * > * >(&pInitialExpression->getNodeList());
226  std::vector< CEvaluationNode * >::iterator it = pNodeList->begin();
227  std::vector< CEvaluationNode * >::iterator end = pNodeList->end();
228 
229  CEvaluationNodeObject * pNode;
230  const CCopasiObject * pObject;
231  const CCopasiContainer * pObjectParent;
232  const CModelEntity * pEntity;
233  const CMetab * pMetab;
234 
235  for (; it != end; ++it)
236  {
237  if ((pNode = dynamic_cast< CEvaluationNodeObject * >(*it)) != NULL)
238  {
239  assert(pDataModel != NULL);
240 
241  if ((pObject = static_cast< const CCopasiObject * >(pDataModel->getObject(pNode->getObjectCN()))) != NULL &&
242  (pObjectParent = pObject->getObjectParent()) != NULL &&
243  (pEntity = dynamic_cast<const CModelEntity * >(pObjectParent)) != NULL)
244  {
245  if (pEntity->getValueReference() == pObject)
246  pNode->setData("<" + pEntity->getInitialValueReference()->getCN() + ">");
247  else if ((pMetab = dynamic_cast<const CMetab * >(pEntity)) != NULL &&
248  pMetab->getConcentrationReference() == pObject)
249  pNode->setData("<" + pMetab->getInitialConcentrationReference()->getCN() + ">");
250  }
251  }
252  }
253 
254  pInitialExpression->updateTree();
255 
256  while (CCopasiMessage::size() > Size)
258 
259  return pInitialExpression;
260 }
Header file of class CExpression.
CCopasiDataModel * getObjectDataModel()
CCopasiObject * getDataObject(const CCopasiObjectName &CN) const
virtual ~CExpression()
Definition: CExpression.cpp:47
virtual bool setInfix(const std::string &infix)
Definition: CExpression.cpp:63
void setRefresh(CType *pType, void(CType::*method)(void))
std::vector< CEvaluationNode * > * mpNodeList
virtual CCopasiObjectName getCN() const
void initObjects()
Definition: CExpression.cpp:49
const CObjectInterface * getNodeObject(const CCopasiObjectName &CN) const
const CRegisteredObjectName & getObjectCN() const
CCopasiObject * getInitialValueReference() const
CObjectInterface * ObjectFromCN(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &objName) const
std::string buildInfix() const
const std::string & getDisplayString() const
bool updateInfix()
std::string mInfix
std::string buildCCodeString() const
void setIsBoolean(const bool &booleanRequired)
Definition: CExpression.cpp:58
Definition: CMetab.h:178
std::string mDisplayString
Definition: CExpression.h:161
static CExpression * createInitialExpression(const CExpression &expression, const CCopasiDataModel *pDataModel)
std::string getBerkeleyMadonnaString() const
const std::vector< CCopasiContainer * > & getListOfContainer() const
std::string buildXPPString() const
std::string buildDisplayString() const
virtual const C_FLOAT64 & calcValue()
#define SPC(level)
CExpression(const std::string &name="Expression", const CCopasiContainer *pParent=NULL)
Definition: CExpression.cpp:26
std::string getCCodeString() const
static size_t size()
long int flag
Definition: f2c.h:52
std::string buildMMLString(bool expand, const std::vector< std::vector< std::string > > &variables) const
bool mIsBoolean
Definition: CExpression.h:166
std::string getXPPString() const
#define C_FLOAT64
Definition: copasi.h:92
virtual bool isBoolean() const
static CCopasiMessage getLastMessage()
const std::vector< CCopasiContainer * > * mpListOfContainer
Definition: CExpression.h:156
virtual bool compile()
std::string buildBerkeleyMadonnaString() const
virtual bool setData(const Data &data)
void writeMathML(std::ostream &out, bool fullExpand, size_t l) const
virtual const CObjectInterface * getObject(const CCopasiObjectName &cn) const
CConcentrationReference * getInitialConcentrationReference() const
Definition: CMetab.cpp:861
void refresh()
const std::vector< CEvaluationNode * > & getNodeList() const
bool isBoolean() const
CCopasiContainer * getObjectParent() const
virtual bool setInfix(const std::string &infix)
CEvaluationNode * mpRoot
CConcentrationReference * getConcentrationReference() const
Definition: CMetab.cpp:864
CCopasiObject * getValueReference() const