Search in sources :

Example 1 with FunctionDefinition

use of org.sbml.jsbml.FunctionDefinition in project vcell by virtualcell.

the class SBMLImporter method addFunctionDefinitions.

private void addFunctionDefinitions() {
    if (sbmlModel == null) {
        throw new SBMLImportException("SBML model is NULL");
    }
    ListOf<FunctionDefinition> listofFunctionDefinitions = sbmlModel.getListOfFunctionDefinitions();
    if (listofFunctionDefinitions == null) {
        System.out.println("No Function Definitions");
        return;
    }
    // The function definitions contain lambda function definition.
    // Each lambda function has a name, (list of) argument(s), function body
    // which is represented as a math element.
    lambdaFunctions = new LambdaFunction[(int) sbmlModel.getNumFunctionDefinitions()];
    try {
        for (int i = 0; i < sbmlModel.getNumFunctionDefinitions(); i++) {
            FunctionDefinition fnDefn = (FunctionDefinition) listofFunctionDefinitions.get(i);
            String functionName = new String(fnDefn.getId());
            ASTNode math = null;
            Vector<String> argsVector = new Vector<String>();
            Vector<String> secureArgsVector = new Vector<>();
            String[] functionArgs = null;
            if (fnDefn.isSetMath()) {
                math = fnDefn.getMath();
                // Function body.
                if (math.getNumChildren() == 0) {
                    System.out.println("(no function body defined)");
                    continue;
                }
                // Note that lambda function always should have at least 2 children
                for (int j = 0; j < math.getNumChildren() - 1; ++j) {
                    String baseName = new String(math.getChild(j).getName());
                    argsVector.addElement(baseName);
                    secureArgsVector.addElement(baseName + "_" + functionName + FormalArgumentSuffix);
                }
                functionArgs = secureArgsVector.toArray(new String[0]);
                math = math.getChild(math.getNumChildren() - 1);
                // formula = libsbml.formulaToString(math);
                Expression fnExpr = getExpressionFromFormula(math);
                for (int j = 0; j < argsVector.size(); j++) {
                    fnExpr.substituteInPlace(new Expression(argsVector.get(j)), new Expression(secureArgsVector.get(j)));
                }
                lambdaFunctions[i] = new LambdaFunction(functionName, fnExpr, functionArgs);
            }
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new SBMLImportException("Error adding Lambda function" + e.getMessage(), e);
    }
}
Also used : Expression(cbit.vcell.parser.Expression) ASTNode(org.sbml.jsbml.ASTNode) FunctionDefinition(org.sbml.jsbml.FunctionDefinition) LambdaFunction(cbit.vcell.parser.LambdaFunction) Vector(java.util.Vector) InteriorPoint(org.sbml.jsbml.ext.spatial.InteriorPoint) XMLStreamException(javax.xml.stream.XMLStreamException) SbmlException(org.vcell.sbml.SbmlException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) DivideByZeroException(cbit.vcell.parser.DivideByZeroException) SBMLException(org.sbml.jsbml.SBMLException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelPropertyVetoException(cbit.vcell.model.ModelPropertyVetoException) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

ModelPropertyVetoException (cbit.vcell.model.ModelPropertyVetoException)1 DivideByZeroException (cbit.vcell.parser.DivideByZeroException)1 Expression (cbit.vcell.parser.Expression)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 LambdaFunction (cbit.vcell.parser.LambdaFunction)1 XmlParseException (cbit.vcell.xml.XmlParseException)1 PropertyVetoException (java.beans.PropertyVetoException)1 IOException (java.io.IOException)1 Vector (java.util.Vector)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ASTNode (org.sbml.jsbml.ASTNode)1 FunctionDefinition (org.sbml.jsbml.FunctionDefinition)1 SBMLException (org.sbml.jsbml.SBMLException)1 InteriorPoint (org.sbml.jsbml.ext.spatial.InteriorPoint)1 SbmlException (org.vcell.sbml.SbmlException)1