Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tmp/
.idea/
__pycache__
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ Attempt at using XSLT to translate CellML 1.0 and 1.1 documents into the propose
Note that it needs an XSLT 2 processor to handle namespace nodes nicely. Tested using the Saxon engine within oXygen XML and Saxon on the command line, e.g., `saxon -s:test-models/cellml1.0.xml -xsl:cellml1to2.xsl`.

This is primarily an effort to enable the production of a suite of test CellML 2.0 models for use in testing the functionality of [libCellML](http://libcellml.readthedocs.io/).

## tests

There are some simple tests that can be run with Python, saxon, and libCellML.

From the repository root directory run:

`python -m unittest`

The final result should be:

`OK`
10 changes: 10 additions & 0 deletions cellml1to2.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@
<xsl:template match="cellml10:model | cellml11:model">
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
<xsl:copy-of select="@name"/>
<!-- Units elements can only be declared as a child of the model element -->
<xsl:for-each select="cellml10:component/cellml10:units | cellml11:component/cellml11:units" >
<xsl:element name="{local-name()}" namespace="http://www.cellml.org/cellml/2.0#">
<xsl:copy-of select="@name"/>
<xsl:apply-templates select="@* | node()"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will result in attributes on the units elements being ignored, since the template for @* above does nothing. Have you checked this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, now I have added a copy command to copy the name attribute. Testing with the van der pol model seems to give the correct result now.

</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="@* | node()"/>
</model>
</xsl:template>

<!-- Don't do anything with units that are children of components -->
<xsl:template match="cellml10:component/cellml10:units | cellml11:component/cellml11:units"/>

<!-- Variable elements need special handling for their interface attributes -->
<xsl:template match="cellml10:variable | cellml11:variable">
<xsl:element name="variable" namespace="http://www.cellml.org/cellml/2.0#">
Expand Down
16 changes: 16 additions & 0 deletions test-models/basic_model.cellml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<model xmlns="http://www.cellml.org/cellml/1.1#" xmlns:cellml="http://www.cellml.org/cellml/1.1#" xmlns:xlink="http://www.w3.org/1999/xlink" name="basic_model" id="metaid0">
<component name="environment">
<variable id="environment.t" initial_value="0" name="t" units="second"/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq/>
<ci>t</ci>
<apply>
<plus/>
<cn xmlns:cellml="http://www.cellml.org/cellml/1.1#" cellml:units="dimensionless">1</cn>
<cn xmlns:cellml="http://www.cellml.org/cellml/1.1#" cellml:units="dimensionless">3</cn>
</apply>
</apply>
</math>
</component>
</model>
61 changes: 61 additions & 0 deletions test-models/van_der_pol_model.cellml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version='1.0'?>
<model name="van_der_pol_model_1928" xmlns="http://www.cellml.org/cellml/1.0#" xmlns:cellml="http://www.cellml.org/cellml/1.0#">
<component name="main">
<units name="per_second">
<unit exponent="-1" units="second"/>
</units>
<variable name="time" units="second"/>
<variable initial_value="-2" name="x" units="dimensionless"/>
<variable initial_value="0" name="y" units="dimensionless"/>
<variable initial_value="1" name="epsilon" units="dimensionless"/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>time</ci>
</bvar>
<ci>x</ci>
</apply>
<apply>
<times/>
<ci>y</ci>
<cn cellml:units="per_second">1</cn>
</apply>
</apply>
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>time</ci>
</bvar>
<ci>y</ci>
</apply>
<apply>
<times/>
<apply>
<minus/>
<apply>
<times/>
<ci>epsilon</ci>
<apply>
<minus/>
<cn cellml:units="dimensionless">1</cn>
<apply>
<power/>
<ci>x</ci>
<cn cellml:units="dimensionless">2</cn>
</apply>
</apply>
<ci>y</ci>
</apply>
<ci>x</ci>
</apply>
<cn cellml:units="per_second">1</cn>
</apply>
</apply>
</math>
</component>
</model>
Empty file added test-scripts/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions test-scripts/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest

import libcellml as lc

from subprocess import Popen, PIPE


def run_saxon(model_name):
with Popen(["saxon", f"-s:test-models/{model_name}", "-xsl:cellml1to2.xsl"], stdout=PIPE) as proc:
return proc.stdout.read().decode()


class TranslationTestCase(unittest.TestCase):

def setUp(self):
self._p = lc.Parser()
self._v = lc.Validator()

def _validate_model(self, model_name):
translated_model = run_saxon(model_name)

m = self._p.parseModel(translated_model)

self._v.validateModel(m)

def test_basic_model(self):
self._validate_model('basic_model.cellml')

self.assertEqual(0, self._v.errorCount())

def test_van_der_pol_model(self):
self._validate_model('van_der_pol_model.cellml')

self.assertEqual(0, self._v.errorCount())

def test_cellml_1_0_model(self):
self._validate_model('cellml1.0.xml')

self.assertEqual(2, self._v.errorCount())
self.assertIn('celsius', self._v.error(0).description())
self.assertEqual('1.3.1.2', self._v.error(1).referenceHeading())

def test_cellml_1_1_model(self):
self._validate_model('cellml1.1.xml')

self.assertEqual(2, self._v.errorCount())
self.assertIn('celsius', self._v.error(0).description())
self.assertEqual('1.3.1.2', self._v.error(1).referenceHeading())


if __name__ == '__main__':
unittest.main()