void Module::SetAssignmentFor(Model* sbmlmod, const Variable* var)
{
  char cc = g_registry.GetCC();
  formula_type ftype = var->GetFormulaType();
  const Formula* formula = var->GetFormula();
  if (!formula->IsEmpty()) {
    ASTNode* math = parseStringToASTNode(formula->ToSBMLString());
    if (ftype == formulaASSIGNMENT) {
      AssignmentRule* ar = sbmlmod->createAssignmentRule();
      ar->setVariable(var->GetNameDelimitedBy(cc));
      ar->setMath(math);
    }
    else if (!formula->IsDouble() &&
             !(IsSpecies(var->GetType()) && formula->IsAmountIn(var->GetCompartment()))) {
      //if it was a double or a species with an amount, we already dealt with it.  Otherwise:
      InitialAssignment* ia = sbmlmod->createInitialAssignment();
      ia->setSymbol(var->GetNameDelimitedBy(cc));
      ia->setMath(math);
    }
    delete math;
  }
  if (ftype == formulaRATE) {
    formula = var->GetRateRule();
    if (!formula->IsEmpty()) {
      ASTNode* math = parseStringToASTNode(var->GetRateRule()->ToSBMLString());
      RateRule* rr = sbmlmod->createRateRule();
      rr->setVariable(var->GetNameDelimitedBy(cc));
      rr->setMath(math);
      delete math;
    }
  }
}
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;
}
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());
        }
      }
    }
  }
}