cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

How to set MathML string to libcellml component? #1024

Closed aditya-ml closed 2 years ago

aditya-ml commented 2 years ago

Following on the discussion in issue #1017 I tried creating math equations using libsbml.parseL3Formula. However, my program crashes on the last line rather than set the equation to libcellml component.

Snippet:

    comp_parameters.setName("parameters")
    model.addComponent(comp_parameters)
    math_ast1: libsbml.ASTNode = libsbml.parseL3Formula( "eff / t_ave")
    var_math_ast1 = libsbml.writeMathMLToString(math_ast1)
    print(var_math_ast1)
    comp_parameters.setMath(var_math_ast1)

Output:

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <apply>
    <divide/>
    <ci> eff </ci>
    <ci> t_ave </ci>
  </apply>
</math>
matthiaskoenig commented 2 years ago

We got this working, the issue were the correct namespaces on the MathML string.

hsorby commented 2 years ago

Would you mind clarifying which line the program segfaulted on. If it segfaulted on comp_parameters.setMath(var_math_ast1) I would definitely need to look into that.

matthiaskoenig commented 2 years ago

This was not a segfault. @aditya-ml got this wrong. The thing which happens is that the library just does not create the model if the namespace was missing on the MathML string. I.e. everything else in the component/model was not written out if the MathML was not correct. It would be nice if the library could be much more robust here. How libsbml is solving this is

I hope this makes sense.

hsorby commented 2 years ago

If you don't mind I will re-open this issue. I have found that I do get a segfault when printing out model using the output of libsbml.writeMathMLToString(math_ast1).

The output of this function is not what is given above in the output but the following:

<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <apply>
    <divide/>
    <ci> eff </ci>
    <ci> t_ave </ci>
  </apply>
</math>

And this is definitely causing a segfault when attempting to print the model.

hsorby commented 2 years ago

I don't get a crash when setting the math to a component. At that point the math is only a string and nothing happens with it. It isn't until some action on the string is required that things go South, like printing it.

hsorby commented 2 years ago

libCellML has taken the point of view that we will let the user/modeller do what they want, putting as few restrictions in place as we are able. When setting something we are not returning success or failure states because it muddies the water when you are returning something that isn't a success or failure state. What we do have instead is the Validator and Analyser, the Validator will report on every violation of the CellML 2 specification in the model. The Analyser goes a bit further and has a look at the actual math that is encapsulated in the model.

luciansmith commented 2 years ago

I can say that return values let you find errors much much faster than a post-hoc validation/analysis step. An analysis/validation tells you that something is wrong, not how something went wrong.

matthiaskoenig commented 2 years ago

I agree with Lucian here.

Also it allows to directly react/log issues when using the library in the context of a larger project. I.e. I don't want to do a complete post-hoc validation after every step to check if things worked.