END_TEST

START_TEST ( test_AssignmentRule )
{
  AssignmentRule* r = new AssignmentRule(2, 4);
  
  fail_unless (!(r->hasRequiredAttributes()));

  r->setVariable("r");

  fail_unless (r->hasRequiredAttributes());

  delete r;
}
END_TEST


START_TEST ( test_AssignmentRule_parent_create )
{
    Model *m = new Model(2, 4);
    AssignmentRule *r = m->createAssignmentRule();

    ListOf *lo = m->getListOfRules();

    fail_unless(lo == m->getRule(0)->getParentSBMLObject());
    fail_unless(lo == r->getParentSBMLObject());
    fail_unless(m == lo->getParentSBMLObject());
}
END_TEST

START_TEST ( test_AssignmentRule )
{
  AssignmentRule* r = new AssignmentRule(2, 4);
  
  fail_unless (!(r->hasRequiredElements()));

  r->setMath(SBML_parseFormula("ar"));

  fail_unless (r->hasRequiredElements());

  delete r;
}
bool AssignmentRuleEvaluator::visit(const AssignmentRule& x)
{
    cout << __FUNC__ << ", id: " << x.getId() << "\n";


    return true;
}
void
Model::convertStoichiometryMath()
{
  unsigned int n, j;
  Reaction * r;
  SpeciesReference *sr;
  unsigned int idCount = 0;
  char newid[15];
  std::string id;

  for (n = 0; n < getNumReactions(); n++)
  {
    r = getReaction(n);
    for (j = 0; j < r->getNumReactants(); j++)
    {
      sr = r->getReactant(j);
      if (sr->isSetStoichiometryMath())
      {
        if (!sr->isSetId())
        {
          sprintf(newid, "generatedId_%u", idCount);
          id.assign(newid);
          sr->setId(id);
          idCount++;
        }
        else
        {
          id = sr->getId();
        }
        sr->setConstant(false);

        AssignmentRule * ar = createAssignmentRule();
        ar->setVariable(id);
        if (sr->getStoichiometryMath()->isSetMath())
        {
          ar->setMath(sr->getStoichiometryMath()->getMath());
        }
      }
    }
    for (j = 0; j < r->getNumProducts(); j++)
    {
      sr = r->getProduct(j);
      if (sr->isSetStoichiometryMath())
      {
        if (!sr->isSetId())
        {
          sprintf(newid, "generatedId_%u", idCount);
          id.assign(newid);
          sr->setId(id);
          idCount++;
        }
        else
        {
          id = sr->getId();
        }

        sr->setConstant(false);
        AssignmentRule * ar = createAssignmentRule();
        ar->setVariable(id);
        if (sr->getStoichiometryMath()->isSetMath())
        {
          ar->setMath(sr->getStoichiometryMath()->getMath());
        }
      }
    }
  }
}
Beispiel #6
0
void SBML_sim::loadSBML(SBMLDocument * doc)
{
	if (!doc || doc->getNumErrors() > 0)
	{
	}
	else
	{
		Model * model = doc->getModel();
		ListOfParameters * params = model->getListOfParameters();
		ListOfReactions * reacs = model->getListOfReactions();
		ListOfSpecies * species = model->getListOfSpecies();
		ListOfSpeciesTypes * types = model->getListOfSpeciesTypes();
		ListOfEvents * events = model->getListOfEvents();
		ListOfRules * rules = model->getListOfRules();

		vector<string> assignmentEquations, rateEquations, eventTriggers;
		vector< vector<string> > eventResponses;

		if (events)
			for (int i=0; i < events->size(); ++i)
			{
				Event * e = events->get(i);
				eventTriggers.push_back( SBML_formulaToString( e->getTrigger()->getMath() ) );
				ListOfEventAssignments * eventAssn = e->getListOfEventAssignments();
				vector<string> responses;
				string s;
				for (int j=0; j < eventAssn->size(); ++j)
				{
					s = eventAssn->get(j)->getVariable();
					s.append("=");
					s.append( SBML_formulaToString( eventAssn->get(j)->getMath() ) );
					responses.push_back(s);
				}

				eventResponses.push_back( responses );
			}

		if (rules)
			for (int i=0; i < rules->size(); ++i)
			{
				Rule * r = rules->get(i);
			
				if (r->isAssignment())
				{
					AssignmentRule * ar  = (AssignmentRule*)r;
					assignmentVariables.push_back(ar->getVariable());
					assignmentValues.push_back(1.0);
					assignmentEquations.push_back(ar->getFormula());
				}
			}

		if (species)
			for (int i=0; i < species->size(); ++i)
				if (!species->get(i)->getConstant() && !species->get(i)->getBoundaryCondition())
				{
					variableNames.push_back(species->get(i)->getId());
					if (species->get(i)->isSetInitialAmount())
						variableValues.push_back(species->get(i)->getInitialAmount());
					else
					if (species->get(i)->isSetInitialConcentration())
						variableValues.push_back(species->get(i)->getInitialConcentration());
					else
						variableValues.push_back(0.0);
				}
				else
				{
					parameterNames.push_back(species->get(i)->getId());
					if (species->get(i)->isSetInitialAmount())
						parameterValues.push_back(species->get(i)->getInitialAmount());
					else
					if (species->get(i)->isSetInitialConcentration())
						parameterValues.push_back(species->get(i)->getInitialConcentration());
					else
						parameterValues.push_back(0.0);
				}

		if (params)
			for (int i=0; i < params->size(); ++i)
			{
				parameterNames.push_back(params->get(i)->getId());
				parameterValues.push_back(params->get(i)->getValue());
			}

		int numReacs = 0;
		
		if (reacs)
			numReacs = reacs->size();

		stoichiometryMatrix = new double[ numReacs * variableNames.size() ];

		for (int i=0; i < numReacs; ++i)
		{
			Reaction * r = reacs->get(i);
			reactionNames.push_back(r->getId());
			rateEquations.push_back(r->getKineticLaw()->getFormula());
			ListOfSpeciesReferences * reactants = r->getListOfReactants(),
									* products  = r->getListOfProducts();

			for (int j=0; j < variableNames.size(); ++j)
			{
				stoichiometryMatrix[ j*numReacs + i ] = 0.0;

				for (int k=0; k < reactants->size(); ++k)
					if (reactants->get(k) && reactants->get(k)->getSpecies() == variableNames[j])
						stoichiometryMatrix[ j*numReacs + i ] -= 1.0;
						//stoichiometryMatrix[ j*numReacs + i ] -= SpeciesReference_getStoichiometry(reactants->get(k));
					
				for (int k=0; k < products->size(); ++k)
					if (products->get(k) && products->get(k)->getSpecies() == variableNames[j])
						stoichiometryMatrix[ j*numReacs + i ] += 1.0;
						//stoichiometryMatrix[ j*numReacs + i ] += SpeciesReference_getStoichiometry(reactants->get(k));
			}
		}
		
		for (int i=0; i < rateEquations.size(); ++i)
		{
			mu::Parser p;
			addSBMLFunctions(p);
			p.SetExpr(rateEquations[i]);

			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar("time",&(this->time));

			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar("Time",&(this->time));
			
			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar(variableNames[j],&variableValues[j]);

			for (int j=0; j < parameterNames.size(); ++j)
				p.DefineVar(parameterNames[j],&parameterValues[j]);
			
			for (int j=0; j < assignmentVariables.size(); ++j)
				p.DefineVar(assignmentVariables[j],&assignmentValues[j]);

			p.SetVarFactory(muparser_add_variable, (void*)this);

			try
			{
				p.Eval();
				rateEqns.push_back(p);
			}
			catch(...)
			{
				//reactionNames.clear();
				//rateEqns.clear();
				break;
			}
		}
		
		for (int i=0; i < assignmentEquations.size(); ++i)
		{
			mu::Parser p;
			addSBMLFunctions(p);
			p.SetExpr(assignmentEquations[i]);
			
			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar(variableNames[j],&variableValues[j]);

			for (int j=0; j < parameterNames.size(); ++j)
				p.DefineVar(parameterNames[j],&parameterValues[j]);
			
			for (int j=0; j < assignmentVariables.size(); ++j)
				p.DefineVar(assignmentVariables[j],&assignmentValues[j]);

			//p.SetVarFactory(muparser_add_variable, (void*)this);

			try
			{
				p.Eval();
				assignmentEqns.push_back(p);
			}
			catch(...)
			{
				std::cout << assignmentEquations[i] << std::endl;
				//assignmentVariables.clear();
				//assignmentEqns.clear();
				break;
			}
		}

		for (int i=0; i < eventTriggers.size(); ++i)
		{
			mu::Parser p;
			addSBMLFunctions(p);
			p.SetExpr(eventTriggers[i]);

			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar("time",&(this->time));

			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar("Time",&(this->time));
			
			for (int j=0; j < variableNames.size(); ++j)
				p.DefineVar(variableNames[j],&variableValues[j]);

			for (int j=0; j < parameterNames.size(); ++j)
				p.DefineVar(parameterNames[j],&parameterValues[j]);
			
			for (int j=0; j < assignmentVariables.size(); ++j)
				p.DefineVar(assignmentVariables[j],&assignmentValues[j]);

			try
			{
				p.Eval();
				
				//resposes for the trigger
				vector<mu::Parser> responses;
				for (int j=0; j < eventResponses[i].size(); ++j)
				{
					mu::Parser p;
					addSBMLFunctions(p);
					p.SetExpr(eventResponses[i][j]);

					try
					{
						p.Eval();
						responses.push_back(p);
					}
					catch(...) {}
				}

				if (responses.size() > 0)
				{
					triggerEqns.push_back(p);
					responseEqns.push_back(responses);
				}
			}
			catch(...)
			{
				//assignmentVariables.clear();
				//assignmentEqns.clear();
				break;
			}
		}

		//delete params;
		//delete reacs;
	}
}
Beispiel #7
0
void test000009::test_references_to_species()
{
  // load the CPS file
  // export to SBML
  // check the resulting SBML model
  CCopasiDataModel* pDataModel = pCOPASIDATAMODEL;
  std::istringstream iss(test000009::MODEL_STRING);
  CPPUNIT_ASSERT(load_cps_model_from_stream(iss, *pDataModel) == true);
  CPPUNIT_ASSERT(pDataModel->getModel() != NULL);
  CPPUNIT_ASSERT(pDataModel->exportSBMLToString(NULL, 2, 3).empty() == false);
  SBMLDocument* pDocument = pDataModel->getCurrentSBMLDocument();
  CPPUNIT_ASSERT(pDocument != NULL);
  Model* pModel = pDocument->getModel();
  CPPUNIT_ASSERT(pModel != NULL);
  // assert that there is only one compartment and
  // assert the compartment is constant
  CPPUNIT_ASSERT(pModel->getNumCompartments() == 1);
  Compartment* pCompartment = pModel->getCompartment(0);
  CPPUNIT_ASSERT(pCompartment->getConstant() == false);
  CPPUNIT_ASSERT(pModel->getNumSpecies() == 2);
  Species* pSpecies = pModel->getSpecies(1);
  CPPUNIT_ASSERT(pSpecies->getHasOnlySubstanceUnits() == true);
  pSpecies = pModel->getSpecies(0);
  std::string idSpeciesA = pSpecies->getId();
  CPPUNIT_ASSERT(pSpecies->getHasOnlySubstanceUnits() == true);
  CPPUNIT_ASSERT(pModel->getNumRules() == 2);
  // there are two rules, one is the rule for the compartment
  AssignmentRule* pRule = dynamic_cast<AssignmentRule*>(pModel->getRule(0));
  CPPUNIT_ASSERT(pRule != NULL);
  CPPUNIT_ASSERT(pModel->getNumParameters() == 1);
  Parameter* pParameter = pModel->getParameter(0);
  CPPUNIT_ASSERT(pParameter != NULL);

  if (pRule->getVariable() != pParameter->getId())
    {
      pRule = dynamic_cast<AssignmentRule*>(pModel->getRule(1));
    }

  CPPUNIT_ASSERT(pRule->getVariable() == pParameter->getId());
  const ASTNode* pMath = pRule->getMath();
  CPPUNIT_ASSERT(pMath != NULL);
  // the expression should be the species divided by the volume
  CPPUNIT_ASSERT(pMath->getType() == AST_DIVIDE);
  CPPUNIT_ASSERT(pMath->getChild(0) != NULL);
  CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(0)->getName() == pSpecies->getId());
  CPPUNIT_ASSERT(pMath->getChild(1) != NULL);
  CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(1)->getName() == pCompartment->getId());
  CPPUNIT_ASSERT(pModel->getNumReactions() == 2);
  Reaction* pReaction = pModel->getReaction(0);
  // make sure this is reaction A ->
  CPPUNIT_ASSERT(pReaction != NULL);
  CPPUNIT_ASSERT(pReaction->getNumReactants() == 1);
  CPPUNIT_ASSERT(pReaction->getNumProducts() == 0);
  // check if all references in the kinetic law are unmodified
  // math element must be a multiplication of the mass action term by
  // the compartment volume
  // the mass action term is a multiplication of the parameter node by
  // the species node
  // the code that multiplies the reaction by the compartments volume
  // recognizes the division of the species by the compartment and cancels
  // those two
  CPPUNIT_ASSERT(pReaction->isSetKineticLaw() == true);
  KineticLaw* pLaw = pReaction->getKineticLaw();
  CPPUNIT_ASSERT(pLaw != NULL);
  CPPUNIT_ASSERT(pLaw->isSetMath() == true);
  pMath = pLaw->getMath();
  CPPUNIT_ASSERT(pMath->getType() == AST_TIMES);
  CPPUNIT_ASSERT(pMath->getNumChildren() == 2);
  CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(0)->getName() == std::string("k1"));
  CPPUNIT_ASSERT(pMath->getChild(1) != NULL);
  CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(1)->getName() == idSpeciesA);

  pReaction = pModel->getReaction(1);
  // make sure this is reaction A -> S
  CPPUNIT_ASSERT(pReaction != NULL);
  CPPUNIT_ASSERT(pReaction->getNumReactants() == 1);
  CPPUNIT_ASSERT(pReaction->getNumProducts() == 1);
  // check if all references in the kinetic law are unmodified
  // math element must be a multiplication of the compartments volume with
  // a function call with three arguments
  // the first argument is the reference to the species
  CPPUNIT_ASSERT(pReaction->isSetKineticLaw() == true);
  pLaw = pReaction->getKineticLaw();
  CPPUNIT_ASSERT(pLaw != NULL);
  CPPUNIT_ASSERT(pLaw->isSetMath() == true);
  pMath = pLaw->getMath();
  CPPUNIT_ASSERT(pMath->getType() == AST_TIMES);
  CPPUNIT_ASSERT(pMath->getNumChildren() == 2);
  CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(0)->getName() == pCompartment->getId());
  pMath = pMath->getChild(1);
  CPPUNIT_ASSERT(pMath != NULL);
  CPPUNIT_ASSERT(pMath->getType() == AST_FUNCTION);
  CPPUNIT_ASSERT(pMath->getNumChildren() == 3);
  pMath = pMath->getChild(0);
  CPPUNIT_ASSERT(pMath != NULL);
  CPPUNIT_ASSERT(pMath->getType() == AST_DIVIDE);
  CPPUNIT_ASSERT(pMath->getNumChildren() == 2);
  CPPUNIT_ASSERT(pMath->getChild(0) != NULL);
  CPPUNIT_ASSERT(pMath->getChild(0)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(0)->getName() == idSpeciesA);
  CPPUNIT_ASSERT(pMath->getChild(1) != NULL);
  CPPUNIT_ASSERT(pMath->getChild(1)->getType() == AST_NAME);
  CPPUNIT_ASSERT(pMath->getChild(1)->getName() == pCompartment->getId());
}