int CompModelPlugin::saveAllReferencedElements(set<SBase*> uniqueRefs, set<SBase*> replacedBys) { SBMLDocument* doc = getSBMLDocument(); Model* model = static_cast<Model*>(getParentSBMLObject()); if (model==NULL) { if (doc) { string error = "Unable to discover any referenced elements in CompModelPlugin::saveAllReferencedElements: no Model parent of the 'comp' model plugin."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error); } return LIBSBML_OPERATION_FAILED; } int ret = LIBSBML_OPERATION_SUCCESS; //Get a list of everything, pull out anything that's a deletion, replacement, or port, and save what they're pointing to. //At the same time, make sure that no two things point to the same thing. set<SBase*> RE_deletions = set<SBase*>(); //Deletions only point to things in the same model. List* allElements = model->getAllElements(); string modname = "the main model in the document"; if (model->isSetId()) { modname = "the model '" + model->getId() + "'"; } set<SBase*> todelete; for (unsigned int el=0; el<allElements->getSize(); el++) { SBase* element = static_cast<SBase*>(allElements->get(el)); int type = element->getTypeCode(); if (type==SBML_COMP_DELETION || type==SBML_COMP_REPLACEDBY || type==SBML_COMP_REPLACEDELEMENT || type==SBML_COMP_PORT) { //Don't worry about SBML_COMP_SBASEREF because they're all children of one of the above types. SBaseRef* reference = static_cast<SBaseRef*>(element); ReplacedElement* re = static_cast<ReplacedElement*>(element); ret = reference->saveReferencedElement(); if (ret != LIBSBML_OPERATION_SUCCESS) { if (type != SBML_COMP_REPLACEDBY && doc) { SBMLErrorLog* errlog = doc->getErrorLog(); SBMLError* lasterr = const_cast<SBMLError*> (doc->getErrorLog()->getError(doc->getNumErrors()-1)); if ( (errlog->contains(UnrequiredPackagePresent) || errlog->contains(RequiredPackagePresent))) { if ( lasterr->getErrorId() == CompIdRefMustReferenceObject) { //Change the error into a warning string fullmsg = lasterr->getMessage() + " However, this may be because of the unrecognized " + "package present in this document: ignoring this " + "element and flattening anyway."; errlog->remove(lasterr->getErrorId()); errlog->logPackageError("comp", CompIdRefMayReferenceUnknownPackage, getPackageVersion(), getLevel(), getVersion(), fullmsg, element->getLine(), element->getColumn(), LIBSBML_SEV_WARNING); } else if ( lasterr->getErrorId() == CompMetaIdRefMustReferenceObject) { //Change the error into a warning string fullmsg = lasterr->getMessage() + " However, this may be because of the unrecognized " + "package present in this document: ignoring this " + "element and flattening anyway."; errlog->remove(lasterr->getErrorId()); errlog->logPackageError("comp", CompMetaIdRefMayReferenceUnknownPkg, getPackageVersion(), getLevel(), getVersion(), fullmsg, element->getLine(), element->getColumn(), LIBSBML_SEV_WARNING); } } //Whether or not we could figure out the error, we can always still continue flattening. todelete.insert(element); continue; } else { delete allElements; return ret; } } SBase* direct = reference->getDirectReference(); bool adddirect = true; if (type == SBML_COMP_REPLACEDBY) { SBase* rbParent = reference->getParentSBMLObject(); if (uniqueRefs.insert(rbParent).second == false) { if (doc) { string error = "Error discovered in CompModelPlugin::saveAllReferencedElements when checking " + modname + ": a <" + rbParent->getElementName() + "> "; if (direct->isSetId()) { error += "with the id '" + rbParent->getId() + "'"; if (rbParent->isSetMetaId()) { error += ", and the metaid '" + rbParent->getMetaId() + "'"; } } else if (rbParent->isSetMetaId()) { error += "with the metaId '" + rbParent->getMetaId() + "'"; } error += " has a <replacedBy> child and is also pointed to by a <port>, <deletion>, <replacedElement>, or one or more <replacedBy> objects."; doc->getErrorLog()->logPackageError("comp", CompNoMultipleReferences, getPackageVersion(), getLevel(), getVersion(), error); } delete allElements; return LIBSBML_OPERATION_FAILED; } adddirect = replacedBys.insert(direct).second; } if (type==SBML_COMP_REPLACEDELEMENT && re->isSetDeletion()) { adddirect = RE_deletions.insert(direct).second; } if (adddirect) { if (uniqueRefs.insert(direct).second == false) { if (doc) { string error = "Error discovered in CompModelPlugin::saveAllReferencedElements when checking " + modname + ": "; if (replacedBys.find(direct) != replacedBys.end()) { error += "one or more <replacedBy> elements, plus a <deletion>, <replacedElement>, or <port> element"; } else if (RE_deletions.find(direct) != RE_deletions.end()) { error += "one or more <replacedElement> elements using a 'deletion' attribute, plus a <deletion>, <replacedElement>, or <port> element"; } else { error += "multiple <deletion>, <replacedElement>, and/or <port> elements"; } error += " point directly to the <" + direct->getElementName() + "> "; if (direct->isSetId()) { error += "with the id '" + direct->getId() + "'"; if (direct->isSetMetaId()) { error += ", and the metaid '" + direct->getMetaId() + "'"; } error += "."; } else if (direct->isSetMetaId()) { error += "with the metaId '" + direct->getMetaId() + "'."; } doc->getErrorLog()->logPackageError("comp", CompNoMultipleReferences, getPackageVersion(), getLevel(), getVersion(), error); } delete allElements; return LIBSBML_OPERATION_FAILED; } } } } for(set<SBase*>::iterator el=todelete.begin(); el != todelete.end(); el++) { (*el)->removeFromParentAndDelete(); } delete allElements; //Now call saveAllReferencedElements for all instantiated submodels. for (unsigned long sm=0; sm<getNumSubmodels(); ++sm) { Model* sub = getSubmodel((unsigned int)sm)->getInstantiation(); if (sub==NULL) { return LIBSBML_OPERATION_FAILED; } CompModelPlugin* subplug = static_cast<CompModelPlugin*>(sub->getPlugin(getPrefix())); if (subplug==NULL) { return LIBSBML_OPERATION_FAILED; } ret = subplug->saveAllReferencedElements(uniqueRefs, replacedBys); if (ret != LIBSBML_OPERATION_SUCCESS) { return ret; } } return LIBSBML_OPERATION_SUCCESS; }
unsigned int operator() (const SBMLError& msg) { return msg.getErrorId(); }
bool operator() (const SBMLError& msg) { return msg.getErrorId() == id; }
LIBSBML_CPP_NAMESPACE_USE /** @endcond */ CK_CPPSTART START_TEST (test_SBMLError_create) { SBMLError* error = new SBMLError; fail_unless( error != NULL ); delete error; error = new SBMLError(EmptyListInReaction); fail_unless( error->getErrorId() == EmptyListInReaction ); fail_unless( error->getSeverity() == LIBSBML_SEV_ERROR ); fail_unless( error->getSeverityAsString() == "Error" ); fail_unless( error->getCategory() == LIBSBML_CAT_SBML ); fail_unless( error->getCategoryAsString() == "General SBML conformance"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(OverdeterminedSystem, 2, 1); fail_unless( error->getErrorId() == OverdeterminedSystem ); fail_unless( error->getSeverity() == LIBSBML_SEV_WARNING ); fail_unless( error->getSeverityAsString() == "Warning" ); fail_unless( error->getCategory() == LIBSBML_CAT_OVERDETERMINED_MODEL ); fail_unless( error->getCategoryAsString() == "Overdetermined model"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(OffsetNoLongerValid, 2, 2); fail_unless( error->getErrorId() == OffsetNoLongerValid ); fail_unless( error->getSeverity() == LIBSBML_SEV_ERROR ); fail_unless( error->getSeverityAsString() == "Error" ); fail_unless( error->getCategory() == LIBSBML_CAT_GENERAL_CONSISTENCY ); fail_unless( error->getCategoryAsString() == "SBML component consistency"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(NoSBOTermsInL1, 2, 2); fail_unless( error->getErrorId() == NoSBOTermsInL1 ); fail_unless( error->getSeverity() == LIBSBML_SEV_WARNING ); fail_unless( error->getSeverityAsString() == "Warning" ); fail_unless( error->getCategory() == LIBSBML_CAT_SBML_L1_COMPAT ); fail_unless( error->getCategoryAsString() == "Translation to SBML L1V2"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(DisallowedMathMLEncodingUse, 2, 2); fail_unless( error->getErrorId() == DisallowedMathMLEncodingUse ); fail_unless( error->getSeverity() == LIBSBML_SEV_ERROR ); fail_unless( error->getSeverityAsString() == "Error" ); fail_unless( error->getCategory() == LIBSBML_CAT_MATHML_CONSISTENCY ); fail_unless( error->getShortMessage() == "Use of the MathML 'encoding' attribute is not allowed on this element"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(DisallowedMathMLEncodingUse, 1, 2); fail_unless( error->getErrorId() == DisallowedMathMLEncodingUse ); fail_unless( error->getSeverity() == LIBSBML_SEV_NOT_APPLICABLE ); fail_unless( error->getCategory() == LIBSBML_CAT_MATHML_CONSISTENCY ); fail_unless( error->isValid() == true); delete error; error = new SBMLError(UnknownError, 2, 4); fail_unless( error->getErrorId() == UnknownError ); fail_unless( error->getSeverity() == LIBSBML_SEV_FATAL ); fail_unless( error->getSeverityAsString() == "Fatal" ); fail_unless( error->getCategory() == LIBSBML_CAT_INTERNAL ); fail_unless( error->getShortMessage() == "Encountered unknown internal libSBML error"); fail_unless( error->isValid() == true); delete error; error = new SBMLError(70912, 2, 4); fail_unless( error->getErrorId() == 70912 ); fail_unless( error->getSeverity() == LIBSBML_SEV_WARNING ); fail_unless( error->getSeverityAsString() == "Warning" ); fail_unless( error->getCategory() == LIBSBML_CAT_INTERNAL ); fail_unless( error->getShortMessage() == "Encountered unknown internal libSBML error"); fail_unless( error->isValid() == false); delete error; }