Skip to content

Commit

Permalink
Merge pull request #295 from sbmlteam/c-modelhistory
Browse files Browse the repository at this point in the history
C modelhistory
  • Loading branch information
skeating committed Mar 7, 2023
2 parents cb901e5 + 17e4484 commit 58f5ed5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 26 deletions.
71 changes: 45 additions & 26 deletions src/sbml/SBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,43 +2229,62 @@ SBase::appendNotes(const std::string& notes)
int
SBase::setModelHistory(ModelHistory * history)
{
// if there is no parent then the required attributes are not
// correctly identified
bool dummyParent = false;
if (history != NULL && history->getParentSBMLObject() == NULL)
{
history->setParentSBMLObject(this);
dummyParent = true;
}

int status = LIBSBML_OPERATION_SUCCESS;

/* ModelHistory is only allowed on Model in L2
* but on any element in L3
*/
if (getLevel() < 3 && getTypeCode() != SBML_MODEL)
{
return LIBSBML_UNEXPECTED_ATTRIBUTE;
status = LIBSBML_UNEXPECTED_ATTRIBUTE;
}
// shouldnt add a history to an object with no metaid
if (!isSetMetaId())
{
return LIBSBML_MISSING_METAID;
}

if (mHistory == history)
if (status == LIBSBML_OPERATION_SUCCESS && !isSetMetaId())
{
return LIBSBML_OPERATION_SUCCESS;
}
else if (history == NULL)
{
delete mHistory;
mHistory = NULL;
mHistoryChanged = true;
return LIBSBML_OPERATION_SUCCESS;
}
else if (!(history->hasRequiredAttributes()))
{
delete mHistory;
mHistory = NULL;
return LIBSBML_INVALID_OBJECT;
status = LIBSBML_MISSING_METAID;
}
else

if (status == LIBSBML_OPERATION_SUCCESS)
{
delete mHistory;
mHistory = static_cast<ModelHistory*>( history->clone() );
mHistoryChanged = true;
return LIBSBML_OPERATION_SUCCESS;
if (mHistory == history)
{
status = LIBSBML_OPERATION_SUCCESS;
}
else if (history == NULL)
{
delete mHistory;
mHistory = NULL;
mHistoryChanged = true;
status = LIBSBML_OPERATION_SUCCESS;
}
else if (!(history->hasRequiredAttributes()))
{
delete mHistory;
mHistory = NULL;
status = LIBSBML_INVALID_OBJECT;
}
else
{
delete mHistory;
mHistory = static_cast<ModelHistory*>(history->clone());
mHistoryChanged = true;
status = LIBSBML_OPERATION_SUCCESS;
}
}
// if we set a dummy parent unset
if (dummyParent)
history->unsetParentSBMLObject();

return status;
}


Expand Down
1 change: 1 addition & 0 deletions src/sbml/annotation/ModelHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class LIBSBML_EXTERN ModelHistory
int unsetParentSBMLObject();

friend class RDFAnnotationParser;
friend class SBase;

/* Can have more than one creator. */

Expand Down
77 changes: 77 additions & 0 deletions src/sbml/test/TestSBase_newSetters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,81 @@ START_TEST (test_SBase_setModelHistoryL3)
END_TEST


START_TEST(test_SBase_setModelHistoryL3_no_date)
{
Species_t sb(3, 1);
sb.setMetaId("_s");
ModelHistory_t *mh = ModelHistory_create();
ModelCreator_t * mc = ModelCreator_create();

ModelCreator_setFamilyName(mc, "Keating");
ModelCreator_setGivenName(mc, "Sarah");
ModelCreator_setEmail(mc, "sbml-team@caltech.edu");
ModelCreator_setOrganisation(mc, "UH");

ModelHistory_addCreator(mh, mc);

int i = SBase_setModelHistory(&sb, mh);

fail_unless(i == LIBSBML_OPERATION_SUCCESS);
fail_unless(SBase_isSetModelHistory(&sb) == 1);

ModelHistory_free(mh);
mh = SBase_getModelHistory(&sb);

fail_unless(mh != NULL);

SBase_unsetModelHistory(&sb);
mh = SBase_getModelHistory(&sb);

fail_unless(SBase_isSetModelHistory(&sb) == 0);
fail_unless(mh == NULL);

ModelCreator_free(mc);
}
END_TEST


START_TEST(test_SBase_setModelHistoryL3_noParent)
{
Species_t sb(3, 1);
sb.setMetaId("_s");
ModelHistory_t *mh = ModelHistory_create();
ModelCreator_t * mc = ModelCreator_create();
Date_t * date =
Date_createFromValues(2005, 12, 30, 12, 15, 45, 1, 2, 0);

ModelCreator_setFamilyName(mc, "Keating");
ModelCreator_setGivenName(mc, "Sarah");
ModelCreator_setEmail(mc, "sbml-team@caltech.edu");
ModelCreator_setOrganisation(mc, "UH");

ModelHistory_addCreator(mh, mc);
ModelHistory_setCreatedDate(mh, date);
ModelHistory_setModifiedDate(mh, date);

int i = SBase_setModelHistory(&sb, mh);

fail_unless(i == LIBSBML_OPERATION_SUCCESS);
fail_unless(SBase_isSetModelHistory(&sb) == 1);

ModelHistory_free(mh);
mh = SBase_getModelHistory(&sb);

fail_unless(mh != NULL);

SBase_unsetModelHistory(&sb);
mh = SBase_getModelHistory(&sb);

fail_unless(SBase_isSetModelHistory(&sb) == 0);
fail_unless(mh == NULL);

ModelCreator_free(mc);
Date_free(date);
}
END_TEST


Suite *
create_suite_SBase_newSetters (void)
{
Expand Down Expand Up @@ -2331,6 +2406,8 @@ create_suite_SBase_newSetters (void)
tcase_add_test(tcase, test_SBase_setModelHistory );
tcase_add_test(tcase, test_SBase_setModelHistory_Model );
tcase_add_test(tcase, test_SBase_setModelHistoryL3 );
tcase_add_test(tcase, test_SBase_setModelHistoryL3_no_date);
tcase_add_test(tcase, test_SBase_setModelHistoryL3_noParent);

suite_add_tcase(suite, tcase);

Expand Down

0 comments on commit 58f5ed5

Please sign in to comment.