Skip to content

Commit

Permalink
- ensure that annotation ns is correct, allow using l3v2
Browse files Browse the repository at this point in the history
 fixes #241  and #244
  • Loading branch information
fbergmann committed Jul 14, 2022
1 parent 30ae4dc commit e087303
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 159 deletions.
169 changes: 88 additions & 81 deletions src/sbml/packages/fbc/extension/FbcSBasePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,87 +872,94 @@ FbcSBasePlugin::createObject(XMLInputStream& stream)

/** @endcond */

/** @cond doxygenLibsbmlInternal */
/**
* Synchronizes the annotation of this SBML object.
*
* Annotation element (XMLNode* mAnnotation) is synchronized with the
* current CVTerm objects (List* mCVTerm).
* Currently, this method is called in getAnnotation, isSetAnnotation,
* and writeElements methods.
*/
void
FbcSBasePlugin::writeKeyValuePairsAnnotation(SBase* parentObject) const
{
if (parentObject == NULL) return;


XMLNode *parentAnnotation = parentObject->getAnnotation();
if (parentAnnotation != NULL && parentAnnotation->getNumChildren() > 0)
{
// deleteFbcAnnotation(parentAnnotation);
}

XMLToken ann_token = XMLToken(XMLTriple("annotation", "", ""), XMLAttributes());
XMLNode* annt = new XMLNode(ann_token);



if (mKeyValuePairs.size() > 0)
{
XMLAttributes loga_attr = XMLAttributes();
loga_attr.add("xmlns", FbcExtension::getXmlnsL3V1V3());
XMLToken loga_token = XMLToken(XMLTriple("listOfKeyValuePairs", FbcExtension::getXmlnsL3V1V1(), ""), loga_attr);
XMLNode loga = XMLNode(loga_token);

for (unsigned int i = 0; i < mKeyValuePairs.size(); ++i)
loga.addChild(mKeyValuePairs.get(i)->toXML());

// then add the ones toXML()
annt->addChild(loga);
}


if (annt && annt->getNumChildren() > 0)
{
parentObject->appendAnnotation(annt);
}
delete annt;
}
/** @endcond */


/** @cond doxygenLibsbmlInternal */
/**
* Parse L2 annotation if supported
*
*/
void
FbcSBasePlugin::parseAnnotation(SBase *parentObject, XMLNode *pAnnotation)
{
mKeyValuePairs.setSBMLDocument(mSBML);
// don't read if we have an invalid node or already a gene associations object
if (pAnnotation == NULL || mKeyValuePairs.size() > 0)
return;

// annotation element has been parsed by the parent element
// (Model) of this plugin object, thus the annotation element
// set to the above pAnnotation variable is parsed in this block.

XMLNode& listOfKeyValuePairs = pAnnotation->getChild("listOfKeyValuePairs");
if (listOfKeyValuePairs.getNumChildren() == 0)
return;

// read the xml node, overriding that all errors are flagged as
// warnings
mKeyValuePairs.read(listOfKeyValuePairs, LIBSBML_OVERRIDE_WARNING);
// remove listOfLayouts annotation
parentObject->removeTopLevelAnnotationElement("listOfKeyValuePairs", "", false);

}
/** @endcond */


/** @cond doxygenLibsbmlInternal */
/**
* Synchronizes the annotation of this SBML object.
*
* Annotation element (XMLNode* mAnnotation) is synchronized with the
* current CVTerm objects (List* mCVTerm).
* Currently, this method is called in getAnnotation, isSetAnnotation,
* and writeElements methods.
*/
void
FbcSBasePlugin::writeKeyValuePairsAnnotation(SBase* parentObject) const
{
if (parentObject == NULL) return;


XMLNode *parentAnnotation = parentObject->getAnnotation();
if (parentAnnotation != NULL && parentAnnotation->getNumChildren() > 0)
{
// deleteFbcAnnotation(parentAnnotation);
}

XMLToken ann_token = XMLToken(XMLTriple("annotation", "", ""), XMLAttributes());
XMLNode* annt = new XMLNode(ann_token);



if (mKeyValuePairs.size() > 0)
{
XMLAttributes loga_attr = XMLAttributes();
loga_attr.add("xmlns", mKeyValuePairs.getXmlns());
XMLToken loga_token = XMLToken(XMLTriple("listOfKeyValuePairs", mKeyValuePairs.getXmlns(), ""), loga_attr);
XMLNode loga = XMLNode(loga_token);

for (unsigned int i = 0; i < mKeyValuePairs.size(); ++i)
loga.addChild(mKeyValuePairs.get(i)->toXML());

// then add the ones toXML()
annt->addChild(loga);
}


if (annt && annt->getNumChildren() > 0)
{
parentObject->appendAnnotation(annt);
}
delete annt;
}
/** @endcond */


/** @cond doxygenLibsbmlInternal */
/**
* Parse L2 annotation if supported
*
*/
void
FbcSBasePlugin::parseAnnotation(SBase *parentObject, XMLNode *pAnnotation)
{
mKeyValuePairs.setSBMLDocument(mSBML);
// don't read if we have an invalid node or already a gene associations object
if (pAnnotation == NULL || mKeyValuePairs.size() > 0)
return;

// annotation element has been parsed by the parent element
// (Model) of this plugin object, thus the annotation element
// set to the above pAnnotation variable is parsed in this block.

XMLNode& listOfKeyValuePairs = pAnnotation->getChild("listOfKeyValuePairs");
if (listOfKeyValuePairs.getNumChildren() == 0)
return;

XMLNamespaces oldNs = listOfKeyValuePairs.getNamespaces();

// read the xml node, overriding that all errors are flagged as
// warnings
mKeyValuePairs.read(listOfKeyValuePairs, LIBSBML_OVERRIDE_WARNING);

// unfortunately the namespaces are overwritten at that point with the one from the
// document, so restore it
mKeyValuePairs.setXmlns(&oldNs, listOfKeyValuePairs.getPrefix());

// remove listOfLayouts annotation
parentObject->removeTopLevelAnnotationElement("listOfKeyValuePairs", "", false);

}
/** @endcond */





Expand Down
10 changes: 7 additions & 3 deletions src/sbml/packages/fbc/extension/test/TestWriteFbcExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,14 @@ START_TEST(test_FbcExtension_create_and_write_L3V1V3)

FbcSBasePlugin* sbaseplugin3 = dynamic_cast<FbcSBasePlugin*>(objective->getPlugin("fbc"));

ListOfKeyValuePairs *kvp_list = sbaseplugin3->getListOfKeyValuePairs();
fail_unless(kvp_list->isSetXmlns());
fail_unless(kvp_list->getXmlns() == "http://sbml.org/fbc/keyvaluepair");

KeyValuePair * kvp3 = sbaseplugin3->createKeyValuePair();
kvp3->setKey("key3");
kvp3->setUri("my_annotation");
kvp3->setValue("objective-value");
fail_unless(kvp3->setKey("key3") == LIBSBML_OPERATION_SUCCESS);
fail_unless(kvp3->setUri("my_annotation") == LIBSBML_OPERATION_SUCCESS);
fail_unless(kvp3->setValue("objective-value") == LIBSBML_OPERATION_SUCCESS);

//FbcSBasePlugin* sbaseplugin4 = dynamic_cast<FbcSBasePlugin*>(document->getPlugin("fbc"));
//// this dynamic cast is null
Expand Down
17 changes: 4 additions & 13 deletions src/sbml/packages/fbc/extension/test/test-data/fbc_example2_v3.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" xmlns:fbc="http://www.sbml.org/sbml/level3/version1/fbc/version3" level="3" version="1" fbc:required="false">
<!-- <annotation>
<listOfKeyValuePairs xlns="http://www.sbml.org/sbml/level3/version1/fbc/version3">
<keyValuePair key="key4" value="doc-value" uri="my_annotation"/>
</listOfKeyValuePairs>
</annotation>
-->
<model id="m" fbc:strict="true">
<annotation>
<listOfKeyValuePairs xlns="http://www.sbml.org/sbml/level3/version1/fbc/version3">
<listOfKeyValuePairs xmlns="http://sbml.org/fbc/keyvaluepair">
<keyValuePair key="key2" value="model-value" uri="my_annotation"/>
</listOfKeyValuePairs>
</annotation>
<listOfCompartments>
<compartment id="comp" spatialDimensions="3" size="1" constant="true">
<annotation>
<listOfKeyValuePairs xlns="http://www.sbml.org/sbml/level3/version1/fbc/version3">
<listOfKeyValuePairs xmlns="http://sbml.org/fbc/keyvaluepair">
<keyValuePair key="key" value="comp-value" uri="my_annotation"/>
</listOfKeyValuePairs>
</annotation>
Expand All @@ -24,7 +18,7 @@
<listOfSpecies>
<species id="S" compartment="comp" boundaryCondition="false" fbc:charge="2.5">
<annotation>
<listOfKeyValuePairs xlns="http://www.sbml.org/sbml/level3/version1/fbc/version3">
<listOfKeyValuePairs xmlns="http://sbml.org/fbc/keyvaluepair">
<keyValuePair key="key1" value="species-value" uri="my_annotation"/>
</listOfKeyValuePairs>
</annotation>
Expand All @@ -33,7 +27,7 @@
<fbc:listOfObjectives fbc:activeObjective="obj1">
<fbc:objective fbc:id="obj1" fbc:type="maximize">
<annotation>
<listOfKeyValuePairs xlns="http://www.sbml.org/sbml/level3/version1/fbc/version3">
<listOfKeyValuePairs xmlns="http://sbml.org/fbc/keyvaluepair">
<keyValuePair key="key3" value="objective-value" uri="my_annotation"/>
</listOfKeyValuePairs>
</annotation>
Expand All @@ -44,13 +38,10 @@
</fbc:listOfObjectives>
<fbc:listOfUserDefinedConstraints>
<fbc:userDefinedConstraint fbc:id="uc2" fbc:lowerBound="uc2lb" fbc:upperBound="uc2ub">

<fbc:listOfUserDefinedConstraintComponents>

<fbc:userDefinedConstraintComponent fbc:coefficient="2" fbc:variable="Avar" fbc:variableType="linear"/>
</fbc:listOfUserDefinedConstraintComponents>
</fbc:userDefinedConstraint>
</fbc:listOfUserDefinedConstraints>

</model>
</sbml>
54 changes: 24 additions & 30 deletions src/sbml/packages/fbc/sbml/KeyValuePair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,9 @@ int
KeyValuePair::setId(const std::string& id)
{
unsigned int coreLevel = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (coreLevel == 3 && coreVersion == 1 && pkgVersion == 3)
if (coreLevel == 3 && pkgVersion == 3)
{
return SyntaxChecker::checkAndSetSId(id, mId);
}
Expand All @@ -257,10 +256,9 @@ int
KeyValuePair::setName(const std::string& name)
{
unsigned int coreLevel = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (coreLevel == 3 && coreVersion == 1 && pkgVersion == 3)
if (coreLevel == 3 && pkgVersion == 3)
{
mName = name;
return LIBSBML_OPERATION_SUCCESS;
Expand All @@ -279,10 +277,9 @@ int
KeyValuePair::setKey(const std::string& key)
{
unsigned int coreLevel = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (coreLevel == 3 && coreVersion == 1 && pkgVersion == 3)
if (coreLevel == 3 && pkgVersion == 3)
{
mKey = key;
return LIBSBML_OPERATION_SUCCESS;
Expand All @@ -301,10 +298,9 @@ int
KeyValuePair::setValue(const std::string& value)
{
unsigned int coreLevel = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (coreLevel == 3 && coreVersion == 1 && pkgVersion == 3)
if (coreLevel == 3 && pkgVersion == 3)
{
mValue = value;
return LIBSBML_OPERATION_SUCCESS;
Expand All @@ -323,10 +319,9 @@ int
KeyValuePair::setUri(const std::string& uri)
{
unsigned int coreLevel = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (coreLevel == 3 && coreVersion == 1 && pkgVersion == 3)
if (coreLevel == 3 && pkgVersion == 3)
{
mUri = uri;
return LIBSBML_OPERATION_SUCCESS;
Expand Down Expand Up @@ -860,10 +855,9 @@ KeyValuePair::addExpectedAttributes(ExpectedAttributes& attributes)
SBase::addExpectedAttributes(attributes);

unsigned int level = getLevel();
unsigned int coreVersion = getVersion();
unsigned int pkgVersion = getPackageVersion();

if (level == 3 && coreVersion == 1 && pkgVersion == 3)
if (level == 3 && pkgVersion == 3)
{
attributes.add("id");
attributes.add("name");
Expand Down Expand Up @@ -1122,16 +1116,16 @@ KeyValuePair::writeL3V1V3Attributes(XMLOutputStream& stream) const
/** @endcond */


/*
* Creates an XMLNode object from this.
*/
XMLNode
KeyValuePair::toXML() const
{
XMLNamespaces xmlns = XMLNamespaces();
XMLTriple triple = XMLTriple(getElementName(), "", "");
XMLAttributes att = XMLAttributes();

/*
* Creates an XMLNode object from this.
*/
XMLNode
KeyValuePair::toXML() const
{
XMLNamespaces xmlns = XMLNamespaces();
XMLTriple triple = XMLTriple(getElementName(), "", "");
XMLAttributes att = XMLAttributes();

if (isSetId() == true)
{
att.add("id", mId);
Expand All @@ -1156,14 +1150,14 @@ KeyValuePair::toXML() const
{
att.add("uri", mUri);
}

XMLToken token = XMLToken(triple, att, xmlns);
XMLNode node(token);

return node;
}



XMLToken token = XMLToken(triple, att, xmlns);
XMLNode node(token);

return node;
}




#endif /* __cplusplus */
Expand Down
Loading

0 comments on commit e087303

Please sign in to comment.