void ReactionsWidget1::slotNewFunction() { // FunctionWidget1 * fw = new FunctionWidget1(NULL); // fw->show(); // TODO: we could think about calling the function widget as a dialogue here... std::string name = std::string("Rate Law for ") + mpObject->getObjectName(); std::string nname = name; size_t i = 0; CCopasiVectorN<CFunction>& FunctionList = CCopasiRootContainer::getFunctionList()->loadedFunctions(); CFunction* pFunc; while (FunctionList.getIndex(nname) != C_INVALID_INDEX) { i++; nname = name + "_"; nname += TO_UTF8(QString::number(i)); } CCopasiRootContainer::getFunctionList()->add(pFunc = new CKinFunction(nname), true); protectedNotify(ListViews::FUNCTION, ListViews::ADD, pFunc->getKey()); mpListView->switchToOtherWidget(C_INVALID_INDEX, pFunc->getKey()); }
CFunction* CGeneratorContext::AddFunction(const char* name) { CFunction* function = new CFunction(); function->SetName(name); mFunctions.push_back(function); return function; }
void CInsertFunctionDialog::FillFunctionList( int category ) { mFunctionList->clear(); //NamesToListBox( CMapFunction::GetInstance(), *GetFunctionList(), category ); auto &mapf = CMapFunction::GetInstance(); for ( CMapFunction::const_iterator it = mapf.begin(); it != mapf.end(); it++ ) { CFunction* value = dynamic_cast< CFunction* >( it->second ); if ( value != nullptr ) { if ( category != -1 ) { if ( value->GetCategory() != category ) { continue; } } QListWidgetItem *item = new QListWidgetItem( value->GetName().c_str(), mFunctionList ); Q_UNUSED( item ); } } //int sel = 0; //GetFunctionList()->SetSelection( sel ); //FunctionList( sel ); mFunctionList->setCurrentRow( 0 ); }
CFunction * add_c_override(Module &mod, c_function f , const std::string &protomod, const std::string &protoname , const std::string &name, uint8_t argc , const std::string ¶m_types) { CFunction *cfunc = add_c_function(mod, f, name, argc, param_types); cfunc->set_protocol(protomod, protoname); }
bool FunctionWidget1::functionParametersChanged() { CFunction* func = dynamic_cast<CFunction*>(CCopasiRootContainer::getKeyFactory()->get(mKey)); if (!func) return false; return (!(func->getVariables() == mpFunction->getVariables())); }
C_INT32 CFunctionDB::load(CReadConfig &configbuffer) { CFunction Function; CFunction * pFunction = NULL; C_INT32 Size = 0; C_INT32 Fail = 0; configbuffer.getVariable("TotalUDKinetics", "C_INT32", &Size, CReadConfig::LOOP); for (C_INT32 i = 0; i < Size; i++) { Function.load(configbuffer); switch (Function.getType()) { case CEvaluationTree::Function: pFunction = new CFunction(Function); break; case CEvaluationTree::MassAction: pFunction = new CMassAction(Function); break; case CEvaluationTree::PreDefined: case CEvaluationTree::UserDefined: pFunction = new CKinFunction(Function, &configbuffer); break; default: fatalError(); break; } pFunction->compile(); if (!mLoadedFunctions.add(pFunction, true)) { pdelete(pFunction); // We ignore: // CCopasiVector (2): Object '%s' allready exists. if ((MCCopasiVector + 2) != CCopasiMessage::peekLastMessage().getNumber()) return Fail = 1; // Remove the ignored meesage. CCopasiMessage::getLastMessage(); } } return Fail; }
/* * Manual page at process.def */ INT32 CGEN_PUBLIC CProcess::Start() { const SMic* pMic = NULL; // Method invocation context of Start() CFunction* iCaller = NULL; // Function calling Start() CFunction* iFnc = NULL; // Process function StkItm* pStkItm = NULL; // Stack item INT32 nArgs = 0; // Number of process function arguments // Validate and initialize // ------------------------------------ if (m_nState!=0) // Not virginal return IERROR(this,PRC_CANTSTART,"multiple starts not allowed",0,0); // Forget it! if (!(pMic = CDlpObject_MicGet(_this))) return -1; // Get method invocation context iCaller = (CFunction*)CDlpObject_OfKind("function",pMic->iCaller); // Get calling CFunction if (!iCaller) return -1; // Must be a function! // Initialize process // ------------------------------------ sprintf(m_psTmpFile,"%s%ld",dlp_tempnam(NULL,"~dLabPro#process#"),(long)dlp_time());// Initialize temp. file name prefix // Marshal arguments // ------------------------------------ if (!(pStkItm=iCaller->StackGet(0))) return IERROR(this,PRC_TOOFEWARGS,0,0,0);// Get stack top if (pStkItm->nType==T_INSTANCE) // Stack top is an instance iFnc = (CFunction*)CDlpObject_OfKind("function",pStkItm->val.i); // Get function to be called if (iFnc) // This process is a function call { // >> IFIELD_RESET(CDlpObject,"dto"); // Create data transfer object nArgs = CData_GetNRecs(iFnc->m_idArg); // Get number of function arguments Marshal(m_iDto,iCaller,nArgs); // Marshal arguments for transfer } // << else // This process is a program call dlp_strcpy(m_psCmdLine,iCaller->PopString(0)); // Get program command line #ifdef USE_FORK if (iFnc) // This process is a function call { // >> m_hPid=fork(); // Fork the process if(m_hPid>0){ // Parent process >> m_nState |= PRC_DATASENT; // Remember data have been sent m_nState |= PRC_RUNNING; // Set running flag m_hThread = 0; // Clear thread handle return O_K; // Everything is fine } // << if(m_hPid==0) return DoJobFork(iCaller,iFnc); // The child process runs the function return IERROR(this,PRC_CANTSTART,"fork() failed",0,0); // On error (fid<0) we return } // << #endif // Start job in watcher thread // ------------------------------------ m_hPid = 0; // Reset process id SendData(); // Send transfer data m_hThread = dlp_create_thread(DoJob,this); // Do the job and watch it return O_K; // Yo! }
CFunction * CFunction::createCopy() const { CFunction* newFunction = new CFunction(); //newFunction->mVariables = this->mVariables; //WRONG! only shallow copy!! newFunction->mReversible = this->mReversible; if (this->mpRoot) newFunction->setRoot(this->mpRoot->copyBranch()); //newFunction->mInfix = newFunction->mpRoot->getInfix(); return newFunction; }
bool CQFunctionDM::removeRows(QModelIndexList rows, const QModelIndex&) { if (rows.isEmpty()) return false; assert(mpDataModel != NULL); CModel * pModel = mpDataModel->getModel(); if (pModel == NULL) return false; //Build the list of pointers to items to be deleted //before actually deleting any item. QList <CEvaluationTree *> pFunctions; CFunction * pFunction; QModelIndexList::const_iterator i; for (i = rows.begin(); i != rows.end(); ++i) { if (!isDefaultRow(*i) && (pFunction = &CRootContainer::getFunctionList()->loadedFunctions()[i->row()]) != NULL && !pFunction->isReadOnly()) pFunctions.append(&CRootContainer::getFunctionList()->loadedFunctions()[i->row()]); } QList <CEvaluationTree *>::const_iterator j; for (j = pFunctions.begin(); j != pFunctions.end(); ++j) { CEvaluationTree * pFunction = *j; size_t delRow = CRootContainer::getFunctionList()->loadedFunctions().CDataVector< CFunction >::getIndex(pFunction); if (delRow != C_INVALID_INDEX) { QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(NULL, "function", FROM_UTF8(pFunction->getObjectName()), pFunction); if (choice == QMessageBox::Ok) removeRow((int) delRow); } } return true; }
bool FunctionWidget1::saveToFunction() { CFunction* func = dynamic_cast<CFunction*>(CCopasiRootContainer::getKeyFactory()->get(mKey)); if (!func) return false; //radio buttons TriLogic tmpl; if (RadioButton1->isChecked() == true) { tmpl = TriTrue; } else if (RadioButton2->isChecked() == true) { tmpl = TriFalse; } else { tmpl = TriUnspecified; } if (tmpl != func->isReversible()) { func->setReversible(tmpl); flagChanged = true; } if (flagChanged) { copyFunctionContentsToFunction(mpFunction, func); protectedNotify(ListViews::FUNCTION, ListViews::CHANGE, mKey); if (mpDataModel != NULL) { mpDataModel->changed(); } flagChanged = false; } func->compile(); return true; }
std::vector<CFunction*> CFunctionDB::suitableFunctions(const unsigned C_INT32 noSubstrates, const unsigned C_INT32 noProducts, const TriLogic reversibility) { std::vector<CFunction*> ret; CFunction *pFunction; unsigned C_INT32 i, imax = mLoadedFunctions.size(); for (i = 0; i < imax; i++) { pFunction = dynamic_cast<CFunction *>(mLoadedFunctions[i]); if (!pFunction) continue; if (pFunction->isSuitable(noSubstrates, noProducts, reversibility)) ret.push_back(pFunction); } //always add constant flux it is is missing if (reversibility == TriTrue) { if ((noSubstrates > 0) || (noProducts > 0)) //constant flux was not yet added { pFunction = dynamic_cast<CFunction*>(findFunction("Constant flux (reversible)")); if (!pFunction) fatalError(); ret.push_back(pFunction); } } else //irreversible { if (noSubstrates > 0) //constant flux was not yet added { pFunction = dynamic_cast<CFunction*>(findFunction("Constant flux (irreversible)")); if (!pFunction) fatalError(); ret.push_back(pFunction); } } return ret; }
bool CQFunctionDM::insertRows(int position, int rows, const QModelIndex & parent) { beginInsertRows(parent, position, position + rows - 1); for (int row = 0; row < rows; ++row) { CFunction *pFunc; QString Name = createNewName(mNewName, COL_NAME_FUNCTIONS); CRootContainer::getFunctionList()->add(pFunc = new CKinFunction(TO_UTF8(Name)), true); emit notifyGUI(ListViews::ObjectType::FUNCTION, ListViews::ADD, pFunc->getCN()); } endInsertRows(); mNewName = "function"; return true; }
//! Slot for being activated wehenver New button is clicked void FunctionWidget1::slotBtnNew() { std::string name = "function_1"; int i = 1; CFunction* pFunc; CCopasiVectorN<CFunction>& FunctionList = CCopasiRootContainer::getFunctionList()->loadedFunctions(); while (FunctionList.getIndex(name) != C_INVALID_INDEX) { i++; name = "function_"; name += TO_UTF8(QString::number(i)); } CCopasiRootContainer::getFunctionList()->add(pFunc = new CKinFunction(name), true); std::string key = pFunc->getKey(); protectedNotify(ListViews::FUNCTION, ListViews::ADD, key); // enter(key); mpListView->switchToOtherWidget(C_INVALID_INDEX, key); }
/* * Manual page at process.def */ INT16 CGEN_PUBLIC CProcess::MarshalRetval() { CDlpObject* iDto = NULL; // Data transfer object const SMic* pMic = NULL; // Current method invocation context CFunction* iCaller = NULL; // Calling function StkItm* pStkItm = NULL; // Stack item to marshal // Validate and initialize // ------------------------------------ if (!(pMic = CDlpObject_MicGet(_this))) return -1; // Get method invocation context iCaller = (CFunction*)CDlpObject_OfKind("function",pMic->iCaller); // Get calling CFunction if (!iCaller) return -1; // Must be a function! // Pack second stack element into the CDlpObject on the stack top // ------------------------------------ iDto = iCaller->PopInstance(0); // Get instance on stack top if (!iDto) return NOT_EXEC; // No instance on stack top --> puuh! if (iCaller->StackGetLength()==0) return O_K; // Nothing more on the stack --> ok pStkItm = iCaller->StackGet(0); // Get stack top Pack(iDto,NULL,pStkItm,PRC_S_RETV); // Pack it as "~iRetv" iCaller->Pop(0); // Remove stack top return O_K; // Ok }
ASTNode* CEvaluationNodeCall::toAST(const CCopasiDataModel* pDataModel) const { ASTNode* pNode = NULL; pNode = new ASTNode(AST_FUNCTION); const std::string funName = this->getData(); CFunction * pFun = CCopasiRootContainer::getFunctionList()->findFunction(funName); assert(pFun != NULL); if (pFun == NULL || pFun->getSBMLId().empty()) fatalError(); pNode->setName(pFun->getSBMLId().c_str()); const CEvaluationNode* child = static_cast<const CEvaluationNode*>(this->getChild()); while (child) { pNode->addChild(child->toAST(pDataModel)); child = static_cast<const CEvaluationNode*>(child->getSibling()); } return pNode; }
void Map::checkFunction(CFunction& function) const throw(InvalidProcessException) { if (function.getInputParameters().size() == 1) { if (function.getReturnDataType()->getFunctionReturnDataTypeString() == "void") { THROW_EXCEPTION(InvalidProcessException, string("Leaf \"") + getId()->getString() + "\" of type \"" + type() + "\": function arguments with one input " "parameter must return data (i.e. have return " "data type other than \"void\")"); } if (function.getReturnDataType()->isArray()) { THROW_EXCEPTION(InvalidProcessException, string("Leaf \"") + getId()->getString() + "\" of type \"" + type() + "\": return type of function arguments " "with one input parameter must not be an array"); } } else if (function.getInputParameters().size() == 2) { if (function.getReturnDataType()->getFunctionReturnDataTypeString() != "void") { THROW_EXCEPTION(InvalidProcessException, string("Leaf \"") + getId()->getString() + "\" of type \"" + type() + "\": function arguments with two input " "parameters must not return data (i.e. have return " "data type \"void\")"); } } else { THROW_EXCEPTION(InvalidProcessException, string("Leaf \"") + getId()->getString() + "\" of type \"" + type() + "\" must have a function argument with " "one or two input parameters"); } CDataType first_input_data_type = *function.getInputParameters().front()->getDataType(); if (first_input_data_type.isArray() && !first_input_data_type.isConst()) { THROW_EXCEPTION(InvalidProcessException, string("Leaf \"") + getId()->getString() + "\" of type \"" + type() + "\": first input parameter is a " "reference or array but not declared const"); } }
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(); }
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; }
void GraphmlParser::findFunctionArraySizes(CFunction& function, ticpp::Element* xml) throw(InvalidArgumentException, ParseException, IOException, RuntimeException) { if (!xml) { THROW_EXCEPTION(InvalidArgumentException, "\"xml\" must not be NULL"); } list<Element*> elements = getElementsByName(xml, "port"); // If return data type or last input parameter is an array, find the array // size by analyzing the out port XML elements CDataType* output_data_type = NULL; if (function.getReturnDataType()->isArray()) { output_data_type = function.getReturnDataType(); logger_.logMessage(Logger::DEBUG, "Searching array size for return " "data type..."); } else if (function.getNumInputParameters() > 1) { output_data_type = function.getInputParameters().back()->getDataType(); // Reset to NULL if the parameter is not what we are looking for if (output_data_type->isArray()) { logger_.logMessage(Logger::DEBUG, "Searching array size for second " "input parameter data type..."); } else { output_data_type = NULL; } } if (output_data_type) { list<Element*>::iterator it; for (it = elements.begin(); it != elements.end(); ++it) { logger_.logMessage(Logger::DEBUG, string("Analyzing line " + tools::toString((*it)->Row()) + "...")); string port_name = getName(*it); if (isOutPort(port_name)) { size_t array_size = findArraySize(*it); if (array_size > 0) { logger_.logMessage(Logger::DEBUG, string("Found array size ") + tools::toString(array_size)); output_data_type->setArraySize(array_size); } break; } } } // Find array sizes for the input parameters which are arrays by analyzing // the in port XML elements list<CVariable*> parameters = function.getInputParameters(); list<CVariable*>::iterator param_it = parameters.begin(); list<CVariable*>::iterator param_stop_point; list<Element*>::iterator xml_it = elements.begin(); if (function.getNumInputParameters() > 1) { param_stop_point = --parameters.end(); } else { param_stop_point = parameters.end(); } while (param_it != param_stop_point && xml_it != elements.end()) { if (param_it == parameters.begin()) { logger_.logMessage(Logger::DEBUG, "Searching array size for " "input parameter data type..."); } logger_.logMessage(Logger::DEBUG, string("Analyzing line " + tools::toString((*xml_it)->Row()) + "...")); if (!isInPort(getName(*xml_it))) { logger_.logMessage(Logger::DEBUG, "Not an in port, moving to next"); ++xml_it; continue; } if ((*param_it)->getDataType()->isArray()) { size_t array_size = findArraySize(*xml_it); if (array_size > 0) { logger_.logMessage(Logger::DEBUG, string("Found array size ") + tools::toString(array_size)); (*param_it)->getDataType()->setArraySize(array_size); } else { logger_.logMessage(Logger::DEBUG, "No array size key"); } } ++param_it; ++xml_it; } }
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; }
bool CQRDFTreeView::enterProtected() { clear(); CCopasiObject *pObject = dynamic_cast< CCopasiObject * >(CCopasiRootContainer::getKeyFactory()->get(mKey)); if (pObject != NULL) { CModelEntity * pEntity = NULL; CEvent * pEvent = NULL; CReaction * pReaction = NULL; CFunction * pFunction = NULL; const std::string * pMiriamAnnotation = NULL; if ((pEntity = dynamic_cast< CModelEntity * >(pObject)) != NULL) pMiriamAnnotation = &pEntity->getMiriamAnnotation(); else if ((pEvent = dynamic_cast< CEvent * >(pObject)) != NULL) pMiriamAnnotation = &pEvent->getMiriamAnnotation(); else if ((pReaction = dynamic_cast< CReaction * >(pObject)) != NULL) pMiriamAnnotation = &pReaction->getMiriamAnnotation(); else if ((pFunction = dynamic_cast< CFunction * >(pObject)) != NULL) pMiriamAnnotation = &pFunction->getMiriamAnnotation(); if (pMiriamAnnotation && *pMiriamAnnotation != "") mpGraph = CRDFParser::graphFromXml(*pMiriamAnnotation); } CCopasiMessage::clearDeque(); if (CCopasiMessage::size() != 0) { QString Message = FROM_UTF8(CCopasiMessage::getAllMessageText()); CQMessageBox::warning(this, QString("RDF Warning"), Message, QMessageBox::Ok, QMessageBox::Ok); } if (mpGraph == NULL) mpGraph = new CRDFGraph; // We make sure that we always have an about node. mpGraph->createAboutNode(mKey); // We iterate of all triplets std::set< CRDFTriplet >::const_iterator it = mpGraph->getTriplets().begin(); std::set< CRDFTriplet >::const_iterator end = mpGraph->getTriplets().end(); for (; it != end; ++it) { CQRDFTreeViewItem * pSubjectItem = find(it->pSubject); if (pSubjectItem == NULL) { pSubjectItem = new CQRDFTreeViewItem(mpTreeWidget, NULL); insert(it->pSubject, pSubjectItem); // Display the subject information const CRDFSubject & Subject = it->pSubject->getSubject(); switch (Subject.getType()) { case CRDFSubject::RESOURCE: pSubjectItem->setText(COL_SUBJECT, FROM_UTF8(Subject.getResource())); break; case CRDFSubject::BLANK_NODE: pSubjectItem->setText(COL_SUBJECT, FROM_UTF8(Subject.getBlankNodeID())); break; } } CQRDFTreeViewItem * pObjectItem = NULL; if (it->Predicate.getURI() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#subject") { pObjectItem = new CQRDFTreeViewItem(pSubjectItem, NULL); insert(it->pObject, pObjectItem); } else pObjectItem = find(it->pObject); if (pObjectItem == NULL) { pObjectItem = new CQRDFTreeViewItem(pSubjectItem, NULL); insert(it->pObject, pObjectItem); } else { QTreeWidgetItem * pParent = pObjectItem->parent(); if (pParent == NULL) { mpTreeWidget->invisibleRootItem()->removeChild(pObjectItem); pSubjectItem->addChild(pObjectItem); } else { pParent->removeChild(pObjectItem); pSubjectItem->addChild(pObjectItem); } } pObjectItem->setTriplet(*it); } mpTreeWidget->setFocus(); return true; }
// Wrap the function to allow creating an CasADi function void dae_res_c_wrapper(CFunction &f, int nfwd, int nadj, void* user_data){ casadi_assert(nfwd==0 && nadj==0); dae_res_c(f.input(DAE_T).front(), &f.input(DAE_X).front(), &f.input(DAE_XDOT).front(), &f.input(DAE_P).front(), &f.output(DAE_ODE).front(), &f.output(DAE_QUAD).front()); }
int main() { // The number that the variable "x" will point to double_complex x; // Creates a variable named "x" and which value will be x CVar xvar ( "x" , &x ); // Asks for a fomula depending on the variable x, e.g. "sin 2x" char s[500]=""; printf("Enter a formula depending on the variable x:\n"); gets(s); // Creates an operation with this formula. The operation depends on one // variable, which is xvar ; the third argument is an array of pointers // to variables; the previous argument is its size CVar* vararray[1]; vararray[0]=&xvar; COperation op ( s, 1, vararray ); // Affects (indirectly) a value to xvar x=3; // Printfs the value of the formula for x=3; printf("%s = %s for x=3\n\n", op.Expr(), PrettyPrint(op.Val()) ); // Creates a function name which can be used in later functions to refer to the operation op(x) CFunction f (op, &xvar); f.SetName("f"); // Creates a second variable named y, and a formula depending on both x and y double_complex y; CVar yvar ( "y" , &y ); CVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar vararray2[0]=&xvar; vararray2[1]=&yvar; // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y) printf("Enter a formula using x, y and the function f(x): x -> %s that you just entered, e.g. x+f(3y) :\n", op.Expr()); gets(s); CFunction* funcarray[1]; funcarray[0]=&f; COperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray ); // vararray2 is a CVar* array with two elements // funcarray is a CFunction* array with one element y=5; printf("Value for x=3, y=5 : %s\n", PrettyPrint(op2.Val()) ); // Turns the last expression into a function of x and y CFunction g(op2, 2, vararray2); g.SetName("g"); // Here is another way to do it double_complex z,t; CVar zvar("z", &z), tvar("t", &t); COperation op3,zop,top; zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( ) // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2 z=5;t=7; printf("\nLet g be the function g : x,y -> %s\n", op2.Expr()); printf("Value of %s for z=5,t=7:\n %s\n", op3.Expr(), PrettyPrint(op3.Val()) ); COperation dopdt=op3.Diff(tvar); // Computes the derivative of op3 w.r.t t printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdt.Expr(), PrettyPrint(dopdt.Val()) ); COperation dopdtbar=op3.DiffConj(tvar); // Computes the derivative of op3 w.r.t the conjugate of t printf("Value of d/dtbar (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdtbar.Expr(), PrettyPrint(dopdtbar.Val()) ); return 0; }