Exemple #1
0
bool FunctionWidget1::functionParametersChanged()
{
  CFunction* func = dynamic_cast<CFunction*>(CCopasiRootContainer::getKeyFactory()->get(mKey));

  if (!func) return false;

  return (!(func->getVariables() == mpFunction->getVariables()));
}
Exemple #2
0
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;
}
Exemple #3
0
bool CODEExporterC::exportSingleFunction(const CFunction *func, std::set<std::string>& isExported)
{
  CFunctionDB* pFunctionDB = CCopasiRootContainer::getFunctionList();

  CFunction* tmpfunc = NULL;
  tmpfunc = new CFunction(*func, NO_PARENT);

  if (func->getType() != CEvaluationTree::MassAction)
    {

      CCopasiTree< CEvaluationNode>::iterator treeIt = tmpfunc->getRoot();
      CCopasiTree< CEvaluationNode>::iterator newIt = treeIt;

      size_t j, varbs_size = tmpfunc->getVariables().size();
      std::map< std::string, std::string > parameterNameMap;
      std::set<std::string> parameterNameSet;

      std::map< CFunctionParameter::Role, std::string > constName;
      std::map< CFunctionParameter::Role, size_t > tmpIndex;

      constName[CFunctionParameter::SUBSTRATE] = "sub_"; tmpIndex[CFunctionParameter::SUBSTRATE] = 0;
      constName[CFunctionParameter::PRODUCT] = "prod_"; tmpIndex[CFunctionParameter::PRODUCT] = 0;
      constName[CFunctionParameter::PARAMETER] = "param_"; tmpIndex[CFunctionParameter::PARAMETER] = 0;
      constName[CFunctionParameter::MODIFIER] = "modif_"; tmpIndex[CFunctionParameter::MODIFIER] = 0;
      constName[CFunctionParameter::VOLUME] = "volume_"; tmpIndex[CFunctionParameter::VOLUME] = 0;
      constName[CFunctionParameter::VARIABLE] = "varb_"; tmpIndex[CFunctionParameter::VARIABLE] = 0;
      constName[CFunctionParameter::TIME] = "time_"; tmpIndex[CFunctionParameter::VARIABLE] = 0;

      for (j = 0; j < varbs_size; ++j)
        {
          if (parameterNameSet.find(tmpfunc->getVariables()[j]->getObjectName()) == parameterNameSet.end())
            {
              std::ostringstream tmpName;
              CFunctionParameter::Role role = tmpfunc->getVariables()[j]->getUsage();

              tmpName << constName[role] << tmpIndex[role];
              parameterNameMap[ tmpfunc->getVariables()[j]->getObjectName()] = tmpName.str();
              parameterNameSet.insert(tmpfunc->getVariables()[j]->getObjectName());
              tmpIndex[role]++;
            }
        }

      CODEExporter::modifyTreeForMassAction(tmpfunc);

      while (newIt != NULL)
        {
          if (newIt->mainType() == CEvaluationNode::T_VARIABLE)
            {
              newIt->setData(parameterNameMap[ tmpfunc->getVariables()[newIt->getData()]->getObjectName()]);
            }

          if (newIt->mainType() == CEvaluationNode::T_CALL)
            {
              const CFunction* callfunc;
              callfunc = static_cast<CFunction*>(pFunctionDB->findFunction((*newIt).getData()));

              if (callfunc->getType() != CEvaluationTree::MassAction)
                newIt->setData(NameMap[callfunc->getKey()]);
            }

          ++newIt;
        }

      std::string name = func->getObjectName();

      if (isExported.find(name) == isExported.end())
        {
          size_t j, varbs_size = tmpfunc->getVariables().size();

          std::string mappedName = NameMap[func->getKey()];

          if (mappedName.empty())
            {
              NameMap[func->getKey()]  = translateObjectName(name);
              mappedName = NameMap[func->getKey()];
            }

          functions << "double " << mappedName << "(";
          headers << "double " << mappedName << "(";

          for (j = 0; j < varbs_size; ++j)
            {
              functions << "double " << parameterNameMap[ tmpfunc->getVariables()[j]->getObjectName().c_str()];

              if (j != varbs_size - 1) functions << ", ";

              headers << "double " << parameterNameMap[ tmpfunc->getVariables()[j]->getObjectName().c_str()];

              if (j != varbs_size - 1) headers << ", ";
            }

          functions << ") ";
          functions << '\t' << "//" << name << std::endl;
          functions << "{return  " << tmpfunc->getRoot()->buildCCodeString().c_str() << ";} " << std::endl;

          headers << "); " << std::endl;

          isExported.insert(name);
        }
    }

  return true;
}