use of cbit.vcell.parser.Expression.FunctionFilter 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.Expression.FunctionFilter 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);
}
}
}
use of cbit.vcell.parser.Expression.FunctionFilter 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()]);
}
}
use of cbit.vcell.parser.Expression.FunctionFilter 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);
}
}
}
}
}
use of cbit.vcell.parser.Expression.FunctionFilter 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;
}
Aggregations