Search in sources :

Example 1 with ExpressionMathMLParser

use of cbit.vcell.parser.ExpressionMathMLParser in project vcell by virtualcell.

the class CellQuanVCTranslator method addFunction.

private Function addFunction(Element temp, Element comp, String mangledName) {
    String expStr = null;
    Expression exp = null;
    Element parent = (Element) temp.getParent();
    Element sibling = parent.getChild(MathMLTags.APPLY, mathns);
    if (sibling == null) {
        // check if its value is assigned to another variable (i.e. A = B)
        @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(parent.getChildren(MathMLTags.IDENTIFIER, mathns));
        if (list.size() == 2) {
            expStr = (list.get(1)).getTextTrim();
        }
        if (expStr == null || expStr.length() == 0) {
            expStr = parent.getChildText(MathMLTags.CONSTANT, mathns);
        }
        if (expStr == null || expStr.length() == 0) {
            // check if 'piecewise'
            sibling = parent.getChild(MathMLTags.PIECEWISE, mathns);
        }
        if (expStr != null) {
            try {
                exp = new Expression(expStr);
                exp = processMathExp(comp, exp);
                nl.mangleString(expStr);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException(e.getMessage());
            }
        }
    }
    if (sibling != null) {
        Element trimmedMath = new Element(CELLMLTags.MATH, mathns).addContent(sibling.detach());
        fixMathMLBug(trimmedMath);
        try {
            exp = (new ExpressionMathMLParser(null)).fromMathML(trimmedMath);
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException(e.getMessage());
        }
        exp = processMathExp(comp, exp);
        expStr = exp.infix();
        nl.mangleString(expStr);
    }
    Domain domain = null;
    return new Function(mangledName, exp, domain);
}
Also used : Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 2 with ExpressionMathMLParser

use of cbit.vcell.parser.ExpressionMathMLParser in project vcell by virtualcell.

the class CellQuanVCTranslator method addCompartmentSubDomain.

/**
 * 	addCompartmentSubDomain : we pick the variable name by reading through the mathML.
 * 	Redundancy in variable name with the volume variable?
 */
protected void addCompartmentSubDomain() throws Exception {
    String csdName = sRoot.getAttributeValue(CELLMLTags.name, sAttNamespace);
    CompartmentSubDomain csd = new CompartmentSubDomain(csdName, CompartmentSubDomain.NON_SPATIAL_PRIORITY);
    Iterator<?> compElementIter = sRoot.getChildren(CELLMLTags.COMPONENT, sNamespace).iterator();
    // JDOMTreeWalker walker = new JDOMTreeWalker(sRoot, new ElementFilter(CELLMLTags.COMPONENT));
    Element comp, math;
    String compName, varName, mangledName;
    while (compElementIter.hasNext()) {
        comp = (Element) compElementIter.next();
        compName = comp.getAttributeValue(CELLMLTags.name, sAttNamespace);
        @SuppressWarnings("unchecked") Iterator<Element> mathIter = comp.getChildren(CELLMLTags.MATH, mathns).iterator();
        while (mathIter.hasNext()) {
            math = mathIter.next();
            Element apply, apply2, apply3, ci;
            // allow multiple 'apply' children.
            @SuppressWarnings("unchecked") Iterator<Element> applyIter = math.getChildren(MathMLTags.APPLY, mathns).iterator();
            while (applyIter.hasNext()) {
                apply = applyIter.next();
                @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(apply.getChildren());
                if (list.size() < 3)
                    continue;
                if (!(list.get(0)).getName().equals(MathMLTags.EQUAL))
                    continue;
                apply2 = list.get(1);
                if (!apply2.getName().equals(MathMLTags.APPLY))
                    continue;
                @SuppressWarnings("unchecked") ArrayList<Element> list2 = new ArrayList<Element>(apply2.getChildren());
                if (list2.size() < 3)
                    continue;
                if (!(list2.get(0)).getName().equals(MathMLTags.DIFFERENTIAL))
                    continue;
                // skip the time variable
                ci = list2.get(2);
                varName = ci.getTextTrim();
                // can be a constant
                apply3 = list.get(2);
                mangledName = nm.getMangledName(compName, varName);
                Element trimmedMath = new Element(CELLMLTags.MATH, mathns).addContent(apply3.detach());
                fixMathMLBug(trimmedMath);
                Expression rateExp = null;
                try {
                    rateExp = (new ExpressionMathMLParser(null)).fromMathML(trimmedMath);
                    rateExp = processMathExp(comp, rateExp);
                    rateExp = rateExp.flatten();
                    nl.mangleString(rateExp.infix());
                } catch (ExpressionException e) {
                    e.printStackTrace(System.out);
                    throw new RuntimeException(e.getMessage());
                }
                Expression initExp = new Expression(getInitial(comp, varName));
                Domain domain = null;
                OdeEquation ode = new OdeEquation(new VolVariable(mangledName, domain), initExp, rateExp);
                csd.addEquation(ode);
            }
        }
    }
    mathDescription.addSubDomain(csd);
}
Also used : VolVariable(cbit.vcell.math.VolVariable) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser) ExpressionException(cbit.vcell.parser.ExpressionException) OdeEquation(cbit.vcell.math.OdeEquation) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain)

Example 3 with ExpressionMathMLParser

use of cbit.vcell.parser.ExpressionMathMLParser in project vcell by virtualcell.

the class SBMLImporter method getExpressionFromFormula.

/**
 * getExpressionFromFormula : Convert the math formula string in a
 * kineticLaw, rule or lambda function definition into MathML and use
 * ExpressionMathMLParser to convert the MathML into an expression to be
 * brought into the VCell. NOTE : ExpressionMathMLParser will handle only
 * the <apply> elements of the MathML string, hence the
 * ExpressionMathMLParser is given a substring of the MathML containing the
 * <apply> elements.
 * @throws XMLStreamException
 * @throws SBMLException
 */
private Expression getExpressionFromFormula(ASTNode math) throws ExpressionException, SBMLException, XMLStreamException {
    String mathMLStr = JSBML.writeMathMLToString(math);
    if (mathMLStr.contains(DELAY_URL)) {
        throw new SBMLImportException("unsupported SBML element 'delay'", SBMLImportException.Category.DELAY);
    }
    ExpressionMathMLParser exprMathMLParser = new ExpressionMathMLParser(lambdaFunctions);
    Expression expr = exprMathMLParser.fromMathML(mathMLStr);
    return expr;
}
Also used : Expression(cbit.vcell.parser.Expression) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser)

Aggregations

Expression (cbit.vcell.parser.Expression)3 ExpressionMathMLParser (cbit.vcell.parser.ExpressionMathMLParser)3 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)2 Domain (cbit.vcell.math.Variable.Domain)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 ArrayList (java.util.ArrayList)2 Element (org.jdom.Element)2 Function (cbit.vcell.math.Function)1 OdeEquation (cbit.vcell.math.OdeEquation)1 VolVariable (cbit.vcell.math.VolVariable)1