Search in sources :

Example 16 with RationalExp

use of cbit.vcell.matrix.RationalExp in project vcell by virtualcell.

the class UnitSymbol method parseUnits.

private void parseUnits(String unitString) {
    try {
        String unit = unitString.trim();
        if (!unit.endsWith(";")) {
            unit = unit + ";";
        }
        UnitSymbolParser unitParser;
        unitParser = new UnitSymbolParser(new ByteArrayInputStream(unit.getBytes()));
        rootNode = unitParser.UnitSymbol();
        double numericScale = rootNode.getNumericScale();
        String infixWithoutNumericScale = rootNode.toInfixWithoutNumericScale();
        Expression exp = null;
        RationalExp rationalExp = null;
        try {
            exp = new Expression(infixWithoutNumericScale);
            rationalExp = RationalExpUtils.getRationalExp(exp);
            // Converting rationalExp back to expression since RationalExp represents power (^) of 'x' as multiple 'x's (e.g., x^3 == x.x.x)
            exp = rationalExp.simplifyAsExpression();
            // Converting rationalExp to exp will add parentheses and spaces in the expression.
            // These need to be removed from expression infix string for unitParser to parse expression infix.
            String infix2 = exp.infix_UNITS();
            // remove start "(" and end ")" if they exist
            if (infix2.contains("(") && infix2.contains(")") && infix2.startsWith("(") && infix2.endsWith(")")) {
                // remove start "("
                infix2 = infix2.substring(1);
                // remove last ")"
                infix2 = infix2.substring(0, infix2.length() - 1);
            }
            // replace blank spaces " " with no space ""
            infix2 = infix2.replace(" ", "");
            // add ";" to end of infix string for unitParser
            if (numericScale != 1.0) {
                infix2 = numericScale + " " + infix2;
            }
            infix2 = infix2 + ";";
            unitParser = new UnitSymbolParser(new ByteArrayInputStream(infix2.getBytes()));
            rootNode = unitParser.UnitSymbol();
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            System.out.println("Failed to simply unitsymbol '" + rootNode.toInfix() + "'" + e.getMessage());
        } catch (jscl.text.ParseException e) {
            e.printStackTrace(System.out);
            System.out.println("Failed to simply unitsymbol '" + rootNode.toInfix() + "'" + e.getMessage());
        }
    } catch (ParseException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Parse Error while parsing expression '" + unitString + "'.\n " + e.getMessage());
    } catch (TokenMgrError e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Parse Error while parsing expression '" + unitString + "'.\n " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Expression(cbit.vcell.parser.Expression) RationalExp(cbit.vcell.matrix.RationalExp) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

RationalExp (cbit.vcell.matrix.RationalExp)16 Expression (cbit.vcell.parser.Expression)12 RationalExpMatrix (cbit.vcell.matrix.RationalExpMatrix)6 FastInvariant (cbit.vcell.math.FastInvariant)5 Variable (cbit.vcell.math.Variable)5 ExpressionException (cbit.vcell.parser.ExpressionException)5 MemVariable (cbit.vcell.math.MemVariable)4 ReservedVariable (cbit.vcell.math.ReservedVariable)4 VolVariable (cbit.vcell.math.VolVariable)4 MatrixException (cbit.vcell.matrix.MatrixException)4 FastRate (cbit.vcell.math.FastRate)3 MathException (cbit.vcell.math.MathException)3 RationalNumber (cbit.vcell.matrix.RationalNumber)3 MappingException (cbit.vcell.mapping.MappingException)2 StructureMapping (cbit.vcell.mapping.StructureMapping)2 PseudoConstant (cbit.vcell.math.PseudoConstant)2 Kinetics (cbit.vcell.model.Kinetics)2 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)2 LumpedKinetics (cbit.vcell.model.LumpedKinetics)2 Membrane (cbit.vcell.model.Membrane)2