Search in sources :

Example 1 with SymbolTableFunctionEntry

use of cbit.vcell.parser.SymbolTableFunctionEntry 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 SymbolTableFunctionEntry

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

the class SpeciesContextSpec method getEntry.

/**
 * getEntry method comment.
 */
public SymbolTableEntry getEntry(String identifierString) {
    SymbolTableEntry ste = getLocalEntry(identifierString);
    if (ste != null) {
        return ste;
    }
    ste = getNameScope().getExternalEntry(identifierString, this);
    if (ste instanceof SymbolTableFunctionEntry) {
        return ste;
    }
    if (ste != null) {
        if (ste instanceof SymbolTableFunctionEntry) {
            return ste;
        } else {
            return addProxyParameter(ste);
        }
    }
    return null;
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 3 with SymbolTableFunctionEntry

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

the class MathUtilities method substituteModelParameters.

/**
 * This method was created in VisualAge.
 * @return cbit.vcell.parser.Expression
 * @param exp cbit.vcell.parser.Expression
 * @exception java.lang.Exception The exception description.
 */
public static Expression substituteModelParameters(Expression exp, SymbolTable symbolTable) throws ExpressionException {
    Expression exp2 = new Expression(exp);
    // 
    // do until no more functions to substitute
    // 
    int count = 0;
    boolean bSubstituted = true;
    while (bSubstituted) {
        bSubstituted = false;
        if (count++ > 30) {
            throw new ExpressionBindingException("infinite loop in eliminating function nesting");
        }
        String[] symbols = exp2.getSymbols();
        if (symbols != null) {
            for (int i = 0; i < symbols.length; i++) {
                SymbolTableEntry ste = exp2.getSymbolBinding(symbols[i]);
                if (ste != null && !(ste instanceof SymbolTableFunctionEntry)) {
                    Expression steExp = ste.getExpression();
                    if (steExp != null) {
                        exp2.substituteInPlace(new Expression(ste.getName()), steExp);
                        bSubstituted = true;
                    }
                }
            }
        }
    }
    exp2.bindExpression(symbolTable);
    return exp2;
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 4 with SymbolTableFunctionEntry

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

the class ReservedMathSymbolEntries method getEntry.

public static SymbolTableEntry getEntry(String symbolName, boolean bIncludePostProcessing) {
    SymbolTableEntry ste = getSymbolTableEntries().get(symbolName);
    if (ste != null) {
        return ste;
    }
    SymbolTableFunctionEntry steFunction = getMathSymbolTableFunctionEntries().get(symbolName);
    if (steFunction != null) {
        return steFunction;
    }
    if (bIncludePostProcessing) {
        steFunction = getPostProcessingSymbolTableFunctionEntries().get(symbolName);
        if (steFunction != null) {
            return steFunction;
        }
    }
    return null;
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 5 with SymbolTableFunctionEntry

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

the class MathDescription method getEntry.

/**
 * This method was created by a SmartGuide.
 * @return cbit.vcell.parser.SymbolTableEntry
 * @param id java.lang.String
 * @param qualifier java.lang.String
 * @exception java.lang.Exception The exception description.
 */
public SymbolTableEntry getEntry(String id) {
    SymbolTableEntry entry = null;
    entry = ReservedMathSymbolEntries.getEntry(id, false);
    if (entry != null) {
        if (entry instanceof SymbolTableFunctionEntry) {
            if (entry.equals(MathFunctionDefinitions.Function_regionArea_current) || entry.equals(MathFunctionDefinitions.Function_regionArea_indexed) || entry.equals(MathFunctionDefinitions.Function_regionVolume_current) || entry.equals(MathFunctionDefinitions.Function_regionVolume_indexed)) {
                bRegionSizeFunctionsUsed = true;
            }
        }
        return entry;
    }
    entry = getVariable(id);
    if (entry != null) {
        return entry;
    }
    return null;
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Aggregations

SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)9 SymbolTableFunctionEntry (cbit.vcell.parser.SymbolTableFunctionEntry)9 Expression (cbit.vcell.parser.Expression)3 FunctionType (cbit.vcell.parser.ASTFuncNode.FunctionType)2 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)2 FunctionInvocation (cbit.vcell.parser.FunctionInvocation)2 HashMap (java.util.HashMap)2 FunctionFilter (cbit.vcell.parser.Expression.FunctionFilter)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 ArrayList (java.util.ArrayList)1 LineBorder (javax.swing.border.LineBorder)1 ASTExpression (org.vcell.model.bngl.ASTExpression)1