COPASI API  4.16.103
CNormalChoiceLogical.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/CNormalChoiceLogical.cpp,v $
3 // $Revision: 1.4 $
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 "CNormalChoiceLogical.h"
23 #include "CNormalLogical.h"
24 #include "CNormalItem.h"
25 #include "CNormalFraction.h"
26 
27 CNormalChoiceLogical::CNormalChoiceLogical(): 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.mpTrue != NULL)
53  mpTrue = new CNormalLogical(*src.mpTrue);
54  if (src.mpFalse != NULL)
55  mpFalse = new CNormalLogical(*src.mpFalse);
56 
57  return *this;
58 }
59 
61 {
62  bool result = true;
64 
65  // check the cond if it is OK
66  result = checkConditionTree(cond);
67  if (result == true)
68  mpCondition = new CNormalLogical(cond);
69  return result;
70 }
71 
73 {
74  bool result = true;
75  pdelete(mpTrue);
76 
77  // check the branch if it is OK
78  result = checkConditionTree(branch);
79  mpTrue = new CNormalLogical(branch);
80  return result;
81 }
82 
84 {
85  bool result = true;
87 
88  // check the branch if it is OK
89  result = checkConditionTree(branch);
90  mpFalse = new CNormalLogical(branch);
91  return result;
92 }
93 
95  {
96  return *mpCondition;
97  }
98 
100 {
101  return *mpCondition;
102 }
103 
105  {
106  return *mpTrue;
107  }
108 
110 {
111  return *mpTrue;
112 }
113 
115  {
116  return *mpFalse;
117  }
118 
120 {
121  return *mpFalse;
122 }
123 
125  {
126  return new CNormalChoiceLogical(*this);
127  }
128 
130 {
131  bool result = true;
132  const CNormalChoiceLogical* pChoice = dynamic_cast<const CNormalChoiceLogical*>(&branch);
133  if (pChoice != NULL)
134  {
135  // check the condition and the two branches
136  result = checkConditionTree(pChoice->getCondition());
137  if (result == true) result = checkConditionTree(pChoice->getTrueExpression());
138  if (result == true) result = checkConditionTree(pChoice->getFalseExpression());
139  }
140  else
141  {
142  const CNormalLogical* pLogical = dynamic_cast<const CNormalLogical*>(&branch);
143  if (pLogical == NULL)
144  {
145  const CNormalItem* pItem = dynamic_cast<const CNormalItem*>(&branch);
146  if (pItem == NULL)
147  {
148  result = false;
149  }
150  else
151  {
152  result = pItem->getType() == CNormalItem::CONSTANT;
153  if (result == true)
154  {
155  std::string name = pItem->getName();
156  if (name != "true" && name != "TRUE" && name != "false" && name != "FALSE")
157  {
158  result = false;
159  }
160  }
161  }
162  }
163  }
164  return result;
165 }
166 
168  {
169  std::ostringstream os;
170  os << "if(" << this->mpCondition->toString() << ") then (" << this->mpTrue->toString() << ") else (" << this->mpFalse->toString() << ")";
171  return os.str();
172  }
173 
174 std::ostream& operator<<(std::ostream& os, const CNormalChoiceLogical& choice)
175 {
176  os << choice.toString();
177  return os;
178 }
179 
181 {
182  bool result = true;
183  if (this->mpCondition != NULL) result &= this->mpCondition->simplify();
184  if (this->mpTrue != NULL) result &= this->mpTrue->simplify();
185  if (this->mpFalse != NULL) result &= this->mpFalse->simplify();
186  return result;
187 }
188 
190 {
191  CNormalLogical* pTmp = this->mpTrue;
192  this->mpTrue = this->mpFalse;
193  this->mpFalse = pTmp;
194 }
195 
197  {
198  return ((*this->mpCondition) == (*rhs.mpCondition) && (*this->mpTrue == *rhs.mpTrue) && (*this->mpFalse == *rhs.mpFalse));
199  }
200 
202  {
203  bool result = true;
204  if ((*rhs.mpCondition) < (*this->mpCondition))
205  {
206  result = false;
207  }
208  else if ((*this->mpCondition) == (*rhs.mpCondition))
209  {
210  if ((*rhs.mpTrue) < (*this->mpTrue))
211  {
212  result = false;
213  }
214  else if ((*rhs.mpTrue) == (*this->mpTrue))
215  {
216  if (!((*this->mpFalse) < (*rhs.mpFalse)))
217  {
218  result = false;
219  }
220  }
221  }
222  return result;
223  }
#define pdelete(p)
Definition: copasi.h:215
std::ostream & operator<<(std::ostream &os, const CNormalChoiceLogical &choice)
CNormalLogical * mpCondition
virtual CNormalBase * copy() const
const CNormalLogical & getCondition() const
const CNormalLogical & getFalseExpression() const
virtual bool simplify()
const CNormalLogical & getTrueExpression() const
bool setTrueExpression(const CNormalLogical &branch)
bool operator==(const CNormalChoiceLogical &rhs) const
const Type & getType() const
Definition: CNormalItem.cpp:91
bool setFalseExpression(const CNormalLogical &branch)
virtual std::string toString() const
bool operator<(const CNormalChoiceLogical &rhs) const
bool setCondition(const CNormalLogical &cond)
static bool checkConditionTree(const CNormalBase &branch)
const std::string getName() const
Definition: CNormalItem.cpp:82
virtual std::string toString() const
CNormalChoiceLogical & operator=(const CNormalChoiceLogical &src)
CNormalLogical * mpFalse