void AssignmentRuleOrdering::logForwardReference (const ASTNode & node, const SBase& object, std::string name) { char * formula = SBML_formulaToString(&node); msg = "The AssignmentRule with variable '"; msg += object.getId(); msg += "' refers to the variable '"; msg += name; msg += "' within the math formula '"; msg += formula; msg += "'. '"; msg += name; msg += "' is the subject of a later assignment rule."; safe_free(formula); logFailure(object); }
int ReplacedBy::performReplacementAndCollect(set<SBase*>* removed, set<SBase*>* toremove) { SBMLDocument* doc = getSBMLDocument(); //Find the various objects and plugin objects we need for this to work. SBase* parent = getParentSBMLObject(); if (parent==NULL) { if (doc) { string error = "Unable to perform replacement in ReplacedBy::performReplacement: no parent object for this <replacedBy> could be found."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } SBase* ref = getReferencedElement(); if (ref==NULL) { //getReferencedElement sets its own error messages. return LIBSBML_INVALID_OBJECT; } //Update the IDs. (Will set its own error messages.) int ret = updateIDs(ref, parent); //ReplacedBy elements do get the name of the top-level element, assuming it has one: if (parent->isSetId()) { ref->setId(parent->getId()); } if (parent->isSetMetaId()) { ref->setMetaId(parent->getMetaId()); } if (ret != LIBSBML_OPERATION_SUCCESS) return ret; //And finally, get ready to delete the parent object. if (toremove) { toremove->insert(parent); } return LIBSBML_OPERATION_SUCCESS; }
/** @cond doxygenLibsbmlInternal */ void CompModelPlugin::resetPorts() { for (unsigned int p=0; p<getNumPorts(); p++) { Port* port = getPort(p); SBase* referenced = port->getReferencedElement(); if (port->isSetSBaseRef()) { port->unsetSBaseRef(); port->unsetIdRef(); port->unsetMetaIdRef(); port->unsetUnitRef(); int type = referenced->getTypeCode(); if (referenced->isSetId() && type != SBML_INITIAL_ASSIGNMENT && type != SBML_ASSIGNMENT_RULE && type != SBML_RATE_RULE && type != SBML_EVENT_ASSIGNMENT) { if (type==SBML_UNIT_DEFINITION) { port->setUnitRef(referenced->getId()); } else { port->setIdRef(referenced->getId()); } } else if (referenced->isSetMetaId()) { port->setMetaIdRef(referenced->getMetaId()); } else { stringstream newname; newname << "auto_port_" << p; referenced->setMetaId(newname.str()); port->setMetaIdRef(newname.str()); } } port->clearReferencedElement(); } }
std::string SEDMLUtils::findIdByNameAndType( const std::map<CCopasiObject*, SBase*>& map, int typeCode, const std::string& name) { std::map<CCopasiObject*, SBase*>::const_iterator it = map.begin(); std::string::size_type compartmentStart = name.find("{"); std::string nameOnly = name.substr(0, compartmentStart); while (it != map.end()) { SBase* current = it->second; if (((current->getTypeCode() & typeCode) == typeCode) && current->getName() == name) return current->getId(); if (typeCode == SBML_SPECIES && compartmentStart != std::string::npos) { if (((current->getTypeCode() & typeCode) == typeCode) && current->getName() == nameOnly) { std::string compName = name.substr(compartmentStart + 1, name.size() - compartmentStart - 2); std::string compId = findIdByNameAndType(map, SBML_COMPARTMENT, compName); Species* species = (Species*) current; if (species->getCompartment() == compId) return species->getId(); } } ++it; } return ""; }
/** @cond doxygenLibsbmlInternal */ SBase* CompSBasePlugin::createObject(XMLInputStream& stream) { SBase* object = 0; const std::string& name = stream.peek().getName(); const XMLNamespaces& xmlns1 = stream.peek().getNamespaces(); const std::string& prefix = stream.peek().getPrefix(); const std::string& targetPrefix = (xmlns1.hasURI(mURI)) ? xmlns1.getPrefix(mURI) : mPrefix; const SBase* parent = getParentSBMLObject(); string message = ""; if (parent) { message = "The <" + parent->getElementName() + "> "; if (parent->isSetId()) { //LS DEBUG message += "with the id '" + parent->getId() + "' "; } } if (prefix == targetPrefix) { if ( name == "listOfReplacedElements" ) { if (mListOfReplacedElements != NULL) { getErrorLog()->logPackageError("comp", CompOneListOfReplacedElements, getPackageVersion(), getLevel(), getVersion(), message + "may only have one <listOfReplacedElements>"); } createListOfReplacedElements(); object = mListOfReplacedElements; if (targetPrefix.empty()) { // // (NOTE) // // A top-level element (listOfReplacedElements) of the comp extension is located // in a default namespace, and thus xmlns=".." attribute must be added to // the element. // This is done by invoking SBMLDocument::enableDefaultNS() function with // the two arguments (the URI of this package and true value). // mListOfReplacedElements->getSBMLDocument()->enableDefaultNS(mURI,true); } } if ( name == "replacedBy" ) { if (mReplacedBy != NULL) { if (mSBML != NULL && getErrorLog() != NULL) { getErrorLog()->logPackageError("comp", CompOneReplacedByElement, getPackageVersion(), getLevel(), getVersion(), message + "may only have one <replacedBy> child."); } } delete mReplacedBy; COMP_CREATE_NS(compns, getSBMLNamespaces()); mReplacedBy = new ReplacedBy(compns); object = mReplacedBy; object->connectToParent(getParentSBMLObject()); delete compns; } } return object; }
int CompModelPlugin::collectRenameAndConvertReplacements(set<SBase*>* removed, set<SBase*>* toremove) { int ret = LIBSBML_OPERATION_SUCCESS; SBMLDocument* doc = getSBMLDocument(); Model* model = static_cast<Model*>(getParentSBMLObject()); if (model==NULL) { if (doc) { string error = "Unable to perform replacements in CompModelPlugin::collectRenameAndConvertReplacements: no parent model could be found for the given 'comp' model plugin element."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error); } return LIBSBML_OPERATION_FAILED; } List* allElements = model->getAllElements(); vector<ReplacedElement*> res; vector<ReplacedBy*> rbs; //Collect replaced elements and replaced by's. for (unsigned int e=0; e<allElements->getSize(); e++) { SBase* element = static_cast<SBase*>(allElements->get(e)); int type = element->getTypeCode(); if (type==SBML_COMP_REPLACEDELEMENT) { ReplacedElement* reference = static_cast<ReplacedElement*>(element); res.push_back(reference); } if (type==SBML_COMP_REPLACEDBY) { ReplacedBy* reference = static_cast<ReplacedBy*>(element); rbs.push_back(reference); } } delete allElements; //ReplacedElement replacements for (size_t re=0; re<res.size(); re++) { ret = res[re]->performReplacementAndCollect(removed, toremove); if (ret != LIBSBML_OPERATION_SUCCESS) { return ret; } } //Now do the same thing for anything left over in the submodels for (unsigned int sub=0; sub<getNumSubmodels(); sub++) { Submodel* submodel = getSubmodel(sub); Model* mod = submodel->getInstantiation(); if (mod==NULL) return LIBSBML_OPERATION_FAILED; CompModelPlugin* modplug = static_cast<CompModelPlugin*>(mod->getPlugin(getPrefix())); if (modplug==NULL) return LIBSBML_OPERATION_FAILED; //'left behind' converions (not LaHaye-style) ret = submodel->convertTimeAndExtent(); if (ret != LIBSBML_OPERATION_SUCCESS) return ret; ret = modplug->collectRenameAndConvertReplacements(removed, toremove); if (ret != LIBSBML_OPERATION_SUCCESS) return ret; } //Perform ReplacedBy replacements *after* the submodels are done, so that the topmost-level names take precedence. for (size_t rb=0; rb<rbs.size(); rb++) { ret = rbs[rb]->performReplacementAndCollect(removed, toremove); if (ret != LIBSBML_OPERATION_SUCCESS) { return ret; } } return ret; }
/** @cond doxygenLibsbmlInternal */ int Replacing::updateIDs(SBase* oldnames, SBase* newnames) { int ret = LIBSBML_OPERATION_SUCCESS; SBMLDocument* doc = getSBMLDocument(); if (oldnames->isSetId() && !newnames->isSetId()) { if (doc) { string error = "Unable to transform IDs in Replacing::updateIDs during replacement: the '" + oldnames->getId() + "' element's replacement does not have an ID set."; doc->getErrorLog()->logPackageError("comp", CompMustReplaceIDs, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } if (oldnames->isSetMetaId() && !newnames->isSetMetaId()) { if (doc) { string error = "Unable to transform IDs in Replacing::updateIDs during replacement: the replacement of the element with metaid '" + oldnames->getMetaId() + "' does not have a metaid."; doc->getErrorLog()->logPackageError("comp", CompMustReplaceMetaIDs, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } //LS DEBUG Somehow we need to check identifiers from other packages here (like spatial id's). How, exactly, is anyone's guess. Model* replacedmod = const_cast<Model*>(CompBase::getParentModel(oldnames)); KineticLaw* replacedkl; ASTNode newkl; if (replacedmod==NULL) { if (doc) { string error = "Unable to transform IDs in Replacing::updateIDs during replacement: the replacement of '" + oldnames->getId() + "' does not have a valid model."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } List* allElements = replacedmod->getAllElements(); string oldid = oldnames->getId(); string newid = newnames->getId(); if (!oldid.empty()) { switch(oldnames->getTypeCode()) { case SBML_UNIT_DEFINITION: replacedmod->renameUnitSIdRefs(oldid, newid); for (unsigned int e=0; e<allElements->getSize(); e++) { SBase* element = static_cast<SBase*>(allElements->get(e)); element->renameUnitSIdRefs(oldid, newid); } break; case SBML_LOCAL_PARAMETER: replacedkl = static_cast<KineticLaw*>(oldnames->getAncestorOfType(SBML_KINETIC_LAW)); if (replacedkl->isSetMath()) { newkl = *replacedkl->getMath(); newkl.renameSIdRefs(oldid, newid); replacedkl->setMath(&newkl); } break; case SBML_COMP_PORT: break; //LS DEBUG And here is where we would need some sort of way to check if the id wasn't an SId for some objects. default: replacedmod->renameSIdRefs(oldnames->getId(), newnames->getId()); for (unsigned int e=0; e<allElements->getSize(); e++) { SBase* element = static_cast<SBase*>(allElements->get(e)); element->renameSIdRefs(oldid, newid); } } } string oldmetaid = oldnames->getMetaId(); string newmetaid = newnames->getMetaId(); if (oldnames->isSetMetaId()) { replacedmod->renameMetaIdRefs(oldmetaid, newmetaid); for (unsigned int e=0; e<allElements->getSize(); e++) { SBase* element = static_cast<SBase*>(allElements->get(e)); element->renameMetaIdRefs(oldmetaid, newmetaid); } } //LS DEBUG And here is where we would need some sort of way to check for ids that were not 'id' or 'metaid'. delete allElements; return ret; }
int Submodel::instantiate() { SBMLDocument* doc = getSBMLDocument(); SBMLDocument* rootdoc = doc; if (doc==NULL) { return LIBSBML_OPERATION_FAILED; } CompSBMLDocumentPlugin* docplugin = static_cast<CompSBMLDocumentPlugin*>(doc->getPlugin(getPrefix())); if (docplugin==NULL) { return LIBSBML_OPERATION_FAILED; } SBase* parent = getParentSBMLObject(); string parentmodelname = ""; string parentURI = ""; set<string> uniqueModels; while (parent != NULL && parent->getTypeCode() != SBML_DOCUMENT) { if (parent->getTypeCode() == SBML_COMP_SUBMODEL) { const Submodel* parentsub = static_cast<const Submodel*>(parent); uniqueModels.insert(parentsub->mInstantiationOriginalURI + "::" + parentsub->getModelRef()); if (parentURI=="") { parentURI=parentsub->mInstantiationOriginalURI; } } if (parent->getTypeCode() == SBML_MODEL || parent->getTypeCode() == SBML_COMP_MODELDEFINITION) { if (parentmodelname == "") { parentmodelname = parent->getId(); } } rootdoc = parent->getSBMLDocument(); parent = parent->getParentSBMLObject(); } if (mInstantiatedModel != NULL) { delete mInstantiatedModel; mInstantiatedModel = NULL; mInstantiationOriginalURI.clear(); } if (!hasRequiredAttributes()) { string error = "Instantiation error in Submodel::instantiate: "; if (!isSetId()) { error += "A submodel in model '" + getParentModel(this)->getId() + "' does not have an 'id' attribute."; } else if (!isSetModelRef()) { error += "The submodel '" + getId() + "' does not have a 'modelRef' attribute."; } rootdoc->getErrorLog()->logPackageError("comp", CompSubmodelAllowedAttributes, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); return LIBSBML_INVALID_OBJECT; } SBase* origmodel = docplugin->getModel(getModelRef()); if (origmodel==NULL) { string error = "In Submodel::instantiate, unable to instantiate submodel '" + getId() + "' because the referenced model ('" + getModelRef() +"') does not exist."; rootdoc->getErrorLog()->logPackageError("comp", CompSubmodelMustReferenceModel, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); return LIBSBML_INVALID_OBJECT; } ExternalModelDefinition* extmod; SBMLDocument* origdoc = NULL; string newmodel = parentURI + "::" + getModelRef(); set<pair<string, string> > parents; switch(origmodel->getTypeCode()) { case SBML_MODEL: case SBML_COMP_MODELDEFINITION: origdoc = origmodel->getSBMLDocument(); mInstantiatedModel = static_cast<Model*>(origmodel)->clone(); if (uniqueModels.insert(newmodel).second == false) { //Can't instantiate this model, because we are already a child of it. string error = "Error in Submodel::instantiate: cannot instantiate submodel '" + getId() + "' in model '" + parentmodelname + "' because it references the model '" + getModelRef() + "', which is already an ancestor of the submodel."; rootdoc->getErrorLog()->logPackageError("comp", CompSubmodelCannotReferenceSelf, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); return LIBSBML_OPERATION_FAILED; } mInstantiationOriginalURI = parentURI; break; case SBML_COMP_EXTERNALMODELDEFINITION: extmod = static_cast<ExternalModelDefinition*>(origmodel); if (extmod==NULL) { //No error message: it should be impossible, if origmodel has the type code 'external model definition', for it to not be castable to an external model definition. mInstantiatedModel = NULL; mInstantiationOriginalURI = ""; return LIBSBML_OPERATION_FAILED; } mInstantiatedModel = extmod->getReferencedModel(rootdoc, parents); if (mInstantiatedModel == NULL) { string error = "In Submodel::instantiate, unable to instantiate submodel '" + getId() + "' because the external model definition it referenced (model '" + getModelRef() +"') could not be resolved."; rootdoc->getErrorLog()->logPackageError("comp", CompSubmodelMustReferenceModel, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); mInstantiationOriginalURI = ""; return LIBSBML_OPERATION_FAILED; } mInstantiationOriginalURI = extmod->getSource(); origdoc = mInstantiatedModel->getSBMLDocument(); newmodel = extmod->getSource() + "::" + getModelRef(); if (uniqueModels.insert(newmodel).second == false) { //Can't instantiate this model, because we are already a child of it. string error = "Error in Submodel::instantiate: cannot instantiate submodel '" + getId() + "' in model '" + parentmodelname + "' because it references the model '" + getModelRef() + "', which is already an ancestor of the submodel."; rootdoc->getErrorLog()->logPackageError("comp", CompSubmodelCannotReferenceSelf, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); mInstantiatedModel = NULL; mInstantiationOriginalURI = ""; return LIBSBML_OPERATION_FAILED; } mInstantiatedModel = mInstantiatedModel->clone(); mInstantiationOriginalURI = extmod->getSource(); break; default: //Should always be one of the above, unless someone extends one of the above and doesn't tell us. string error = "Instantiation error in Submodel::instantiate: unable to parse the model '" + origmodel->getId() + "', as it was not of the type 'model' 'modelDefinition', or 'externalModelDefinition'. The most likely cause of this situation is if some other package extended one of those three types, but the submodel code was not updated."; rootdoc->getErrorLog()->logPackageError("comp", CompUnresolvedReference, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); mInstantiatedModel = NULL; mInstantiationOriginalURI = ""; return LIBSBML_OPERATION_FAILED; } if (mInstantiatedModel==NULL) { string error = "Instantiation error in Submodel::instantiate: unable to create a valid copy of model '" + getModelRef() + "'."; rootdoc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); mInstantiationOriginalURI = ""; return LIBSBML_OPERATION_FAILED; } mInstantiatedModel->connectToParent(this); mInstantiatedModel->setSBMLDocument(origdoc); mInstantiatedModel->enablePackage(getPackageURI(), getPrefix(), true); CompModelPlugin* instmodplug = static_cast<CompModelPlugin*>(mInstantiatedModel->getPlugin(getPrefix())); if (instmodplug == NULL) { mInstantiatedModel->enablePackageInternal(getPackageURI(), getPrefix(), true); } // call all registered callbacks std::vector<ModelProcessingCallbackData*>::iterator it = mProcessingCBs.begin(); while(it != mProcessingCBs.end()) { ModelProcessingCallbackData* current = *it; int result = current->cb(mInstantiatedModel, rootdoc->getErrorLog(), current->data); if (result != LIBSBML_OPERATION_SUCCESS) return result; ++it; } CompModelPlugin* origmodplug = static_cast<CompModelPlugin*>(rootdoc->getModel()->getPlugin(getPrefix())); instmodplug = static_cast<CompModelPlugin*>(mInstantiatedModel->getPlugin(getPrefix())); if (instmodplug == NULL) return LIBSBML_OPERATION_SUCCESS; // if we have a transformer specified, then we need to propagate it, so it can // be used if (origmodplug->isSetTransformer()) { if (instmodplug != NULL) instmodplug->setTransformer(origmodplug->getTransformer()); } for (unsigned int sub=0; sub<instmodplug->getNumSubmodels(); sub++) { Submodel* instsub = instmodplug->getSubmodel(sub); int ret = instsub->instantiate(); if (ret != LIBSBML_OPERATION_SUCCESS) { //'instantiate' already sets its own error messages. delete mInstantiatedModel; mInstantiatedModel = NULL; mInstantiationOriginalURI = ""; return ret; } } return LIBSBML_OPERATION_SUCCESS; }
/* * Constructs and returns a UnitDefinition that expresses the units of this * Parameter. */ UnitDefinition * Parameter::getDerivedUnitDefinition() { /* if we have the whole model but it is not in a document * it is still possible to determine the units */ Model * m = static_cast <Model *> (getAncestorOfType(SBML_MODEL)); if (m != NULL) { if (!m->isPopulatedListFormulaUnitsData()) { m->populateListFormulaUnitsData(); } /* need to distinguish between a global and local parameter * for a global parameter a unit definition will have been created * for a local parameter need to create one based on the units field */ bool globalParameter = false; SBase *parent = getParentSBMLObject(); SBase *pparent = (parent) ? parent->getParentSBMLObject() : NULL; if (pparent != NULL && dynamic_cast<Model*>(pparent) != 0) globalParameter = true; if (globalParameter) { if (m->getFormulaUnitsData(getId(), getTypeCode()) != NULL) { return m->getFormulaUnitsData(getId(), getTypeCode()) ->getUnitDefinition(); } else { return NULL; } } else { UnitDefinition *ud = NULL; const char * units = getUnits().c_str(); if (!strcmp(units, "")) { ud = new UnitDefinition(getSBMLNamespaces()); return ud; } else { if (UnitKind_isValidUnitKindString(units, getLevel(), getVersion())) { Unit * unit = new Unit(getSBMLNamespaces()); unit->setKind(UnitKind_forName(units)); unit->initDefaults(); ud = new UnitDefinition(getSBMLNamespaces()); ud->addUnit(unit); delete unit; } else { /* must be a unit definition */ ud = static_cast <Model *> (getAncestorOfType(SBML_MODEL))->getUnitDefinition(units); } return ud; } } } else { return NULL; } }
// tests whether we are importing global render information void test000098::test_export_notes() { CPPUNIT_ASSERT(pDataModel != NULL); std::istringstream iss(test000098::CPS_MODEL_1); CPPUNIT_ASSERT(load_cps_model_from_stream(iss, *pDataModel) == true); std::string s; try { s = pDataModel->exportSBMLToString(NULL, 2, 1); } catch (...) { CPPUNIT_ASSERT(false); } CPPUNIT_ASSERT(!s.empty()); SBMLReader reader; SBMLDocument* pSBMLDocument = NULL; try { pSBMLDocument = reader.readSBMLFromString(s); } catch (...) { CPPUNIT_ASSERT(false); } CPPUNIT_ASSERT(pSBMLDocument != NULL); Model* pModel = pSBMLDocument->getModel(); CPPUNIT_ASSERT(pModel != NULL); CPPUNIT_ASSERT(pModel->isSetNotes() == true); std::string notes = pModel->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on model") != std::string::npos); CPPUNIT_ASSERT(pModel->getNumCompartments() == 1); CPPUNIT_ASSERT(pModel->getNumSpecies() == 1); CPPUNIT_ASSERT(pModel->getNumParameters() == 1); CPPUNIT_ASSERT(pModel->getNumReactions() == 1); CPPUNIT_ASSERT(pModel->getNumEvents() == 1); // compartment SBase* pObject = pModel->getCompartment(0); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isSetNotes() == true); notes = pObject->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on compartment") != std::string::npos); // species pObject = pModel->getSpecies(0); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isSetNotes() == true); notes = pObject->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on species") != std::string::npos); // parameter pObject = pModel->getParameter(0); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isSetNotes() == true); notes = pObject->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on parameter") != std::string::npos); // reaction pObject = pModel->getReaction(0); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isSetNotes() == true); notes = pObject->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on reaction") != std::string::npos); // event pObject = pModel->getEvent(0); CPPUNIT_ASSERT(pObject != NULL); CPPUNIT_ASSERT(pObject->isSetNotes() == true); notes = pObject->getNotesString(); CPPUNIT_ASSERT(!notes.empty()); CPPUNIT_ASSERT(notes.find("Simple note on event") != std::string::npos); }
int SBMLIdConverter::convert() { if (mDocument == NULL) return LIBSBML_INVALID_OBJECT; Model* mModel = mDocument->getModel(); if (mModel == NULL) return LIBSBML_INVALID_OBJECT; // nothing to do if (!mProps->hasOption("currentIds") || !mProps->hasOption("newIds")) return LIBSBML_OPERATION_SUCCESS; bool success = true; IdList currentIds(mProps->getOption("currentIds")->getValue()); IdList newIds(mProps->getOption("newIds")->getValue()); // if the size does not match something is wrong. if (newIds.size() != currentIds.size()) return LIBSBML_UNEXPECTED_ATTRIBUTE; List* allElements = mDocument->getAllElements(); std::map<std::string, std::string> renamed; // rename ids for (unsigned int i = 0; i < allElements->getSize(); ++i) { SBase* current = static_cast<SBase*>(allElements->get(i)); if (current == NULL || !current->isSetId() || current->getTypeCode() == SBML_LOCAL_PARAMETER) continue; for (unsigned int j = 0; j < currentIds.size(); ++j) { if (current->getId() != currentIds.at((int)j)) continue; // return error code in case new id is invalid if (!SyntaxChecker::isValidSBMLSId(newIds.at((int)j))) { delete allElements; return LIBSBML_INVALID_ATTRIBUTE_VALUE; } current->setId(newIds.at((int)j)); renamed[currentIds.at((int)j)] = newIds.at((int)j); break; } } // update all references that we changed std::map<std::string, std::string>::const_iterator it; for (unsigned int i = 0; i < allElements->getSize(); ++i) { SBase* current = static_cast<SBase*>(allElements->get(i)); for (it = renamed.begin(); it != renamed.end(); ++it) { current->renameSIdRefs(it->first, it->second); } } delete allElements; if (success) return LIBSBML_OPERATION_SUCCESS; return LIBSBML_OPERATION_FAILED; }
//static void SBMLUtils::collectIds(Model* pModel, std::map<std::string, const SBase*>& ids, std::map<std::string, const SBase*>& metaIds) { if (pModel != NULL) { // the model itself SBase* pSBase = NULL; std::string id; if (pModel->isSetId()) { id = pModel->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pModel->isSetMetaId()) { id = pModel->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // ListOfFunctionDefinitions pSBase = pModel->getListOfFunctionDefinitions(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // all FunctionDefinitions unsigned int i, iMax = pModel->getListOfFunctionDefinitions()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getListOfFunctionDefinitions()->get(i); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfUnitDefinition pSBase = pModel->getListOfUnitDefinitions(); if (pSBase != NULL) { if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // all UnitDefinitions // for each UnitDefinition: ListOfUnits, each Unit in ListOfUnits unsigned int i, iMax = pModel->getListOfUnitDefinitions()->size(); for (i = 0; i < iMax; ++i) { /* UnitDefinitions have their ids in a different namespace so we only consider meta ids. */ UnitDefinition* pUDef = pModel->getUnitDefinition(i); assert(pUDef != NULL); if (pUDef->isSetMetaId()) { id = pUDef->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pUDef)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } ListOf* pList = pUDef->getListOfUnits(); if (pList != NULL) { if (pList->isSetMetaId()) { id = pList->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pList)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned j, jMax = pList->size(); for (j = 0; j < jMax; ++j) { pSBase = pList->get(j); assert(pSBase != NULL); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } } } // ListOfCompartmentTypes pSBase = pModel->getListOfCompartmentTypes(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each compartment type unsigned int i, iMax = pModel->getListOfCompartmentTypes()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getCompartmentType(i); assert(pSBase != NULL); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfSpeciesTypes pSBase = pModel->getListOfSpeciesTypes(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each species type unsigned int i, iMax = pModel->getListOfSpeciesTypes()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getSpeciesType(i); assert(pSBase != NULL); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfCompartments pSBase = pModel->getListOfCompartments(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // all compartments unsigned int i, iMax = pModel->getListOfCompartments()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getCompartment(i); assert(pSBase != NULL); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfSpecies pSBase = pModel->getListOfSpecies(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // all species unsigned int i, iMax = pModel->getListOfSpecies()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getSpecies(i); assert(pSBase != NULL); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfParameters pSBase = pModel->getListOfParameters(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each parameter unsigned int i, iMax = pModel->getListOfParameters()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getParameter(i); assert(pSBase != NULL); if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfInitialAssignments pSBase = pModel->getListOfInitialAssignments(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each initial assignment unsigned int i, iMax = pModel->getListOfInitialAssignments()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getInitialAssignment(i); assert(pSBase != NULL); // initial assignments have no ids if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfRules pSBase = pModel->getListOfRules(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each rule unsigned int i, iMax = pModel->getListOfRules()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getRule(i); assert(pSBase != NULL); // rules don't have ids if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfConstraints pSBase = pModel->getListOfConstraints(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each constraint unsigned int i, iMax = pModel->getListOfConstraints()->size(); for (i = 0; i < iMax; ++i) { pSBase = pModel->getConstraint(i); assert(pSBase != NULL); // constraints don't have ids if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } // ListOfReactions pSBase = pModel->getListOfReactions(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // all reactions unsigned int i, iMax = pModel->getListOfReactions()->size(); for (i = 0; i < iMax; ++i) { Reaction* pReaction = pModel->getReaction(i); assert(pReaction != NULL); if (pReaction->isSetId()) { id = pReaction->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pReaction)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pReaction->isSetMetaId()) { id = pReaction->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pReaction)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // for each reaction: ListOfSubstrates, each substrate, ListOfProducts, each // Product, ListOfModifieres, each modifier, KineticLaw, ListOfparameters, // each parameter if (pReaction->getListOfReactants() != NULL) { pSBase = pReaction->getListOfReactants(); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned int j, jMax = pReaction->getListOfReactants()->size(); for (j = 0; j < jMax; ++j) { pSBase = pReaction->getReactant(j); assert(pSBase != NULL); // since L2V2 species references can have ids if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } if (pReaction->getListOfProducts() != NULL) { pSBase = pReaction->getListOfProducts(); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned int j, jMax = pReaction->getListOfProducts()->size(); for (j = 0; j < jMax; ++j) { pSBase = pReaction->getProduct(j); assert(pSBase != NULL); // since L2V2 species references can have ids if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } if (pReaction->getListOfModifiers() != NULL) { pSBase = pReaction->getListOfModifiers(); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned int j, jMax = pReaction->getListOfModifiers()->size(); for (j = 0; j < jMax; ++j) { pSBase = pReaction->getModifier(j); assert(pSBase != NULL); // since L2V2 species references can have ids if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } KineticLaw* pKLaw = pReaction->getKineticLaw(); if (pKLaw != NULL) { if (pKLaw->isSetMetaId()) { id = pKLaw->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pKLaw)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } pSBase = pKLaw->getListOfParameters(); if (pSBase != NULL) { if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned int j, jMax = pKLaw->getListOfParameters()->size(); for (j = 0; j < jMax; ++j) { pSBase = pKLaw->getParameter(j); assert(pSBase != NULL); // local parameters have their ids in a // different namespace if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } } } } // ListOfEvents pSBase = pModel->getListOfEvents(); if (pSBase != NULL) { if (pSBase->isSetId()) { id = pSBase->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pModel)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // each event unsigned int i, iMax = pModel->getListOfEvents()->size(); for (i = 0; i < iMax; ++i) { Event* pEvent = pModel->getEvent(i); assert(pEvent != NULL); if (pEvent->isSetId()) { id = pEvent->getId(); if (ids.find(id) == ids.end()) { ids.insert(std::pair<const std::string, const SBase*>(id, pEvent)); } else { CCopasiMessage(CCopasiMessage::EXCEPTION, MCSBML + 68, id.c_str()); } } if (pEvent->isSetMetaId()) { id = pEvent->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pEvent)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } // in each event Trigger,Delay,ListOfEventAssignments, each event assignment if (pEvent->isSetTrigger()) { pSBase = pEvent->getTrigger(); assert(pSBase != NULL); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } if (pEvent->isSetDelay()) { pSBase = pEvent->getDelay(); assert(pSBase != NULL); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } if (pEvent->getListOfEventAssignments() != NULL) { pSBase = pEvent->getListOfEventAssignments(); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } unsigned int j, jMax = pEvent->getListOfEventAssignments()->size(); for (j = 0; j < jMax; ++j) { pSBase = pEvent->getEventAssignment(j); assert(pSBase != NULL); if (pSBase->isSetMetaId()) { id = pSBase->getMetaId(); if (metaIds.find(id) == metaIds.end()) { metaIds.insert(std::pair<const std::string, const SBase*>(id, pSBase)); } else { CCopasiMessage(CCopasiMessage::WARNING, MCSBML + 67, id.c_str()); } } } } } } } }
bool UniPAX::SBase::merge(SBase& object) { if (metaId.empty()) { metaId = object.getMetaId(); } else { if (metaId.compare(object.getMetaId()) != 0) { std::cerr << "Error during merging: UniPAX::SBase::metaId not equal ..." << metaId << " != " << object.getMetaId() << std::endl; return false; } } if (sboTerm.empty()) { sboTerm = object.getSboTerm(); } else { if (sboTerm.compare(object.getSboTerm()) != 0) { std::cerr << "Error during merging: UniPAX::SBase::sboTerm not equal ..." << sboTerm << " != " << object.getSboTerm() << std::endl; return false; } } if (notes.empty()) { notes = object.getNotes(); } else { if (notes.compare(object.getNotes()) != 0) { std::cerr << "Error during merging: UniPAX::SBase::notes not equal ..." << notes << " != " << object.getNotes() << std::endl; return false; } } if (annotation.empty()) { annotation = object.getAnnotation(); } else { if (annotation.compare(object.getAnnotation()) != 0) { std::cerr << "Error during merging: UniPAX::SBase::annotation not equal ..." << annotation << " != " << object.getAnnotation() << std::endl; return false; } } // if (unipaxSource != 0) // { // if (object.getUnipaxSource() != 0) // { // // if (unipaxSource->getUnipaxId() != object.getUnipaxSource()->getUnipaxId()) // { // std::cerr << "Error during merging: UniPAX::SBase::unipaxSource not equal ..." // << unipaxSource->getUnipaxId() << " != " << object.getUnipaxSource()->getUnipaxId() << std::endl; // return false; // } // } // } // else // { // unipaxSource = object.getUnipaxSource(); // } return UniPAX::UIBase::merge(object); }
LIBSBML_CPP_NAMESPACE_USE BEGIN_C_DECLS int main (int argc, char* argv[]) { if (argc != 5) { cout << endl << "Usage: renameSId filename oldSId newSId output" << endl << endl; return 1; } const char* filename = argv[1]; const char* oldSId = argv[2]; const char* newSId = argv[3]; const char* output = argv[4]; if (strcmp(oldSId, newSId) == 0) { cout << "The Ids are identical, renaming stopped." << endl; return 1; } if (!SyntaxChecker::isValidInternalSId(newSId)) { cout << "The new SId '" << newSId << "' does not represent a valid SId." << endl; return 1; } SBMLDocument* document; SBMLReader reader; #ifdef __BORLANDC__ unsigned long start, stop; #else unsigned long long start, stop; #endif start = getCurrentMillis(); document = reader.readSBML(filename); stop = getCurrentMillis(); unsigned int errors = document->getNumErrors(LIBSBML_SEV_ERROR); cout << endl; cout << " filename: " << filename << endl; cout << " file size: " << getFileSize(filename) << endl; cout << " read time (ms): " << stop - start << endl; cout << " error(s): " << errors << endl; cout << endl; if (errors > 0) { document->printErrors(cerr); delete document; return errors; } // find elements for old id SBase* element = document->getElementBySId(oldSId); if (element == NULL) { cout << "Found no element with SId '" << oldSId << "'." << endl; return 1; } // found element --> renaming element->setId(newSId); // update all references to this element List *allElements = document->getAllElements(); for (unsigned int i = 0; i < allElements->getSize(); ++i) static_cast<SBase*>(allElements->get(i))->renameSIdRefs(oldSId, newSId); // write to file writeSBMLToFile(document, output); delete document; return errors; }
END_TEST START_TEST (test_GetMultipleObjects_getMetaId) { SBMLReader reader; SBMLDocument* d; std::string filename(TestDataDirectory); filename += "multiple-ids.xml"; d = reader.readSBML(filename); if (d == NULL) { fail("readSBML(\"multiple-ids.xml\") returned a NULL pointer."); } SBase* obj = d->getElementByMetaId("no_id"); fail_unless(obj == NULL); obj = d->getElementByMetaId(""); fail_unless(obj == NULL); obj = d->getElementByMetaId("meta1"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_DOCUMENT); obj = d->getElementByMetaId("meta2"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_MODEL); obj = d->getElementByMetaId("meta3"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_COMPARTMENT); obj = d->getElementByMetaId("meta4"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_COMPARTMENT); obj = d->getElementByMetaId("meta5"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_SPECIES); obj = d->getElementByMetaId("meta6"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_SPECIES); obj = d->getElementByMetaId("meta7"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_REACTION); obj = d->getElementByMetaId("meta8"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_REACTION); obj = d->getElementByMetaId("meta9"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_SPECIES_REFERENCE); obj = d->getElementByMetaId("meta10"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_SPECIES_REFERENCE); obj = d->getElementByMetaId("meta11"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_KINETIC_LAW); obj = d->getElementByMetaId("meta12"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_EVENT); obj = d->getElementByMetaId("meta13"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_EVENT); obj = d->getElementByMetaId("meta14"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_TRIGGER); obj = d->getElementByMetaId("meta15"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_EVENT_ASSIGNMENT); obj = d->getElementByMetaId("meta16"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_EVENT_ASSIGNMENT); obj = d->getElementByMetaId("meta17"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_DELAY); obj = d->getElementByMetaId("meta18"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_PARAMETER); obj = d->getElementByMetaId("meta19"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_PRIORITY); obj = d->getElementByMetaId("meta20"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_FUNCTION_DEFINITION); obj = d->getElementByMetaId("meta21"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_FUNCTION_DEFINITION); obj = d->getElementByMetaId("meta22"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_INITIAL_ASSIGNMENT); obj = d->getElementByMetaId("meta23"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_INITIAL_ASSIGNMENT); obj = d->getElementByMetaId("meta24"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_RULE); obj = d->getElementByMetaId("meta25"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_RATE_RULE); obj = d->getElementByMetaId("meta26"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_ASSIGNMENT_RULE); obj = d->getElementByMetaId("meta27"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_LOCAL_PARAMETER); obj = d->getElementByMetaId("meta28"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LOCAL_PARAMETER); obj = d->getElementByMetaId("meta29"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_UNIT_DEFINITION); obj = d->getElementByMetaId("meta30"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_UNIT_DEFINITION); obj = d->getElementByMetaId("meta31"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_UNIT); obj = d->getElementByMetaId("meta32"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_UNIT); obj = d->getElementByMetaId("meta33"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_PARAMETER); obj = d->getElementByMetaId("meta34"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_LIST_OF); fail_unless(static_cast<ListOf*>(obj)->getItemTypeCode() == SBML_MODIFIER_SPECIES_REFERENCE); obj = d->getElementByMetaId("meta35"); fail_unless(obj != NULL); fail_unless(obj->getTypeCode() == SBML_MODIFIER_SPECIES_REFERENCE); delete d; }
virtual bool visit(const SBase &x) { if (&x == NULL || x.getPackageName() != "multi") { return SBMLVisitor::visit(x); } int code = x.getTypeCode(); const ListOf* list = dynamic_cast<const ListOf*>(&x); if (list != NULL) { return SBMLVisitor::visit(x); } else { if (code == SBML_MULTI_POSSIBLE_SPECIES_FEATURE_VALUE) { return visit((const PossibleSpeciesFeatureValue&)x); } else if (code == SBML_MULTI_SPECIES_FEATURE_VALUE) { return visit((const SpeciesFeatureValue&)x); } else if (code == SBML_MULTI_COMPARTMENT_REFERENCE) { return visit((const CompartmentReference&)x); } else if (code == SBML_MULTI_SPECIES_TYPE_INSTANCE) { return visit((const SpeciesTypeInstance&)x); } else if (code == SBML_MULTI_IN_SPECIES_TYPE_BOND) { return visit((const InSpeciesTypeBond&)x); } else if (code == SBML_MULTI_DENOTED_SPECIES_TYPE_COMPONENT_INDEX) { return visit((const DenotedSpeciesTypeComponentIndex&)x); } else if (code == SBML_MULTI_OUTWARD_BINDING_SITE) { return visit((const OutwardBindingSite&)x); } else if (code == SBML_MULTI_SPECIES_FEATURE_CHANGE) { return visit((const SpeciesFeatureChange&)x); } else if (code == SBML_MULTI_SPECIES_FEATURE_TYPE) { return visit((const SpeciesFeatureType&)x); } else if (code == SBML_MULTI_SPECIES_TYPE_COMPONENT_INDEX) { return visit((const SpeciesTypeComponentIndex&)x); } else if (code == SBML_MULTI_SPECIES_FEATURE) { return visit((const SpeciesFeature&)x); } else if (code == SBML_MULTI_SPECIES_TYPE_COMPONENT_MAP_IN_PRODUCT) { return visit((const SpeciesTypeComponentMapInProduct&)x); } else if (code == SBML_MULTI_SPECIES_TYPE) { return visit((const MultiSpeciesType&)x); } else { return SBMLVisitor::visit(x); } } }
void Model::dealWithModelUnits() { UnitRefsFilter filter; List * elements = getAllElements(&filter); unsigned int n = 0; unsigned int num = elements->getSize(); if (isSetVolumeUnits()) { std::string volume = getVolumeUnits(); // if in an L3 model a user used volume as an id of a UnitDefinition // but they declared the volume units of teh model to be something // else then the UD with id volume is nothing to do with the // L2 interpretation of volume // so replace that UD and all references to it if (volume != "volume") { UnitDefinition * existingUD = removeUnitDefinition("volume"); if (existingUD != NULL) { std::string newSubsName = "volumeFromOriginal"; existingUD->setId(newSubsName); SBase* obj; for (n = 0; n < num; n++) { obj = (SBase*)(elements->get(n)); obj->renameUnitSIdRefs("volume", newSubsName); } addUnitDefinition(existingUD); } } UnitDefinition * ud = getUnitDefinition(volume) != NULL ? getUnitDefinition(volume)->clone() : NULL; if (ud != NULL) { ud->setId("volume"); } else { Unit *u = new Unit(getSBMLNamespaces()); u->initDefaults(); u->setKind(UnitKind_forName(volume.c_str())); ud = new UnitDefinition(getSBMLNamespaces()); ud->setId("volume"); ud->addUnit(u); } addUnitDefinition(ud); } if (isSetAreaUnits()) { std::string area = getAreaUnits(); // if in an L3 model a user used area as an id of a UnitDefinition // but they declared the area units of teh model to be something // else then the UD with id area is nothing to do with the // L2 interpretation of area // so replace that UD and all references to it if (area != "area") { UnitDefinition * existingUD = removeUnitDefinition("area"); if (existingUD != NULL) { std::string newSubsName = "areaFromOriginal"; existingUD->setId(newSubsName); SBase* obj; for (n = 0; n < num; n++) { obj = (SBase*)(elements->get(n)); obj->renameUnitSIdRefs("area", newSubsName); } addUnitDefinition(existingUD); } } UnitDefinition * ud = getUnitDefinition(area) != NULL ? getUnitDefinition(area)->clone() : NULL; if (ud != NULL) { ud->setId("area"); } else { Unit *u = new Unit(getSBMLNamespaces()); u->initDefaults(); u->setKind(UnitKind_forName(area.c_str())); ud = new UnitDefinition(getSBMLNamespaces()); ud->setId("area"); ud->addUnit(u); } addUnitDefinition(ud); } if (isSetLengthUnits()) { std::string length = getLengthUnits(); // if in an L3 model a user used length as an id of a UnitDefinition // but they declared the length units of teh model to be something // else then the UD with id length is nothing to do with the // L2 interpretation of length // so replace that UD and all references to it if (length != "length") { UnitDefinition * existingUD = removeUnitDefinition("length"); if (existingUD != NULL) { std::string newSubsName = "lengthFromOriginal"; existingUD->setId(newSubsName); SBase* obj; for (n = 0; n < num; n++) { obj = (SBase*)(elements->get(n)); obj->renameUnitSIdRefs("length", newSubsName); } addUnitDefinition(existingUD); } } UnitDefinition * ud = getUnitDefinition(length) != NULL ? getUnitDefinition(length)->clone() : NULL; if (ud != NULL) { ud->setId("length"); } else { Unit *u = new Unit(getSBMLNamespaces()); u->initDefaults(); u->setKind(UnitKind_forName(length.c_str())); ud = new UnitDefinition(getSBMLNamespaces()); ud->setId("length"); ud->addUnit(u); } addUnitDefinition(ud); } if (isSetSubstanceUnits()) { std::string substance = getSubstanceUnits(); // if in an L3 model a user used substance as an id of a UnitDefinition // but they declared the substance units of teh model to be something // else then the UD with id substance is nothing to do with the // L2 interpretation of substance // so replace that UD and all references to it if (substance != "substance") { UnitDefinition * existingUD = removeUnitDefinition("substance"); if (existingUD != NULL) { std::string newSubsName = "substanceFromOriginal"; existingUD->setId(newSubsName); SBase* obj; for (n = 0; n < num; n++) { obj = (SBase*)(elements->get(n)); obj->renameUnitSIdRefs("substance", newSubsName); } addUnitDefinition(existingUD); } } UnitDefinition * ud = getUnitDefinition(substance) != NULL ? getUnitDefinition(substance)->clone() : NULL; if (ud != NULL) { ud->setId("substance"); } else { Unit *u = new Unit(getSBMLNamespaces()); u->initDefaults(); u->setKind(UnitKind_forName(substance.c_str())); ud = new UnitDefinition(getSBMLNamespaces()); ud->setId("substance"); ud->addUnit(u); } addUnitDefinition(ud); } if (isSetTimeUnits()) { std::string time = getTimeUnits(); // if in an L3 model a user used time as an id of a UnitDefinition // but they declared the time units of teh model to be something // else then the UD with id time is nothing to do with the // L2 interpretation of time // so replace that UD and all references to it if (time != "time") { UnitDefinition * existingUD = removeUnitDefinition("time"); if (existingUD != NULL) { std::string newSubsName = "timeFromOriginal"; existingUD->setId(newSubsName); SBase* obj; for (n = 0; n < num; n++) { obj = (SBase*)(elements->get(n)); obj->renameUnitSIdRefs("time", newSubsName); } addUnitDefinition(existingUD); } } UnitDefinition * ud = getUnitDefinition(time) != NULL ? getUnitDefinition(time)->clone() : NULL; if (ud != NULL) { ud->setId("time"); } else { ud = new UnitDefinition(getSBMLNamespaces()); ud->setId("time"); Unit *u = ud->createUnit(); u->initDefaults(); u->setKind(UnitKind_forName(time.c_str())); } addUnitDefinition(ud); } }
int ReplacedElement::performReplacementAndCollect(set<SBase*>* removed, set<SBase*>* toremove) { SBMLDocument* doc = getSBMLDocument(); if (isSetDeletion()) { //Deletions don't need to be replaced. return LIBSBML_OPERATION_SUCCESS; } //Find the various objects and plugin objects we need for this to work. SBase* lore = getParentSBMLObject(); ListOf* lorelist = static_cast<ListOf*>(lore); if (lore == NULL) { if (doc) { string error = "Cannot carry out replacement in ReplacedElement::performReplacement: no parent <listOfReplacedElements> could be found for the given replacement element."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } if (lore->getTypeCode() != SBML_LIST_OF || lorelist->getItemTypeCode() != SBML_COMP_REPLACEDELEMENT) { if (doc) { string error = "Cannot carry out replacement in ReplacedElement::performReplacement: no parent <listOfReplacedElements> could be found for the given replacement element."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } SBase* parent = lore->getParentSBMLObject(); if (parent==NULL) { if (doc) { string error = "Cannot carry out replacement in ReplacedElement::performReplacement: no parent could be found for the parent <listOfReplacedElements> object."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } SBase* ref = getReferencedElement(); if (ref==NULL) { //getReferencedElement sets its own error messages. return LIBSBML_INVALID_OBJECT; } if (removed && removed->find(ref)!=removed->end()) { //Already deleted: can't get the deleted element's ID to if (doc) { string error = "Cannot carry out replacement in ReplacedElement::performReplacement: a <" + parent->getElementName() + ">"; if (parent->isSetId()) { error += " with the ID '" + parent->getId() + "'"; } error += " has a child <replacedElement> that points to something that has already been deleted, probably because its parent was deleted."; doc->getErrorLog()->logPackageError("comp", CompDeletedReplacement, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return LIBSBML_INVALID_OBJECT; } //Update the IDs. int ret = updateIDs(ref, parent); if (ret != LIBSBML_OPERATION_SUCCESS) { return ret; } //Perform any conversions on references in the submodel. ASTNode* blank = NULL; ret = performConversions(parent, &blank); if (ret != LIBSBML_OPERATION_SUCCESS) { if (blank != NULL) { delete blank; } return ret; } CompSBasePlugin* refplug = static_cast<CompSBasePlugin*>(ref->getPlugin(getPrefix())); if (refplug != NULL) { //Now recurse down the 'replace*' tree, renaming IDs and deleting things as we go. for (unsigned int re=0; re<refplug->getNumReplacedElements(); re++) { refplug->getReplacedElement(re)->replaceWithAndMaybeDelete(parent, true, blank); if (toremove) { toremove->insert(refplug->getReplacedElement(re)->getReferencedElement()); } } if (refplug->isSetReplacedBy()) { //Even if the subelement used to be replaced by something further down, it is now being replaced by the parent. It just can't catch a break, it seems. refplug->getReplacedBy()->replaceWithAndMaybeDelete(parent, true, blank); if (toremove) { toremove->insert(refplug->getReplacedBy()->getReferencedElement()); } } } if (toremove) { toremove->insert(ref); } if (blank != NULL) { delete blank; } return LIBSBML_OPERATION_SUCCESS; }
int Submodel::convertTimeAndExtentWith(const ASTNode* tcf, const ASTNode* xcf, const ASTNode* klmod) { if (tcf==NULL && xcf==NULL) return LIBSBML_OPERATION_SUCCESS; Model* model = getInstantiation(); if (model==NULL) { //getInstantiation sets its own error messages. return LIBSBML_OPERATION_FAILED; } ASTNode tcftimes(AST_TIMES); ASTNode tcfdiv(AST_DIVIDE); if (tcf != NULL) { tcftimes.addChild(tcf->deepCopy()); tcfdiv.addChild(tcf->deepCopy()); } ASTNode rxndivide(AST_DIVIDE); if (klmod != NULL) { ASTNode rxnref(AST_NAME); rxndivide.addChild(rxnref.deepCopy()); rxndivide.addChild(klmod->deepCopy()); } List* allElements = model->getAllElements(); for (ListIterator iter = allElements->begin(); iter != allElements->end(); ++iter) { SBase* element = static_cast<SBase*>(*iter); assert(element != NULL); ASTNode* ast1 = NULL; ASTNode* ast2 = NULL; Constraint* constraint = NULL; Delay* delay = NULL; EventAssignment* ea = NULL; InitialAssignment* ia = NULL; KineticLaw* kl = NULL; Priority* priority = NULL; RateRule* rrule = NULL; Rule* rule = NULL; Submodel* submodel = NULL; Trigger* trigger = NULL; string cf = ""; //Reaction math will be converted below, in the bits with the kinetic law. But because of that, we need to handle references *to* the reaction: even if it has no kinetic law, the units have changed, and this needs to be reflected by the flattening routine. if (rxndivide.getNumChildren() != 0 && element->getTypeCode()==SBML_REACTION && element->isSetId()) { rxndivide.getChild(0)->setName(element->getId().c_str()); for (ListIterator iter = allElements->begin(); iter != allElements->end(); ++iter) { SBase* subelement = static_cast<SBase*>(*iter); subelement->replaceSIDWithFunction(element->getId(), &rxndivide); } } //Submodels need their timeConversionFactor and extentConversionFactor attributes converted. We're moving top-down, so all we need to do here is fix the conversion factor attributes themselves, pointing them to new parameters if need be. if ((tcf !=NULL || xcf != NULL) && element->getTypeCode()==SBML_COMP_SUBMODEL) { submodel = static_cast<Submodel*>(element); if (tcf != NULL) { if (submodel->isSetTimeConversionFactor()) { createNewConversionFactor(cf, tcf, submodel->getTimeConversionFactor(), model); submodel->setTimeConversionFactor(cf); } else { submodel->setTimeConversionFactor(tcf->getName()); } } if (xcf != NULL) { if (submodel->isSetExtentConversionFactor()) { createNewConversionFactor(cf, xcf, submodel->getExtentConversionFactor(), model); submodel->setExtentConversionFactor(cf); } else { submodel->setExtentConversionFactor(xcf->getName()); } } } if (tcf==NULL) { if (klmod !=NULL && element->getTypeCode()==SBML_KINETIC_LAW) { kl = static_cast<KineticLaw*>(element); if (kl->isSetMath()) { ast1 = new ASTNode(AST_TIMES); ast1->addChild(klmod->deepCopy()); ast1->addChild(kl->getMath()->deepCopy()); kl->setMath(ast1); delete ast1; } } } else { // All math 'time' and 'delay' csymbols must still be converted. // Also, several constructs are modified directly. switch(element->getTypeCode()) { //This would be a WHOLE LOT SIMPLER if there was a 'hasMath' class in libsbml. But even so, it would have to // handle the kinetic laws, rate rules, and delays separately. case SBML_KINETIC_LAW: //Kinetic laws are multiplied by 'klmod'. kl = static_cast<KineticLaw*>(element); ast1 = kl->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); if (klmod !=NULL) { kl = static_cast<KineticLaw*>(element); if (kl->isSetMath()) { ast2 = new ASTNode(AST_TIMES); ast2->addChild(klmod->deepCopy()); ast2->addChild(ast1); kl->setMath(ast2); delete ast2; } } else { kl->setMath(ast1); delete ast1; } break; case SBML_DELAY: //Delays are multiplied by the time conversion factor. delay = static_cast<Delay*>(element); if (delay->isSetMath()) { ast1 = delay->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); tcftimes.addChild(ast1); delay->setMath(&tcftimes); tcftimes.removeChild(1); delete ast1; } break; case SBML_RATE_RULE: //Rate rules are divided by the time conversion factor. rrule = static_cast<RateRule*>(element); if (rrule->isSetMath()) { ast1 = rrule->getMath()->deepCopy(); tcfdiv.insertChild(0, ast1); rrule->setMath(&tcfdiv); tcfdiv.removeChild(0); delete ast1; } //Fall through to: case SBML_ASSIGNMENT_RULE: case SBML_ALGEBRAIC_RULE: //Rules in general need csymbols converted. rule = static_cast<Rule*>(element); if (rule->isSetMath()) { ast1 = rule->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); rule->setMath(ast1); delete ast1; } break; case SBML_EVENT_ASSIGNMENT: //Event assignments need csymbols converted. ea = static_cast<EventAssignment*>(element); if (ea->isSetMath()) { ast1 = ea->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); ea->setMath(ast1); delete ast1; } break; case SBML_INITIAL_ASSIGNMENT: //Initial assignments need csymbols converted. ia = static_cast<InitialAssignment*>(element); if (ia->isSetMath()) { ast1 = ia->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); ia->setMath(ast1); delete ast1; } break; case SBML_CONSTRAINT: //Constraints need csymbols converted. constraint = static_cast<Constraint*>(element); if (constraint->isSetMath()) { ast1 = constraint->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); constraint->setMath(ast1); delete ast1; } break; case SBML_PRIORITY: //Priorities need csymbols converted. priority = static_cast<Priority*>(element); if (priority->isSetMath()) { ast1 = priority->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); priority->setMath(ast1); delete ast1; } break; case SBML_TRIGGER: //Triggers need csymbols converted. trigger = static_cast<Trigger*>(element); if (trigger->isSetMath()) { ast1 = trigger->getMath()->deepCopy(); convertCSymbols(ast1, &tcfdiv, &tcftimes); trigger->setMath(ast1); delete ast1; } break; default: //Do nothing! If we wanted to call a plugin routine, this would be the place. The only other alternative is to #ifdef some code in here that deals with the math-containing package objects explicitly. Which might be the best option, all told. break; } } } delete allElements; return LIBSBML_OPERATION_SUCCESS; }
END_TEST START_TEST (test_RemoveFromParent_alreadyRemoved) { SBMLReader reader; SBMLDocument* d; std::string filename(TestDataDirectory); filename += "multiple-ids.xml"; d = reader.readSBML(filename); if (d == NULL) { fail("readSBML(\"multiple-ids.xml\") returned a NULL pointer."); } SBase* obj; //List of function definitions obj = d->getElementByMetaId("meta20"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta20"); fail_unless(obj == NULL); //Function definition obj = d->getElementByMetaId("meta21"); fail_unless(obj == NULL); //Unit Definition obj = d->getElementByMetaId("meta30"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta30"); fail_unless(obj == NULL); //Unit obj = d->getElementByMetaId("meta32"); fail_unless(obj == NULL); //List of units obj = d->getElementByMetaId("meta31"); fail_unless(obj == NULL); //List of compartments obj = d->getElementByMetaId("meta3"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta3"); fail_unless(obj == NULL); //Compartment obj = d->getElementByMetaId("meta4"); fail_unless(obj == NULL); //List of species obj = d->getElementByMetaId("meta5"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta5"); fail_unless(obj == NULL); //Species obj = d->getElementByMetaId("meta6"); fail_unless(obj == NULL); //Kinetic law obj = d->getElementByMetaId("meta11"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta11"); fail_unless(obj == NULL); //Local parameter obj = d->getElementByMetaId("meta28"); fail_unless(obj == NULL); //List of local parameters obj = d->getElementByMetaId("meta27"); fail_unless(obj == NULL); //List of modifiers obj = d->getElementByMetaId("meta34"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta34"); fail_unless(obj == NULL); //Modifier species reference obj = d->getElementByMetaId("meta35"); fail_unless(obj == NULL); //Reaction obj = d->getElementByMetaId("meta8"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta8"); fail_unless(obj == NULL); //Species reference obj = d->getElementByMetaId("meta10"); fail_unless(obj == NULL); //List of reactants obj = d->getElementByMetaId("meta9"); fail_unless(obj == NULL); //List of parameters obj = d->getElementByMetaId("meta33"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta33"); fail_unless(obj == NULL); //Parameter obj = d->getElementByMetaId("meta18"); fail_unless(obj == NULL); //List of event assignments obj = d->getElementByMetaId("meta15"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta15"); fail_unless(obj == NULL); //Event assignment obj = d->getElementByMetaId("meta16"); fail_unless(obj == NULL); //Event obj = d->getElementByMetaId("meta13"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta13"); fail_unless(obj == NULL); //Trigger obj = d->getElementByMetaId("meta14"); fail_unless(obj == NULL); //Delay obj = d->getElementByMetaId("meta17"); fail_unless(obj == NULL); //Priority obj = d->getElementByMetaId("meta19"); fail_unless(obj == NULL); //List of initial assignments obj = d->getElementByMetaId("meta22"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta22"); fail_unless(obj == NULL); //Initial assignment obj = d->getElementByMetaId("meta23"); fail_unless(obj == NULL); //List of rules obj = d->getElementByMetaId("meta24"); fail_unless(obj != NULL); fail_unless(obj->removeFromParentAndDelete() == LIBSBML_OPERATION_SUCCESS); obj = d->getElementByMetaId("meta24"); fail_unless(obj == NULL); //Rate rule obj = d->getElementByMetaId("meta25"); fail_unless(obj == NULL); //Assignment rule obj = d->getElementByMetaId("meta26"); fail_unless(obj == NULL); delete d; }
/** * Checks that the units of the power function are consistent * * If inconsistent units are found, an error message is logged. * * The two arguments to power, which are of the form power(a, b) * with the meaning a^b, should be as follows: * (1) if the second argument is an integer, * then the first argument can have any units; * (2) if the second argument b is a rational number n/m, * it must be possible to derive the m-th root of (a{unit})n, * where {unit} signifies the units associated with a; * otherwise, (3) the units of the first argument must be “dimensionless”. * The second argument (b) should always have units of “dimensionless”. * */ void PowerUnitsCheck::checkUnitsFromPower (const Model& m, const ASTNode& node, const SBase & sb, bool inKL, int reactNo) { /* check that node has 2 children */ if (node.getNumChildren() != 2) { return; } double value; UnitDefinition dim(m.getSBMLNamespaces()); Unit unit(m.getSBMLNamespaces()); unit.setKind(UNIT_KIND_DIMENSIONLESS); unit.initDefaults(); dim.addUnit(&unit); UnitFormulaFormatter *unitFormat = new UnitFormulaFormatter(&m); UnitDefinition *tempUD = NULL; UnitDefinition *unitsArg1, *unitsArgPower; unitsArg1 = unitFormat->getUnitDefinition(node.getLeftChild(), inKL, reactNo); unsigned int undeclaredUnits = unitFormat->getContainsUndeclaredUnits(); ASTNode *child = node.getRightChild(); unitFormat->resetFlags(); unitsArgPower = unitFormat->getUnitDefinition(child, inKL, reactNo); unsigned int undeclaredUnitsPower = unitFormat->getContainsUndeclaredUnits(); // The second argument (b) should always have units of “dimensionless”. // or it has undeclared units that we assume are correct if (undeclaredUnitsPower == 0 && !UnitDefinition::areEquivalent(&dim, unitsArgPower)) { logNonDimensionlessPowerConflict(node, sb); } // The first argument is dimensionless then it doesnt matter // what the power is if (undeclaredUnits == 0 && !UnitDefinition::areEquivalent(&dim, unitsArg1)) { // if not argument needs to be an integer or a rational unsigned int isInteger = 0; unsigned int isRational = 0; unsigned int isExpression = 0; /* power must be an integer * but need to check that it is not a real * number that is integral * i.e. mathml <cn> 2 </cn> will record a "real" */ if (child->isRational()) { isRational = 1; } else if (child->isInteger()) { isInteger = 1; } else if (child->isReal()) { if (ceil(child->getReal()) == child->getReal()) { isInteger = 1; } } else if (child->getNumChildren() > 0) { // power might itself be an expression tempUD = unitFormat->getUnitDefinition(child, inKL, reactNo); UnitDefinition::simplify(tempUD); if (tempUD->isVariantOfDimensionless()) { SBMLTransforms::mapComponentValues(&m); double value = SBMLTransforms::evaluateASTNode(child); SBMLTransforms::clearComponentValues(); #if defined(__linux) if (!std::isnan(value)) #else if (!isnan(value)) #endif { if (floor(value) != value) isExpression = 1; else isInteger = 1; } else { isExpression = 1; } } else { /* here the child is an expression with units * flag the expression as not checked */ isExpression = 1; } } else { // power could be a parameter or a speciesReference in l3 const Parameter *param = NULL; const SpeciesReference *sr = NULL; if (child->isName()) { /* Parameters may be declared in two places (the model and the * kinetic law's local parameter list), so we have to check both. */ if (sb.getTypeCode() == SBML_KINETIC_LAW) { const KineticLaw* kl = dynamic_cast<const KineticLaw*>(&sb); /* First try local parameters and if null is returned, try * the global parameters */ if (kl != NULL) { param = kl->getParameter(child->getName()); } } if (param == NULL) { param = m.getParameter(child->getName()); } if (param == NULL && m.getLevel() > 2) { // could be a species reference sr = m.getSpeciesReference(child->getName()); } } if (param != NULL) { /* We found a parameter with this identifier. */ if (UnitDefinition::areEquivalent(&dim, unitsArgPower) || undeclaredUnitsPower) { value = param->getValue(); if (value != 0) { if (ceil(value) == value) { isInteger = 1; } } } else { /* No parameter definition found for child->getName() */ logUnitConflict(node, sb); } } else if (sr != NULL) { // technically here there is an issue // stoichiometry is dimensionless SBMLTransforms::mapComponentValues(&m); double value = SBMLTransforms::evaluateASTNode(child, &m); SBMLTransforms::clearComponentValues(); // but it may not be an integer #if defined(__linux) if (!std::isnan(value)) #else if (!isnan(value)) #endif // we cant check { isExpression = 1; } else { if (ceil(value) == value) { isInteger = 1; } } } } if (isRational == 1) { //FIX-ME will need sorting for double exponents //* (2) if the second argument b is a rational number n/m, //* it must be possible to derive the m-th root of (a{unit})n, //* where {unit} signifies the units associated with a; unsigned int impossible = 0; for (unsigned int n = 0; impossible == 0 && n < unitsArg1->getNumUnits(); n++) { if ((int)(unitsArg1->getUnit(n)->getExponent()) * child->getInteger() % child->getDenominator() != 0) impossible = 1; } if (impossible) logRationalPowerConflict(node, sb); } else if (isExpression == 1) { logExpressionPowerConflict(node, sb); } else if (isInteger == 0) { logNonIntegerPowerConflict(node, sb); } } // if (!areEquivalent(dim, unitsPower)) // { // /* 'v' does not have units of dimensionless. */ // /* If the power 'n' is a parameter, check if its units are either // * undeclared or declared as dimensionless. If either is the case, // * the value of 'n' must be an integer. // */ // const Parameter *param = NULL; // if (child->isName()) // { // /* Parameters may be declared in two places (the model and the // * kinetic law's local parameter list), so we have to check both. // */ // if (sb.getTypeCode() == SBML_KINETIC_LAW) // { // const KineticLaw* kl = dynamic_cast<const KineticLaw*>(&sb); // /* First try local parameters and if null is returned, try // * the global parameters */ // if (kl != NULL) // { // param = kl->getParameter(child->getName()); // } // } // if (param == NULL) // { // param = m.getParameter(child->getName()); // } // // } // if (param != NULL) // { // /* We found a parameter with this identifier. */ // if (areEquivalent(dim, unitsArgPower) || unitFormat->hasUndeclaredUnits(child)) // { // value = param->getValue(); // if (value != 0) // { // if (ceil(value) != value) // { // logUnitConflict(node, sb); // } // } // } // else // { ///* No parameter definition found for child->getName() */ // logUnitConflict(node, sb); // } // } // else if (child->isFunction() || child->isOperator()) // { // /* cannot test whether the value will be appropriate */ // if (!areEquivalent(dim, unitsArgPower)) // { // logUnitConflict(node, sb); // } // } // /* power must be an integer // * but need to check that it is not a real // * number that is integral // * i.e. mathml <cn> 2 </cn> will record a "real" // */ // else if (!child->isInteger()) // { // if (!child->isReal()) // { // logUnitConflict(node, sb); // } // else if (ceil(child->getReal()) != child->getReal()) // { // logUnitConflict(node, sb); // } // } // } // else if (!areEquivalent(dim, unitsArgPower)) // { // /* power (3, k) */ // logUnitConflict(node, sb); // } checkUnits(m, *node.getLeftChild(), sb, inKL, reactNo); delete unitFormat; delete unitsArg1; delete unitsArgPower; }
void UnitReplacementCheck::checkReferencedElement(ReplacedBy& repBy) { unsigned int numErrsB4 = repBy.getSBMLDocument()->getNumErrors(); SBase* refElem = repBy.getReferencedElement(); // if there is an issue with references the getReferencedElement // will log errors and possibly fail // we dont want to try any further unsigned int numErrsAfter = repBy.getSBMLDocument()->getNumErrors(); if (numErrsB4 != numErrsAfter || refElem == NULL) { return; } SBase * parent = repBy.getParentSBMLObject(); UnitDefinition *parentUnits = parent->getDerivedUnitDefinition(); //bool delparunit = parent->getTypeCode()==SBML_PARAMETER; UnitDefinition *refElemUnits = refElem->getDerivedUnitDefinition(); //bool delrefunit = refElem->getTypeCode()==SBML_PARAMETER; if (parentUnits == NULL || refElemUnits == NULL) { //if (delparunit) //{ // delete parentUnits; //} //if (delrefunit) //{ // delete refElemUnits; //} return; } if (parent->containsUndeclaredUnits() == true || refElem->containsUndeclaredUnits() == true) { //if (delparunit) //{ // delete parentUnits; //} //if (delrefunit) //{ // delete refElemUnits; //} return; } if (UnitDefinition::areIdentical(parentUnits, refElemUnits) == false) { logMismatchUnits(repBy, refElem, parent); } else { // if we have Compartments that have spatialDimensions but no units // we can check this if (parent->getTypeCode() == SBML_COMPARTMENT && parentUnits->getNumUnits() == 0 && refElem->getTypeCode() == SBML_COMPARTMENT && refElemUnits->getNumUnits() == 0) { if (static_cast<Compartment *>(parent)->isSetSpatialDimensions() == true && static_cast<Compartment *>(refElem)->isSetSpatialDimensions() == true) { if (util_isEqual( static_cast<Compartment *>(parent)->getSpatialDimensionsAsDouble(), static_cast<Compartment *>(refElem)->getSpatialDimensionsAsDouble()) == 0) { logMismatchSpatialDimensions(repBy, refElem, parent); } } } } //if (delparunit) //{ // delete parentUnits; //} //if (delrefunit) //{ // delete refElemUnits; //} }
/** @cond doxygenLibsbmlInternal */ void CompModelPlugin::renameIDs(List* allElements, const string& prefix) { if (prefix=="") return; //Nothing to prepend. vector<pair<string, string> > renamedSIds; vector<pair<string, string> > renamedUnitSIds; vector<pair<string, string> > renamedMetaIds; // if a custom prefix transformer was specified, then set the // current prefix if (isSetTransformer()) mTransformer->setPrefix(prefix); for (unsigned long el=0; el < allElements->getSize(); ++el) { SBase* element = static_cast<SBase*>(allElements->get((unsigned int)el)); string id = element->getId(); string metaid = element->getMetaId(); // if a custom prefix transformer was specified, use it, other wise // default to the sbase method. if (isSetTransformer()) element->transformIdentifiers(mTransformer); else element->prependStringToAllIdentifiers(prefix); if (element->getTypeCode() == SBML_LOCAL_PARAMETER) { element->setId(id); //Change it back. This would perhaps be better served by overriding 'prependStringToAllIdentifiers' but hey. } string newid = element->getId(); string newmetaid = element->getMetaId(); if (id != newid) { int type = element->getTypeCode(); if (type==SBML_UNIT_DEFINITION) { renamedUnitSIds.push_back(make_pair(id, newid)); } else if (type==SBML_COMP_PORT) { //Do nothing--these can only be referenced from outside the Model, so they need to be handled specially. // (In the default case, we throw them away). } else { //This is a little dangerous, but hey! What's a little danger between friends! //(What we are assuming is that any attribute you can get with 'getId' is of the type 'SId') renamedSIds.push_back(make_pair(id, newid)); } } if (metaid != newmetaid) { renamedMetaIds.push_back(make_pair(metaid, newmetaid)); } } for (unsigned long el=0; el<allElements->getSize(); el++) { SBase* element = static_cast<SBase*>(allElements->get((unsigned int)el)); for (size_t id=0; id<renamedSIds.size(); id++) { element->renameSIdRefs(renamedSIds[id].first, renamedSIds[id].second); } for (size_t uid=0; uid<renamedUnitSIds.size(); uid++) { element->renameUnitSIdRefs(renamedUnitSIds[uid].first, renamedUnitSIds[uid].second); } for (size_t mid=0; mid<renamedMetaIds.size(); mid++) { element->renameMetaIdRefs(renamedMetaIds[mid].first, renamedMetaIds[mid].second); } } }
void UnitReplacementCheck::checkReferencedElement(ReplacedElement& repE, const Model& m) { // if the deletion attribute is set then it is not a true replacement if (repE.isSetDeletion() == true) { return; } unsigned int numErrsB4 = repE.getSBMLDocument()->getNumErrors(); SBase* refElem = repE.getReferencedElement(); // if there is an issue with references the getReferencedElement // will log errors and possibly fail // we dont want to try any further unsigned int numErrsAfter = repE.getSBMLDocument()->getNumErrors(); if (numErrsB4 != numErrsAfter || refElem == NULL) { return; } SBase * parent = repE.getParentSBMLObject()->getParentSBMLObject(); UnitDefinition *parentUnits = parent->getDerivedUnitDefinition(); UnitDefinition *refElemUnits = refElem->getDerivedUnitDefinition(); bool delrefelem = false; bool cfPresent = false; /* adjust the refElement units for conversion factor */ if (repE.isSetConversionFactor() == true && refElemUnits && refElemUnits->getNumUnits() > 0) { Parameter * p = const_cast<Model *>(&m) ->getParameter(repE.getConversionFactor()); UnitDefinition *ud = p->getDerivedUnitDefinition(); UnitDefinition *newRefElemUnits = UnitDefinition::combine(refElemUnits, ud); refElemUnits = newRefElemUnits; delrefelem = true; cfPresent = true; } if (parentUnits == NULL) { if (delrefelem) { delete refElemUnits; } return; } if (refElemUnits == NULL) { return; } if (parent->containsUndeclaredUnits() == true || refElem->containsUndeclaredUnits() == true) { if (delrefelem) { delete refElemUnits; } return; } if (UnitDefinition::areIdentical(parentUnits, refElemUnits) == false) { logMismatchUnits(repE, refElem, parent, cfPresent); } else { // if we have Compartments that have spatialDimensions but no units // we can check this if (parent->getTypeCode() == SBML_COMPARTMENT && parentUnits->getNumUnits() == 0 && refElem->getTypeCode() == SBML_COMPARTMENT && refElemUnits->getNumUnits() == 0) { if (static_cast<Compartment *>(parent)->isSetSpatialDimensions() == true && static_cast<Compartment *>(refElem)->isSetSpatialDimensions() == true) { if (util_isEqual( static_cast<Compartment *>(parent)->getSpatialDimensionsAsDouble(), static_cast<Compartment *>(refElem)->getSpatialDimensionsAsDouble()) == 0) { logMismatchSpatialDimensions(repE, refElem, parent); } } } } if (delrefelem) { delete refElemUnits; } }
Model* CompModelPlugin::flattenModel() const { //First make a copy of our parent (the model to be flattened): const Model* parent = static_cast<const Model*>(getParentSBMLObject()); if (parent==NULL) { return NULL; } //doc needs to be non-const so that the error messages can be updated. Otherwise, nothing changes. SBMLDocument* doc = const_cast<SBMLDocument*>(getSBMLDocument()); if (doc==NULL) { return NULL; } //Set the original document so that it can find the model definitions //and external model definitions while we flatten. Model* flat = parent->clone(); flat->setSBMLDocument(doc); CompModelPlugin* flatplug = static_cast<CompModelPlugin*>(flat->getPlugin(getPrefix())); // Now instantiate its submodels and // follow all renaming/deletion/replacement rules. vector<const Model*> submods; int success = flatplug->instantiateSubmodels(); if (success != LIBSBML_OPERATION_SUCCESS) { //instantiateSubmodels sets its own error messages. delete flat; return NULL; } //Now start the aggregation process. //This goes from the bottom up, calling 'appendFrom' iteratively //(from the plugin). for (unsigned int sm=0; sm<flatplug->getNumSubmodels(); sm++) { Model* submodel = flatplug->getSubmodel(sm)->getInstantiation(); if (submodel==NULL) { //getInstantiation should be calling a cached value by now, but if not, it will set its own error messages. delete flat; return NULL; } CompModelPlugin* submodplug = static_cast<CompModelPlugin*>(submodel->getPlugin(getPrefix())); if (submodplug != NULL) { //Strip the ports from the submodel, as we no longer need them. while (submodplug->getNumPorts() > 0) { delete submodplug->removePort(0); } } success = flat->appendFrom(submodel); if (success != LIBSBML_OPERATION_SUCCESS) { string error = "Unable to flatten model in CompModelPlugin::flattenModel: appending elements from the submodel '" + submodel->getId() + "' to the elements of the parent model failed."; doc->getErrorLog()->logPackageError("comp", CompModelFlatteningFailed, getPackageVersion(), getLevel(), getVersion(), error); delete flat; return NULL; } } // Now we clear the saved referenced elements in the local Port objects, // but point them to the new object if necessary. flatplug->resetPorts(); // Next, strip the package info from 'flat'. // We're going to remove everything but the Ports: flatplug->mListOfSubmodels.clear(); flatplug->clearReplacedElements(); flatplug->unsetReplacedBy(); List* allelements = flat->getAllElements(); vector<SBase*> nonReplacedElements; 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_REPLACEDBY || type==SBML_COMP_REPLACEDELEMENT || type==SBML_COMP_SBASEREF)) { nonReplacedElements.push_back(element); } } // delete the list delete allelements; for (unsigned int el=0; el<nonReplacedElements.size(); el++) { SBase* element = nonReplacedElements[el]; CompSBasePlugin* elplug = static_cast<CompSBasePlugin*>(element->getPlugin(getPrefix())); if (elplug != NULL) { elplug->clearReplacedElements(); elplug->unsetReplacedBy(); } } //Finally, unset the document again. flat->setSBMLDocument(NULL); return flat; }
LIBSBML_EXTERN void copySBaseAttributes(const SBase& source,SBase& target) { target.setMetaId(source.getMetaId()); // target.setId(source.getId()); // target.setName(source.getName()); target.setSBMLDocument(const_cast<SBMLDocument*>(source.getSBMLDocument())); target.setSBOTerm(source.getSBOTerm()); if(source.isSetAnnotation()) { target.setAnnotation(new XMLNode(*const_cast<SBase&>(source).getAnnotation())); } if(source.isSetNotes()) { target.setNotes(new XMLNode(*const_cast<SBase&>(source).getNotes())); } if (source.getSBMLNamespaces()) { target.setSBMLNamespaces(source.getSBMLNamespaces()); } List* pCVTerms=target.getCVTerms(); // first delete all the old CVTerms if(pCVTerms) { while(pCVTerms->getSize()>0) { CVTerm* object=static_cast<CVTerm*>(pCVTerms->remove(0)); delete object; } // add the cloned CVTerms from source if(source.getCVTerms()!=NULL) { unsigned int i=0,iMax=source.getCVTerms()->getSize(); while(i<iMax) { target.addCVTerm(static_cast<CVTerm*>(static_cast<CVTerm*>(source.getCVTerms()->get(i))->clone())); ++i; } } } }
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; }
SBase* SBaseRef::getReferencedElementFrom(Model* model) { SBMLDocument* doc = getSBMLDocument(); if (!hasRequiredAttributes()) { if (doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element from <" + getElementName() + "> "; if (isSetId()) { error += "with ID '" + getId() + "' "; } error += "as it does not have the required attributes."; int en = CompSBaseRefMustReferenceObject; switch(getTypeCode()) { case SBML_COMP_REPLACEDBY: en = CompReplacedByAllowedAttributes; break; case SBML_COMP_REPLACEDELEMENT: en = CompReplacedElementAllowedAttributes; break; case SBML_COMP_PORT: en = CompPortAllowedAttributes; break; case SBML_COMP_DELETION: en = CompDeletionAllowedAttributes; } doc->getErrorLog()->logPackageError("comp", en, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return NULL; } SBase* referent = NULL; if (isSetPortRef()) { CompModelPlugin* mplugin = static_cast<CompModelPlugin*>(model->getPlugin(getPrefix())); Port* port = mplugin->getPort(getPortRef()); if (port==NULL) { if (doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element from SBase reference "; if (isSetId()) { error += "'" + getId() + "' "; } error += "as the port it references ('" + getPortRef() +"') could not be found."; doc->getErrorLog()->logPackageError("comp", CompPortRefMustReferencePort, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return NULL; } mDirectReference = port; referent = port->getReferencedElementFrom(model); } else if (isSetIdRef()) { referent = model->getElementBySId(getIdRef()); if (referent == NULL && doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element: no such SId in the model: '" + getIdRef() + "'."; if (doc->getErrorLog()->contains(UnrequiredPackagePresent) || doc->getErrorLog()->contains(RequiredPackagePresent)) { doc->getErrorLog()->logPackageError("comp", CompIdRefMayReferenceUnknownPackage, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } else { doc->getErrorLog()->logPackageError("comp", CompIdRefMustReferenceObject, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } } } else if (isSetUnitRef()) { referent = model->getUnitDefinition(getUnitRef()); if (referent == NULL && doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element: no such Unit in the model: '" + getUnitRef() + "'."; doc->getErrorLog()->logPackageError("comp", CompUnitRefMustReferenceUnitDef, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } } else if (isSetMetaIdRef()) { referent = model->getElementByMetaId(getMetaIdRef()); if (referent == NULL && doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element: no such metaid in the model: '" + getMetaIdRef() + "'."; if (doc->getErrorLog()->contains(UnrequiredPackagePresent) || doc->getErrorLog()->contains(RequiredPackagePresent)) { doc->getErrorLog()->logPackageError("comp", CompIdRefMayReferenceUnknownPackage, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } else { doc->getErrorLog()->logPackageError("comp", CompMetaIdRefMustReferenceObject, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } } } else { //This is actually possible if the subclass overrides getNumReferents() (which some do). In that case, we just return NULL and let the overriding function find the referent instead. return NULL; } if (referent == NULL) { //No need to set an error message--one was already set above. return NULL; } if (isSetSBaseRef()) { //We're drilling into the submodels here, so our referent must be a submodel. if (referent->getTypeCode() != SBML_COMP_SUBMODEL) { if (doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element: the element "; if (referent->isSetId()) { error += "'" + referent->getId() + "'"; } else if (referent->isSetMetaId()) { error += "with the metaid '" + referent->getMetaId() + "'"; } error += " is not a submodel, and therefore has no subobjects for the child <sBaseRef> to refer to."; doc->getErrorLog()->logPackageError("comp", CompParentOfSBRefChildMustBeSubmodel, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return NULL; } Submodel* subm = static_cast<Submodel*>(referent); if (subm==NULL) { //Note: should be impossible. if (doc) { string error = "In SBaseRef::getReferencedElementFrom, unable to find referenced element: the element "; if (referent->isSetId()) { error += "'" + referent->getId() + "' "; } else if (referent->isSetMetaId()) { error += "with the metaid '" + referent->getMetaId() + "' "; } error += "claims to be a Submodel, but could not be programmatically turned into one."; doc->getErrorLog()->logPackageError("comp", CompParentOfSBRefChildMustBeSubmodel, getPackageVersion(), getLevel(), getVersion(), error, getLine(), getColumn()); } return NULL; } Model* inst = subm->getInstantiation(); if (inst==NULL) { //No need to set an additional error, as 'getInstantiation' will set one itself. return NULL; } //Recursive, so will set its own error messages: referent = getSBaseRef()->getReferencedElementFrom(inst); mDirectReference = getSBaseRef()->getDirectReference(); } return referent; }