COPASI API  4.16.103
ConvertToCEvaluationNode.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/ConvertToCEvaluationNode.cpp,v $
3 // $Revision: 1.41 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/05/15 15:56:21 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 #ifdef WIN32
24 # pragma warning (disable: 4786)
25 # pragma warning (disable: 4243)
26 // warning C4355: 'this' : used in base member initializer list
27 # pragma warning (disable: 4355)
28 #endif // WIN32
29 
30 #include <assert.h>
31 #include <sstream>
32 #include <exception>
33 #include <vector>
34 
36 
47 
48 #include "CNormalItem.h"
49 #include "CNormalItemPower.h"
50 #include "CNormalSum.h"
51 #include "CNormalProduct.h"
52 #include "CNormalLcm.h"
53 #include "CNormalFraction.h"
54 #include "CNormalBase.h"
55 #include "CNormalChoice.h"
56 #include "CNormalChoiceLogical.h"
57 #include "CNormalLogical.h"
58 #include "CNormalLogicalItem.h"
59 #include "CNormalFunction.h"
60 #include "CNormalCall.h"
61 #include "CNormalGeneralPower.h"
62 #include "CNormalTranslation.h"
63 
64 std::map<std::string, const CEvaluationNode*> str2eval;
65 
67 {
68  CEvaluationNode* pResult = NULL;
69 
70  if (fraction.checkDenominatorOne())
71  {
72  pResult = convertToCEvaluationNode(fraction.getNumerator());
73  }
74  else
75  {
78  pDivision->addChild(pChild);
79  pChild = convertToCEvaluationNode(fraction.getDenominator());
80  pDivision->addChild(pChild);
81  pResult = pDivision;
82  }
83 
84  return pResult;
85 }
86 
88 {
89  CEvaluationNode* pNode = NULL;
91 
92  switch (item.getType())
93  {
95 
96  // create a CEvaluationNodeNumber
97  if (item.getName() == "pi" || item.getName() == "PI")
98  {
100  }
101  else if (item.getName() == "EXPONENTIALE" || item.getName() == "exponentiale")
102  {
104  }
105  else if (item.getName() == "TRUE" || item.getName() == "true")
106  {
108  }
109  else if (item.getName() == "FALSE" || item.getName() == "false")
110  {
112  }
113  else if (item.getName() == "INFINITY" || item.getName() == "infinity")
114  {
116  }
117  else if (item.getName() == "NAN" || item.getName() == "nan" || item.getName() == "Nan")
118  {
120  }
121 
123  break;
125  // create a CEvaluationNodeVariable
127  break;
128  }
129 
130  // TODO what about object nodes ?????
131  return pNode;
132 }
133 
135 {
136  CEvaluationNode* pResult = NULL;
137 
138  if (fabs(itemPower.getExp() - 1.0) < 1e-12)
139  {
140  pResult = convertToCEvaluationNode(itemPower.getItem());
141  }
142  else
143  {
144  std::ostringstream sstream;
146  CEvaluationNode* pChild = convertToCEvaluationNode(itemPower.getItem());
147  pPowerNode->addChild(pChild);
148  sstream << itemPower.getExp();
149  pPowerNode->addChild(new CEvaluationNodeNumber(CEvaluationNodeNumber::DOUBLE, sstream.str()));
150  pResult = pPowerNode;
151  }
152 
153  return pResult;
154 }
155 
157 {
158  CEvaluationNode* pResult = NULL;
159  const std::set<CNormalItemPower*, compareItemPowers >& itemPowers = lcm.getItemPowers();
160  std::set<CNormalItemPower*, compareItemPowers >::const_iterator it = itemPowers.begin();
161  std::set<CNormalItemPower*, compareItemPowers >::const_iterator itEnd = itemPowers.end();
163  pResult = pMult;
164  CEvaluationNode* pChild = NULL;
165 
166  while (it != itEnd)
167  {
168  assert(pMult != NULL);
170  pMult->addChild(pChild);
171  pChild = convertToCEvaluationNode(**it);
172  pMult->addChild(pChild);
173  pMult = dynamic_cast<CEvaluationNodeOperator*>(pMult->getChild());
174  ++it;
175  }
176 
177  const std::vector<CNormalSum*>& sums = lcm.getSums();
178 
179  std::vector<CNormalSum*>::const_iterator it2 = sums.begin();
180 
181  std::vector<CNormalSum*>::const_iterator itEnd2 = sums.end();
182 
183  while (it2 != itEnd2)
184  {
185  assert(pMult != NULL);
187  pMult->addChild(pChild);
188  pChild = convertToCEvaluationNode(**it);
189  pMult->addChild(pChild);
190  pMult = dynamic_cast<CEvaluationNodeOperator*>(pMult->getChild());
191  ++it;
192  }
193 
194  if (pMult->getParent() == pResult)
195  {
196  pResult->removeChild(pMult);
197  delete pMult;
198  }
199  else
200  {
201  CEvaluationNode* pParent = dynamic_cast<CEvaluationNode*>(pMult->getParent());
202  assert(pParent != NULL);
203  pParent->removeChild(pMult);
204  delete pMult;
205  pMult = dynamic_cast<CEvaluationNodeOperator*>(pParent->getParent());
206  assert(pMult != NULL);
207  pMult->removeChild(pParent);
208  pMult->addChild(dynamic_cast<CEvaluationNode*>(pParent->getChild())->copyBranch());
209  delete pParent;
210  }
211 
212  return pResult;
213 }
214 
216 {
217  CEvaluationNode* pResult = NULL;
218  std::ostringstream sstream;
219 
220  if (product.getItemPowers().size() == 0)
221  {
222  sstream.precision(18);
223  sstream << product.getFactor();
224  pResult = new CEvaluationNodeNumber(CEvaluationNodeNumber::DOUBLE, sstream.str());
225  }
226  else
227  {
228  const std::set<CNormalItemPower*, compareItemPowers >& itemPowers = product.getItemPowers();
229  std::set<CNormalItemPower*, compareItemPowers >::const_iterator it = itemPowers.begin();
230  std::set<CNormalItemPower*, compareItemPowers >::const_iterator itEnd = itemPowers.end();
231  CEvaluationNode* pChild = NULL;
232  std::vector<CEvaluationNode*> products;
233 
234  while (it != itEnd)
235  {
236  pChild = convertToCEvaluationNode(**it);
237  products.push_back(pChild);
238  ++it;
239  }
240 
241  if (fabs(product.getFactor() - 1.0) >= 1e-12)
242  {
243  sstream.precision(18);
244  sstream << product.getFactor();
245  products.push_back(new CEvaluationNodeNumber(CEvaluationNodeNumber::DOUBLE, sstream.str()));
246  }
247 
249  }
250 
251  return pResult;
252 }
253 
255 {
256  const std::set<CNormalFraction*>& fractions = sum.getFractions();
257  std::set<CNormalFraction*>::const_iterator it = fractions.begin();
258  std::set<CNormalFraction*>::const_iterator itEnd = fractions.end();
259  std::vector<const CEvaluationNode*> summands;
260  CEvaluationNode* pChild = NULL;
261 
262  while (it != itEnd)
263  {
264  pChild = convertToCEvaluationNode(**it);
265  summands.push_back(pChild);
266  ++it;
267  }
268 
269  const std::set<CNormalProduct*, compareProducts >& products = sum.getProducts();
270 
271  std::set<CNormalProduct*, compareProducts >::const_iterator it2 = products.begin();
272 
273  std::set<CNormalProduct*, compareProducts >::const_iterator itEnd2 = products.end();
274 
275  while (it2 != itEnd2)
276  {
277  pChild = convertToCEvaluationNode(**it2);
278  summands.push_back(pChild);
279  ++it2;
280  }
281 
282  CEvaluationNode* pResult = NULL;
283 
284  if (!summands.empty())
285  {
287  }
288  else
289  {
291  }
292 
293  std::vector<const CEvaluationNode*>::iterator vIt = summands.begin(), vEndit = summands.end();
294 
295  while (vIt != vEndit)
296  {
297  delete *vIt;
298  ++vIt;
299  }
300 
301  return pResult;
302 }
303 
304 /**
305  * Create a fraction from a node. node is necessarily a DIVIDE operator if not the root node of the tree.
306  * @return CNormalFraction*, pointer to newly created fraction.
307  */
309 {
310  CNormalFraction* pFraction = new CNormalFraction();
311 
312  if (node->getData() == "/")
313  {// always executed except on root node possibly not
314  // find a product chain and create new temporary nodes for the
315  // numerator and denominator which are then converted to sums
316  std::vector<const CEvaluationNode*> multiplications, divisions;
317  CNormalTranslation::splitProduct(node, multiplications, divisions, false);
318  std::vector<CEvaluationNode*> tmp;
319  std::vector<const CEvaluationNode*>::const_iterator it = multiplications.begin(), endit = multiplications.end();
320 
321  while (it != endit)
322  {
323  tmp.push_back((*it)->copyBranch());
324  ++it;
325  }
326 
328  assert(pTmpNode != NULL);
329  CNormalSum* pNum = createSum(pTmpNode);
330  assert(pNum != NULL);
331  delete pTmpNode;
332  tmp.clear();
333  it = divisions.begin();
334  endit = divisions.end();
335 
336  while (it != endit)
337  {
338  tmp.push_back((*it)->copyBranch());
339  ++it;
340  }
341 
343  assert(pTmpNode != NULL);
344  CNormalSum* pDenom = createSum(pTmpNode);
345  assert(pDenom != NULL);
346  delete pTmpNode;
347  pFraction->setNumerator(*pNum);
348  pFraction->setDenominator(*pDenom);
349  //pFraction->cancel();
350  delete pNum;
351  delete pDenom;
352  }
353  else
354  {// only possible for root node
355  CNormalSum* pNum = createSum(node);
356  CNormalSum* pDenom = new CNormalSum();
357  CNormalProduct* pProduct = new CNormalProduct();
358  pDenom->add(*pProduct);
359  pFraction->setNumerator(*pNum);
360  pFraction->setDenominator(*pDenom);
361  pFraction->cancel();
362  delete pProduct;
363  delete pNum;
364  delete pDenom;
365  }
366 
367  return pFraction;
368 };
369 
370 /**
371  * Create an item from an evaluation node that need not be of specific type.
372  * @return CNormalItem*, pointer to newly created item.
373  */
375 {
376  CNormalItem* pItem = NULL;
377 
378  switch (CEvaluationNode::type(pNode->getType()))
379  {
381  pItem = new CNormalItem(pNode->buildInfix(), CNormalItem::VARIABLE);
382  break;
384  pItem = new CNormalItem(pNode->buildInfix(), CNormalItem::CONSTANT);
385  break;
386  /*
387  case CEvaluationNode::OPERATOR:
388  if (((CEvaluationNodeOperator::SubType)CEvaluationNode::subType(pNode->getType()))==CEvaluationNodeOperator::POWER
389  || ((CEvaluationNodeOperator::SubType)CEvaluationNode::subType(pNode->getType()))==CEvaluationNodeOperator::MODULUS)
390  {
391  // calling createItem will add the wrong pNode for the
392  // string into the lookup table
393  CNormalItem * child1 = createItem(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
394  CNormalItem * child2 = createItem(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
395 
396  std::stringstream tmp;
397  if ((child1->getType() == CNormalItem::VARIABLE) || (child1->getType() == CNormalItem::CONSTANT))
398  tmp << *child1;
399  else
400  {
401  tmp << "(" << *child1 << ")";
402  }
403  tmp << pNode->getData();
404  if ((child2->getType() == CNormalItem::VARIABLE) || (child2->getType() == CNormalItem::CONSTANT))
405  tmp << *child2;
406  else
407  {
408  tmp << "(" << *child2 << ")";
409  }
410 
411  pItem = new CNormalItem(tmp.str(), CNormalItem::FUNCTION);
412  std::string s=tmp.str();
413  std::map<std::string,const CEvaluationNode*>::const_iterator pos=str2eval.find(tmp.str());
414  if(pos==str2eval.end())
415  {
416  CEvaluationNodeOperator::SubType type=CEvaluationNodeOperator::POWER;
417  std::string s("^");
418  if(pNode->getData()=="%")
419  {
420  type=CEvaluationNodeOperator::MODULUS;
421  s="%";
422  }
423  CEvaluationNodeOperator* pNode=new CEvaluationNodeOperator(type,s);
424  // when the child pNode is converted, the wrongly added
425  // pNode from above is returned
426 
427  pNode->addChild(convertToCEvaluationNode(*child1));
428  pNode->addChild(convertToCEvaluationNode(*child2));
429  //pNode->printRecursively(std::cerr);
430  str2eval[tmp.str()]=pNode;
431  }
432  delete child1;
433  delete child2;
434  }
435  else // can be called only by createItem('OPERATOR pNode')
436  {
437  CNormalFraction * normedNode = createFraction(pNode);
438  normedNode->simplify();
439  CEvaluationNode* pTmpNode=convertToCEvaluationNode(*normedNode);
440  std::stringstream tmp;
441  tmp << *normedNode;
442  pItem = new CNormalItem(tmp.str(), CNormalItem::FUNCTION);
443  std::map<std::string,const CEvaluationNode*>::const_iterator pos=str2eval.find(normedNode->toString());
444  if(pos==str2eval.end())
445  {
446  str2eval[tmp.str()]=pTmpNode;
447  }
448 
449  delete normedNode;
450  }
451  break;
452  */
453  default: //cases CALL, CHOICE, LOGICAL, OBJECT, VECTOR. NUMBER should not occur!
454  throw std::exception();
455  break;
456  }
457 
458  return pItem;
459 }
460 
461 /**
462  * Create an item power from an evaluation node.
463  * Depending on the given node, we have to create different items.
464  * Either we have to create a CNormalItem, a
465  * CNormalGeneralPower, a function or a choice.
466  */
468 {
469  // make sure we create the correct item for the given node
470  CNormalItemPower * pItemPower = new CNormalItemPower();
471 
473  {
474  // check if the second child is a number
475  if (CEvaluationNode::type(dynamic_cast<const CEvaluationNode*>(node->getChild()->getSibling())->getType()) == CEvaluationNode::NUMBER)
476  {
477  // we set the exponent to that number
478  pItemPower->setExp((C_FLOAT64)dynamic_cast<const CEvaluationNodeNumber*>(node->getChild()->getSibling())->getValue());
479  // check if we can create a CNormalItem object for the fist child, else we
480  // create a general power with exponent 1
481  CEvaluationNode::Type type = CEvaluationNode::type(dynamic_cast<const CEvaluationNode*>(node->getChild())->getType());
482 
484  {
485  CNormalBase* pItem = createItemPowerItem(dynamic_cast<const CEvaluationNode*>(node->getChild()));
486  assert(pItem != NULL);
487  pItemPower->setItem(*pItem);
488  delete pItem;
489  }
490  else
491  {
492  // create a general power for the child node
493  CNormalGeneralPower* pGeneralPower = createGeneralPower(dynamic_cast<const CEvaluationNode*>(node->getChild()));
494  assert(pGeneralPower != NULL);
495  pItemPower->setItem(*pGeneralPower);
496  delete pGeneralPower;
497  }
498  }
499  else
500  {
501  // create a general power for the node
502  CNormalGeneralPower* pGeneralPower = createGeneralPower(node);
503  assert(pGeneralPower != NULL);
504  pItemPower->setItem(*pGeneralPower);
505  pItemPower->setExp(1.0);
506  delete pGeneralPower;
507  }
508  }
510  {
512  {
513  // multiply the second node by -1 and call createItempower with the
514  // result
517  pOperatorNode->addChild(pNumberNode);
518  pOperatorNode->addChild(dynamic_cast<const CEvaluationNode*>(node->getChild())->copyBranch());
519  // delete the old item power object
520  delete pItemPower;
521  // create a new item power object by calling this method again with
522  // the newly created multiplication node
523  pItemPower = createItemPower(pOperatorNode);
524  // delete the multiplication node
525  delete pOperatorNode;
526  }
527  else
528  {
529  CNormalFunction* pFunction = createFunction(node);
530  assert(pFunction != NULL);
531  pItemPower->setItem(*pFunction);
532  delete pFunction;
533  pItemPower->setExp(1.0);
534  }
535  }
537  {
538  CNormalCall* pCall = createCall(node);
539  assert(pCall != NULL);
540  pItemPower->setItem(*pCall);
541  delete pCall;
542  pItemPower->setExp(1.0);
543  }
545  {
546  CNormalChoice* pChoice = createChoice(node);
547  assert(pChoice != NULL);
548  pItemPower->setItem(*pChoice);
549  delete pChoice;
550  pItemPower->setExp(1.0);
551  }
553  {
554  CNormalItem* pItem = createItem(node);
555  assert(pItem != NULL);
556  pItemPower->setItem(*pItem);
557  delete pItem;
558  pItemPower->setExp(1.0);
559  }
561  {
562  CNormalBase* pItem = createItemPowerItem(node);
563  assert(pItem != NULL);
564  pItemPower->setItem(*pItem);
565  delete pItem;
566  pItemPower->setExp(1.0);
567  }
568  else
569  {
570  // create a general power for the node
571  CNormalGeneralPower* pGeneralPower = createGeneralPower(node);
572  assert(pGeneralPower != NULL);
573  pItemPower->setItem(*pGeneralPower);
574  pItemPower->setExp(1.0);
575  delete pGeneralPower;
576  }
577 
578  return pItemPower;
579 }
580 
581 /*
582  * Create a product from an evaluation node that is not necessarily a multiply operator.
583  * @return CNormalProduct*, pointer to newly created product.
584  */
586 {
587  CNormalProduct * pProduct = new CNormalProduct();
588 
590  {
591  // find the product chain, if there are divisions, we have to create a
592  // general power, else we create a product with as many items as the
593  // multiplication vector has entries
594  std::vector<const CEvaluationNode*> multiplications, divisions;
595  CNormalTranslation::splitProduct(node, multiplications, divisions, false);
596  double factor = 1.0;
597 
598  if (divisions.empty())
599  {
600  std::vector<const CEvaluationNode*>::const_iterator it = multiplications.begin(), endit = multiplications.end();
601  CNormalItemPower* pItemPower = NULL;
602 
603  while (it != endit)
604  {
605  // check if the node is a pure number
606  // if so, use it to update the factor
607  // instead of creating an item for it
608  if (CEvaluationNode::type((*it)->getType()) == CEvaluationNode::NUMBER)
609  {
610  factor *= dynamic_cast<const CEvaluationNodeNumber*>(*it)->getValue();
611  }
612  else
613  {
614  pItemPower = createItemPower(*it);
615  assert(pItemPower != NULL);
616  pProduct->multiply(*pItemPower);
617  delete pItemPower;
618  }
619 
620  ++it;
621  }
622  }
623  else
624  {
626  std::vector<CEvaluationNode*> tmp;
627  std::vector<const CEvaluationNode*>::const_iterator it = multiplications.begin(), endit = multiplications.end();
628  // check if the multiplications and divisions contain only numbers
629  // in that case, done create a general item
630  bool empty = false;
631 
632  while (it != endit)
633  {
634  // check if the node is a pure number
635  // if so, use it to update the factor
636  // instead of adding it to the factor
637  if (CEvaluationNode::type((*it)->getType()) == CEvaluationNode::NUMBER)
638  {
639  factor *= dynamic_cast<const CEvaluationNodeNumber*>(*it)->getValue();
640  }
641  else
642  {
643  tmp.push_back((*it)->copyBranch());
644  }
645 
646  ++it;
647  }
648 
649  empty = tmp.empty();
650  CEvaluationNode* pTmpNode1 = NULL;
651 
652  // if we have non number nodes, we need to combine those again into a
653  // multiplication chain
654  // otherwise the numerator is 1
655  if (!empty)
656  {
658  }
659  else
660  {
662  }
663 
664  pTmpOperator->addChild(pTmpNode1);
665  tmp.clear();
666  it = divisions.begin();
667  endit = divisions.end();
668 
669  while (it != endit)
670  {
671  // check if the node is a pure number
672  // if so, use it to update the factor
673  // instead of adding it to the vector
674  if (CEvaluationNode::type((*it)->getType()) == CEvaluationNode::NUMBER)
675  {
676  factor /= dynamic_cast<const CEvaluationNodeNumber*>(*it)->getValue();
677  }
678  else
679  {
680  tmp.push_back((*it)->copyBranch());
681  }
682 
683  ++it;
684  }
685 
686  // if tmp was empty both times, empty must be true
687  empty = (empty & tmp.empty());
688 
689  if (!empty)
690  {
691  CNormalItemPower* pItemPower = NULL;
692 
693  if (!tmp.empty())
694  {
696  pTmpOperator->addChild(pTmpNode1);
697  pItemPower = createItemPower(pTmpOperator);
698  }
699  else
700  {
701  pItemPower = createItemPower(pTmpNode1);
702  }
703 
704  assert(pItemPower != NULL);
705  pProduct->multiply(*pItemPower);
706  delete pItemPower;
707  }
708 
709  delete pTmpOperator;
710  }
711 
712  pProduct->setFactor(factor);
713  }
715  {
716  double factor = dynamic_cast<const CEvaluationNodeNumber*>(node)->getValue();
717  // set the factor
718  pProduct->setFactor(factor);
719  }
720  else
721  {
722  // we create the appropriate item
723  CNormalItemPower* pItemPower = createItemPower(node);
724  assert(pItemPower != NULL);
725  pProduct->multiply(*pItemPower);
726  delete pItemPower;
727  }
728 
729  return pProduct;
730 }
731 
732 /** THIS CODE should be obsolete, RG (06/04/6.2008)
733  * Create a product from an evaluation node that is not necessarily a multiply operator.
734  * @return CNormalProduct*, pointer to newly created product.
735  */
736 //CNormalProduct * createProduct(const CEvaluationNode* node)
737 //{
738 // CNormalProduct * product = new CNormalProduct();
739 // CNormalChoice * pChoice = NULL;
740 // switch (CEvaluationNode::type(node->getType()))
741 // {
742 // case CEvaluationNode::OPERATOR: // PLUS(->createSum), MINUS(translated as +(-..)) and DIVIDE(->createFraction) do not occur.
743 // if (node->getData() == "^")
744 // {
745 // if (CEvaluationNode::type(static_cast<const CEvaluationNode*>(node->getChild()->getSibling())->getType()) == CEvaluationNode::NUMBER &&
746 // (node->getChild()->getChild() == NULL || CEvaluationNode::type(static_cast<const CEvaluationNode*>(node->getChild())->getType()) == CEvaluationNode::CHOICE || CEvaluationNode::type(static_cast<const CEvaluationNode*>(node->getChild())->getType()) == CEvaluationNode::FUNCTION))
747 // {
748 // CNormalItemPower* power = createItemPower(node);
749 // product->multiply(*power);
750 // delete power;
751 //}
752 // else
753 // {
754 // CNormalGeneralPower* item = createGeneralPower(node);
755 // product->multiply(*item);
756 // delete item;
757 //}
758 //}
759 // else if (node->getData() == "*")
760 // {
761 // CNormalProduct* product1 = createProduct(static_cast<const CEvaluationNode*>(node->getChild()));
762 // if (product1 != NULL)
763 // {
764 // CNormalProduct* product2 = createProduct(static_cast<const CEvaluationNode*>(node->getChild()->getSibling()));
765 // if (product2 != NULL)
766 // {
767 // product->multiply(*product1);
768 // product->multiply(*product2);
769 // delete product2;
770 //}
771 // else
772 // {
773 // product = NULL;
774 //}
775 // delete product1;
776 //}
777 // else
778 // {
779 // product = NULL;
780 //}
781 //}
782 // else if (node->getData() == "%")
783 //
784 // {
785 // CNormalGeneralPower* item = createGeneralPower(node);
786 // product->multiply(*item);
787 // delete item;
788 //}
789 // else
790 // {
791 // delete product;
792 // product = NULL;
793 //}
794 // break;
795 // case CEvaluationNode::NUMBER:
796 // product->multiply(node->value());
797 // break;
798 // case CEvaluationNode::FUNCTION:
799 // if (((CEvaluationNodeFunction::SubType)CEvaluationNode::subType(node->getType())) == CEvaluationNodeFunction::MINUS)
800 // {
801 // product->multiply(-1.0);
802 // CNormalProduct * product2 = createProduct(dynamic_cast<const CEvaluationNode*>(node->getChild()));
803 // assert(product2 != NULL);
804 // product->multiply(*product2);
805 // delete product2;
806 //}
807 // else if (((CEvaluationNodeFunction::SubType)CEvaluationNode::subType(node->getType())) == CEvaluationNodeFunction::PLUS)
808 // {
809 // CNormalProduct * product2 = createProduct(dynamic_cast<const CEvaluationNode*>(node->getChild()));
810 // assert(product2 != NULL);
811 // product->multiply(*product2);
812 // delete product2;
813 //}
814 // else
815 // {
816 // CNormalFunction * item = createFunction(node);
817 // product->multiply(*item);
818 // delete item;
819 //}
820 // break;
821 // case CEvaluationNode::LOGICAL:
822 // throw std::exception(/*"Error. Logical Nodes are not allowed here."*/);
823 // break;
824 // case CEvaluationNode::CHOICE:
825 // pChoice = createChoice(dynamic_cast<const CEvaluationNode*>(node));
826 // product->multiply(*pChoice);
827 // delete pChoice;
828 // break;
829 // default: //cases VARIABLE, CONSTANT, OBJECT, VECTOR
830 // CNormalItem * item = createItem(node);
831 // product->multiply(*item);
832 // delete item;
833 // break;
834 //}
835 // return product;
836 //}
837 
838 /**
839  * Create a sum from an evaluation node -node does not need to be a PLUS operator!
840  * @return CNormalSum*, pointer to newly created sum
841  */
843 {
844  CNormalSum* pSum = new CNormalSum();
845 
847  {
848  // find a summation chain and create a product node for each addition and
849  // subtraction node
850  std::vector<const CEvaluationNode*> additions, subtractions;
851  CNormalTranslation::splitSum(node, additions, subtractions, false);
852  std::vector<const CEvaluationNode*>::iterator it = additions.begin(), endit = additions.end();
853  CNormalProduct* pProduct = NULL;
854 
855  while (it != endit)
856  {
857  pProduct = createProduct(*it);
858  assert(pProduct != NULL);
859  pSum->add(*pProduct);
860  delete pProduct;
861  ++it;
862  }
863 
864  it = subtractions.begin(), endit = subtractions.end();
865 
866  while (it != endit)
867  {
868  pProduct = createProduct(*it);
869  assert(pProduct != NULL);
870  // since these are subtractions, we need to set the factor the -1.0
871  // times the old factor
872  pProduct->setFactor(-1.0 * pProduct->getFactor());
873  pSum->add(*pProduct);
874  delete pProduct;
875  ++it;
876  }
877  }
878  else
879  {
880  // create a sum and add the product made from the given node
881  CNormalProduct* pProduct = createProduct(node);
882  assert(pProduct != NULL);
883  pSum->add(*pProduct);
884  delete pProduct;
885  /* This code should be obsolete, RG (4.6.2008)
886  if (node->getData() == "/")
887  {
888  CNormalFraction* fraction = createFraction(node);
889  sum->add(*fraction);
890  delete fraction;
891  return sum;
892  }
893  else
894  {
895  CNormalProduct* product = createProduct(node);
896  if (product != NULL)
897  {
898  sum->add(*product);
899  delete product;
900  }
901  else
902  {
903  CNormalFraction* pFraction = createFraction(node);
904  sum->add(*pFraction);
905  delete pFraction;
906  }
907  return sum;
908  }
909  */
910  }
911 
912  return pSum;
913 }
914 
915 /**
916  * Create an item power from an evaluation node.
917  * Must be a CEvaluationNodeChoice.
918  * @return CNormalChoice*, pointer to newly created choice node.
919 CNormalChoice * createChoice(const CEvaluationNode* node)
920 {
921  // there are two kinds of choice nodes, one where mpTrue and mpFalse are
922  // fractions and one where they are choice or logical
923  // if the child node directly is a logical or a comparison, we create the
924  // Logical, otherwise we create the fraction.
925  CNormalChoice* pChoice=NULL;
926  const CEvaluationNodeChoice* pChoiceNode=dynamic_cast<const CEvaluationNodeChoice*>(node);
927  if(pChoiceNode!=NULL)
928  {
929  pChoice=new CNormalChoice();
930  const CEvaluationNode* pTmpNode=dynamic_cast<const CEvaluationNode*>(pChoiceNode->getChild());
931  if(pTmpNode!=NULL)
932  {
933  // check if this subtree is a logical expression
934  CNormalLogical* pLogic=createLogical(pTmpNode);
935  if(pLogicc)
936  {
937  pChoice->setCondition(*pLogic);
938  delete pLogic;
939  pTmpNode=dynamic_cast<const CEvaluationNode*>(pTmpNode->getSibling());
940  if(pTmpNode!=NULL)
941  {
942  if(isLogical(pTmpNode))
943  {
944  pLogic=createLogical(pTmpNode);
945  if(pLogic!=NULL)
946  {
947  pChoice->setTrueExpression(pLogic);
948  delete pLogic;
949  pTmpNode=dynamic_cast<const CEvaluationNode*>(pTmpNode->getSibling());
950  if(pTmpNode!=NULL && isLogical(pTmpNode))
951  {
952  pLogical=createLogical(pTmpNode);
953  if(pLogical!=NULL)
954  {
955  pChoice->setFalseExpression(*pLogic);
956  delete pLogic;
957  }
958  else
959  {
960  delete pChoice;
961  pChoice=NULL;
962  }
963  }
964  else
965  {
966  delete pChoice;
967  pChoice=NULL;
968  }
969  }
970  else
971  {
972  delete pChoice;
973  pChoice=NULL;
974  delete pLogic;
975  }
976  }
977  else
978  {
979  CNormalFraction* pFrac=createNormalRepresentation(pTmpNode);
980  if(pFrac)
981  {
982  pChoice->setTrueExpression(*pFrac);
983  delete pFrac;
984  pTmpNode=dynamic_cast<const CEvaluationNode*>(pTmpNode->getSibling());
985  if(pTmpNode!=NULL)
986  {
987  CNormalFraction* pFrac=createNormalRepresentation(pTmpNode);
988  if(pFrac)
989  {
990  pChoice->setFalseExpression(*pFrac);
991  delete pFrac;
992  }
993  else
994  {
995  delete pChoice;
996  pChoice=NULL;
997  }
998  }
999  else
1000  {
1001  delete pChoice;
1002  pChoice=NULL;
1003  }
1004  }
1005  else
1006  {
1007  delete pChoice;
1008  pChoice=NULL;
1009  }
1010  }
1011  }
1012  else
1013  {
1014  delete pChoice;
1015  pChoice=NULL;
1016  }
1017  }
1018  else
1019  {
1020  delete pChoice;
1021  pChoice=NULL;
1022  }
1023  }
1024  else
1025  {
1026  delete pChoice;
1027  pChoice=NULL;
1028  }
1029  }
1030  return pChoice;
1031 }
1032  */
1033 
1034 /**
1035  * Create an item power from an evaluation node.
1036  * @return CNormalBase*, pointer to newly created normal base object power.
1037  */
1039 {
1040  CNormalFraction* pFrac = NULL;
1041  CEvaluationNode* pTmp2 = node->copyBranch();
1043 
1044  if (pTmp != NULL)
1045  {
1046  delete pTmp2;
1047  }
1048  else
1049  {
1050  pTmp = pTmp2;
1051  }
1052 
1054 
1055  if (pTmp2 != NULL)
1056  {
1057  delete pTmp;
1058  }
1059  else
1060  {
1061  pTmp2 = pTmp;
1062  }
1063 
1065 
1066  switch (type)
1067  {
1075  case CEvaluationNode::CALL:
1077  pFrac = createFraction(pTmp2);
1078  break;
1079  default:
1080  assert(false);
1081  break;
1082  }
1083 
1084  if (pTmp2 != NULL)
1085  {
1086  delete pTmp2;
1087  }
1088 
1089  return pFrac;
1090 }
1091 
1093 {
1094  CEvaluationNode* pNode = NULL;
1095 
1096  if (dynamic_cast<const CNormalItem*>(&base) != NULL)
1097  {
1098  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalItem&>(base));
1099  }
1100  else if (dynamic_cast<const CNormalItemPower*>(&base) != NULL)
1101  {
1102  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalItemPower&>(base));
1103  }
1104  else if (dynamic_cast<const CNormalGeneralPower*>(&base) != NULL)
1105  {
1106  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalGeneralPower&>(base));
1107  }
1108  else if (dynamic_cast<const CNormalFunction*>(&base) != NULL)
1109  {
1110  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalFunction&>(base));
1111  }
1112  else if (dynamic_cast<const CNormalCall*>(&base) != NULL)
1113  {
1114  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalCall&>(base));
1115  }
1116  else if (dynamic_cast<const CNormalFraction*>(&base) != NULL)
1117  {
1118  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalFraction&>(base));
1119  }
1120  else if (dynamic_cast<const CNormalProduct*>(&base) != NULL)
1121  {
1122  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalProduct&>(base));
1123  }
1124  else if (dynamic_cast<const CNormalSum*>(&base) != NULL)
1125  {
1126  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalSum&>(base));
1127  }
1128  else if (dynamic_cast<const CNormalLogical*>(&base) != NULL)
1129  {
1130  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalLogical&>(base));
1131  }
1132  else if (dynamic_cast<const CNormalChoice*>(&base) != NULL)
1133  {
1134  pNode = convertToCEvaluationNode(dynamic_cast<const CNormalChoice&>(base));
1135  }
1136 
1137  return pNode;
1138 }
1139 
1141 {
1142  CEvaluationNode* pResult = NULL;
1143 
1144  switch (pow.getType())
1145  {
1148  break;
1151  break;
1153  break;
1154  }
1155 
1156  if (pResult != NULL)
1157  {
1158  if (pow.getRight().checkIsOne())
1159  {
1160  delete pResult;
1161  pResult = convertToCEvaluationNode(pow.getLeft());
1162  }
1163  else
1164  {
1166  pResult->addChild(pChild);
1167  pChild = convertToCEvaluationNode(pow.getRight());
1168  pResult->addChild(pChild);
1169  }
1170  }
1171 
1172  return pResult;
1173 }
1174 
1176 {
1177  CEvaluationNode* pCall = NULL;
1178 
1179  // check if the name contains any non-whitespace characters at all
1180  if (call.getName().find_first_not_of("\t\r\n ") != std::string::npos)
1181  {
1183 
1184  switch (call.getType())
1185  {
1188  break;
1189  case CNormalCall::FUNCTION:
1191  break;
1192  case CNormalCall::DELAY:
1193  pCall = new CEvaluationNodeDelay(CEvaluationNodeDelay::DELAY, "delay");
1194  break;
1195  case CNormalCall::INVALID:
1196  break;
1197  }
1198 
1199  if (!pCall)
1200  {
1201  pCall = new CEvaluationNodeCall(type, call.getName());
1202  }
1203 
1204  const std::vector<CNormalFraction*>& children = call.getFractions();
1205 
1206  std::vector<CNormalFraction*>::const_iterator it = children.begin(), endit = children.end();
1207 
1208  while (it != endit)
1209  {
1210  CEvaluationNode* pChild = convertToCEvaluationNode(**it);
1211  assert(pChild != NULL);
1212  pCall->addChild(pChild);
1213  ++it;
1214  }
1215  }
1216 
1217  return pCall;
1218 }
1219 
1221 {
1222  CEvaluationNode* pResult = NULL;
1223  std::string data;
1225 
1226  switch (fun.getType())
1227  {
1228  case CNormalFunction::LOG:
1229  subType = CEvaluationNodeFunction::LOG;
1230  data = CNormalFunction::NAMES[fun.getType()];
1231  break;
1234  data = CNormalFunction::NAMES[fun.getType()];
1235  break;
1236  case CNormalFunction::EXP:
1237  subType = CEvaluationNodeFunction::EXP;
1238  data = CNormalFunction::NAMES[fun.getType()];
1239  break;
1240  case CNormalFunction::SIN:
1241  subType = CEvaluationNodeFunction::SIN;
1242  data = CNormalFunction::NAMES[fun.getType()];
1243  break;
1244  case CNormalFunction:: COS:
1245  subType = CEvaluationNodeFunction::COS;
1246  data = CNormalFunction::NAMES[fun.getType()];
1247  break;
1248  case CNormalFunction::TAN:
1249  subType = CEvaluationNodeFunction::TAN;
1250  data = CNormalFunction::NAMES[fun.getType()];
1251  break;
1252  case CNormalFunction::SEC:
1253  subType = CEvaluationNodeFunction::SEC;
1254  data = CNormalFunction::NAMES[fun.getType()];
1255  break;
1256  case CNormalFunction::CSC:
1257  subType = CEvaluationNodeFunction::CSC;
1258  data = CNormalFunction::NAMES[fun.getType()];
1259  break;
1260  case CNormalFunction::COT:
1261  subType = CEvaluationNodeFunction::COT;
1262  data = CNormalFunction::NAMES[fun.getType()];
1263  break;
1264  case CNormalFunction::SINH:
1266  data = CNormalFunction::NAMES[fun.getType()];
1267  break;
1268  case CNormalFunction::COSH:
1270  data = CNormalFunction::NAMES[fun.getType()];
1271  break;
1272  case CNormalFunction::TANH:
1274  data = CNormalFunction::NAMES[fun.getType()];
1275  break;
1276  case CNormalFunction::SECH:
1278  data = CNormalFunction::NAMES[fun.getType()];
1279  break;
1280  case CNormalFunction::CSCH:
1282  data = CNormalFunction::NAMES[fun.getType()];
1283  break;
1284  case CNormalFunction::COTH:
1286  data = CNormalFunction::NAMES[fun.getType()];
1287  break;
1290  data = CNormalFunction::NAMES[fun.getType()];
1291  break;
1294  data = CNormalFunction::NAMES[fun.getType()];
1295  break;
1298  data = CNormalFunction::NAMES[fun.getType()];
1299  break;
1302  data = CNormalFunction::NAMES[fun.getType()];
1303  break;
1306  data = CNormalFunction::NAMES[fun.getType()];
1307  break;
1310  data = CNormalFunction::NAMES[fun.getType()];
1311  break;
1314  data = CNormalFunction::NAMES[fun.getType()];
1315  break;
1318  data = CNormalFunction::NAMES[fun.getType()];
1319  break;
1322  data = CNormalFunction::NAMES[fun.getType()];
1323  break;
1326  data = CNormalFunction::NAMES[fun.getType()];
1327  break;
1330  data = CNormalFunction::NAMES[fun.getType()];
1331  break;
1334  data = CNormalFunction::NAMES[fun.getType()];
1335  break;
1336  case CNormalFunction::SQRT:
1338  data = CNormalFunction::NAMES[fun.getType()];
1339  break;
1340  case CNormalFunction::ABS:
1341  subType = CEvaluationNodeFunction::ABS;
1342  data = CNormalFunction::NAMES[fun.getType()];
1343  break;
1346  data = CNormalFunction::NAMES[fun.getType()];
1347  break;
1348  case CNormalFunction::CEIL:
1350  data = CNormalFunction::NAMES[fun.getType()];
1351  break;
1354  data = CNormalFunction::NAMES[fun.getType()];
1355  break;
1357  data = "@";
1358  break;
1359  }
1360 
1361  pResult = new CEvaluationNodeFunction(subType, data);
1362 
1363  if (subType != CEvaluationNodeFunction::INVALID)
1364  {
1366  pResult->addChild(pChild);
1367  }
1368 
1369  return pResult;
1370 }
1371 
1372 /**
1373  * Create an general power from an evaluation node.
1374  * if it is a power node, create a for each child and set base and exponent,
1375  * else only create the base from the node and set the exponent to 1
1376  * @return CNormalItemPower*, pointer to newly created general power (or modulo).
1377  */
1379 {
1380  CNormalGeneralPower* pPow = NULL;
1381 
1382  if (node != NULL)
1383  {
1385  {
1387  {
1388  pPow = new CNormalGeneralPower();
1390  }
1392  {
1393  pPow = new CNormalGeneralPower();
1395  }
1396 
1397  if (pPow != NULL)
1398  {
1399  // add the left and the right side
1400  CNormalFraction* pBase = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(node->getChild()));
1401  CNormalFraction* pExponent = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(node->getChild()->getSibling()));
1402  assert(pBase != NULL);
1403  assert(pExponent != NULL);
1404  pPow->setLeft(*pBase);
1405  pPow->setRight(*pExponent);
1406  delete pBase;
1407  delete pExponent;
1408  }
1409  else
1410  {
1411  // create a fraction for the base and a unit fraction for the exponent
1412  pPow = new CNormalGeneralPower();
1414  CNormalFraction* pBase = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(node));
1416  CNormalFraction* pExponent = createNormalRepresentation(pTmpNode);
1417  delete pTmpNode;
1418  assert(pBase != NULL);
1419  assert(pExponent != NULL);
1420  pPow->setLeft(*pBase);
1421  pPow->setRight(*pExponent);
1422  delete pBase;
1423  delete pExponent;
1424  }
1425  }
1426  else
1427  {
1428  // create a fraction for the base and a unit fraction for the exponent
1429  pPow = new CNormalGeneralPower();
1431  CNormalFraction* pBase = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(node));
1433  CNormalFraction* pExponent = createNormalRepresentation(pTmpNode);
1434  delete pTmpNode;
1435  assert(pBase != NULL);
1436  assert(pExponent != NULL);
1437  pPow->setLeft(*pBase);
1438  pPow->setRight(*pExponent);
1439  delete pBase;
1440  delete pExponent;
1441  }
1442  }
1443 
1444  return pPow;
1445 }
1446 
1447 /**
1448  * Create an function call from an evaluation node.
1449  * @return CNormalCall*, pointer to newly created call
1450  */
1452 {
1453  CNormalCall* pCall = NULL;
1455 
1456  if (type == CEvaluationNode::CALL || type == CEvaluationNode::DELAY)
1457  {
1458  // create a call object and add all children
1459  pCall = new CNormalCall();
1460  // set the name
1461  pCall->setName(node->getData());
1462  const CEvaluationNode* pChild = dynamic_cast<const CEvaluationNode*>(node->getChild());
1463 
1464  while (pChild != NULL)
1465  {
1466  CNormalFraction* pFraction = createNormalRepresentation(pChild);
1467  assert(pFraction != NULL);
1468  pCall->add(*pFraction);
1469  delete pFraction;
1470  pChild = dynamic_cast<const CEvaluationNode*>(pChild->getSibling());
1471  }
1472 
1473  if (type == CEvaluationNode::DELAY)
1474  {
1475  pCall->setType(CNormalCall::DELAY);
1476  }
1477  else
1478  {
1480 
1481  switch (subType)
1482  {
1485  break;
1488  break;
1490  pCall->setType(CNormalCall::INVALID);
1491  break;
1492  }
1493  }
1494  }
1495 
1496  return pCall;
1497 }
1498 
1499 /**
1500  * Create an function from an evaluation node.
1501  * @return CNormalFunction*, pointer to newly created function
1502  */
1504 {
1505  CNormalFunction* pFun = NULL;
1507 
1508  if (type == CEvaluationNode::FUNCTION)
1509  {
1511 
1513  {
1515  type = CNormalFunction::LOG;
1516  break;
1518  type = CNormalFunction::LOG10;
1519  break;
1521  type = CNormalFunction::EXP;
1522  break;
1524  type = CNormalFunction::SIN;
1525  break;
1527  type = CNormalFunction::COS;
1528  break;
1530  type = CNormalFunction::TAN;
1531  break;
1533  type = CNormalFunction::SEC;
1534  break;
1536  type = CNormalFunction::CSC;
1537  break;
1539  type = CNormalFunction::COT;
1540  break;
1542  type = CNormalFunction::SINH;
1543  break;
1545  type = CNormalFunction::COSH;
1546  break;
1548  type = CNormalFunction::TANH;
1549  break;
1551  type = CNormalFunction::SECH;
1552  break;
1554  type = CNormalFunction::CSCH;
1555  break;
1557  type = CNormalFunction::COTH;
1558  break;
1560  type = CNormalFunction::ARCSIN;
1561  break;
1563  type = CNormalFunction::ARCCOS;
1564  break;
1566  type = CNormalFunction::ARCTAN;
1567  break;
1569  type = CNormalFunction::ARCSEC;
1570  break;
1572  type = CNormalFunction::ARCCSC;
1573  break;
1575  type = CNormalFunction::ARCCOT;
1576  break;
1578  type = CNormalFunction::ARCSINH;
1579  break;
1581  type = CNormalFunction::ARCCOSH;
1582  break;
1584  type = CNormalFunction::ARCTANH;
1585  break;
1587  type = CNormalFunction::ARCSECH;
1588  break;
1590  type = CNormalFunction::ARCCSCH;
1591  break;
1593  type = CNormalFunction::ARCCOTH;
1594  break;
1596  type = CNormalFunction::SQRT;
1597  break;
1599  type = CNormalFunction::ABS;
1600  break;
1602  type = CNormalFunction::FLOOR;
1603  break;
1605  type = CNormalFunction::CEIL;
1606  break;
1609  break;
1611  default:
1612  assert(false);
1613  break;
1614  }
1615 
1616  if (type != CNormalFunction::INVALID)
1617  {
1618  CNormalFraction* pFrac = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(node->getChild()));
1619 
1620  if (pFrac != NULL)
1621  {
1622  pFun = new CNormalFunction();
1623  pFun->setType(type);
1624  pFun->setFraction(*pFrac);
1625  delete pFrac;
1626  }
1627  }
1628  }
1629 
1630  return pFun;
1631 }
1632 
1633 bool isLogical(const CEvaluationNode* pNode)
1634 {
1635  bool result = false;
1636 
1637  // go through the tree until one of the following is encountered:
1638  // CEvaluationNodeLogical, CEvaluationNodeFunction::NOT or
1639  // CEvaluationNodeConstant::(TRUE|FALSE)
1643  )
1644 
1645  {
1646  result = true;
1647  }
1648 
1649  const CEvaluationNode* pChild = dynamic_cast<const CEvaluationNode*>(pNode->getChild());
1650 
1651  while (result == false && pChild != NULL)
1652  {
1653  result = isLogical(pChild);
1654  pChild = dynamic_cast<const CEvaluationNode*>(pChild->getSibling());
1655  }
1656 
1657  return result;
1658 }
1659 
1661 {
1662  CNormalBase* pResult = NULL;
1665 
1666  switch (type)
1667  {
1670 
1671  if (subType == CEvaluationNodeOperator::POWER)
1672  {
1673  if ((CEvaluationNode::type(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling())->getType())) == CEvaluationNode::NUMBER)
1674  {
1675  pResult = createItemPower(pNode);
1676  }
1677  else
1678  {
1679  pResult = createGeneralPower(pNode);
1680  }
1681  }
1683  {
1684  pResult = createGeneralPower(pNode);
1685  }
1686  else
1687  {
1688  pResult = createFraction(pNode);
1689  }
1690 
1691  break;
1693  pResult = createItem(pNode);
1694  break;
1696  pResult = createFunction(pNode);
1697  break;
1698  case CEvaluationNode::CALL:
1699  pResult = createCall(pNode);
1700  break;
1702  pResult = createChoice(pNode);
1703  break;
1705  pResult = createLogical(pNode);
1706  break;
1708  pResult = createItem(pNode);
1709  break;
1711  pResult = createItem(pNode);
1712  break;
1713  default:
1714  break;
1715  }
1716 
1717  return pResult;
1718 }
1719 
1721 {
1722  // this will become a CEvaluationNodeChoice with an mpIf, an mpTrue and and
1723  // mpFalse
1724  CEvaluationNodeChoice* pChoiceNode = NULL;
1726 
1727  if (pChild1 != NULL)
1728  {
1730 
1731  if (pChild2 != NULL)
1732  {
1734 
1735  if (pChild3 != NULL)
1736  {
1737  pChoiceNode = new CEvaluationNodeChoice(CEvaluationNodeChoice::IF, "IF");
1738  pChoiceNode->addChild(pChild1);
1739  pChoiceNode->addChild(pChild2);
1740  pChoiceNode->addChild(pChild3);
1741  }
1742  }
1743  }
1744 
1745  return pChoiceNode;
1746 }
1747 
1749 {
1750  // this will become a CEvaluationNodeChoice with an mpIf, an mpTrue and and
1751  // mpFalse
1752  CEvaluationNodeChoice* pChoiceNode = NULL;
1754 
1755  if (pChild1 != NULL)
1756  {
1758 
1759  if (pChild2 != NULL)
1760  {
1762 
1763  if (pChild3 != NULL)
1764  {
1765  pChoiceNode = new CEvaluationNodeChoice(CEvaluationNodeChoice::IF, "IF");
1766  pChoiceNode->addChild(pChild1);
1767  pChoiceNode->addChild(pChild2);
1768  pChoiceNode->addChild(pChild3);
1769  }
1770  }
1771  }
1772 
1773  return pChoiceNode;
1774 }
1775 
1777 {
1778  CEvaluationNode* pLogicalNode = NULL;
1779 
1780  switch (item.getType())
1781  {
1783  pLogicalNode = new CEvaluationNodeConstant(CEvaluationNodeConstant::TRUE, "TRUE");
1784  pLogicalNode->compile(NULL);
1785  break;
1787  pLogicalNode = new CEvaluationNodeConstant(CEvaluationNodeConstant::FALSE, "FALSE");
1788  pLogicalNode->compile(NULL);
1789  break;
1791  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::EQ, "==");
1792  break;
1794  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::NE, "!=");
1795  break;
1797  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::LT, "<");
1798  break;
1800  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::GT, ">");
1801  break;
1803  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::GE, ">=");
1804  break;
1806  pLogicalNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::LE, "<=");
1807  break;
1809  break;
1810  }
1811 
1813  {
1815 
1816  if (pChild1 == NULL)
1817  {
1818  delete pLogicalNode;
1819  pLogicalNode = NULL;
1820  }
1821  else
1822  {
1824 
1825  if (pChild2 == NULL)
1826  {
1827  delete pLogicalNode;
1828  pLogicalNode = NULL;
1829  }
1830  else
1831  {
1832  pLogicalNode->addChild(pChild1);
1833  pLogicalNode->addChild(pChild2);
1834  pLogicalNode->compile(NULL);
1835  }
1836  }
1837  }
1838 
1839  return pLogicalNode;
1840 }
1841 
1843 {
1844  CEvaluationNode* pResult = NULL;
1845  // go through all mAndSets and create AND combined chains which are then
1846  // combined with OR
1847  // do the same with all choice sets
1848  // combine the two results with OR
1849  CEvaluationNode* pNotNode = NULL;
1850  CEvaluationNode* pNode = NULL;
1853  std::vector<CEvaluationNode*> andElements;
1854  std::vector<CEvaluationNode*> orElements;
1855  CNormalLogical::ChoiceSetOfSets::const_iterator cIt = logical.getChoices().begin(), cEndit = logical.getChoices().end();
1856 
1857  while (cIt != cEndit)
1858  {
1859  CNormalLogical::ChoiceSet::const_iterator cInnerIt = (*cIt).first.begin(), cInnerEndit = (*cIt).first.end();
1860 
1861  while (cInnerIt != cInnerEndit)
1862  {
1863  pNode = convertToCEvaluationNode(*(*cInnerIt).first);
1864  assert(pNode != NULL);
1865 
1866  if ((*cInnerIt).second == true)
1867  {
1868  // only create the not node if it wouldn't result in the neutral
1869  // element
1871  {
1873  pNotNode->addChild(pNode);
1874  pNode = pNotNode;
1875  }
1876  else
1877  {
1878  pNode = NULL;
1879  }
1880  }
1881 
1882  ++cInnerIt;
1883 
1884  // if it is not the neutral element or it is the last element to be
1885  // inserted into an otherwise empty vector, insert the element
1886  if (((pNode != NULL) && ((*pNode) != CNormalTranslation::NEUTRAL_ELEMENT_AND)) || (andElements.empty() && cInnerIt == cInnerEndit))
1887  {
1888  andElements.push_back(pNode);
1889  }
1890  }
1891 
1892  // create the and chain
1894  assert(pNode != NULL);
1895  andElements.clear();
1896 
1897  // check *cIt.second if it is true
1898  if ((*cIt).second == true)
1899  {
1900  // create a not node if it would not result in the creation of the
1901  // neutral element
1903  {
1905  pNode->addChild(pNode);
1906  pNode = pNotNode;
1907  }
1908  else
1909  {
1910  pNode = NULL;
1911  }
1912  }
1913 
1914  ++cIt;
1915 
1916  // if it is not the neutral element or it is the last element to be
1917  // inserted into an otherwise empty vector, insert the element
1918  if (((pNode != NULL) && ((*pNode) != CNormalTranslation::NEUTRAL_ELEMENT_OR)) || (orElements.empty() && cIt == cEndit))
1919  {
1920  orElements.push_back(pNode);
1921  }
1922  }
1923 
1924  // create the OR chain
1925  if (!orElements.empty())
1926  {
1928  assert(pNode != NULL);
1929  orElements.clear();
1930  }
1931 
1932  pResult = pNode;
1933  pNode = NULL;
1934 
1935  CNormalLogical::ItemSetOfSets::const_iterator iIt = logical.getAndSets().begin(), iEndit = logical.getAndSets().end();
1936 
1937  while (iIt != iEndit)
1938  {
1939  CNormalLogical::ItemSet::const_iterator iInnerIt = (*iIt).first.begin(), iInnerEndit = (*iIt).first.end();
1940 
1941  while (iInnerIt != iInnerEndit)
1942  {
1943  pNode = convertToCEvaluationNode(*(*iInnerIt).first);
1944  assert(pNode != NULL);
1945 
1946  if ((*iInnerIt).second == true)
1947  {
1948  // only create the not node if it wouldn't result in the neutral
1949  // element
1951  {
1953  pNotNode->addChild(pNode);
1954  pNode = pNotNode;
1955  }
1956  else
1957  {
1958  delete pNode;
1959  pNode = NULL;
1960  }
1961  }
1962 
1963  ++iInnerIt;
1964 
1965  // if it is not the neutral element or it is the last element to be
1966  // inserted into an otherwise empty vector, insert the element
1967  if (((pNode != NULL) && ((*pNode) != CNormalTranslation::NEUTRAL_ELEMENT_AND)) || (andElements.empty() && iInnerIt == iInnerEndit))
1968  {
1969  andElements.push_back(pNode);
1970  }
1971  }
1972 
1973  // create the and chain
1975  assert(pNode != NULL);
1976  andElements.clear();
1977 
1978  // check *iIt.second if it is true
1979  if ((*iIt).second == true)
1980  {
1981 
1982  // create a not node if it does not result in the neutral node
1984  {
1986  pNotNode->addChild(pNode);
1987  pNode = pNotNode;
1988  }
1989  else
1990  {
1991  delete pNode;
1992  pNode = NULL;
1993  }
1994  }
1995 
1996  ++iIt;
1997 
1998  // if it is not the neutral element or it is the last element to be
1999  // inserted into an otherwise empty vector, insert the element
2000  if (((pNode != NULL) && ((*pNode) != CNormalTranslation::NEUTRAL_ELEMENT_OR)) || (orElements.empty() && iIt == iEndit))
2001  {
2002  orElements.push_back(pNode);
2003  }
2004  }
2005 
2006  // create the OR chain
2007  if (!orElements.empty())
2008  {
2010  assert(pNode != NULL);
2011  orElements.clear();
2012  }
2013 
2014  if (pResult == NULL)
2015  {
2016  pResult = pNode;
2017  assert(pResult != NULL);
2018  }
2019  else
2020  {
2022  {
2023  pResult = pNode;
2024  }
2025  else if (*pNode == CNormalTranslation::NEUTRAL_ELEMENT_OR)
2026  {
2027  CEvaluationNode* pTmpNode = pOrNode->copyBranch();
2028  pTmpNode->addChild(pResult);
2029  pTmpNode->addChild(pNode);
2030  pResult = pTmpNode;
2031  }
2032  }
2033 
2034  // check if mNot is set
2035  if (logical.isNegated() == true)
2036  {
2038  pNode->addChild(pResult);
2039  pResult = pNotNode;
2040  }
2041 
2042  // clean up
2043  delete pOrNode;
2044  delete pAndNode;
2045  assert(pResult != NULL);
2046  return pResult;
2047 }
2048 
2049 /**
2050  * This version of the method is wrong.
2051 CEvaluationNode* convertToCEvaluationNode(const CNormalLogical& logical)
2052 {
2053  CEvaluationNode* pResult = NULL;
2054  bool error = false;
2055  CEvaluationNodeFunction* pNot = NULL;
2056  CNormalLogical::ItemSetOfSets::const_iterator it = logical.getAndSets().begin(), endit = logical.getAndSets().end();
2057  std::vector<CEvaluationNode*> andItems;
2058  while (it != endit && error == false)
2059  {
2060  CEvaluationNode* pTmpResult = NULL;
2061  if ((*it).first.size() == 1)
2062  {
2063  pTmpResult = convertToCEvaluationNode(*(*(*it).first.begin()).first);
2064  if ((*(*it).first.begin()).second == true)
2065  {
2066  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2067  pNot->addChild(pTmpResult);
2068  pTmpResult = pNot;
2069  }
2070  }
2071  else if ((*it).first.size() > 1)
2072  {
2073  CNormalLogical::ItemSet::const_iterator innerit = (*it).first.begin(), innerendit = (*it).first.end();
2074  // create the not
2075  CEvaluationNode* pOrNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::OR, "OR");
2076  pTmpResult = pOrNode;
2077  while (innerit != innerendit && error == false)
2078  {
2079  CEvaluationNode* pItem = convertToCEvaluationNode(*(*innerit).first);
2080  if (pItem != NULL)
2081  {
2082  if ((*innerit).second == true)
2083  {
2084  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2085  pNot->addChild(pItem);
2086  pItem = pNot;
2087  }
2088  pOrNode->addChild(pItem);
2089  pOrNode->addChild(new CEvaluationNodeLogical(CEvaluationNodeLogical::OR, "OR"));
2090  pOrNode = dynamic_cast<CEvaluationNodeLogical*>(pOrNode->getChild()->getSibling());
2091  }
2092  else
2093  {
2094  error = true;
2095  delete pTmpResult;
2096  pTmpResult = NULL;
2097  }
2098  ++innerit;
2099  }
2100  if (error == false)
2101  {
2102  // replace the parent of the current pOrNode with its first child;
2103  CEvaluationNode * pGrandParent = dynamic_cast<CEvaluationNode*>(pOrNode->getParent());
2104  pGrandParent->removeChild(pOrNode->getParent());
2105  pOrNode = dynamic_cast<CEvaluationNode*>(pOrNode->getParent());
2106  CEvaluationNode* pChild = dynamic_cast<CEvaluationNode*>(pOrNode->getChild());
2107  pOrNode->removeChild(pChild);
2108  pGrandParent->addChild(pChild);
2109  delete pOrNode;
2110  }
2111  }
2112  if (pTmpResult != NULL)
2113  {
2114  if ((*it).second == true)
2115  {
2116  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2117  pNot->addChild(pTmpResult);
2118  }
2119  andItems.push_back(pTmpResult);
2120  }
2121  ++it;
2122  }
2123  CNormalLogical::ChoiceSetOfSets::const_iterator it2 = logical.getChoices().begin(), endit2 = logical.getChoices().end();
2124  while (it2 != endit2 && error == false)
2125  {
2126  CEvaluationNode* pTmpResult = NULL;
2127  if ((*it2).first.size() == 1)
2128  {
2129  pTmpResult = convertToCEvaluationNode(*(*(*it2).first.begin()).first);
2130  if ((*(*it2).first.begin()).second == true)
2131  {
2132  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2133  pNot->addChild(pTmpResult);
2134  pTmpResult = pNot;
2135  }
2136  }
2137  else if ((*it2).first.size() > 1)
2138  {
2139  CNormalLogical::ChoiceSet::const_iterator innerit = (*it2).first.begin(), innerendit = (*it2).first.end();
2140  // create the not
2141  CEvaluationNode* pOrNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::OR, "OR");
2142  pTmpResult = pOrNode;
2143  while (innerit != innerendit && error != true)
2144  {
2145  CEvaluationNode* pItem = convertToCEvaluationNode(*(*innerit).first);
2146  if (pItem != NULL)
2147  {
2148  if ((*innerit).second == true)
2149  {
2150  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2151  pNot->addChild(pItem);
2152  pItem = pNot;
2153  }
2154  pOrNode->addChild(pItem);
2155  pOrNode->addChild(new CEvaluationNodeLogical(CEvaluationNodeLogical::OR, "OR"));
2156  pOrNode = dynamic_cast<CEvaluationNodeLogical*>(pOrNode->getChild()->getSibling());
2157  }
2158  else
2159  {
2160  delete pTmpResult;
2161  pTmpResult = NULL;
2162  error = true;
2163  }
2164  ++innerit;
2165  }
2166  // replace the parent of the current pOrNode with its first child;
2167  if (error == false)
2168  {
2169  CEvaluationNode * pGrandParent = dynamic_cast<CEvaluationNode*>(pOrNode->getParent());
2170  pGrandParent->removeChild(pOrNode->getParent());
2171  pOrNode = dynamic_cast<CEvaluationNode*>(pOrNode->getParent());
2172  CEvaluationNode* pChild = dynamic_cast<CEvaluationNode*>(pOrNode->getChild());
2173  pOrNode->removeChild(pChild);
2174  pGrandParent->addChild(pChild);
2175  delete pOrNode;
2176  }
2177  }
2178  if (pTmpResult != NULL)
2179  {
2180  if ((*it2).second == true)
2181  {
2182  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2183  pNot->addChild(pTmpResult);
2184  }
2185  andItems.push_back(pTmpResult);
2186  }
2187  ++it2;
2188  }
2189  if (!andItems.empty())
2190  {
2191  if (error == false)
2192  {
2193  if (andItems.size() == 1)
2194  {
2195  pResult = *andItems.begin();
2196  }
2197  else
2198  {
2199  std::vector<CEvaluationNode*>::iterator andit = andItems.begin(), andendit = andItems.end();
2200  CEvaluationNode* pAndNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::AND, "AND");
2201  pResult = pAndNode;
2202  while (andit != andendit)
2203  {
2204  pAndNode->addChild(*andit);
2205  pAndNode->addChild(new CEvaluationNodeLogical(CEvaluationNodeLogical::AND, "AND"));
2206  pAndNode = dynamic_cast<CEvaluationNodeLogical*>(pAndNode->getChild()->getSibling());
2207  ++andit;
2208  }
2209  // replace the parent of the current pAndNode with its first child;
2210  CEvaluationNode* pGrandParent = dynamic_cast<CEvaluationNode*>(pAndNode->getParent());
2211  pGrandParent->removeChild(pAndNode->getParent());
2212  pAndNode = dynamic_cast<CEvaluationNode*>(pAndNode->getParent());
2213  CEvaluationNode* pChild = dynamic_cast<CEvaluationNode*>(pAndNode->getChild());
2214  pAndNode->removeChild(pChild);
2215  pGrandParent->addChild(pChild);
2216  delete pAndNode;
2217  }
2218  }
2219  else
2220  {
2221  // an error has occured, we need to free the memory taken by all
2222  // andItems
2223  std::vector<CEvaluationNode*>::iterator andit = andItems.begin(), andendit = andItems.end();
2224  while (andit != andendit)
2225  {
2226  delete (*andit);
2227  ++andit;
2228  }
2229  }
2230  }
2231  if (pResult != NULL)
2232  {
2233  if (logical.isNegated() == true)
2234  {
2235  pNot = new CEvaluationNodeFunction(CEvaluationNodeFunction::NOT, "NOT");
2236  pNot->addChild(pResult);
2237  pResult = pNot;
2238  }
2239  pResult->compile(NULL);
2240  }
2241  return pResult;
2242 }
2243  */
2244 
2246 {
2247  CNormalChoice* pResult = NULL;
2248 
2249  if (pNode != NULL && CEvaluationNode::type(pNode->getType()) == CEvaluationNode::CHOICE)
2250  {
2251  CNormalLogical* pLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2252 
2253  if (pLogical != NULL)
2254  {
2255  CNormalFraction* pTrueFraction = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2256 
2257  if (pTrueFraction != NULL)
2258  {
2259  // the false branch is optional
2260  const CEvaluationNode* pFalseBranch = dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()->getSibling());
2261 
2262  if (pFalseBranch != NULL)
2263  {
2264  CNormalFraction* pFalseFraction = createNormalRepresentation(pFalseBranch);
2265 
2266  if (pFalseFraction != NULL)
2267  {
2268  pResult = new CNormalChoice();
2269  pResult->setCondition(*pLogical);
2270  pResult->setTrueExpression(*pTrueFraction);
2271  pResult->setFalseExpression(*pFalseFraction);
2272  delete pFalseFraction;
2273  }
2274  }
2275  else
2276  {
2277  CNormalFraction* pFalseFraction = new CNormalFraction();
2278  pResult = new CNormalChoice();
2279  pResult->setCondition(*pLogical);
2280  pResult->setTrueExpression(*pTrueFraction);
2281  pResult->setFalseExpression(*pFalseFraction);
2282  delete pFalseFraction;
2283  }
2284 
2285  delete pTrueFraction;
2286  }
2287 
2288  delete pLogical;
2289  }
2290  }
2291 
2292  return pResult;
2293 }
2294 
2296 {
2297  CNormalChoiceLogical* pResult = NULL;
2298 
2299  if (pNode != NULL && CEvaluationNode::type(pNode->getType()) == CEvaluationNode::CHOICE)
2300  {
2301  CNormalLogical* pLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2302 
2303  if (pLogical != NULL)
2304  {
2305  CNormalLogical* pTrueLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2306 
2307  if (pTrueLogical != NULL)
2308  {
2309  // the false branch is optional
2310  const CEvaluationNode* pFalseBranch = dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()->getSibling());
2311 
2312  if (pFalseBranch != NULL)
2313  {
2314  CNormalLogical* pFalseLogical = createLogical(pFalseBranch);
2315 
2316  if (pFalseLogical != NULL)
2317  {
2318  pResult = new CNormalChoiceLogical();
2319  pResult->setCondition(*pLogical);
2320  pResult->setTrueExpression(*pTrueLogical);
2321  pResult->setFalseExpression(*pFalseLogical);
2322  delete pFalseLogical;
2323  }
2324  }
2325  else
2326  {
2327  pResult = new CNormalChoiceLogical();
2328  pResult->setCondition(*pLogical);
2329  pResult->setTrueExpression(*pTrueLogical);
2330  CNormalLogical* pFalseLogical = new CNormalLogical();
2331  pResult->setFalseExpression(*pFalseLogical);
2332  delete pFalseLogical;
2333  }
2334 
2335  delete pTrueLogical;
2336  }
2337 
2338  delete pLogical;
2339  }
2340  }
2341 
2342  return pResult;
2343 }
2344 
2346 {
2347  CNormalLogical* pResult = NULL;
2348 
2349  if (pNode != NULL)
2350  {
2351  CEvaluationNode::Type type = pNode->getType();
2352 
2354  {
2356  {
2357  CNormalLogicalItem* pLogicalItem = createLogicalItem(pNode);
2358 
2359  if (pLogicalItem != NULL)
2360  {
2361  pResult = new CNormalLogical();
2363  tmp.insert(std::make_pair(pLogicalItem, false));
2364  pResult->getAndSets().insert(std::make_pair(tmp, false));
2365  }
2366  }
2367  }
2369  {
2370  CNormalLogicalItem* pLogicalItem = NULL;
2371  CEvaluationNode* pOrNode = NULL;
2372  CEvaluationNode* pAndNode = NULL;
2373  const CEvaluationNode* pA = NULL;
2374  const CEvaluationNode* pB = NULL;
2375  CEvaluationNode* pNotNode = NULL;
2376  CNormalLogical* pLeftLogical = NULL;
2377  CNormalLogical::ChoiceSetOfSets::const_iterator it, endit;
2378  CNormalLogical::ItemSetOfSets::const_iterator it2, endit2;
2379 
2381  {
2388  pLogicalItem = createLogicalItem(pNode);
2389 
2390  if (pLogicalItem != NULL)
2391  {
2392  pResult = new CNormalLogical();
2394  tmp.insert(std::make_pair(pLogicalItem, false));
2395  pResult->getAndSets().insert(std::make_pair(tmp, false));
2396  }
2397 
2398  break;
2400  // replace A xor B by A OR B AND NOT(A AND B)
2401  pA = dynamic_cast<const CEvaluationNode*>(pNode->getChild());
2402  pB = dynamic_cast<const CEvaluationNode*>(pA->getSibling());
2403  pAndNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::AND, "AND");
2404  pAndNode->addChild(pA->copyBranch());
2405  pAndNode->addChild(pB->copyBranch());
2407  pNotNode->addChild(pAndNode);
2409  pOrNode->addChild(pA->copyBranch());
2410  pOrNode->addChild(pB->copyBranch());
2411  pAndNode = new CEvaluationNodeLogical(CEvaluationNodeLogical::AND, "AND");
2412  pAndNode->addChild(pOrNode);
2413  pAndNode->addChild(pNotNode);
2414  pResult = createLogical(pAndNode);
2415  delete pAndNode;
2416  break;
2418  pLeftLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2419 
2420  if (pLeftLogical != NULL)
2421  {
2422  pLeftLogical->simplify();
2423  CNormalLogical* pRightLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2424 
2425  if (pRightLogical != NULL)
2426  {
2427  pRightLogical->simplify();
2428  // integrate the items into a new logical item
2429  // we have to create an AND set for every
2430  // combination between two AND Sets from the
2431  // two logicals
2432  pResult = new CNormalLogical();
2433  CNormalLogical::ChoiceSetOfSets::const_iterator it = pLeftLogical->getChoices().begin(), endit = pLeftLogical->getChoices().end();
2434 
2435  while (it != endit)
2436  {
2437  CNormalLogical::ChoiceSetOfSets::const_iterator it2 = pRightLogical->getChoices().begin(), endit2 = pRightLogical->getChoices().end();
2438 
2439  if ((*it).second == true) throw std::exception();
2440 
2441  while (it2 != endit2)
2442  {
2443  // create an AND set from the two, but only
2444  // if both don't have the NOT flag set
2445  // which should always be the case since we
2446  // called simplify on the items.
2447  if ((*it2).second == true) throw std::exception();
2448 
2450  CNormalLogical::ChoiceSet::const_iterator innerit = (*it).first.begin(), innerendit = (*it).first.end();
2451 
2452  while (innerit != innerendit)
2453  {
2454  tmp.insert(std::make_pair(new CNormalChoiceLogical(*(*innerit).first), (*innerit).second));
2455  ++innerit;
2456  }
2457 
2458  innerit = (*it2).first.begin(), innerendit = (*it2).first.end();
2459 
2460  while (innerit != innerendit)
2461  {
2462  tmp.insert(std::make_pair(new CNormalChoiceLogical(*(*innerit).first), (*innerit).second));
2463  ++innerit;
2464  }
2465 
2466  pResult->getChoices().insert(std::make_pair(tmp, false));
2467  ++it2;
2468  }
2469 
2470  ++it;
2471  }
2472 
2473  CNormalLogical::ItemSetOfSets::const_iterator it2 = pLeftLogical->getAndSets().begin(), endit2 = pLeftLogical->getAndSets().end();
2474 
2475  while (it2 != endit2)
2476  {
2477  CNormalLogical::ItemSetOfSets::const_iterator it3 = pRightLogical->getAndSets().begin(), endit3 = pRightLogical->getAndSets().end();
2478 
2479  if ((*it2).second == true) throw std::exception();
2480 
2481  while (it3 != endit3)
2482  {
2483  // create an AND set from the two, but only
2484  // if both don't have the NOT flag set
2485  // which should always be the case since we
2486  // called simplify on the items.
2487  if ((*it3).second == true) throw std::exception();
2488 
2490  CNormalLogical::ItemSet::const_iterator innerit = (*it2).first.begin(), innerendit = (*it2).first.end();
2491 
2492  while (innerit != innerendit)
2493  {
2494  tmp.insert(std::make_pair(new CNormalLogicalItem(*(*innerit).first), (*innerit).second));
2495  ++innerit;
2496  }
2497 
2498  innerit = (*it3).first.begin(), innerendit = (*it3).first.end();
2499 
2500  while (innerit != innerendit)
2501  {
2502  tmp.insert(std::make_pair(new CNormalLogicalItem(*(*innerit).first), (*innerit).second));
2503  ++innerit;
2504  }
2505 
2506  pResult->getAndSets().insert(std::make_pair(tmp, false));
2507  ++it3;
2508  }
2509 
2510  ++it2;
2511  }
2512 
2513  delete pLeftLogical;
2514  delete pRightLogical;
2515  }
2516  else
2517  {
2518  delete pLeftLogical;
2519  }
2520  }
2521 
2522  break;
2524  // 1. create two logicals, one for the left side and one for the
2525  // right side
2526  pResult = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2527  pLeftLogical = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2528  // 2. add all or items from the second logical to the first and
2529  // delete the second
2530  it = pLeftLogical->getChoices().begin(), endit = pLeftLogical->getChoices().end();
2531 
2532  while (it != endit)
2533  {
2535  CNormalLogical::copySet((*it).first, tmpSet);
2536  pResult->getChoices().insert(std::make_pair(tmpSet, (*it).second));
2537  ++it;
2538  }
2539 
2540  it2 = pLeftLogical->getAndSets().begin(), endit2 = pLeftLogical->getAndSets().end();
2541 
2542  while (it2 != endit2)
2543  {
2544  CNormalLogical::ItemSet tmpSet;
2545  CNormalLogical::copySet((*it2).first, tmpSet);
2546  pResult->getAndSets().insert(std::make_pair(tmpSet, (*it2).second));
2547  ++it2;
2548  }
2549 
2550  delete pLeftLogical;
2551  break;
2552  default:
2553  break;
2554  }
2555  }
2557  {
2558  pResult = createLogical(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2559 
2560  if (pResult != NULL)
2561  {
2562  pResult->negate();
2563  }
2564  }
2566  {
2567  CNormalChoiceLogical* pLogicalChoice = createLogicalChoice(pNode);
2568 
2569  if (pLogicalChoice != NULL)
2570  {
2572  tmp.insert(std::make_pair(pLogicalChoice, false));
2573  pResult = new CNormalLogical();
2574  pResult->getChoices().insert(std::make_pair(tmp, false));
2575  }
2576  }
2577  }
2578 
2579  return pResult;
2580 }
2581 
2583 {
2584  CNormalLogicalItem* pResult = NULL;
2585 
2586  if (pNode != NULL)
2587  {
2588  CEvaluationNode::Type type = pNode->getType();
2589 
2591  {
2593  {
2594  pResult = new CNormalLogicalItem();
2596  }
2598  {
2599  pResult = new CNormalLogicalItem();
2601  }
2602  }
2603 
2605  {
2606  CNormalFraction* pFrac1 = NULL;
2607  CNormalFraction* pFrac2 = NULL;
2608 
2610  {
2612  pResult = new CNormalLogicalItem();
2613  pResult->setType(CNormalLogicalItem::EQ);
2614  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2615 
2616  if (pFrac1 != NULL)
2617  {
2618  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2619 
2620  if (pFrac2 != NULL)
2621  {
2622  pResult->setLeft(*pFrac1);
2623  pResult->setRight(*pFrac2);
2624  delete pFrac1;
2625  delete pFrac2;
2626  }
2627  else
2628  {
2629  delete pFrac1;
2630  pFrac1 = NULL;
2631  }
2632  }
2633 
2634  break;
2636  pResult = new CNormalLogicalItem();
2637  pResult->setType(CNormalLogicalItem::NE);
2638  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2639 
2640  if (pFrac1 != NULL)
2641  {
2642  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2643 
2644  if (pFrac2 != NULL)
2645  {
2646  pResult->setLeft(*pFrac1);
2647  pResult->setRight(*pFrac2);
2648  delete pFrac1;
2649  delete pFrac2;
2650  }
2651  else
2652  {
2653  delete pFrac1;
2654  pFrac1 = NULL;
2655  }
2656  }
2657 
2658  break;
2660  pResult = new CNormalLogicalItem();
2661  pResult->setType(CNormalLogicalItem::GT);
2662  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2663 
2664  if (pFrac1 != NULL)
2665  {
2666  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2667 
2668  if (pFrac2 != NULL)
2669  {
2670  pResult->setLeft(*pFrac1);
2671  pResult->setRight(*pFrac2);
2672  delete pFrac1;
2673  delete pFrac2;
2674  }
2675  else
2676  {
2677  delete pFrac1;
2678  pFrac1 = NULL;
2679  }
2680  }
2681 
2682  break;
2684  pResult = new CNormalLogicalItem();
2685  pResult->setType(CNormalLogicalItem::LT);
2686  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2687 
2688  if (pFrac1 != NULL)
2689  {
2690  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2691 
2692  if (pFrac2 != NULL)
2693  {
2694  pResult->setLeft(*pFrac1);
2695  pResult->setRight(*pFrac2);
2696  delete pFrac1;
2697  delete pFrac2;
2698  }
2699  else
2700  {
2701  delete pFrac1;
2702  pFrac1 = NULL;
2703  }
2704  }
2705 
2706  break;
2708  pResult = new CNormalLogicalItem();
2709  pResult->setType(CNormalLogicalItem::GE);
2710  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2711 
2712  if (pFrac1 != NULL)
2713  {
2714  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2715 
2716  if (pFrac2 != NULL)
2717  {
2718  pResult->setLeft(*pFrac1);
2719  pResult->setRight(*pFrac2);
2720  delete pFrac1;
2721  delete pFrac2;
2722  }
2723  else
2724  {
2725  delete pFrac1;
2726  pFrac1 = NULL;
2727  }
2728  }
2729 
2730  break;
2732  pResult = new CNormalLogicalItem();
2733  pResult->setType(CNormalLogicalItem::LE);
2734  pFrac1 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()));
2735 
2736  if (pFrac1 != NULL)
2737  {
2738  pFrac2 = createNormalRepresentation(dynamic_cast<const CEvaluationNode*>(pNode->getChild()->getSibling()));
2739 
2740  if (pFrac2 != NULL)
2741  {
2742  pResult->setLeft(*pFrac1);
2743  pResult->setRight(*pFrac2);
2744  delete pFrac1;
2745  delete pFrac2;
2746  }
2747  else
2748  {
2749  delete pFrac1;
2750  pFrac1 = NULL;
2751  }
2752  }
2753 
2754  break;
2755  default:
2756  break;
2757  }
2758  }
2759  }
2760 
2761  return pResult;
2762 }
static void splitSum(const CEvaluationNode *pRoot, std::vector< CEvaluationNode * > &additions, std::vector< CEvaluationNode * > &substractions, bool minus)
std::map< std::string, const CEvaluationNode * > str2eval
CNormalItem * createItem(const CEvaluationNode *pNode)
CNormalSum & getNumerator()
CNormalLogicalItem * createLogicalItem(const CEvaluationNode *pNode)
CNormalLogical * createLogical(const CEvaluationNode *pNode)
CEvaluationNode * copyBranch() const
static const CEvaluationNode NEUTRAL_ELEMENT_AND
ChoiceSetOfSets & getChoices()
CNormalSum * createSum(const CEvaluationNode *node)
CCopasiNode< Data > * getSibling()
Definition: CCopasiNode.h:353
bool checkIsOne() const
CNormalChoiceLogical * createLogicalChoice(const CEvaluationNode *pNode)
void setType(Type type)
const C_FLOAT64 & getValue() const
const CNormalFraction & getTrueExpression() const
bool setRight(const CNormalFraction &right)
CNormalFraction & getLeft()
bool setTrueExpression(const CNormalFraction &branch)
const Type & getType() const
const CNormalLogical & getCondition() const
void setFraction(const CNormalFraction &frac)
const std::string & getName() const
CNormalFraction & getLeft()
std::string buildInfix() const
const std::vector< CNormalSum * > & getSums() const
Definition: CNormalLcm.cpp:275
CEvaluationNode * convertToCEvaluationNode(const CNormalFraction &fraction)
const CNormalLogical & getFalseExpression() const
const CNormalLogical & getCondition() const
virtual bool simplify()
const std::vector< CNormalFraction * > & getFractions() const
bool setFalseExpression(const CNormalFraction &branch)
C_LOGICAL logical
Definition: f2c.h:18
const CNormalLogical & getTrueExpression() const
bool setLeft(const CNormalFraction &left)
void setName(const std::string &name)
CNormalChoice * createChoice(const CEvaluationNode *pNode)
bool add(const CNormalProduct &product)
Definition: CNormalSum.cpp:156
bool setTrueExpression(const CNormalLogical &branch)
const CNormalFraction & getFalseExpression() const
bool setItem(const CNormalBase &item)
static Type type(const Type &type)
void setType(Type type)
const std::set< CNormalFraction * > & getFractions() const
Definition: CNormalSum.cpp:425
bool isNegated() const
const Type & getType() const
Definition: CNormalItem.cpp:91
static const CEvaluationNode NEUTRAL_ELEMENT_OR
bool setFalseExpression(const CNormalLogical &branch)
CNormalBase * createItemPowerItem(const CEvaluationNode *pNode)
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:156
static const CEvaluationNode ONE_NODE
CNormalSum & getDenominator()
bool setDenominator(const CNormalSum &denominator)
const std::set< CNormalItemPower *, compareItemPowers > & getItemPowers() const
bool setCondition(const CNormalLogical &cond)
CNormalFraction * createFraction(const CEvaluationNode *node)
static const char * NAMES[]
CNormalFraction & getRight()
static const CEvaluationNode ZERO_NODE
CNormalItemPower * createItemPower(const CEvaluationNode *node)
bool setFactor(const C_FLOAT64 &number)
const C_FLOAT64 & getExp() const
bool add(const CNormalFraction &fraction)
Type getType() const
static void splitProduct(const CEvaluationNode *pRoot, std::vector< const CEvaluationNode * > &multiplications, std::vector< const CEvaluationNode * > &divisions, bool division)
const C_FLOAT64 & getFactor() const
void setRight(const CNormalFraction &right)
const CArrayAnnotation * pResult
static CEvaluationNode * newEvaluateNumbers(const CEvaluationNode *pOrig)
static Type subType(const Type &type)
bool checkDenominatorOne() const
bool setNumerator(const CNormalSum &numerator)
CNormalFraction * createNormalRepresentation(const CEvaluationNode *node)
#define C_FLOAT64
Definition: copasi.h:92
bool setCondition(const CNormalLogical &cond)
CCopasiNode< Data > * getParent()
Definition: CCopasiNode.h:139
static void copySet(const TemplateSet< TYPE > &source, TemplateSet< TYPE > &target)
virtual bool compile(const CEvaluationTree *pTree)
static CEvaluationNode * expandProducts(const CEvaluationNode *pOrig)
void setLeft(const CNormalFraction &left)
CNormalBase & getItem()
static const CEvaluationNode TIMES_NODE
virtual bool removeChild(CCopasiNode< Data > *pChild)
Definition: CCopasiNode.h:181
const std::string getName() const
Definition: CNormalItem.cpp:82
virtual const Data & getData() const
Definition: CCopasiNode.h:118
static CEvaluationNode * createChain(const CEvaluationNode *pLink, const CEvaluationNode *pNeutralElement, const std::vector< const CEvaluationNode * > &elements)
bool multiply(const C_FLOAT64 &number)
static const CEvaluationNode PLUS_NODE
Type getType() const
CCopasiNode< Data > * getChild()
Definition: CCopasiNode.h:210
CNormalGeneralPower * createGeneralPower(const CEvaluationNode *node)
CNormalFraction & getFraction()
const std::set< CNormalProduct *, compareProducts > & getProducts() const
Definition: CNormalSum.cpp:416
const std::set< CNormalItemPower *, compareItemPowers > & getItemPowers() const
Definition: CNormalLcm.cpp:266
CNormalFunction * createFunction(const CEvaluationNode *node)
CNormalProduct * createProduct(const CEvaluationNode *node)
CNormalFraction & getRight()
ItemSetOfSets & getAndSets()
bool setExp(const C_FLOAT64 &number)
bool isLogical(const CEvaluationNode *pNode)
CNormalCall * createCall(const CEvaluationNode *node)