COPASI API  4.16.103
CNormalChoice.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/CNormalChoice.cpp,v $
3 // $Revision: 1.5 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2007/12/11 20:55:55 $
7 // End CVS Header
8 
9 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc. and EML Research, gGmbH.
11 // All rights reserved.
12 
13 #ifdef WIN32
14 # pragma warning (disable: 4786)
15 # pragma warning (disable: 4243)
16 // warning C4355: 'this' : used in base member initializer list
17 # pragma warning (disable: 4355)
18 #endif // WIN32
19 
20 #include <sstream>
21 
22 #include "CNormalChoice.h"
23 #include "CNormalLogical.h"
24 #include "CNormalItem.h"
25 #include "CNormalFraction.h"
26 
27 CNormalChoice::CNormalChoice(): CNormalBase(), mpCondition(NULL), mpTrue(NULL), mpFalse(NULL)
28 {}
29 
31  CNormalBase(src),
32  mpCondition(NULL),
33  mpTrue(NULL),
34  mpFalse(NULL)
35 {*this = src;}
36 
38 {
40  pdelete(mpTrue);
42 }
43 
45 {
47  pdelete(mpTrue);
49 
50  if (src.mpCondition != NULL)
52  if (src.mpCondition != NULL)
53  mpTrue = new CNormalFraction(*src.mpTrue);
54  if (src.mpCondition != NULL)
55  mpFalse = new CNormalFraction(*src.mpFalse);
56 
57  return *this;
58 }
59 
61 {
62  bool result = true;
63 
65 
66  // check the cond if it is OK
67  result = checkConditionTree(cond);
68  if (result == true)
69  mpCondition = new CNormalLogical(cond);
70 
71  return result;
72 }
73 
75 {
76  bool result = true;
77 
78  pdelete(mpTrue);
79 
80  // check the branch if it is OK
81  result = checkExpressionTree(branch);
82 
83  mpTrue = new CNormalFraction(branch);
84  return result;
85 }
86 
88 {
89  bool result = true;
91 
92  // check the branch if it is OK
93  result = checkExpressionTree(branch);
94  mpFalse = new CNormalFraction(branch);
95  return result;
96 }
97 
99  {
100  return *mpCondition;
101  }
102 
104 {
105  return *mpCondition;
106 }
107 
109  {
110  return *mpTrue;
111  }
112 
114 {
115  return *mpTrue;
116 }
117 
119  {
120  return *mpFalse;
121  }
122 
124 {
125  return *mpFalse;
126 }
127 
129  {
130  return new CNormalChoice(*this);
131  }
132 
134 {
135  bool result = true;
136  const CNormalChoice* pChoice = dynamic_cast<const CNormalChoice*>(&branch);
137  if (pChoice != NULL)
138  {
139  // check the condition and the two branches
140  result = checkConditionTree(pChoice->getCondition());
141  if (result == true) result = checkConditionTree(pChoice->getTrueExpression());
142  if (result == true) result = checkConditionTree(pChoice->getFalseExpression());
143  }
144  else
145  {
146  const CNormalLogical* pLogical = dynamic_cast<const CNormalLogical*>(&branch);
147  if (pLogical == NULL)
148  {
149  const CNormalItem* pItem = dynamic_cast<const CNormalItem*>(&branch);
150  if (pItem == NULL)
151  {
152  result = false;
153  }
154  else
155  {
156  result = pItem->getType() == CNormalItem::CONSTANT;
157  if (result == true)
158  {
159  std::string name = pItem->getName();
160  if (name != "true" && name != "TRUE" && name != "false" && name != "FALSE")
161  {
162  result = false;
163  }
164  }
165  }
166  }
167  }
168  return result;
169 }
170 
172 {
173  bool result = true;
174  const CNormalChoice* pChoice = dynamic_cast<const CNormalChoice*>(&branch);
175  if (pChoice != NULL)
176  {
177  result = checkConditionTree(pChoice->getCondition());
178  if (result == true) result = checkExpressionTree(pChoice->getTrueExpression());
179  if (result == true) result = checkExpressionTree(pChoice->getFalseExpression());
180  }
181  else
182  {
183  const CNormalFraction* pFraction = dynamic_cast<const CNormalFraction*>(&branch);
184  if (pFraction == NULL)
185  {
186  result = false;
187  }
188  }
189  return result;
190 }
191 
192 std::string CNormalChoice::toString() const
193  {
194  std::ostringstream os;
195  os << "if(" << this->mpCondition->toString() << ") then (" << this->mpTrue->toString() << ") else (" << this->mpFalse->toString() << ")";
196  return os.str();
197  }
198 
199 std::ostream& operator<<(std::ostream& os, const CNormalChoice& choice)
200 {
201  os << choice.toString();
202  return os;
203 }
204 
206 {
207  bool result = true;
208  if (this->mpCondition != NULL) result &= this->mpCondition->simplify();
209  if (this->mpTrue != NULL) result &= this->mpTrue->simplify();
210  if (this->mpFalse != NULL) result &= this->mpFalse->simplify();
211  return result;
212 }
213 
215  {
216  bool result = true;
217  if (!(((*this->mpCondition) == (*rhs.mpCondition)) && ((*this->mpTrue) == (*rhs.mpTrue)) && ((*this->mpFalse) == (*rhs.mpFalse))))
218  {
219  result = false;
220  }
221  return result;
222  }
223 
225  {
226  bool result = false;
227  if ((*this->mpCondition) < (*rhs.mpCondition))
228  {
229  result = true;
230  }
231  else if ((*this->mpCondition) == (*rhs.mpCondition))
232  {
233  if ((*this->mpTrue) < (*rhs.mpTrue))
234  {
235  result = true;
236  }
237  else if ((*this->mpTrue) == (*rhs.mpTrue))
238  {
239  if ((*this->mpFalse) < (*rhs.mpFalse))
240  {
241  result = true;
242  }
243  }
244  }
245  return result;
246  }
virtual bool simplify()
CNormalFraction * mpFalse
Definition: CNormalChoice.h:28
#define pdelete(p)
Definition: copasi.h:215
static bool checkConditionTree(const CNormalBase &branch)
const CNormalFraction & getTrueExpression() const
bool setTrueExpression(const CNormalFraction &branch)
virtual CNormalBase * copy() const
CNormalFraction * mpTrue
Definition: CNormalChoice.h:27
const CNormalLogical & getCondition() const
virtual bool simplify()
bool setFalseExpression(const CNormalFraction &branch)
std::ostream & operator<<(std::ostream &os, const CNormalChoice &choice)
const CNormalFraction & getFalseExpression() const
static bool checkExpressionTree(const CNormalBase &branch)
const Type & getType() const
Definition: CNormalItem.cpp:91
virtual std::string toString() const
CNormalLogical * mpCondition
Definition: CNormalChoice.h:26
bool setCondition(const CNormalLogical &cond)
bool operator==(const CNormalChoice &rhs) const
CNormalChoice & operator=(const CNormalChoice &src)
virtual std::string toString() const
virtual std::string toString() const
bool operator<(const CNormalChoice &rhs) const
virtual ~CNormalChoice()
const std::string getName() const
Definition: CNormalItem.cpp:82
virtual bool simplify()