Search in sources :

Example 1 with ModelFunction

use of cbit.vcell.model.Model.ModelFunction 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)

Aggregations

MathException (cbit.vcell.math.MathException)1 MatrixException (cbit.vcell.matrix.MatrixException)1 ModelFunction (cbit.vcell.model.Model.ModelFunction)1 ModelException (cbit.vcell.model.ModelException)1 FunctionType (cbit.vcell.parser.ASTFuncNode.FunctionType)1 Expression (cbit.vcell.parser.Expression)1 FunctionFilter (cbit.vcell.parser.Expression.FunctionFilter)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 FunctionInvocation (cbit.vcell.parser.FunctionInvocation)1 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)1 VCUnitEvaluator (cbit.vcell.parser.VCUnitEvaluator)1 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)1 VCUnitException (cbit.vcell.units.VCUnitException)1 PropertyVetoException (java.beans.PropertyVetoException)1 Issue (org.vcell.util.Issue)1