Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xmlns in math #310

Merged
merged 5 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/sbml/math/ASTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ ASTNode::ASTNode (ASTNodeType_t type)
mStyle = "";
mIsBvar = false;
mUserData = NULL;
mNamespaces = NULL;

// move to after we have loaded plugins
//setType(type);
Expand Down Expand Up @@ -307,6 +308,7 @@ ASTNode::ASTNode (Token_t* token)
mStyle = "";
mIsBvar = false;
mUserData = NULL;
mNamespaces = NULL;

mChildren = new List;
mSemanticsAnnotations = new List;
Expand Down Expand Up @@ -378,6 +380,7 @@ ASTNode::ASTNode (const ASTNode& orig) :
,mStyle ( orig.mStyle)
,mIsBvar ( orig.mIsBvar)
,mUserData ( orig.mUserData )
, mNamespaces (orig.mNamespaces)
{
if (orig.mName)
{
Expand Down Expand Up @@ -426,6 +429,8 @@ ASTNode& ASTNode::operator=(const ASTNode& rhs)
mStyle = rhs.mStyle;
mIsBvar = rhs.mIsBvar;
mUserData = rhs.mUserData;
mNamespaces = rhs.mNamespaces;

freeName();
if (rhs.mName)
{
Expand Down Expand Up @@ -4571,6 +4576,20 @@ ASTNode::derivativeExp(const std::string& variable)
return derivative;
}

XMLNamespaces*
ASTNode::getDeclaredNamespaces() const
{
return mNamespaces;
}


void
ASTNode::setDeclaredNamespaces(const XMLNamespaces* xmlns)
{
mNamespaces = xmlns->clone();
}


#endif /* __cplusplus */


Expand Down
13 changes: 13 additions & 0 deletions src/sbml/math/ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,16 @@ setValue(value, 0);

LIBSBML_EXTERN
void printMath(unsigned int level = 0);

/** @cond doxygenLibsbmlInternal */
LIBSBML_EXTERN
XMLNamespaces* getDeclaredNamespaces() const;

LIBSBML_EXTERN
void setDeclaredNamespaces(const XMLNamespaces* xmlns);
/** @endcond */


protected:

friend class SBMLRateRuleConverter;
Expand Down Expand Up @@ -2471,6 +2481,9 @@ setValue(value, 0);
bool mIsBvar;
void *mUserData;
std::string mPackageName;


XMLNamespaces* mNamespaces;

friend class MathMLFormatter;
friend class MathMLHandler;
Expand Down
43 changes: 30 additions & 13 deletions src/sbml/math/MathML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,9 @@ readMathML (XMLInputStream& stream, std::string reqd_prefix, bool inRead)

if (elem.isStart() && elem.isEnd()) return node;

/* record and explicitly decared namespaces*/
node->setDeclaredNamespaces(&(elem.getNamespaces()));

/* check that math tag is followed by an appropriate
* tag
*/
Expand Down Expand Up @@ -2207,28 +2210,42 @@ writeMathML (const ASTNode* node, XMLOutputStream& stream, SBMLNamespaces *sbmln
{

static const string uri = "http://www.w3.org/1998/Math/MathML";
unsigned int level = SBML_DEFAULT_LEVEL;
unsigned int version = SBML_DEFAULT_VERSION;
if (sbmlns != NULL)
{
level = sbmlns->getLevel();
version = sbmlns->getVersion();
}

stream.startElement("math");
stream.writeAttribute("xmlns", uri);

if (node)
{
// FIX-ME need to know what level and version
if (node->hasUnits())
{
unsigned int level = SBML_DEFAULT_LEVEL;
unsigned int version = SBML_DEFAULT_VERSION;
if (sbmlns != NULL)
if (node->hasUnits())
{
level = sbmlns->getLevel();
version = sbmlns->getVersion();
stream.writeAttribute(XMLTriple("sbml", "", "xmlns"),
SBMLNamespaces::getSBMLNamespaceURI(level, version));
}

// write any namespaces that have been specifically declared on math element
//
XMLNamespaces* xmlns = node->getDeclaredNamespaces();
if (xmlns != NULL)
{
for (int i = 0; i < xmlns->getNumNamespaces(); i++)
{
if (xmlns->getURI(i) == uri || xmlns->getURI(i) == SBMLNamespaces::getSBMLNamespaceURI(level, version))
{
continue;
}
stream.writeAttribute(xmlns->getPrefix(i), "xmlns", xmlns->getURI(i));

}
}

stream.writeAttribute(XMLTriple("sbml", "", "xmlns"),
SBMLNamespaces::getSBMLNamespaceURI(level, version));
}

writeNode(*node, stream,sbmlns);
writeNode(*node, stream,sbmlns);
}

stream.endElement("math");
Expand Down
39 changes: 39 additions & 0 deletions src/sbml/packages/multi/extension/test/TestMultiAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ LIBSBML_CPP_NAMESPACE_USE

BEGIN_C_DECLS

static bool
equals(const char* expected, const char* actual)
{
if (!strcmp(expected, actual)) return true;

printf("\nStrings are not equal:\n");
printf("Expected:\n[%s]\n", expected);
printf("Actual:\n[%s]\n", actual);

return false;
}



extern char *TestDataDirectory;

Expand Down Expand Up @@ -204,6 +217,10 @@ START_TEST (test_multi_create_ci)
math->addChild(ci);
math->addChild(ci1);

// need to add multi ns
XMLNamespaces* xmlns = document->getSBMLNamespaces()->getNamespaces();
math->setDeclaredNamespaces(xmlns);

kl->setMath(math);
delete math;

Expand Down Expand Up @@ -334,6 +351,27 @@ START_TEST(test_multi_ast_plugin)
END_TEST


START_TEST(test_multi_math_ns)
{
const char * expected = "<kineticLaw>\n"
" <math xmlns=\"http://www.w3.org/1998/Math/MathML\" xmlns:multi=\"http://www.sbml.org/sbml/level3/version1/multi/version1\">\n"
" <apply>\n"
" <times/>\n"
" <ci multi:speciesReference=\"r1\"> s1 </ci>\n"
" <ci multi:representationType=\"sum\"> s1 </ci>\n"
" </apply>\n"
" </math>\n"
"</kineticLaw>";

string filename(TestDataDirectory);
string cfile = filename + "multi_ci_extension.xml";
SBMLDocument* doc = readSBMLFromFile(cfile.c_str());
KineticLaw* kl = doc->getModel()->getReaction(0)->getKineticLaw();
fail_unless(equals(expected, kl->toSBML()));
}
END_TEST


Suite *
create_suite_MultiAST(void)
{
Expand All @@ -344,6 +382,7 @@ create_suite_MultiAST(void)
tcase_add_test(tcase, test_multi_write_ci);
tcase_add_test(tcase, test_multi_create_ci);
tcase_add_test(tcase, test_multi_ast_plugin);
tcase_add_test(tcase, test_multi_math_ns);

suite_add_tcase(suite, tcase);

Expand Down