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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations