Search in sources :

Example 6 with VCUnitException

use of cbit.vcell.units.VCUnitException in project vcell by virtualcell.

the class MathMapping_4_8 method getIdentifierSubstitutions.

/**
 * Substitutes appropriate variables for speciesContext bindings
 *
 * @return cbit.vcell.parser.Expression
 * @param origExp cbit.vcell.parser.Expression
 * @param structureMapping cbit.vcell.mapping.StructureMapping
 */
protected Expression getIdentifierSubstitutions(Expression origExp, VCUnitDefinition desiredExpUnitDef, StructureMapping structureMapping) throws ExpressionException, MappingException {
    String[] symbols = origExp.getSymbols();
    if (symbols == null) {
        return origExp;
    }
    VCUnitDefinition expUnitDef = null;
    try {
        VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(simContext.getModel().getUnitSystem());
        expUnitDef = unitEvaluator.getUnitDefinition(origExp);
        if (desiredExpUnitDef == null) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', desiredUnits are null");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[null], observed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
        } else if (expUnitDef == null) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', evaluated Units are null");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[null]", Issue.SEVERITY_WARNING));
        } else if (desiredExpUnitDef.isTBD()) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', desiredUnits are [" + desiredExpUnitDef.getSymbol() + "] and expression units are [" + expUnitDef.getSymbol() + "]");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
        } else if (!desiredExpUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD()) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', desiredUnits are [" + desiredExpUnitDef.getSymbol() + "] and expression units are [" + expUnitDef.getSymbol() + "]");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
        }
    } catch (VCUnitException e) {
        String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
        System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    } catch (ExpressionException e) {
        String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
        System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    } catch (Exception e) {
        e.printStackTrace(System.out);
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    }
    Expression newExp = new Expression(origExp);
    for (int i = 0; i < symbols.length; i++) {
        SymbolTableEntry ste = origExp.getSymbolBinding(symbols[i]);
        if (ste == null) {
            throw new ExpressionBindingException("symbol '" + symbols[i] + "' not bound");
        // ste = simContext.getGeometryContext().getModel().getSpeciesContext(symbols[i]);
        }
        if (ste != null) {
            String newName = getMathSymbol(ste, structureMapping);
            if (!newName.equals(symbols[i])) {
                newExp.substituteInPlace(new Expression(symbols[i]), new Expression(newName));
            }
        }
    }
    return newExp;
}
Also used : VCUnitException(cbit.vcell.units.VCUnitException) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Issue(org.vcell.util.Issue) Expression(cbit.vcell.parser.Expression) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MathException(cbit.vcell.math.MathException)

Example 7 with VCUnitException

use of cbit.vcell.units.VCUnitException in project vcell by virtualcell.

the class AbstractMathMapping method getIdentifierSubstitutions.

/**
 * Substitutes appropriate variables for speciesContext bindings
 *
 * @return cbit.vcell.parser.Expression
 * @param origExp cbit.vcell.parser.Expression
 * @param structureMapping cbit.vcell.mapping.StructureMapping
 */
protected Expression getIdentifierSubstitutions(Expression origExp, VCUnitDefinition desiredExpUnitDef, GeometryClass geometryClass) throws ExpressionException, MappingException {
    String[] symbols = origExp.getSymbols();
    if (symbols == null) {
        return origExp;
    }
    VCUnitDefinition expUnitDef = null;
    try {
        VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(simContext.getModel().getUnitSystem());
        expUnitDef = unitEvaluator.getUnitDefinition(origExp);
        if (desiredExpUnitDef == null) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', desiredUnits are null");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[null], observed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
        } else if (expUnitDef == null) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            System.out.println("...........exp='" + expStr + "', evaluated Units are null");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[null]", Issue.SEVERITY_WARNING));
        } else if (desiredExpUnitDef.isTBD()) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            // System.out.println("...........exp='"+expStr+"', desiredUnits are ["+desiredExpUnitDef.getSymbol()+"] and expression units are ["+expUnitDef.getSymbol()+"]");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
        } else if (!desiredExpUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD()) {
            String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
            // System.out.println("...........exp='"+expStr+"', desiredUnits are ["+desiredExpUnitDef.getSymbol()+"] and expression units are ["+expUnitDef.getSymbol()+"]");
            localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
        }
    } catch (VCUnitException e) {
        String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
        System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    } catch (ExpressionException e) {
        String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
        System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    } catch (Exception e) {
        e.printStackTrace(System.out);
        localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
    }
    Expression newExp = new Expression(origExp);
    // 
    // flatten user-defined functions
    // 
    FunctionInvocation[] functionInvocations = newExp.getFunctionInvocations(new FunctionFilter() {

        @Override
        public boolean accept(String functionName, FunctionType functionType) {
            return functionType == FunctionType.USERDEFINED;
        }
    });
    for (FunctionInvocation functionInvocation : functionInvocations) {
        if (functionInvocation.getSymbolTableFunctionEntry() instanceof Model.ModelFunction) {
            ModelFunction modelFunction = (ModelFunction) functionInvocation.getSymbolTableFunctionEntry();
            newExp.substituteInPlace(functionInvocation.getFunctionExpression(), modelFunction.getFlattenedExpression(functionInvocation));
        }
    }
    // 
    // then substitute Math symbols for Biological symbols.
    // 
    newExp.bindExpression(null);
    for (int i = 0; i < symbols.length; i++) {
        SymbolTableEntry ste = origExp.getSymbolBinding(symbols[i]);
        if (ste == null) {
            throw new ExpressionBindingException("symbol '" + symbols[i] + "' not bound");
        // ste = simContext.getGeometryContext().getModel().getSpeciesContext(symbols[i]);
        }
        if (ste != null) {
            String newName = getMathSymbol(ste, geometryClass);
            if (!newName.equals(symbols[i])) {
                newExp.substituteInPlace(new Expression(symbols[i]), new Expression(newName));
            }
        }
    }
    return newExp;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) Issue(org.vcell.util.Issue) ModelFunction(cbit.vcell.model.Model.ModelFunction) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) VCUnitException(cbit.vcell.units.VCUnitException) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression)

Example 8 with VCUnitException

use of cbit.vcell.units.VCUnitException in project vcell by virtualcell.

the class Kinetics method gatherIssues.

/**
 * Insert the method's description here.
 * Creation date: (5/12/2004 2:53:13 PM)
 */
public void gatherIssues(IssueContext issueContext, List<Issue> issueList) {
    issueContext = issueContext.newChildContext(ContextType.ModelProcessDynamics, this);
    // 
    for (int i = 0; fieldUnresolvedParameters != null && i < fieldUnresolvedParameters.length; i++) {
        issueList.add(new Issue(fieldUnresolvedParameters[i], issueContext, IssueCategory.UnresolvedParameter, "Unresolved parameter '" + fieldUnresolvedParameters[i].getName() + "' in reaction '" + reactionStep.getName() + "'", Issue.SEVERITY_ERROR));
    }
    // 
    for (int i = 0; fieldKineticsParameters != null && i < fieldKineticsParameters.length; i++) {
        if (fieldKineticsParameters[i].getRole() == ROLE_UserDefined) {
            try {
                if (!isReferenced(fieldKineticsParameters[i], 0)) {
                    issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.KineticsUnreferencedParameter, "Unreferenced Kinetic Parameter '" + fieldKineticsParameters[i].getName() + "' in reaction '" + reactionStep.getName() + "'", Issue.SEVERITY_WARNING));
                }
            } catch (ExpressionException e) {
                issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.KineticsExpressionError, "error resolving expression " + e.getMessage(), Issue.SEVERITY_WARNING));
            } catch (ModelException e) {
                issueList.add(new Issue(getReactionStep(), issueContext, IssueCategory.CyclicDependency, "cyclic dependency in the parameter definitions", Issue.SEVERITY_ERROR));
            }
        }
    }
    // 
    if (fieldKineticsParameters != null) {
        for (KineticsParameter kineticsParameter : fieldKineticsParameters) {
            if (kineticsParameter.getExpression() == null) {
                issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionMissing, "expression is missing", Issue.SEVERITY_INFO));
            } else {
                Expression exp = kineticsParameter.getExpression();
                String[] symbols = exp.getSymbols();
                String issueMessagePrefix = "Kinetic parameter '" + kineticsParameter.getName() + "' in reaction '" + getReactionStep().getName() + "' ";
                if (symbols != null) {
                    for (int j = 0; j < symbols.length; j++) {
                        SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
                        if (ste instanceof KineticsProxyParameter) {
                            ste = ((KineticsProxyParameter) ste).getTarget();
                        }
                        if (ste == null) {
                            issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined symbol '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                        } else if (ste instanceof SpeciesContext) {
                            if (!getReactionStep().getModel().contains((SpeciesContext) ste)) {
                                issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined species '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                            }
                            if (reactionStep.countNumReactionParticipants((SpeciesContext) ste) == 0) {
                                issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionNonParticipantSymbol, issueMessagePrefix + "references species context '" + symbols[j] + "', but it is not a reactant/product/catalyst of this reaction", Issue.SEVERITY_WARNING));
                            }
                        } else if (ste instanceof ModelParameter) {
                            if (!getReactionStep().getModel().contains((ModelParameter) ste)) {
                                issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined global parameter '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                            }
                        }
                    }
                }
            }
        }
        // looking for local param which masks a global and issueing a warning
        for (KineticsParameter kineticsParameter : fieldKineticsParameters) {
            String name = kineticsParameter.getName();
            SymbolTableEntry ste = getReactionStep().getNameScope().getExternalEntry(name, getReactionStep());
            String steName;
            if (ste != null) {
                if (ste instanceof Displayable) {
                    steName = ((Displayable) ste).getDisplayType() + " " + ste.getName();
                } else {
                    steName = ste.getClass().getSimpleName() + " " + ste.getName();
                }
                String msg = steName + " is overriden by a local parameter " + name + " in reaction " + getReactionStep().getName();
                issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.Identifiers, msg, Issue.SEVERITY_WARNING));
            }
        }
    }
    try {
        // 
        // determine unit consistency for each expression
        // 
        ModelUnitSystem modelUnitSystem = getReactionStep().getModel().getUnitSystem();
        VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(modelUnitSystem);
        for (int i = 0; i < fieldKineticsParameters.length; i++) {
            try {
                VCUnitDefinition paramUnitDef = fieldKineticsParameters[i].getUnitDefinition();
                VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldKineticsParameters[i].getExpression());
                if (paramUnitDef == null) {
                    issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
                } else if (paramUnitDef.isTBD()) {
                    issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "undefined unit " + modelUnitSystem.getInstance_TBD().getSymbol(), Issue.SEVERITY_WARNING));
                } else if (expUnitDef == null) {
                    issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
                } else if (paramUnitDef.isTBD() || (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD())) {
                    issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "inconsistent units, defined=[" + fieldKineticsParameters[i].getUnitDefinition().getSymbol() + "], computed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
                }
            } catch (VCUnitException e) {
                issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
            } catch (ExpressionException e) {
                issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
            }
        }
    } catch (Throwable e) {
        issueList.add(new Issue(getReactionStep(), issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_INFO));
    }
    // 
    for (int i = 0; i < fieldKineticsParameters.length; i++) {
        RealInterval simpleBounds = bounds[fieldKineticsParameters[i].getRole()];
        if (simpleBounds != null) {
            String parmName = reactionStep.getNameScope().getName() + "." + fieldKineticsParameters[i].getName();
            issueList.add(new SimpleBoundsIssue(fieldKineticsParameters[i], issueContext, simpleBounds, "parameter " + parmName + ": must be within " + simpleBounds.toString()));
        }
    }
}
Also used : Displayable(org.vcell.util.Displayable) Issue(org.vcell.util.Issue) RealInterval(net.sourceforge.interval.ia_math.RealInterval) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) ModelParameter(cbit.vcell.model.Model.ModelParameter) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 9 with VCUnitException

use of cbit.vcell.units.VCUnitException in project vcell by virtualcell.

the class VCUnitEvaluator method getUnitDefinition.

private VCUnitDefinition getUnitDefinition(SimpleNode node, UnitsHashMap unitsHashMap) throws ExpressionException, VCUnitException {
    // temp variable
    VCUnitDefinition unit = null;
    if (node == null) {
        return null;
    }
    if (node instanceof ASTAndNode || node instanceof ASTOrNode || node instanceof ASTNotNode) {
        for (int i = 0; i < node.jjtGetNumChildren(); i++) {
            SimpleNode child = (SimpleNode) node.jjtGetChild(i);
            VCUnitDefinition childUnit = getUnitDefinition(child, unitsHashMap);
            if (!childUnit.isEquivalent(unitSystem.getInstance_DIMENSIONLESS())) {
                throw new VCUnitException("argument to boolean expression '" + child.infixString(SimpleNode.LANGUAGE_DEFAULT) + "' must be dimensionless");
            }
        }
        return unitSystem.getInstance_DIMENSIONLESS();
    } else if (node instanceof ASTRelationalNode) {
        ArrayList<VCUnitDefinition> units = new ArrayList<VCUnitDefinition>();
        for (int i = 0; i < node.jjtGetNumChildren(); i++) {
            units.add(getUnitDefinition((SimpleNode) node.jjtGetChild(i), unitsHashMap));
        }
        // looking for imcompatabilities
        computeUnit(node, (VCUnitDefinition[]) units.toArray(new VCUnitDefinition[units.size()]), true);
        return unitSystem.getInstance_DIMENSIONLESS();
    } else if (node instanceof ASTAddNode || node instanceof ASTMinusTermNode) {
        ArrayList<VCUnitDefinition> units = new ArrayList<VCUnitDefinition>();
        for (int i = 0; i < node.jjtGetNumChildren(); i++) {
            units.add(getUnitDefinition((SimpleNode) node.jjtGetChild(i), unitsHashMap));
        }
        return computeUnit(node, (VCUnitDefinition[]) units.toArray(new VCUnitDefinition[units.size()]), true);
    } else if (node instanceof ASTMultNode) {
        if (node.jjtGetNumChildren() == 1) {
            return getUnitDefinition((SimpleNode) node.jjtGetChild(0), unitsHashMap);
        }
        VCUnitDefinition accumUnit = null;
        for (int i = 0; i < node.jjtGetNumChildren(); i++) {
            SimpleNode child = (SimpleNode) node.jjtGetChild(i);
            unit = getUnitDefinition(child, unitsHashMap);
            if (unit.isTBD()) {
                return unit;
            }
            if (accumUnit == null) {
                accumUnit = unit;
            } else {
                accumUnit = accumUnit.multiplyBy(unit);
            }
        }
        return accumUnit;
    } else if (node instanceof ASTInvertTermNode) {
        SimpleNode child = (SimpleNode) node.jjtGetChild(0);
        unit = getUnitDefinition(child, unitsHashMap);
        if (unit.isTBD()) {
            return unit;
        } else {
            return unit.getInverse();
        }
    } else if (node instanceof DerivativeNode) {
        unit = getUnitDefinition((SimpleNode) node.jjtGetChild(1), unitsHashMap);
        return getUnitDefinition((SimpleNode) node.jjtGetChild(0), unitsHashMap).divideBy(unit);
    } else if (node instanceof ASTLaplacianNode) {
        return unitSystem.getInstance_TBD();
    // unit = unitSystem.Instance(ModelUnitSystem.UNITSYMBOL_um);
    // return getUnitDefinition((SimpleNode)node.jjtGetChild(0),unitsHashMap).divideBy(unit).divideBy(unit);
    } else if (node instanceof ASTFloatNode) {
        // return TBD instead of dimensionless.
        return unitSystem.getInstance_TBD();
    } else if (node instanceof ASTFuncNode) {
        String functionName = ((ASTFuncNode) node).getName();
        if (functionName.equalsIgnoreCase("pow")) {
            SimpleNode child0 = (SimpleNode) node.jjtGetChild(0);
            SimpleNode child1 = (SimpleNode) node.jjtGetChild(1);
            VCUnitDefinition unit0 = getUnitDefinition(child0, unitsHashMap);
            VCUnitDefinition unit1 = getUnitDefinition(child1, unitsHashMap);
            if (!unit1.isEquivalent(unitSystem.getInstance_DIMENSIONLESS()) && !unit1.isTBD()) {
                throw new VCUnitException("exponent of '" + node.infixString(SimpleNode.LANGUAGE_DEFAULT) + "' has units of " + unit0);
            }
            if (unit0.isEquivalent(unitSystem.getInstance_DIMENSIONLESS()) || unit0.isTBD()) {
                return unit0;
            }
            try {
                double d = ((SimpleNode) node.jjtGetChild(1)).evaluateConstant();
                RationalNumber rn = RationalNumber.getApproximateFraction(d);
                return unit0.raiseTo(rn);
            } catch (ExpressionException e) {
                // ????? don't know the unit now
                return unitSystem.getInstance_TBD();
            }
        } else if (functionName.equalsIgnoreCase("exp")) {
            SimpleNode child0 = (SimpleNode) node.jjtGetChild(0);
            VCUnitDefinition unit0 = getUnitDefinition(child0, unitsHashMap);
            if (!unit0.isEquivalent(unitSystem.getInstance_DIMENSIONLESS()) && !unit0.isTBD()) {
                throw new VCUnitException("exponent of exp() '" + node.infixString(SimpleNode.LANGUAGE_DEFAULT) + "' has units of " + unit0);
            }
            return unitSystem.getInstance_DIMENSIONLESS();
        } else if (functionName.equalsIgnoreCase("sqrt")) {
            SimpleNode child0 = (SimpleNode) node.jjtGetChild(0);
            VCUnitDefinition unit0 = getUnitDefinition(child0, unitsHashMap);
            if (unit0.isEquivalent(unitSystem.getInstance_DIMENSIONLESS()) || unit0.isTBD()) {
                return unit0;
            }
            RationalNumber rn = new RationalNumber(1, 2);
            return unit0.raiseTo(rn);
        } else if (functionName.equalsIgnoreCase("abs") || functionName.equalsIgnoreCase("min") || functionName.equalsIgnoreCase("max")) {
            return getUnitDefinition((SimpleNode) node.jjtGetChild(0), unitsHashMap);
        } else if (((ASTFuncNode) node).getFunction() == FunctionType.USERDEFINED) {
            ASTFuncNode funcNode = (ASTFuncNode) node;
            SymbolTableFunctionEntry stfe = funcNode.getSymbolTableFunctionEntry();
            if (stfe != null) {
                if (stfe.getUnitDefinition() != null) {
                    return stfe.getUnitDefinition();
                }
            }
            return unitSystem.getInstance_TBD();
        } else {
            return unitSystem.getInstance_DIMENSIONLESS();
        }
    } else if (node instanceof ASTPowerNode) {
        SimpleNode child0 = (SimpleNode) node.jjtGetChild(0);
        SimpleNode child1 = (SimpleNode) node.jjtGetChild(1);
        VCUnitDefinition unit0 = getUnitDefinition(child0, unitsHashMap);
        VCUnitDefinition unit1 = getUnitDefinition(child1, unitsHashMap);
        if (!unit1.isEquivalent(unitSystem.getInstance_DIMENSIONLESS()) && !unit1.isTBD()) {
            throw new VCUnitException("exponent of '" + node.infixString(SimpleNode.LANGUAGE_DEFAULT) + "' has units of " + unit0);
        }
        if (unit0.isEquivalent(unitSystem.getInstance_DIMENSIONLESS())) {
            return unitSystem.getInstance_DIMENSIONLESS();
        }
        boolean bConstantExponent = false;
        double exponentValue = 1;
        try {
            exponentValue = child1.evaluateConstant();
            bConstantExponent = true;
        } catch (ExpressionException e) {
            bConstantExponent = false;
        }
        if (bConstantExponent) {
            // 
            if (unit0.isTBD()) {
                return unitSystem.getInstance_TBD();
            } else {
                RationalNumber rn = RationalNumber.getApproximateFraction(exponentValue);
                return unit0.raiseTo(rn);
            }
        } else {
            return unitSystem.getInstance_TBD();
        }
    } else if (node instanceof ASTIdNode) {
        SymbolTableEntry ste = ((ASTIdNode) node).symbolTableEntry;
        unit = unitsHashMap.get(ste);
        if (unit == null) {
            unit = ste.getUnitDefinition();
            if (unit == null) {
                throw new ExpressionException("No unit found for expression node: " + node.toString());
            } else {
                unitsHashMap.put(ste, unit);
            }
        }
        return unit;
    } else {
        throw new ExpressionException("node type " + node.getClass().toString() + " not supported yet");
    }
}
Also used : ArrayList(java.util.ArrayList) VCUnitException(cbit.vcell.units.VCUnitException) RationalNumber(ucar.units_vcell.RationalNumber) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition)

Aggregations

VCUnitException (cbit.vcell.units.VCUnitException)9 Expression (cbit.vcell.parser.Expression)7 ExpressionException (cbit.vcell.parser.ExpressionException)7 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)7 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)5 VCUnitEvaluator (cbit.vcell.parser.VCUnitEvaluator)5 Issue (org.vcell.util.Issue)5 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)4 PropertyVetoException (java.beans.PropertyVetoException)4 ModelParameter (cbit.vcell.model.Model.ModelParameter)3 ScopedExpression (cbit.gui.ScopedExpression)2 MathException (cbit.vcell.math.MathException)2 MatrixException (cbit.vcell.matrix.MatrixException)2 ModelException (cbit.vcell.model.ModelException)2 ModelQuantity (cbit.vcell.model.ModelQuantity)2 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)2 Parameter (cbit.vcell.model.Parameter)2 SpeciesContext (cbit.vcell.model.SpeciesContext)2 RealInterval (net.sourceforge.interval.ia_math.RealInterval)2 Displayable (org.vcell.util.Displayable)2