|
From: <fbe...@us...> - 2014-11-27 14:32:34
|
Revision: 21770
http://sourceforge.net/p/sbml/code/21770
Author: fbergmann
Date: 2014-11-27 14:32:24 +0000 (Thu, 27 Nov 2014)
Log Message:
-----------
- added new option to strip package converter. With 'stripAllUnrecognized' all from libSBML unrecognized packages can be removed from a model.
- added new options to the sbml document, to get more information about unrecognized packages
- added test
Modified Paths:
--------------
trunk/libsbml/src/sbml/SBMLDocument.cpp
trunk/libsbml/src/sbml/SBMLDocument.h
trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.cpp
trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.h
trunk/libsbml/src/sbml/conversion/test/TestStripPackageConverter.cpp
Modified: trunk/libsbml/src/sbml/SBMLDocument.cpp
===================================================================
--- trunk/libsbml/src/sbml/SBMLDocument.cpp 2014-11-27 10:18:04 UTC (rev 21769)
+++ trunk/libsbml/src/sbml/SBMLDocument.cpp 2014-11-27 14:32:24 UTC (rev 21770)
@@ -1351,6 +1351,52 @@
return false;
}
+
+
+int
+SBMLDocument::getNumUnknownPackages() const
+{
+ int count = 0;
+ for (int i = 0; i < mRequiredAttrOfUnknownPkg.getLength(); ++i)
+ {
+ if (mRequiredAttrOfUnknownPkg.getName(i) == "required")
+ ++count;
+ }
+ return count;
+}
+
+std::string
+SBMLDocument::getUnknownPackageURI(int index) const
+{
+ std::string result;
+ int count = 0;
+ for (int i = 0; i < mRequiredAttrOfUnknownPkg.getLength(); ++i)
+ {
+ if (mRequiredAttrOfUnknownPkg.getName(i) != "required")
+ continue;
+ if (i == count)
+ return mRequiredAttrOfUnknownPkg.getURI(i);
+ ++count;
+ }
+ return result;
+}
+
+std::string
+SBMLDocument::getUnknownPackagePrefix(int index) const
+{
+ std::string result;
+ int count = 0;
+ for (int i = 0; i < mRequiredAttrOfUnknownPkg.getLength(); ++i)
+ {
+ if (mRequiredAttrOfUnknownPkg.getName(i) != "required")
+ continue;
+ if (i == count)
+ return mRequiredAttrOfUnknownPkg.getPrefix(i);
+ ++count;
+ }
+ return result;
+}
+
/** @endcond */
Modified: trunk/libsbml/src/sbml/SBMLDocument.h
===================================================================
--- trunk/libsbml/src/sbml/SBMLDocument.h 2014-11-27 10:18:04 UTC (rev 21769)
+++ trunk/libsbml/src/sbml/SBMLDocument.h 2014-11-27 14:32:24 UTC (rev 21770)
@@ -1572,6 +1572,12 @@
bool hasUnknownPackage(const std::string& pkgURI);
+ int getNumUnknownPackages() const;
+
+ std::string getUnknownPackageURI(int index) const;
+
+ std::string getUnknownPackagePrefix(int index) const;
+
/** @endcond */
protected:
Modified: trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.cpp
===================================================================
--- trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.cpp 2014-11-27 10:18:04 UTC (rev 21769)
+++ trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.cpp 2014-11-27 14:32:24 UTC (rev 21770)
@@ -117,6 +117,7 @@
{
prop.addOption("stripPackage", true,
"Strip SBML Level 3 package constructs from the model");
+ prop.addOption("stripAllUnrecognized", false, "If set, all unsupported packages will be removed.");
prop.addOption("package", "",
"Name of the SBML Level 3 package to be stripped");
init = true;
@@ -133,7 +134,37 @@
return true;
}
+/** @cond doxygenLibsbmlInternal */
+bool
+SBMLStripPackageConverter::stripPackage(const std::string& packageToStrip)
+{
+ bool conversion = false;
+ const std::string& pkgURI =
+ mDocument->getSBMLNamespaces()->getNamespaces()->getURI(packageToStrip);
+
+ // TO DO - SK Comment
+ // pass control to package code to see if needs to do more
+ // additional boolean flag is to do with preseving info
+ // not yet used but I think it will need to be passed
+ // conversion = mDocument->getModel()->getPlugin(packageToStrip)
+ // ->stripPackage(packageToStrip, false);
+
+
+ if (pkgURI.empty() == false)
+ {
+ // disabling the package will literally strip the pkg info
+ mDocument->enablePackage(pkgURI, packageToStrip, false);
+
+ // check it is disabled
+ if (mDocument->isPkgEnabled(packageToStrip) == false)
+ conversion = true;
+ }
+
+ return conversion;
+}
+/** @endcond */
+
int
SBMLStripPackageConverter::convert()
{
@@ -141,13 +172,24 @@
// would like an option that put the extension classes somewhere were I
// could get at them later
- bool conversion = false;
+ if (isStripAllUnrecognizedPackages())
+ {
+ int numPackages = mDocument->getNumUnknownPackages();
+ bool result = true;
+ for (int i = 0; i < numPackages; ++i)
+ {
+ const std::string& current = mDocument->getUnknownPackagePrefix(i);
+ result &= stripPackage(current);
+ if (!result)
+ return LIBSBML_OPERATION_FAILED;
+ }
+ }
std::string packageToStrip = getPackageToStrip();
if (packageToStrip.empty())
{
- return LIBSBML_INVALID_ATTRIBUTE_VALUE;
+ return LIBSBML_OPERATION_SUCCESS;
}
if (mDocument->isPkgEnabled(packageToStrip) == false)
@@ -160,28 +202,8 @@
}
}
-// TO DO - SK Comment
- // pass control to package code to see if needs to do more
- // additional boolean flag is to do with preseving info
- // not yet used but I think it will need to be passed
- //conversion = mDocument->getModel()->getPlugin(packageToStrip)
- // ->stripPackage(packageToStrip, false);
+ bool conversion = stripPackage(packageToStrip);
-
- std::string pkgURI =
- mDocument->getSBMLNamespaces()->getNamespaces()->getURI(packageToStrip);
- if (pkgURI.empty() == false)
- {
- // disabling the package will literally strip the pkg info
- mDocument->enablePackage(pkgURI, packageToStrip, false);
-
- // check it is disabled
- if (mDocument->isPkgEnabled(packageToStrip) == false)
- conversion = true;
- }
-
-
-
// TO DO - SK Comment
// test that package is stripped
@@ -193,9 +215,8 @@
}
-/** @cond doxygenLibsbmlInternal */
-std::string
-SBMLStripPackageConverter::getPackageToStrip()
+std::string
+SBMLStripPackageConverter::getPackageToStrip() const
{
if (getProperties()->getOption("package") != NULL)
{
@@ -206,8 +227,19 @@
return "";
}
}
-/** @endcond */
+bool
+SBMLStripPackageConverter::isStripAllUnrecognizedPackages() const
+{
+ if (getProperties()->getOption("stripAllUnrecognized") != NULL)
+ {
+ return getProperties()->getOption("stripAllUnrecognized")->getBoolValue();
+ }
+ else
+ {
+ return false;
+ }
+}
/** @cond doxygenIgnored */
Modified: trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.h
===================================================================
--- trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.h 2014-11-27 10:18:04 UTC (rev 21769)
+++ trunk/libsbml/src/sbml/conversion/SBMLStripPackageConverter.h 2014-11-27 14:32:24 UTC (rev 21770)
@@ -175,23 +175,33 @@
*/
virtual ConversionProperties getDefaultProperties() const;
+ /**
+ * @return the package to be stripped
+ */
+ std::string getPackageToStrip() const;
+ /**
+ * @return whether all unrecognized packages should be removed
+ */
+ bool isStripAllUnrecognizedPackages() const;
+
#ifndef SWIG
#endif // SWIG
+protected:
-
-private:
/** @cond doxygenLibsbmlInternal */
-
- std::string getPackageToStrip();
-
-
+
+ /**
+ * This function strips the packge with given prefix
+ *
+ * @return true, if the conversion succeeded, false otherwise.
+ */
+ bool stripPackage(const std::string& packageToStrip);
+
/** @endcond */
-
-
};
LIBSBML_CPP_NAMESPACE_END
Modified: trunk/libsbml/src/sbml/conversion/test/TestStripPackageConverter.cpp
===================================================================
--- trunk/libsbml/src/sbml/conversion/test/TestStripPackageConverter.cpp 2014-11-27 10:18:04 UTC (rev 21769)
+++ trunk/libsbml/src/sbml/conversion/test/TestStripPackageConverter.cpp 2014-11-27 14:32:24 UTC (rev 21770)
@@ -120,7 +120,32 @@
}
END_TEST
+START_TEST(test_strip_unkown)
+{
+ const std::string model =
+ "<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<sbml xmlns='http://www.sbml.org/sbml/level3/version1/core' level='3' version='1' "
+ "xmlns:pkg1='http://www.sbml.org/sbml/level3/version1/pkg1/version1' pkg1:required='true' "
+ "xmlns:pkg2='http://www.sbml.org/sbml/level3/version1/pkg2/version1' pkg2:required='true' "
+ "xmlns:pkg3='http://www.sbml.org/sbml/level3/version1/pkg3/version1' pkg3:required='false' "
+ ">\n\t<model/>\n"
+ "</sbml>";
+ SBMLDocument* doc = readSBMLFromString(model.c_str());
+ fail_unless(doc->getNumUnknownPackages() == 3);
+
+ ConversionProperties props;
+ props.addOption("stripPackage", true);
+ props.addOption("stripAllUnrecognized", true);
+
+ fail_unless(doc->convert(props) == LIBSBML_OPERATION_SUCCESS);
+
+ fail_unless(doc->getNumUnknownPackages() == 0);
+
+ delete doc;
+}
+END_TEST
+
Suite *
create_suite_TestStripPackageConverter (void)
{
@@ -130,6 +155,7 @@
tcase_add_test(tcase, test_strip_unknownreq);
tcase_add_test(tcase, test_strip_comp);
+ tcase_add_test(tcase, test_strip_unkown);
suite_add_tcase(suite, tcase);
|