Search in sources :

Example 6 with FunctionFilter

use of cbit.vcell.parser.Expression.FunctionFilter in project vcell by virtualcell.

the class MathFunctionDefinitions method fixFunctionSyntax.

public static Expression fixFunctionSyntax(Expression exp) throws ExpressionException {
    FunctionInvocation[] functionInvocations = exp.getFunctionInvocations(new FunctionFilter() {

        @Override
        public boolean accept(String functionName, FunctionType functionType) {
            return (functionName.equals(FunctionName_field_old) || functionName.equals(FunctionName_grad_old));
        }
    });
    for (int i = 0; functionInvocations != null && i < functionInvocations.length; i++) {
        Expression[] argExpressions = functionInvocations[i].getArguments();
        if (functionInvocations[i].getFunctionName().equals(FunctionName_field_old)) {
            if (argExpressions.length >= 2 && argExpressions.length <= 4) {
                StringBuffer functionBuffer = new StringBuffer(FieldFunctionDefinition.FUNCTION_name + "(");
                if (argExpressions[0].isIdentifier()) {
                    functionBuffer.append("'" + argExpressions[0].infix() + "'");
                } else {
                    throw new ExpressionException("unexpected function format '" + functionInvocations[i].getFunctionExpression() + "'");
                }
                functionBuffer.append(",");
                if (argExpressions[1].isIdentifier()) {
                    functionBuffer.append("'" + argExpressions[1].infix() + "'");
                } else {
                    throw new ExpressionException("unexpected function format '" + functionInvocations[i].getFunctionExpression() + "'");
                }
                functionBuffer.append(",");
                if (argExpressions.length >= 3) {
                    functionBuffer.append(argExpressions[2].infix());
                } else {
                    functionBuffer.append("0.0");
                }
                functionBuffer.append(",");
                if (argExpressions.length >= 4) {
                    if (argExpressions[3].isIdentifier()) {
                        VariableType varType = VariableType.getVariableTypeFromVariableTypeNameIgnoreCase(argExpressions[3].infix());
                        functionBuffer.append("'" + varType.getTypeName() + "'");
                    } else {
                        throw new ExpressionException("unexpected function format '" + functionInvocations[i].getFunctionExpression() + "'");
                    }
                } else {
                    functionBuffer.append("'" + VariableType.VOLUME.getTypeName() + "'");
                }
                functionBuffer.append(")");
                exp = exp.getSubstitutedExpression(functionInvocations[i].getFunctionExpression(), new Expression(functionBuffer.toString()));
            }
        } else if (functionInvocations[i].getFunctionName().equals(FunctionName_grad_old)) {
            if (argExpressions.length == 2 && !argExpressions[1].isLiteral()) {
                // grad(x,calcium) ==> vcGrad(calcium,'x') ... where x is [x y z m]
                StringBuffer functionBuffer = new StringBuffer(GradientFunctionDefinition.FUNCTION_name + "(");
                Expression componentArg = argExpressions[0];
                Expression variableArg = argExpressions[1];
                functionBuffer.append(variableArg.infix());
                functionBuffer.append(",");
                functionBuffer.append("'" + componentArg.infix() + "'");
                functionBuffer.append(")");
                exp = exp.getSubstitutedExpression(functionInvocations[i].getFunctionExpression(), new Expression(functionBuffer.toString()));
            }
        }
    }
    return exp;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 7 with FunctionFilter

use of cbit.vcell.parser.Expression.FunctionFilter in project vcell by virtualcell.

the class SolverUtilities method getSizeFunctionInvocations.

public static Set<FunctionInvocation> getSizeFunctionInvocations(Expression expression) {
    if (expression == null) {
        return null;
    }
    FunctionInvocation[] functionInvocations = expression.getFunctionInvocations(new FunctionFilter() {

        @Override
        public boolean accept(String functionName, FunctionType functionType) {
            if (functionName.equals(MathFunctionDefinitions.Function_regionArea_current.getFunctionName()) || functionName.equals(MathFunctionDefinitions.Function_regionVolume_current.getFunctionName())) {
                return true;
            }
            return false;
        }
    });
    Set<FunctionInvocation> fiSet = new HashSet<FunctionInvocation>();
    for (FunctionInvocation fi : functionInvocations) {
        fiSet.add(fi);
    }
    return fiSet;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) HashSet(java.util.HashSet)

Aggregations

FunctionFilter (cbit.vcell.parser.Expression.FunctionFilter)7 FunctionInvocation (cbit.vcell.parser.FunctionInvocation)7 Expression (cbit.vcell.parser.Expression)5 FunctionType (cbit.vcell.parser.ASTFuncNode.FunctionType)4 ExpressionException (cbit.vcell.parser.ExpressionException)2 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)2 MathException (cbit.vcell.math.MathException)1 VariableType (cbit.vcell.math.VariableType)1 MatrixException (cbit.vcell.matrix.MatrixException)1 ModelFunction (cbit.vcell.model.Model.ModelFunction)1 ModelException (cbit.vcell.model.ModelException)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 SymbolTableFunctionEntry (cbit.vcell.parser.SymbolTableFunctionEntry)1 VCUnitEvaluator (cbit.vcell.parser.VCUnitEvaluator)1 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)1 VCUnitException (cbit.vcell.units.VCUnitException)1 PropertyVetoException (java.beans.PropertyVetoException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ASTExpression (org.vcell.model.bngl.ASTExpression)1