void test_compare_utilities::test_copasi_function_expansion() { CCopasiDataModel* pDataModel = pCOPASIDATAMODEL;; std::istringstream iss(test_compare_utilities::MODEL_STRING1); CPPUNIT_ASSERT(load_cps_model_from_stream(iss, *pDataModel) == true); CFunctionDB* pFunctionDB = CCopasiRootContainer::getFunctionList(); // function_5 CEvaluationTree* pTree = pFunctionDB->findFunction("function_4"); CPPUNIT_ASSERT(pTree != NULL); // generate a call node CFunction* pFunction = dynamic_cast<CFunction*>(pTree); CPPUNIT_ASSERT(pFunction != NULL); CEvaluationNodeCall* pCallNode = new CEvaluationNodeCall(CEvaluationNode::S_FUNCTION, pFunction->getObjectName()); CPPUNIT_ASSERT(pCallNode != NULL); CFunctionParameters* pFunctionParameters = &pFunction->getVariables(); unsigned int i = 0, iMax = pFunctionParameters->size(); while (i < iMax) { CFunctionParameter* pParameter = (*pFunctionParameters)[i]; CPPUNIT_ASSERT(pParameter != NULL); CEvaluationNodeVariable* pVariableNode = new CEvaluationNodeVariable(CEvaluationNode::S_DEFAULT, pParameter->getObjectName()); pCallNode->addChild(pVariableNode); ++i; } CEvaluationNode* pExpanded = expand_function_calls(pCallNode, pFunctionDB); delete pCallNode; CPPUNIT_ASSERT(pExpanded != NULL); CPPUNIT_ASSERT(pExpanded->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pExpanded->subType() == CEvaluationNode::S_DIVIDE); CEvaluationNode* pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_PLUS); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("y")); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("x")); CPPUNIT_ASSERT(pChild->getSibling() == NULL); pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 2.0) / 2.0) < 1e-6); CPPUNIT_ASSERT(pChild->getSibling() == NULL); delete pExpanded; // function_5 pTree = pFunctionDB->findFunction("function_5"); CPPUNIT_ASSERT(pTree != NULL); // generate a call node pFunction = dynamic_cast<CFunction*>(pTree); CPPUNIT_ASSERT(pFunction != NULL); pCallNode = new CEvaluationNodeCall(CEvaluationNode::S_FUNCTION, pFunction->getObjectName()); CPPUNIT_ASSERT(pCallNode != NULL); pFunctionParameters = &pFunction->getVariables(); i = 0, iMax = pFunctionParameters->size(); while (i < iMax) { CFunctionParameter* pParameter = (*pFunctionParameters)[i]; CPPUNIT_ASSERT(pParameter != NULL); CEvaluationNodeVariable* pVariableNode = new CEvaluationNodeVariable(CEvaluationNode::S_DEFAULT, pParameter->getObjectName()); pCallNode->addChild(pVariableNode); ++i; } pExpanded = expand_function_calls(pCallNode, pFunctionDB); delete pCallNode; CPPUNIT_ASSERT(pExpanded != NULL); CPPUNIT_ASSERT(pExpanded->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pExpanded->subType() == CEvaluationNode::S_PLUS); pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MINUS); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("a")); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MULTIPLY); CPPUNIT_ASSERT(pChild->getSibling() == NULL); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("c")); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 1.3) / 1.3) < 1e-6); CPPUNIT_ASSERT(pChild->getSibling() == NULL); // (3*b)-5.23 pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MINUS); // 3*b pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MULTIPLY); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 3.0) / 3.0) < 1e-6); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("b")); CPPUNIT_ASSERT(pChild->getSibling() == NULL); // 5.23 pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()->getSibling()->getChild()->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 5.23) / 5.23) < 1e-6); CPPUNIT_ASSERT(pChild->getSibling() == NULL); delete pExpanded; // function_6 pTree = pFunctionDB->findFunction("function_6"); CPPUNIT_ASSERT(pTree != NULL); // generate a call node pFunction = dynamic_cast<CFunction*>(pTree); CPPUNIT_ASSERT(pFunction != NULL); pCallNode = new CEvaluationNodeCall(CEvaluationNode::S_FUNCTION, pFunction->getObjectName()); CPPUNIT_ASSERT(pCallNode != NULL); pFunctionParameters = &pFunction->getVariables(); i = 0, iMax = pFunctionParameters->size(); while (i < iMax) { CFunctionParameter* pParameter = (*pFunctionParameters)[i]; CPPUNIT_ASSERT(pParameter != NULL); CEvaluationNodeVariable* pVariableNode = new CEvaluationNodeVariable(CEvaluationNode::S_DEFAULT, pParameter->getObjectName()); pCallNode->addChild(pVariableNode); ++i; } pExpanded = expand_function_calls(pCallNode, pFunctionDB); delete pCallNode; CPPUNIT_ASSERT(pExpanded != NULL); // (k1-k3*1.3)+((3*k2)-5.23) CPPUNIT_ASSERT(pExpanded->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pExpanded->subType() == CEvaluationNode::S_PLUS); pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MINUS); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("k1")); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MULTIPLY); CPPUNIT_ASSERT(pChild->getSibling() == NULL); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("k3")); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 1.3) / 1.3) < 1e-6); CPPUNIT_ASSERT(pChild->getSibling() == NULL); // (3*b)-5.23 pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MINUS); // 3*b pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_OPERATOR); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_MULTIPLY); pChild = dynamic_cast<CEvaluationNode*>(pChild->getChild()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 3.0) / 3.0) < 1e-6); pChild = dynamic_cast<CEvaluationNode*>(pChild->getSibling()); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_VARIABLE); CPPUNIT_ASSERT(pChild->getData() == std::string("k2")); CPPUNIT_ASSERT(pChild->getSibling() == NULL); // 5.23 pChild = dynamic_cast<CEvaluationNode*>(pExpanded->getChild()->getSibling()->getChild()->getSibling()); CPPUNIT_ASSERT(pChild != NULL); CPPUNIT_ASSERT(pChild->mainType() == CEvaluationNode::T_NUMBER); CPPUNIT_ASSERT(pChild->subType() == CEvaluationNode::S_DOUBLE); CPPUNIT_ASSERT((fabs(pChild->getValue() - 5.23) / 5.23) < 1e-6); CPPUNIT_ASSERT(pChild->getSibling() == NULL); delete pExpanded; }
CMathExpression::CMathExpression(const CFunction & src, const CCallParameters< C_FLOAT64 > & callParameters, CMathContainer & container, const bool & replaceDiscontinuousNodes): CEvaluationTree(src.getObjectName(), &container, CEvaluationTree::MathExpression), mPrerequisites() { clearNodes(); // Deal with the different function types switch (src.getType()) { case CEvaluationTree::Function: case CEvaluationTree::PreDefined: case CEvaluationTree::UserDefined: { // Create a vector of CEvaluationNodeObject for each variable CMath::Variables< CEvaluationNode * > Variables; CCallParameters< C_FLOAT64 >::const_iterator it = callParameters.begin(); CCallParameters< C_FLOAT64 >::const_iterator end = callParameters.end(); for (; it != end; ++it) { Variables.push_back(createNodeFromValue(it->value)); } // Create a converted copy of the existing expression tree. mpRootNode = container.copyBranch(src.getRoot(), Variables, replaceDiscontinuousNodes); // Deleted the created variables CMath::Variables< CEvaluationNode * >::iterator itVar = Variables.begin(); CMath::Variables< CEvaluationNode * >::iterator endVar = Variables.end(); for (; itVar != endVar; ++itVar) { pdelete(*itVar); } } break; case CEvaluationTree::MassAction: { // We build a mass action expression based on the call parameters. CCallParameters< C_FLOAT64 >::const_iterator it = callParameters.begin(); // Handle the case we were have an invalid number of call parameters. if (callParameters.size() < 2) { mpRootNode = NULL; } else { // We always have reactants const C_FLOAT64 * pK = it->value; ++it; const CCallParameters< C_FLOAT64 > * pSpecies = it->vector; ++it; CEvaluationNode * pPart = createMassActionPart(pK, pSpecies); if (callParameters.size() < 4) { mpRootNode = pPart; } else { mpRootNode = new CEvaluationNodeOperator(CEvaluationNode::S_MINUS, "-"); mpRootNode->addChild(pPart); pK = it->value; ++it; pSpecies = it->vector; ++it; pPart = createMassActionPart(pK, pSpecies); mpRootNode->addChild(pPart); } } } break; case CEvaluationTree::MathExpression: case CEvaluationTree::Expression: // This cannot happen and is only here to satisfy the compiler. break; } compile(); }