//===============================================================================
//
//
// Helper functions for writing/validating the given SBML documents.
//
//
//===============================================================================
/**
*
* Validates the given SBMLDocument.
*
* This function is based on validateSBML.cpp implemented by
* Sarah Keating, Ben Bornstein, and Michael Hucka.
*
*/
private static bool validateExampleSBML(SBMLDocument sbmlDoc)
{
if (sbmlDoc == null)
{
Console.Error.WriteLine("validateExampleSBML: given a null SBML Document");
return false;
}
string consistencyMessages = "";
string validationMessages = "";
bool noProblems = true;
int numCheckFailures = 0;
int numConsistencyErrors = 0;
int numConsistencyWarnings = 0;
int numValidationErrors = 0;
int numValidationWarnings = 0;
// LibSBML 3.3 is lenient when generating models from scratch using the
// API for creating objects. Once the whole model is done and before it
// gets written out, it's important to check that the whole model is in
// fact complete, consistent and valid.
numCheckFailures = (int)sbmlDoc.checkInternalConsistency();
if (numCheckFailures > 0)
{
noProblems = false;
for (int i = 0;
i < numCheckFailures;
i++)
{
SBMLError sbmlErr = sbmlDoc.getError(i);
if (sbmlErr.isFatal() || sbmlErr.isError())
{
++numConsistencyErrors;
}
else
{
++numConsistencyWarnings;
}
}
consistencyMessages = sbmlDoc.getErrorLog().toString();
}
// If the internal checks fail, it makes little sense to attempt
// further validation, because the model may be too compromised to
// be properly interpreted.
if (numConsistencyErrors > 0)
{
consistencyMessages += "Further validation aborted.";
}
else
{
numCheckFailures = (int)sbmlDoc.checkConsistency();
if (numCheckFailures > 0)
{
noProblems = false;
for (int i = 0;
i < numCheckFailures;
i++)
{
SBMLError sbmlErr = sbmlDoc.getError(i);
if (sbmlErr.isFatal() || sbmlErr.isError())
{
++numValidationErrors;
}
else
{
++numValidationWarnings;
}
}
validationMessages = sbmlDoc.getErrorLog().toString();
}
}
if (noProblems)
return true;
else
{
if (numConsistencyErrors > 0)
{
Console.WriteLine("ERROR: encountered " + numConsistencyErrors
+ " consistency error" + (numConsistencyErrors == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
if (numConsistencyWarnings > 0)
{
Console.WriteLine("Notice: encountered " + numConsistencyWarnings
+ " consistency warning" + (numConsistencyWarnings == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
Console.WriteLine();
Console.WriteLine(consistencyMessages);
if (numValidationErrors > 0)
{
Console.WriteLine("ERROR: encountered " + numValidationErrors
+ " validation error" + (numValidationErrors == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
if (numValidationWarnings > 0)
{
Console.WriteLine("Notice: encountered " + numValidationWarnings
+ " validation warning" + (numValidationWarnings == 1 ? "" : "s")
+ " in model '" + sbmlDoc.getModel().getId() + "'.");
}
Console.WriteLine();
Console.WriteLine(validationMessages);
return (numConsistencyErrors == 0 && numValidationErrors == 0);
}
}