unsigned int 
RenderSBMLDocumentPlugin::checkConsistency()
{
  unsigned int nerrors = 0;
  unsigned int total_errors = 0;

  
  SBMLDocument* doc = static_cast<SBMLDocument *>(this->getParentSBMLObject());
  SBMLErrorLog *log = doc->getErrorLog();

  unsigned char applicableValidators = doc->getApplicableValidators();

  /* determine which validators to run */
  bool id    = ((applicableValidators & 0x01) == 0x01);
  bool sbml  = ((applicableValidators & 0x02) == 0x02);
  /* LIST OTHERS HERE */

  RenderIdentifierConsistencyValidator id_validator;
  RenderConsistencyValidator validator;
  /* LIST OTHERS HERE */

  if (id)
  {
    id_validator.init();
    nerrors = id_validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add(id_validator.getFailures() );
      /* only want to bail if errors not warnings */
      if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      {
        return total_errors;
      }
    }
  }

  if (sbml)
  {
    validator.init();
    nerrors = validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add(validator.getFailures() );
	  // DO NOT NEED THIS IN LAST CALL
      // /* only want to bail if errors not warnings */
      // if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      // {
      //   return total_errors;
      // }
    }
  }

  /* ADD OTHERS HERE */


  return total_errors;  
}
int Registry::CheckAndAddSBMLIfGood(SBMLDocument* document)
{
  document->setConsistencyChecks(LIBSBML_CAT_UNITS_CONSISTENCY, false);
  document->checkConsistency();
  SBMLErrorLog* log = document->getErrorLog();
  if (log->getNumFailsWithSeverity(2) == 0 && log->getNumFailsWithSeverity(3) == 0) {
    //It's a valid SBML file.
    const Model* sbml = document->getModel();
    string sbmlname = getNameFromSBMLObject(sbml, "file");
    if (sbmlname != MAINMODULE) {
      while (NewCurrentModule(&sbmlname)) {
        //Duplicated module name
        sbmlname += "_";
      }
    }
    CurrentModule()->LoadSBML(document);
    if (sbmlname != MAINMODULE) {
      RevertToPreviousModule();
    }
    return 2;
  }
  return 0;
}
/*
 * Performs a set of semantic consistency checks on the document.  Query
 * the results by calling getNumErrors() and getError().
 *
 * @return the number of failed checks (errors) encountered.
 */
unsigned int
SBMLInternalValidator::checkConsistency (bool writeDocument)
{
  unsigned int nerrors = 0;
  unsigned int total_errors = 0;

  //if (getLevel() == 3)
  //{
  //  logError(L3NotSupported);
  //  return 1;
  //}
  /* determine which validators to run */
  bool id    = ((mApplicableValidators & 0x01) == 0x01);
  bool sbml  = ((mApplicableValidators & 0x02) == 0x02);
  bool sbo   = ((mApplicableValidators & 0x04) == 0x04);
  bool math  = ((mApplicableValidators & 0x08) == 0x08);
  bool units = ((mApplicableValidators & 0x10) == 0x10);
  bool over  = ((mApplicableValidators & 0x20) == 0x20);
  bool practice = ((mApplicableValidators & 0x40) == 0x40);

  /* taken the state machine concept out for now
  if (LibSBMLStateMachine::isActive()) 
  {
    units = LibSBMLStateMachine::getUnitState();
  }
  */

  IdentifierConsistencyValidator id_validator;
  ConsistencyValidator validator;
  SBOConsistencyValidator sbo_validator;
  MathMLConsistencyValidator math_validator;
  UnitConsistencyValidator unit_validator;
  OverdeterminedValidator over_validator;
  ModelingPracticeValidator practice_validator;

  SBMLDocument *doc;
  SBMLErrorLog *log = getErrorLog();
  
  if (writeDocument)
  {
    char* sbmlString = writeSBMLToString(getDocument());
    log->clearLog();
    doc = readSBMLFromString(sbmlString);
    free (sbmlString);  
  }
  else
  {
    doc = getDocument();
  }

  /* calls each specified validator in turn 
   * - stopping when errors are encountered */

  if (id)
  {
    id_validator.init();
    nerrors = id_validator.validate(*doc);
    if (nerrors > 0) 
    {
      unsigned int origNum = log->getNumErrors();
      log->add( id_validator.getFailures() );

      if (origNum > 0 && log->contains(InvalidUnitIdSyntax) == true)
      {
        /* do not log dangling ref */
        while (log->contains(DanglingUnitSIdRef) == true)
        {
          log->remove(DanglingUnitSIdRef);
          nerrors--;
        }
        
        total_errors += nerrors;
        if (nerrors > 0)
        {
          if (writeDocument)
            SBMLDocument_free(doc);
          return total_errors;
        }
      }
      else if (log->contains(DanglingUnitSIdRef) == false)
      {
        total_errors += nerrors;
        if (writeDocument)
          SBMLDocument_free(doc);
        return total_errors;
      }
      else
      {
        bool onlyDangRef = true;
        for (unsigned int a = 0; a < log->getNumErrors(); a++)
        {
          if (log->getError(a)->getErrorId() != DanglingUnitSIdRef)
          {
            onlyDangRef = false;
            break;
          }
        }
        total_errors += nerrors;

        if (onlyDangRef == false)
        {
          if (writeDocument)
            SBMLDocument_free(doc);
          return total_errors;
        }
      }
    }
  }

  if (sbml)
  {
    validator.init();
    nerrors = validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add( validator.getFailures() );
      /* only want to bail if errors not warnings */
      if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      {
        if (writeDocument)
          SBMLDocument_free(doc);
        return total_errors;
      }
    }
  }

  if (sbo)
  {
    sbo_validator.init();
    nerrors = sbo_validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add( sbo_validator.getFailures() );
      /* only want to bail if errors not warnings */
      if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      {
        if (writeDocument)
          SBMLDocument_free(doc);
        return total_errors;
      }
    }
  }

  if (math)
  {
    math_validator.init();
    nerrors = math_validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add( math_validator.getFailures() );
      /* at this point bail if any problems
       * unit checks may crash if there have been math errors/warnings
       */
      if (writeDocument)
        SBMLDocument_free(doc);
      return total_errors;
    }
  }


  if (units)
  {
    unit_validator.init();
    nerrors = unit_validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add( unit_validator.getFailures() );
      /* only want to bail if errors not warnings */
      if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      {
        if (writeDocument)
          SBMLDocument_free(doc);
        return total_errors;
      }
    }
  }

  /* do not even try if there have been unit warnings 
   * changed this as would have bailed */
  if (over)
  {
    over_validator.init();
    nerrors = over_validator.validate(*doc);
    total_errors += nerrors;
    if (nerrors > 0) 
    {
      log->add( over_validator.getFailures() );
      /* only want to bail if errors not warnings */
      if (log->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0)
      {
        if (writeDocument)
          SBMLDocument_free(doc);
        return total_errors;
      }
    }
  }

  if (practice)
  {
    practice_validator.init();
    nerrors = practice_validator.validate(*doc);
    if (nerrors > 0) 
    {
      unsigned int errorsAdded = 0;
      const std::list<SBMLError> practiceErrors = practice_validator.getFailures();
      list<SBMLError>::const_iterator end = practiceErrors.end();
      list<SBMLError>::const_iterator iter;
      for (iter = practiceErrors.begin(); iter != end; ++iter)
      {
        if (SBMLError(*iter).getErrorId() != 80701)
        {
          log->add( SBMLError(*iter) );
          errorsAdded++;
        }
        else
        {
          if (units) 
          {
            log->add( SBMLError(*iter) );
            errorsAdded++;
          }
        }
      }
      total_errors += errorsAdded;

    }
  }

  if (writeDocument)
    SBMLDocument_free(doc);
  return total_errors;
}