Search in sources :

Example 1 with FunctionInvocation

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

the class RbmUtils method getBoundExpression.

public static Expression getBoundExpression(String expressionString, final SymbolTable symbolTable) throws ExpressionException {
    Expression exp = null;
    if (expressionString == null || expressionString.equals("")) {
        exp = new Expression(0);
    } else {
        exp = new Expression(expressionString);
    }
    // 
    // look for function invocations of unsupported but known functions
    // 
    FunctionInvocation[] invocations = exp.getFunctionInvocations(new FunctionFilter() {

        @Override
        public boolean accept(String functionName, FunctionType functionType) {
            if (functionName.equalsIgnoreCase("if")) {
                return true;
            }
            return false;
        }
    });
    if (invocations != null && invocations.length > 0) {
        for (FunctionInvocation invocation : invocations) {
            if (invocation.getFunctionName().equalsIgnoreCase("if")) {
                // build new expression
                // if (testExp, trueExp, falseExp)
                // 
                Expression testExp = invocation.getArguments()[0];
                Expression trueExp = invocation.getArguments()[1];
                Expression falseExp = invocation.getArguments()[2];
                Expression testPassed = Expression.relational("!=", testExp, new Expression(0.0));
                Expression testFailed = Expression.relational("==", testExp, new Expression(0.0));
                Expression newExp = Expression.add(Expression.mult(testPassed, trueExp), Expression.mult(testFailed, falseExp));
                // substitute new expression replacing if()
                exp.substituteInPlace(invocation.getFunctionExpression(), newExp);
            }
        }
        System.out.println(invocations.toString());
    }
    // 
    // "if()" functions are goine ... but lets look for BNGL function invocations
    // 1) if they have no arguments, drop the "()".
    // 2) if they have arguments, have to substitute the arguments in the expression ... flatten it.
    // 
    invocations = exp.getFunctionInvocations(new FunctionFilter() {

        @Override
        public boolean accept(String functionName, FunctionType functionType) {
            return true;
        }
    });
    if (invocations != null && invocations.length > 0) {
        for (FunctionInvocation invocation : invocations) {
            if (invocation.getArguments().length == 0) {
                // 
                // no arguments, look for existing parameter by name (parameter name is function name).
                // 
                // look up "identifier()" as "identifier" to find a model parameter generated earlier when processing functions (or prior functions)
                // 
                SymbolTableEntry parameter = symbolTable.getEntry(invocation.getFunctionName());
                if (parameter != null) {
                    exp.substituteInPlace(invocation.getFunctionExpression(), new Expression(parameter, parameter.getNameScope()));
                } else {
                    // 
                    // didn't find a parameter, may be a built-in function with zero arguments built into VCell. (none exists right now).
                    // 
                    SymbolTableFunctionEntry vcellFunction = (SymbolTableFunctionEntry) symbolTable.getEntry(invocation.getFormalDefinition());
                    if (vcellFunction != null) {
                    // 
                    // nothing to do, vcell will parse and interpret this correctly
                    // 
                    } else {
                        throw new RuntimeException("function \"" + invocation.getFunctionExpression().infix() + "\" not found as a bngl function or as a vcell built-in function");
                    }
                }
            } else {
                // 
                // should be a build-in vcell function with arguments ... user defined functions with arguments not supported yet in bngl import.
                // 
                FunctionType builtinFunctionType = cbit.vcell.parser.ASTFuncNode.FunctionType.fromFunctionName(invocation.getFunctionName());
                if (builtinFunctionType == null) {
                    throw new RuntimeException("function \"" + invocation.getFunctionExpression().infix() + "\" not found as a built-in VCell function (and bngl functions with arguments are not yet supported");
                } else {
                    if (invocation.getArguments().length != builtinFunctionType.getArgTypes().length) {
                        throw new RuntimeException("built-in function \"" + invocation.getFunctionExpression().infix() + "\" expects " + builtinFunctionType.getArgTypes().length + " arguments");
                    }
                }
            }
            System.out.println(invocations.toString());
        }
    }
    exp.bindExpression(symbolTable);
    return exp;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression) ASTExpression(org.vcell.model.bngl.ASTExpression) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 2 with FunctionInvocation

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

the class FieldUtilities method addFieldFuncArgsAndExpToCollection.

public static void addFieldFuncArgsAndExpToCollection(Hashtable<FieldFunctionArguments, Vector<Expression>> fieldFuncArgsAndExpHash, Expression expression) {
    if (expression == null) {
        return;
    }
    FunctionFilter functionFilter = new FieldFunctionFilter();
    FunctionInvocation[] functionInvocations = expression.getFunctionInvocations(functionFilter);
    for (int i = 0; i < functionInvocations.length; i++) {
        Vector<Expression> expV = null;
        FieldFunctionArguments fieldFunctionArgs = new FieldFunctionArguments(functionInvocations[i]);
        if (fieldFuncArgsAndExpHash.contains(fieldFunctionArgs)) {
            expV = fieldFuncArgsAndExpHash.get(fieldFunctionArgs);
        } else {
            expV = new Vector<Expression>();
            fieldFuncArgsAndExpHash.put(fieldFunctionArgs, expV);
        }
        if (!expV.contains(expression)) {
            expV.add(expression);
        }
    }
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression)

Example 3 with FunctionInvocation

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

the class FieldUtilities method getFieldFunctionArguments.

public static FieldFunctionArguments[] getFieldFunctionArguments(Expression expression) {
    if (expression == null) {
        return null;
    }
    FunctionFilter functionFilter = new FieldFunctionFilter();
    FunctionInvocation[] functionInvocations = expression.getFunctionInvocations(functionFilter);
    ArrayList<FieldFunctionArguments> fieldFuncArgsList = new ArrayList<FieldFunctionArguments>();
    for (int i = 0; i < functionInvocations.length; i++) {
        FieldFunctionArguments fieldFunctionArgs = new FieldFunctionArguments(functionInvocations[i]);
        if (!fieldFuncArgsList.contains(fieldFunctionArgs)) {
            fieldFuncArgsList.add(fieldFunctionArgs);
        }
    }
    if (fieldFuncArgsList.size() == 0) {
        return null;
    } else {
        return fieldFuncArgsList.toArray(new FieldFunctionArguments[fieldFuncArgsList.size()]);
    }
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) ArrayList(java.util.ArrayList)

Example 4 with FunctionInvocation

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

the class SimulationSymbolTable method getFunctionVariableType.

/**
 * Insert the method's description here.
 * Creation date: (2/19/2004 11:17:15 AM)
 * @return cbit.vcell.simdata.VariableType
 * @param function cbit.vcell.math.Function
 * @param variableNames java.lang.String[]
 * @param variableTypes cbit.vcell.simdata.VariableType[]
 */
public static VariableType getFunctionVariableType(Function function, MathDescription mathDescription, String[] variableNames, VariableType[] variableTypes, boolean isSpatial) throws InconsistentDomainException {
    if (!isSpatial) {
        return VariableType.NONSPATIAL;
    }
    VariableType domainFuncType = null;
    // initial guess, restrict variable type to be consistent with domain.
    if (function.getDomain() != null) {
        String domainName = function.getDomain().getName();
        if (mathDescription != null) {
            SubDomain subdomain = mathDescription.getSubDomain(domainName);
            if (subdomain instanceof MembraneSubDomain) {
                domainFuncType = VariableType.MEMBRANE_REGION;
            } else {
                domainFuncType = VariableType.VOLUME_REGION;
            }
        }
    }
    Expression exp = function.getExpression();
    String[] symbols = exp.getSymbols();
    ArrayList<VariableType> varTypeList = new ArrayList<VariableType>();
    boolean bExplicitFunctionOfSpace = false;
    if (symbols != null) {
        for (int j = 0; j < symbols.length; j++) {
            if (symbols[j].equals(ReservedVariable.X.getName()) || symbols[j].equals(ReservedVariable.Y.getName()) || symbols[j].equals(ReservedVariable.Z.getName())) {
                bExplicitFunctionOfSpace = true;
                continue;
            }
            for (int k = 0; k < variableNames.length; k++) {
                if (symbols[j].equals(variableNames[k])) {
                    varTypeList.add(variableTypes[k]);
                    break;
                } else if (symbols[j].equals(variableNames[k] + InsideVariable.INSIDE_VARIABLE_SUFFIX) || symbols[j].equals(variableNames[k] + OutsideVariable.OUTSIDE_VARIABLE_SUFFIX)) {
                    if (variableTypes[k].equals(VariableType.VOLUME)) {
                        varTypeList.add(VariableType.MEMBRANE);
                    } else if (variableTypes[k].equals(VariableType.VOLUME_REGION)) {
                        varTypeList.add(VariableType.MEMBRANE_REGION);
                    }
                    break;
                }
            }
        }
    }
    // Size Functions
    Set<FunctionInvocation> sizeFunctionInvocationSet = SolverUtilities.getSizeFunctionInvocations(function.getExpression());
    for (FunctionInvocation fi : sizeFunctionInvocationSet) {
        String functionName = fi.getFunctionName();
        if (functionName.equals(MathFunctionDefinitions.Function_regionArea_current.getFunctionName())) {
            varTypeList.add(VariableType.MEMBRANE_REGION);
        } else if (functionName.equals(MathFunctionDefinitions.Function_regionVolume_current.getFunctionName())) {
            varTypeList.add(VariableType.VOLUME_REGION);
        }
    }
    // Membrane Normal Functions
    FunctionInvocation[] functionInvocations = function.getExpression().getFunctionInvocations(null);
    for (FunctionInvocation fi : functionInvocations) {
        String functionName = fi.getFunctionName();
        if (functionName.equals(MathFunctionDefinitions.Function_normalX.getFunctionName()) || functionName.equals(MathFunctionDefinitions.Function_normalY.getFunctionName())) {
            varTypeList.add(VariableType.MEMBRANE);
        }
    }
    FieldFunctionArguments[] fieldFuncArgs = FieldUtilities.getFieldFunctionArguments(function.getExpression());
    if (fieldFuncArgs != null && fieldFuncArgs.length > 0) {
        varTypeList.add(fieldFuncArgs[0].getVariableType());
    }
    VariableType funcType = domainFuncType;
    for (VariableType vt : varTypeList) {
        if (funcType == null) {
            funcType = vt;
        } else {
            // 
            if (vt.isExpansionOf(funcType)) {
                funcType = vt;
            } else if (vt.equals(VariableType.VOLUME)) {
                if (funcType.equals(VariableType.MEMBRANE_REGION)) {
                    funcType = VariableType.MEMBRANE;
                }
            } else if (vt.equals(VariableType.VOLUME_REGION)) {
            } else if (vt.equals(VariableType.MEMBRANE)) {
                if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                    throw new InconsistentDomainException("Function '" + function.getName() + "' defined on a volume subdomain '" + function.getDomain().getName() + "' references a variable or a function defined on a membrane subdomain");
                }
            } else if (vt.equals(VariableType.MEMBRANE_REGION)) {
                if (funcType.equals(VariableType.VOLUME)) {
                    if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                        throw new InconsistentDomainException("Function '" + function.getName() + "' defined on '" + function.getDomain().getName() + "' references a size function defined on a membrane");
                    }
                    funcType = VariableType.MEMBRANE;
                } else if (funcType.equals(VariableType.VOLUME_REGION)) {
                    if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                        throw new InconsistentDomainException("Function '" + function.getName() + "' defined on '" + function.getDomain().getName() + "' references a size function defined on a membrane");
                    }
                    funcType = VariableType.MEMBRANE_REGION;
                }
            } else if (vt.incompatibleWith(funcType)) {
                throw new InconsistentDomainException("Function domains conflict between variable domains '" + vt.getDefaultLabel() + "' and '" + funcType.getDefaultLabel() + " for function " + function.getName());
            }
        }
    }
    // 
    if (funcType != null && bExplicitFunctionOfSpace) {
        if (funcType.equals(VariableType.MEMBRANE_REGION)) {
            funcType = VariableType.MEMBRANE;
        } else if (funcType.equals(VariableType.VOLUME_REGION)) {
            funcType = VariableType.VOLUME;
        } else if (funcType.equals(VariableType.CONTOUR_REGION)) {
            funcType = VariableType.CONTOUR;
        }
    }
    if (funcType == null) {
        // no knowledge from expression, default variable type
        return VariableType.VOLUME;
    }
    return funcType;
}
Also used : MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) FunctionInvocation(cbit.vcell.parser.FunctionInvocation) VariableType(cbit.vcell.math.VariableType) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ArrayList(java.util.ArrayList) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Expression(cbit.vcell.parser.Expression) InconsistentDomainException(cbit.vcell.math.InconsistentDomainException)

Example 5 with FunctionInvocation

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

the class FieldUtilities method substituteFieldFuncNames.

public static void substituteFieldFuncNames(Hashtable<String, ExternalDataIdentifier> oldFieldFuncArgsNameNewID, Hashtable<FieldFunctionArguments, Vector<Expression>> fieldFuncArgsExpHash) throws MathException, ExpressionException {
    Enumeration<FieldFunctionArguments> keyEnum = fieldFuncArgsExpHash.keys();
    FunctionFilter functionFilter = new FieldFunctionFilter();
    while (keyEnum.hasMoreElements()) {
        Vector<Expression> value = fieldFuncArgsExpHash.get(keyEnum.nextElement());
        for (int i = 0; i < value.size(); i++) {
            Expression exp = value.elementAt(i);
            FunctionInvocation[] functionInvocations = exp.getFunctionInvocations(functionFilter);
            for (int j = 0; j < functionInvocations.length; j++) {
                Expression[] arguments = functionInvocations[j].getArguments();
                String oldFieldName = removeLiteralQuotes(arguments[0]);
                if (oldFieldFuncArgsNameNewID.containsKey(oldFieldName)) {
                    String varName = removeLiteralQuotes(arguments[1]);
                    Expression timeExp = arguments[2];
                    VariableType varType = VariableType.UNKNOWN;
                    if (arguments.length > 3) {
                        String vt = removeLiteralQuotes(arguments[3]);
                        varType = VariableType.getVariableTypeFromVariableTypeName(vt);
                    }
                    String newFieldName = oldFieldFuncArgsNameNewID.get(oldFieldName).getName();
                    FieldFunctionArguments newFieldFunctionArguments = new FieldFunctionArguments(newFieldName, varName, timeExp, varType);
                    Expression newFunctionExp = new Expression(newFieldFunctionArguments.infix());
                    exp.substituteInPlace(functionInvocations[j].getFunctionExpression(), newFunctionExp);
                }
            }
        }
    }
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) VariableType(cbit.vcell.math.VariableType) Expression(cbit.vcell.parser.Expression)

Aggregations

FunctionInvocation (cbit.vcell.parser.FunctionInvocation)12 Expression (cbit.vcell.parser.Expression)9 FunctionFilter (cbit.vcell.parser.Expression.FunctionFilter)7 FunctionType (cbit.vcell.parser.ASTFuncNode.FunctionType)5 ExpressionException (cbit.vcell.parser.ExpressionException)4 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)3 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)2 VariableType (cbit.vcell.math.VariableType)2 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)2 SymbolTableFunctionEntry (cbit.vcell.parser.SymbolTableFunctionEntry)2 ArrayList (java.util.ArrayList)2 GradientFunctionDefinition (cbit.vcell.math.GradientFunctionDefinition)1 InconsistentDomainException (cbit.vcell.math.InconsistentDomainException)1 MathException (cbit.vcell.math.MathException)1 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)1 SubDomain (cbit.vcell.math.SubDomain)1 MatrixException (cbit.vcell.matrix.MatrixException)1 ModelFunction (cbit.vcell.model.Model.ModelFunction)1 ModelException (cbit.vcell.model.ModelException)1 VCUnitEvaluator (cbit.vcell.parser.VCUnitEvaluator)1