diff --git a/src/sbml/SBMLDocument.cpp b/src/sbml/SBMLDocument.cpp index 02f7aab81..74693d6ce 100644 --- a/src/sbml/SBMLDocument.cpp +++ b/src/sbml/SBMLDocument.cpp @@ -2485,6 +2485,13 @@ SBMLDocument_getError (SBMLDocument_t *d, unsigned int n) return (d != NULL) ? d->getError(n) : NULL; } +LIBSBML_EXTERN +const SBMLErrorLog_t * +SBMLDocument_getErrorLog(SBMLDocument_t *d) +{ + return (d != NULL) ? d->getErrorLog() : NULL; +} + LIBSBML_EXTERN const SBMLError_t * SBMLDocument_getErrorWithSeverity(SBMLDocument_t *d, unsigned int n, unsigned int severity) diff --git a/src/sbml/SBMLDocument.h b/src/sbml/SBMLDocument.h index 15b73bf9e..f94b74531 100644 --- a/src/sbml/SBMLDocument.h +++ b/src/sbml/SBMLDocument.h @@ -2472,8 +2472,8 @@ SBMLDocument_checkL2v2Compatibility (SBMLDocument_t *d); * @memberof SBMLDocument_t */ LIBSBML_EXTERN -unsigned int -SBMLDocument_checkL2v3Compatibility (SBMLDocument_t *d); +unsigned int +SBMLDocument_checkL2v3Compatibility(SBMLDocument_t *d); /** @@ -2588,6 +2588,11 @@ const SBMLError_t * SBMLDocument_getError (SBMLDocument_t *d, unsigned int n); +LIBSBML_EXTERN +const SBMLErrorLog_t * +SBMLDocument_getErrorLog(SBMLDocument_t *d); + + /** * Returns the nth error or warning with the given severity * encountered during parsing, consistency checking, or attempted diff --git a/src/sbml/SBMLErrorLog.cpp b/src/sbml/SBMLErrorLog.cpp index 0d3ba18d5..5f154e33d 100644 --- a/src/sbml/SBMLErrorLog.cpp +++ b/src/sbml/SBMLErrorLog.cpp @@ -397,7 +397,7 @@ SBMLErrorLog::getError (unsigned int n) const * @return the nth SBMLError in this log. */ const SBMLError* -SBMLErrorLog::getErrorWithSeverity(unsigned int n, unsigned int severity) const +SBMLErrorLog::getErrorWithSeverity(unsigned int n, unsigned int severity, unsigned int fromCAPI) const { unsigned int count = 0; MatchSeverity matcher(severity); @@ -406,7 +406,17 @@ SBMLErrorLog::getErrorWithSeverity(unsigned int n, unsigned int severity) const { if (matcher(*it)) { - if (count == n) return dynamic_cast(*it); + if (count == n) + { + if (fromCAPI == 0) + { + return dynamic_cast(*it); + } + else + { + return static_cast(*it); + } + } ++count; } } @@ -417,6 +427,104 @@ SBMLErrorLog::getErrorWithSeverity(unsigned int n, unsigned int severity) const /** @cond doxygenIgnored */ + +LIBSBML_EXTERN +SBMLErrorLog_t* +SBMLErrorLog_create(void) +{ + return new(nothrow) SBMLErrorLog; +} + + +LIBSBML_EXTERN +void +SBMLErrorLog_free(SBMLErrorLog_t* log) +{ + delete log; +} + + +LIBSBML_EXTERN +void +SBMLErrorLog_add(SBMLErrorLog_t *log, const SBMLError_t *error) +{ + if (log == NULL || error == NULL) return; + log->add(*error); +} + + +LIBSBML_EXTERN +const SBMLError_t * +SBMLErrorLog_getError(const SBMLErrorLog_t *log, unsigned int n) +{ + if (log == NULL) + return NULL; + + return (const SBMLError_t*)(XMLErrorLog_getError((const XMLErrorLog_t*)(log), n)); +} + + +LIBSBML_EXTERN +const SBMLError_t * +SBMLErrorLog_getErrorWithSeverity(const SBMLErrorLog_t *log, unsigned int n, unsigned int severity) +{ + return (log != NULL) ? log->getErrorWithSeverity(n, severity, 1) : NULL; +} + +LIBSBML_EXTERN +unsigned int +SBMLErrorLog_getNumFailsWithSeverity(const SBMLErrorLog_t *log, unsigned int severity) +{ + return (log != NULL) ? log->getNumFailsWithSeverity(severity) : 0; +} + +LIBSBML_EXTERN +unsigned int +SBMLErrorLog_getNumErrors(const SBMLErrorLog_t *log) +{ + return (log != NULL) ? log->getNumErrors() : 0; +} + + +LIBSBML_EXTERN +bool +SBMLErrorLog_contains(const SBMLErrorLog_t *log, unsigned int errorId) +{ + return (log != NULL) ? log->contains(errorId) : false; +} + +LIBSBML_EXTERN +void +SBMLErrorLog_remove(SBMLErrorLog_t *log, unsigned int errorId) +{ + if (log != NULL) + { + log->remove(errorId); + } +} + +LIBSBML_EXTERN +void +SBMLErrorLog_removeAll(SBMLErrorLog_t *log, unsigned int errorId) +{ + if (log != NULL) + { + log->removeAll(errorId); + } +} + +LIBSBML_EXTERN +void +SBMLErrorLog_clearLog(SBMLErrorLog_t *log) +{ + if (log != NULL) + { + log->clearLog(); + } +} + + + /** @endcond */ LIBSBML_CPP_NAMESPACE_END diff --git a/src/sbml/SBMLErrorLog.h b/src/sbml/SBMLErrorLog.h index b5017186d..2d7cb91df 100644 --- a/src/sbml/SBMLErrorLog.h +++ b/src/sbml/SBMLErrorLog.h @@ -163,7 +163,7 @@ class LIBSBML_EXTERN SBMLErrorLog : public XMLErrorLog * * @see getNumFailsWithSeverity(unsigned int severity) */ - const SBMLError* getErrorWithSeverity(unsigned int n, unsigned int severity) const; + const SBMLError* getErrorWithSeverity(unsigned int n, unsigned int severity, unsigned int fromCAPI=0) const; /** @@ -360,4 +360,185 @@ class LIBSBML_EXTERN SBMLErrorLog : public XMLErrorLog LIBSBML_CPP_NAMESPACE_END #endif /* __cplusplus */ + +#ifndef SWIG + +LIBSBML_CPP_NAMESPACE_BEGIN +BEGIN_C_DECLS + +/** + * Creates a new empty SBMLErrorLog_t structure and returns it. + * + * @return the new SBMLErrorLog_t structure. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +SBMLErrorLog_t* +SBMLErrorLog_create(void); + + +/** + * Frees the SBMLErrorLog_t structure. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +void +SBMLErrorLog_free(SBMLErrorLog_t*); + + +/** + * Logs the given XMLError_t structure. + * + * @param log SBMLErrorLog_t, the error log to be added to. + * @param error XMLError_t, the error to be logged. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +void +SBMLErrorLog_add(SBMLErrorLog_t *log, const SBMLError_t *error); + + +/** + * Returns the nth SBMLError_t in this log. + * + * @param log SBMLErrorLog_t, the error log to be queried. + * @param n unsigned int number of the error to retrieve. + * + * @return the nth SBMLError_t in this log. + * If the index @p n is invalid, @c NULL is returned. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +const SBMLError_t * +SBMLErrorLog_getError(const SBMLErrorLog_t *log, unsigned int n); + + +/** + * Returns the nth SBMLError object with given severity in this log. + * + * Index @p n is counted from 0. Callers should first inquire about the + * number of items in the log by using the + * @if java SBMLErrorLog::getNumFailsWithSeverity(long severity)@else getNumFailsWithSeverity()@endif@~ method. + * Attempts to use an error index number that exceeds the actual number + * of errors in the log will result in a @c NULL being returned. + * + * @param log SBMLErrorLog_t, the error log to be queried. + * @param n the index number of the error to retrieve (with 0 being the + * first error). + * @param severity the severity of the error to retrieve. + * + * @return the nth SBMLError in this log, or @c NULL if @p n is + * greater than or equal to + * @if java SBMLErrorLog::getNumFailsWithSeverity(long severity)@else getNumFailsWithSeverity()@endif. + * + * @see getNumFailsWithSeverity(unsigned int severity) + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +const SBMLError_t * +SBMLErrorLog_getErrorWithSeverity(const SBMLErrorLog_t *log, unsigned int n, unsigned int severity); + + +/** + * Returns the number of errors that have been logged with the given + * severity code. + * + * @copydetails doc_errorlog_what_are_severities + * + * @param log SBMLErrorLog_t, the error log to be queried. + * @if clike @param severity a value from + * #SBMLErrorSeverity_t @endif@if java @param severity a + * value from the set of LIBSBML_SEV_ constants defined by + * the interface class libsbmlConstants @endif@if python @param severity a + * value from the set of LIBSBML_SEV_ constants defined by + * the interface class @link libsbml libsbml@endlink. @endif@~ + * + * @return a count of the number of errors with the given severity code. + * + * @see getNumErrors() + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +unsigned int +SBMLErrorLog_getNumFailsWithSeverity(const SBMLErrorLog_t *log, unsigned int severity); + + +/** + * Returns the number of errors that have been logged. + * + * @param log SBMLErrorLog_t, the error log to be queried. + * + * @return the number of errors that have been logged. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +unsigned int +SBMLErrorLog_getNumErrors(const SBMLErrorLog_t *log); + + +/** + * Returns @c true if SBMLErrorLog contains an errorId + * + * @param log SBMLErrorLog_t, the error log to be queried. + * @param errorId the error identifier of the error to be found. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +bool +SBMLErrorLog_contains(const SBMLErrorLog_t *log, unsigned int errorId); + + +/** + * Removes an error having errorId from the SBMLError list. + * + * Only the first item will be removed if there are multiple errors + * with the given errorId. + * + * @param log SBMLErrorLog_t, the error log to be cleared. + * @param errorId the error identifier of the error to be removed. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +void +SBMLErrorLog_remove(SBMLErrorLog_t *log, unsigned int errorId); + + +/** + * Removes all errors having errorId from the SBMLError list. + * + * @param log SBMLErrorLog_t, the error log to be cleared. + * @param errorId the error identifier of the error to be removed. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +void +SBMLErrorLog_removeAll(SBMLErrorLog_t *log, unsigned int errorId); + + +/** + * Removes all errors from this log. + * + * @param log SBMLErrorLog_t, the error log to be cleared. + * + * @memberof SBMLErrorLog_t + */ +LIBSBML_EXTERN +void +SBMLErrorLog_clearLog(SBMLErrorLog_t *log); + +END_C_DECLS +LIBSBML_CPP_NAMESPACE_END + +#endif /* !SWIG */ #endif /* SBMLErrorLog_h */ diff --git a/src/sbml/common/sbmlfwd.h b/src/sbml/common/sbmlfwd.h index d6ade0f63..6118859c0 100644 --- a/src/sbml/common/sbmlfwd.h +++ b/src/sbml/common/sbmlfwd.h @@ -256,6 +256,12 @@ typedef CLASS_OR_STRUCT SBMLWriter SBMLWriter_t; */ typedef CLASS_OR_STRUCT SBMLError SBMLError_t; +/** +* @var typedef class SBMLErrorLog SBMLErrorLog_t +* @copydoc SBMLErrorLog +*/ +typedef CLASS_OR_STRUCT SBMLErrorLog SBMLErrorLog_t; + /** * @var typedef class ASTNode ASTNode_t * @copydoc ASTNode diff --git a/src/sbml/test/TestRunner.c b/src/sbml/test/TestRunner.c index a7a7d35a3..4a97c1ef0 100644 --- a/src/sbml/test/TestRunner.c +++ b/src/sbml/test/TestRunner.c @@ -117,6 +117,7 @@ Suite *create_suite_SBMLConvertStrict (void); Suite *create_suite_SBMLConvertFromL3V2 (void); Suite *create_suite_SBMLDocument (void); Suite *create_suite_SBMLError (void); +Suite *create_suite_SBMLErrorLog (void); Suite *create_suite_SBMLReader (void); Suite *create_suite_SBMLWriter (void); Suite *create_suite_SimpleSpeciesReference (void); @@ -223,6 +224,7 @@ main (int argc, char* argv[]) int num_failed; setTestDataDirectory(); + //SRunner *runner = srunner_create(create_suite_SBMLErrorLog()); SRunner *runner = srunner_create( create_suite_ReadSBML () ); srunner_add_suite( runner, create_suite_SBMLValidatorAPI () ); @@ -298,6 +300,7 @@ main (int argc, char* argv[]) srunner_add_suite( runner, create_suite_SBMLConvertFromL3V2 () ); srunner_add_suite( runner, create_suite_SBMLDocument () ); srunner_add_suite( runner, create_suite_SBMLError () ); + srunner_add_suite( runner, create_suite_SBMLErrorLog () ); srunner_add_suite( runner, create_suite_TestReadFromFile1 () ); srunner_add_suite( runner, create_suite_TestReadFromFile2 () ); srunner_add_suite( runner, create_suite_TestReadFromFile3 () ); diff --git a/src/sbml/test/TestSBMLErrorLog.c b/src/sbml/test/TestSBMLErrorLog.c new file mode 100644 index 000000000..3a98cfa60 --- /dev/null +++ b/src/sbml/test/TestSBMLErrorLog.c @@ -0,0 +1,168 @@ +/** + * \file TestErrorLog.c + * \brief ErrorLog unit tests + * \author Ben Bornstein + * + * */ + +#include + +#include +#include +#include +#include +#include + +#include + + + +#include + +LIBSBML_CPP_NAMESPACE_USE + + +BEGIN_C_DECLS + +START_TEST(test_SBMLErrorLog_removeAll) +{ + SBMLErrorLog_t *log = SBMLErrorLog_create(); + XMLError_t *e = XMLError_createWithIdAndMessage(1234, "1"); + + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + + fail_unless(SBMLErrorLog_contains(log, 1234) == true); + fail_unless(SBMLErrorLog_getNumErrors(log) == 4); + + SBMLErrorLog_remove(log, 1234); + fail_unless(SBMLErrorLog_contains(log, 1234) == true); + fail_unless(SBMLErrorLog_getNumErrors(log) == 3); + + SBMLErrorLog_removeAll(log, 1234); + fail_unless(SBMLErrorLog_contains(log, 1234) == false); + fail_unless(SBMLErrorLog_getNumErrors(log) == 0); + + XMLError_free(e); + SBMLErrorLog_free(log); + +} +END_TEST + + +START_TEST(test_SBMLErrorLog_add_clear) +{ + SBMLErrorLog_t *log = SBMLErrorLog_create(); + XMLError_t *e = XMLError_createWithIdAndMessage(1234, "1"); + XMLError_t *e1 = XMLError_createWithIdAndMessage(2345, "1"); + + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e1)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + + fail_unless(SBMLErrorLog_contains(log, 1234) == true); + fail_unless(SBMLErrorLog_contains(log, 2345) == true); + fail_unless(SBMLErrorLog_getNumErrors(log) == 4); + + SBMLErrorLog_remove(log, 2345); + fail_unless(SBMLErrorLog_contains(log, 1234) == true); + fail_unless(SBMLErrorLog_contains(log, 2345) == false); + fail_unless(SBMLErrorLog_getNumErrors(log) == 3); + + SBMLErrorLog_clearLog(log); + fail_unless(SBMLErrorLog_getNumErrors(log) == 0); + + XMLError_free(e); + XMLError_free(e1); + SBMLErrorLog_free(log); +} +END_TEST + + +START_TEST(test_SBMLErrorLog_get) +{ + SBMLErrorLog_t *log = SBMLErrorLog_create(); + XMLError_t *e = XMLError_createWithIdAndMessage(1234, "1"); + XMLError_t *e1 = XMLError_createWithIdAndMessage(2345, "1"); + + SBMLErrorLog_add(log, (const SBMLError_t*)(e)); + SBMLErrorLog_add(log, (const SBMLError_t*)(e1)); + + const SBMLError_t* r = SBMLErrorLog_getError(log, 1); + + fail_unless(r != NULL); + fail_unless(XMLError_getSeverity(r) == 1); + fail_unless(XMLError_getErrorId(r) == 2345); + + const SBMLError_t* r1 = SBMLErrorLog_getErrorWithSeverity(log, 0, 1); + + fail_unless(r1 != NULL); + fail_unless(XMLError_getSeverity(r1) == 1); + fail_unless(XMLError_getErrorId(r1) == 1234); + + fail_unless(SBMLErrorLog_getNumFailsWithSeverity(log, 1) == 2); + + XMLError_free(e); + XMLError_free(e1); + //XMLError_free((SBMLError_t*)(r)); + //XMLError_free((SBMLError_t*)(r1)); + SBMLErrorLog_free(log); +} +END_TEST + + + +Suite * +create_suite_SBMLErrorLog (void) +{ + Suite *suite = suite_create("ErrorLog"); + TCase *tcase = tcase_create("ErrorLog"); + + tcase_add_test(tcase, test_SBMLErrorLog_removeAll); + tcase_add_test(tcase, test_SBMLErrorLog_add_clear); + tcase_add_test(tcase, test_SBMLErrorLog_get); + + suite_add_tcase(suite, tcase); + + return suite; +} + +END_C_DECLS +