From: <sar...@us...> - 2008-09-11 22:56:35
|
Revision: 8162 http://sbml.svn.sourceforge.net/sbml/?rev=8162&view=rev Author: sarahkeating Date: 2008-09-11 22:56:32 +0000 (Thu, 11 Sep 2008) Log Message: ----------- Two things happening: 1) future proofing the code that reads and writes attributes 2) adding tests for read/write of l2v2 and l2v3 components/attributes that have never been tested Modified Paths: -------------- trunk/libsbml/src/sbml/CompartmentType.cpp trunk/libsbml/src/sbml/Constraint.cpp trunk/libsbml/src/sbml/Delay.cpp trunk/libsbml/src/sbml/Event.cpp trunk/libsbml/src/sbml/test/TestReadFromFile6.cpp trunk/libsbml/src/sbml/test/TestReadFromFile7.cpp trunk/libsbml/src/sbml/test/TestWriteSBML.cpp trunk/libsbml/src/sbml/test/test-data/l2v2-newComponents.xml trunk/libsbml/src/sbml/test/test-data/l2v3-all.xml Modified: trunk/libsbml/src/sbml/CompartmentType.cpp =================================================================== --- trunk/libsbml/src/sbml/CompartmentType.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/CompartmentType.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -139,6 +139,12 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + if (level < 2 || (level == 2 && version == 1)) + { + logError(NotSchemaConformant, getLevel(), getVersion(), + "CompartmentType is not a valid component for this level/version."); + } + std::vector<std::string> expectedAttributes; expectedAttributes.clear(); @@ -202,6 +208,12 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + /* invalid level/version */ + if (level < 2 || (level == 2 && version == 1)) + { + return; + } + // // id: SId { use="required" } (L2v2 ->) // Modified: trunk/libsbml/src/sbml/Constraint.cpp =================================================================== --- trunk/libsbml/src/sbml/Constraint.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/Constraint.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -344,14 +344,18 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); - std::vector<std::string> expectedAttributes; - expectedAttributes.clear(); - if (level == 2 && version > 1) + if (level < 2 || (level == 2 && version == 1)) { - expectedAttributes.push_back("metaid"); - expectedAttributes.push_back("sboTerm"); + logError(NotSchemaConformant, getLevel(), getVersion(), + "Constraint is not a valid component for this level/version."); + return; } + std::vector<std::string> expectedAttributes; + expectedAttributes.clear(); + expectedAttributes.push_back("metaid"); + expectedAttributes.push_back("sboTerm"); + // check that all attributes are expected for (int i = 0; i < attributes.getLength(); i++) { @@ -365,10 +369,9 @@ } // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v2 -> ) // - if (level == 2 && version > 1) - mSBOTerm = SBO::readTerm(attributes, this->getErrorLog()); + mSBOTerm = SBO::readTerm(attributes, this->getErrorLog()); } /** @endcond doxygen-libsbml-internal */ @@ -387,11 +390,16 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + /* invalid level/version */ + if (level < 2 || (level == 2 && version == 1)) + { + return; + } + // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v2 ->) // - if (level == 2 && version > 1) - SBO::writeTerm(stream, mSBOTerm); + SBO::writeTerm(stream, mSBOTerm); } /** @endcond doxygen-libsbml-internal */ Modified: trunk/libsbml/src/sbml/Delay.cpp =================================================================== --- trunk/libsbml/src/sbml/Delay.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/Delay.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -347,14 +347,18 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + if (level < 2) + { + logError(NotSchemaConformant, getLevel(), getVersion(), + "Delay is not a valid component for this level/version."); + } std::vector<std::string> expectedAttributes; expectedAttributes.clear(); - if (level == 2) + + expectedAttributes.push_back("metaid"); + if (!(level == 2 && version < 3)) { - if (version > 1) - expectedAttributes.push_back("metaid"); - if (version > 2) - expectedAttributes.push_back("sboTerm"); + expectedAttributes.push_back("sboTerm"); } // check that all attributes are expected @@ -370,9 +374,9 @@ } // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v3 ->) // - if (level == 2 && version > 1) + if (!(level == 2 && version < 3)) mSBOTerm = SBO::readTerm(attributes, this->getErrorLog()); } /** @endcond doxygen-libsbml-internal */ @@ -392,10 +396,15 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + if (level < 2) + { + return; + } + // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v3 ->) // - if (level == 2 && version > 1) + if (!(level == 2 && version < 3)) SBO::writeTerm(stream, mSBOTerm); } /** @endcond doxygen-libsbml-internal */ Modified: trunk/libsbml/src/sbml/Event.cpp =================================================================== --- trunk/libsbml/src/sbml/Event.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/Event.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -533,23 +533,25 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + if (level < 2) + { + logError(NotSchemaConformant, getLevel(), getVersion(), + "Event is not a valid component for this level/version."); + } std::vector<std::string> expectedAttributes; expectedAttributes.clear(); - if (level == 2) + expectedAttributes.push_back("metaid"); + expectedAttributes.push_back("name"); + expectedAttributes.push_back("id"); + + if (level == 2 && version < 3) { - expectedAttributes.push_back("metaid"); - expectedAttributes.push_back("name"); - expectedAttributes.push_back("id"); + expectedAttributes.push_back("timeUnits"); + } - if (version < 3) - { - expectedAttributes.push_back("timeUnits"); - } - - if (version != 1) - { - expectedAttributes.push_back("sboTerm"); - } + if (!(level == 2 && version == 1)) + { + expectedAttributes.push_back("sboTerm"); } // check that all attributes are expected @@ -565,7 +567,7 @@ } // - // id: SId { use="optional" } (L2v1, L2v2) + // id: SId { use="optional" } (L2v1 ->) // bool assigned = attributes.readInto("id", mId, getErrorLog(), false); if (assigned && mId.size() == 0) @@ -575,7 +577,7 @@ SBase::checkIdSyntax(); // - // name: string { use="optional" } (L2v1, L2v2) + // name: string { use="optional" } (L2v1 ->) // attributes.readInto("name", mName); @@ -583,14 +585,16 @@ // timeUnits: SId { use="optional" } (L2v1, L2v2) // removed in l2v3 // - attributes.readInto("timeUnits", mTimeUnits); - SBase::checkUnitSyntax(); + if (level == 2 && version < 3) + { + attributes.readInto("timeUnits", mTimeUnits); + SBase::checkUnitSyntax(); + } - // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v2 ->) // - if (level == 2 && version > 1) + if (!(level == 2 && version == 1)) mSBOTerm = SBO::readTerm(attributes, this->getErrorLog()); } /** @endcond doxygen-libsbml-internal */ @@ -610,18 +614,23 @@ const unsigned int level = getLevel(); const unsigned int version = getVersion(); + if (level < 2) + { + return; + } + // - // id: SId { use="optional" } (L2v1, L2v2) + // id: SId { use="optional" } (L2v1 ->) // if (!mInternalIdOnly) stream.writeAttribute("id", mId); // - // name: string { use="optional" } (L2v1, L2v2) + // name: string { use="optional" } (L2v1->) // stream.writeAttribute("name", mName); - if (version < 3) + if (level == 2 && version < 3) { // // timeUnits: SId { use="optional" } (L2v1, L2v2) @@ -632,9 +641,9 @@ // - // sboTerm: SBOTerm { use="optional" } (L2v2) + // sboTerm: SBOTerm { use="optional" } (L2v2 ->) // - if (level == 2 && version > 1) + if (!(level == 2 && version == 1)) SBO::writeTerm(stream, mSBOTerm); } /** @endcond doxygen-libsbml-internal */ Modified: trunk/libsbml/src/sbml/test/TestReadFromFile6.cpp =================================================================== --- trunk/libsbml/src/sbml/test/TestReadFromFile6.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/test/TestReadFromFile6.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -81,6 +81,9 @@ KineticLaw* kl; UnitDefinition* ud; Reaction* r1; + Constraint* con; + + const ASTNode* ast; std::string filename(TestDataDirectory); filename += "l2v2-newComponents.xml"; @@ -135,6 +138,28 @@ fail_unless( ct != NULL , NULL ); fail_unless( ct->getId() == "mitochondria", NULL ); + //<listOfConstraints> + // <constraint sboTerm="SBO:0000064"> + // <math xmlns="http://www.w3.org/1998/Math/MathML"> + // <apply> + // <lt/> + // <cn type="integer"> 1 </cn> + // <ci> cell </ci> + // </apply> + // </math> + // </constraint> + //</listOfConstraints> + fail_unless( m->getNumConstraints() == 1, NULL ); + + con = m->getConstraint(0); + fail_unless( con != NULL , NULL ); + fail_unless(con->getSBOTerm() == 64, NULL); + fail_unless(con->getSBOTermID() == "SBO:0000064", NULL); + + ast = con->getMath(); + fail_unless(!strcmp(SBML_formulaToString(ast), "lt(1, cell)"), NULL); + + ///** // * tests for the unit API functions // */ Modified: trunk/libsbml/src/sbml/test/TestReadFromFile7.cpp =================================================================== --- trunk/libsbml/src/sbml/test/TestReadFromFile7.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/test/TestReadFromFile7.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -81,6 +81,12 @@ KineticLaw* kl; UnitDefinition* ud; Reaction* r1; + Constraint* con; + Event* e; + Delay* delay; + Trigger* t; + + const ASTNode* ast; std::string filename(TestDataDirectory); filename += "l2v3-all.xml"; @@ -133,8 +139,78 @@ fail_unless( ct->getSBOTerm() == 236, NULL ); fail_unless( ct->getSBOTermID() == "SBO:0000236", NULL ); + //<listOfConstraints> + // <constraint> + // <math xmlns="http://www.w3.org/1998/Math/MathML"> + // <apply> + // <lt/> + // <ci> x </ci> + // <cn type="integer"> 3 </cn> + // </apply> + // </math> + // </constraint> + //</listOfConstraints> + fail_unless( m->getNumConstraints() == 1, NULL ); + con = m->getConstraint(0); + fail_unless( con != NULL , NULL ); + ast = con->getMath(); + fail_unless(!strcmp(SBML_formulaToString(ast), "lt(x, 3)"), NULL); + + //<event id="e1" sboTerm="SBO:0000231"> + // <trigger> + // <math xmlns="http://www.w3.org/1998/Math/MathML"> + // <apply> + // <lt/> + // <ci> x </ci> + // <cn type="integer"> 3 </cn> + // </apply> + // </math> + // </trigger> + // <delay sboTerm="SBO:0000064"> + // <math xmlns="http://www.w3.org/1998/Math/MathML"> + // <apply> + // <plus/> + // <ci> x </ci> + // <cn type="integer"> 3 </cn> + // </apply> + // </math> + // </delay> + // <listOfEventAssignments> + // <eventAssignment variable="a"> + // <math xmlns="http://www.w3.org/1998/Math/MathML"> + // <apply> + // <times/> + // <ci> x </ci> + // <ci> p3 </ci> + // </apply> + // </math> + // </eventAssignment> + // </listOfEventAssignments> + //</event> + fail_unless( m->getNumEvents() == 1, NULL ); + + e = m->getEvent(0); + fail_unless(e != NULL, NULL); + + fail_unless(e->getId() == "e1", NULL); + + fail_unless(e->getSBOTerm() == 231, NULL); + fail_unless(e->getSBOTermID() == "SBO:0000231"); + + fail_unless(e->isSetDelay(), NULL); + + delay = e->getDelay(); + fail_unless(delay != NULL, NULL); + + fail_unless(delay->getSBOTerm() == 64, NULL); + fail_unless(delay->getSBOTermID() == "SBO:0000064"); + + ast = delay->getMath(); + fail_unless(!strcmp(SBML_formulaToString(ast), "p + 3"), NULL); + + ///** // * tests for the unit API functions // */ Modified: trunk/libsbml/src/sbml/test/TestWriteSBML.cpp =================================================================== --- trunk/libsbml/src/sbml/test/TestWriteSBML.cpp 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/test/TestWriteSBML.cpp 2008-09-11 22:56:32 UTC (rev 8162) @@ -26,6 +26,7 @@ #include <sstream> #include <sbml/xml/XMLOutputStream.h> +#include <sbml/xml/XMLNode.h> #include <sbml/util/util.h> #include <sbml/SBMLTypes.h> @@ -1577,7 +1578,22 @@ END_TEST +START_TEST (test_WriteSBML_Event_WithSBO) +{ + const char* expected = wrapXML("<event id=\"e\" sboTerm=\"SBO:0000076\"/>"); + + Event e; + e.setId("e"); + e.setSBOTerm(76); + + e.write(*XOS); + + fail_unless( equals(expected) ); +} +END_TEST + + START_TEST (test_WriteSBML_Event_trigger) { const char* expected = wrapXML @@ -1633,6 +1649,32 @@ END_TEST +START_TEST (test_WriteSBML_Event_delayWithSBO) +{ + const char* expected = wrapXML + ( + "<event id=\"e\">\n" + " <delay sboTerm=\"SBO:0000064\">\n" + " <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" + " <cn type=\"integer\"> 5 </cn>\n" + " </math>\n" + " </delay>\n" + "</event>" + ); + + Event e("e"); + ASTNode *node = SBML_parseFormula("5"); + Delay d(node); + d.setSBOTerm(64); + e.setDelay(&d); + + e.write(*XOS); + + fail_unless( equals(expected) ); +} +END_TEST + + START_TEST (test_WriteSBML_Event_both) { const char* expected = wrapXML @@ -1749,7 +1791,96 @@ END_TEST +START_TEST (test_WriteSBML_Constraint) +{ + D->setLevelAndVersion(2, 2); + const char* expected = wrapXML("<constraint sboTerm=\"SBO:0000064\"/>"); + + + Constraint ct; + ct.setSBOTerm(64); + ct.setSBMLDocument(D); + + ct.write(*XOS); + + fail_unless( equals(expected) ); +} +END_TEST + + +START_TEST (test_WriteSBML_Constraint_math) +{ + const char* expected = wrapXML + ( + "<constraint>\n" + " <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" + " <apply>\n" + " <leq/>\n" + " <ci> P1 </ci>\n" + " <ci> t </ci>\n" + " </apply>\n" + " </math>\n" + "</constraint>" + ); + + Constraint c; + ASTNode *node = SBML_parseFormula("leq(P1,t)"); + c.setMath(node); + + c.write(*XOS); + + fail_unless( equals(expected) ); +} +END_TEST + + +START_TEST (test_WriteSBML_Constraint_full) +{ + const char* expected = wrapXML + ( + "<constraint sboTerm=\"SBO:0000064\">\n" + " <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n" + " <apply>\n" + " <leq/>\n" + " <ci> P1 </ci>\n" + " <ci> t </ci>\n" + " </apply>\n" + " </math>\n" + " <message>\n" + " <p xmlns=\"http://www.w3.org/1999/xhtml\"> Species P1 is out of range </p>\n" + " </message>\n" + "</constraint>" + ); + + Constraint c; + ASTNode *node = SBML_parseFormula("leq(P1,t)"); + c.setMath(node); + c.setSBOTerm(64); + + const XMLNode *text = XMLNode::convertStringToXMLNode(" Species P1 is out of range "); + XMLTriple triple = XMLTriple("p", "http://www.w3.org/1999/xhtml", ""); + XMLAttributes att = XMLAttributes(); + att.add("xmlns", "http://www.w3.org/1999/xhtml"); + + XMLNode *p = new XMLNode(triple, att); + p->addChild(*(text)); + + XMLTriple triple1 = XMLTriple("message", "", ""); + XMLAttributes att1 = XMLAttributes(); + XMLNode *message = new XMLNode(triple1, att1); + + message->addChild(*(p)); + + c.setMessage(message); + + c.write(*XOS); + + fail_unless( equals(expected) ); +} +END_TEST + + START_TEST (test_WriteSBML_NaN) { const char* expected = wrapXML("<parameter id=\"p\" value=\"NaN\"/>"); @@ -2057,8 +2188,10 @@ // Event tcase_add_test( tcase, test_WriteSBML_Event ); + tcase_add_test( tcase, test_WriteSBML_Event_WithSBO ); tcase_add_test( tcase, test_WriteSBML_Event_trigger ); tcase_add_test( tcase, test_WriteSBML_Event_delay ); + tcase_add_test( tcase, test_WriteSBML_Event_delayWithSBO ); tcase_add_test( tcase, test_WriteSBML_Event_both ); tcase_add_test( tcase, test_WriteSBML_Event_full ); @@ -2066,6 +2199,11 @@ tcase_add_test( tcase, test_WriteSBML_CompartmentType ); tcase_add_test( tcase, test_WriteSBML_CompartmentType_withSBO ); + //Constraint + tcase_add_test( tcase, test_WriteSBML_Constraint ); + tcase_add_test( tcase, test_WriteSBML_Constraint_math ); + tcase_add_test( tcase, test_WriteSBML_Constraint_full ); + // Miscellaneous tcase_add_test( tcase, test_WriteSBML_NaN ); tcase_add_test( tcase, test_WriteSBML_INF ); Modified: trunk/libsbml/src/sbml/test/test-data/l2v2-newComponents.xml =================================================================== --- trunk/libsbml/src/sbml/test/test-data/l2v2-newComponents.xml 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/test/test-data/l2v2-newComponents.xml 2008-09-11 22:56:32 UTC (rev 8162) @@ -30,7 +30,7 @@ </initialAssignment> </listOfInitialAssignments> <listOfConstraints> - <constraint> + <constraint sboTerm="SBO:0000064"> <math xmlns="http://www.w3.org/1998/Math/MathML"> <apply> <lt/> Modified: trunk/libsbml/src/sbml/test/test-data/l2v3-all.xml =================================================================== --- trunk/libsbml/src/sbml/test/test-data/l2v3-all.xml 2008-09-11 18:22:18 UTC (rev 8161) +++ trunk/libsbml/src/sbml/test/test-data/l2v3-all.xml 2008-09-11 22:56:32 UTC (rev 8162) @@ -31,7 +31,7 @@ <speciesType id="gg"/> </listOfSpeciesTypes> <listOfCompartments> - <compartment id="a" size="2.3" compartmentType="hh" sboTerm="SBO:0000236"/> + <compartment id="a" size="2.3" compartmentType="hh" sboTerm="SBO:0000236" constant="false"/> </listOfCompartments> <listOfSpecies> <species id="s" compartment="a" initialAmount="0"/> @@ -128,7 +128,7 @@ </reaction> </listOfReactions> <listOfEvents> - <event> + <event id="e1" sboTerm="SBO:0000231"> <trigger> <math xmlns="http://www.w3.org/1998/Math/MathML"> <apply> @@ -138,6 +138,15 @@ </apply> </math> </trigger> + <delay sboTerm="SBO:0000064"> + <math xmlns="http://www.w3.org/1998/Math/MathML"> + <apply> + <plus/> + <ci> p </ci> + <cn type="integer"> 3 </cn> + </apply> + </math> + </delay> <listOfEventAssignments> <eventAssignment variable="a"> <math xmlns="http://www.w3.org/1998/Math/MathML"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |