use of cbit.vcell.parser.ASTFuncNode.FunctionType 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.ASTFuncNode.FunctionType in project vcell by virtualcell.
the class TextFieldAutoCompletion method initialize.
private void initialize() {
osi = getOperatingSystemInfo();
getDocument().addDocumentListener(eventHandler);
addKeyListener(eventHandler);
addMouseListener(eventHandler);
autoCompWordList = new HashSet<String>();
listModel = new DefaultListModel<String>();
autoCompJList = new JList<String>(listModel);
autoCompJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
autoCompJList.addMouseListener(eventHandler);
autoCompJPopupMenu = new JPopupMenu();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(autoCompJList);
autoCompJPopupMenu.add(scrollPane);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JPanel panel1 = new JPanel();
JLabel infoLabel1 = new JLabel("\u2191\u2193 to move; 'Esc' to cancel; 'Enter' to accept;");
Font font = getFont();
infoLabel1.setFont(font.deriveFont(AffineTransform.getScaleInstance(0.9, 0.9)));
panel1.add(infoLabel1);
panel.add(panel1);
autoCompJPopupMenu.add(panel);
setupActions();
// functions
for (FunctionType ft : FunctionType.values()) {
functList.add(ft.getName());
}
Collections.sort(functList);
}
use of cbit.vcell.parser.ASTFuncNode.FunctionType 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;
}
use of cbit.vcell.parser.ASTFuncNode.FunctionType 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;
}
use of cbit.vcell.parser.ASTFuncNode.FunctionType in project vcell by virtualcell.
the class TableCellEditorAutoCompletion method stopCellEditing.
@Override
public boolean stopCellEditing() {
if (thisTable.getCellEditor() == null) {
return true;
}
if (textFieldAutoCompletion.getSelectedIndex() >= 0) {
return false;
}
final int editingRow = thisTable.getEditingRow();
final int editingColumn = thisTable.getEditingColumn();
textFieldAutoCompletion.stopEditing();
boolean bExpressionValid = true;
if (thisTable.getColumnClass(editingColumn).equals(ScopedExpression.class)) {
if (textFieldAutoCompletion.getSymbolTable() != null) {
ScopedExpression scopedExpression = (ScopedExpression) thisTable.getValueAt(editingRow, editingColumn);
String text = textFieldAutoCompletion.getText();
if (text.trim().length() > 0) {
try {
Expression exp = new Expression(text);
exp.validateUnscopedSymbols();
if (scopedExpression == null || scopedExpression.isValidateFunctionBinding()) {
FunctionInvocation[] functionInvocations = exp.getFunctionInvocations(null);
for (FunctionInvocation functionInvocation : functionInvocations) {
String formalDefinition = functionInvocation.getFormalDefinition();
if (functionInvocation.getFunctionId() == FunctionType.USERDEFINED) {
SymbolTableFunctionEntry stfe = (SymbolTableFunctionEntry) textFieldAutoCompletion.getSymbolTable().getEntry(formalDefinition);
if (stfe == null) {
//
// check for wrong number of arguments
//
Map<String, SymbolTableEntry> entries = new HashMap<String, SymbolTableEntry>();
textFieldAutoCompletion.getSymbolTable().getEntries(entries);
System.out.println("available symbols");
for (String symbol : entries.keySet()) {
System.out.print(symbol + ",");
}
System.out.println("");
throw new ExpressionBindingException("unknown function " + formalDefinition, formalDefinition);
}
} else {
// built in function, check arguments
FunctionType functionType = functionInvocation.getFunctionId();
String formalDefinitionBuiltin = ASTFuncNode.getFormalDefinition(functionType.getName(), functionType.getArgTypes());
if (!formalDefinition.equals(formalDefinitionBuiltin)) {
throw new ExpressionBindingException("expecting " + formalDefinitionBuiltin, formalDefinition);
}
}
}
}
if (scopedExpression == null || scopedExpression.isValidateIdentifierBinding()) {
exp.bindExpression(textFieldAutoCompletion.getSymbolTable());
}
} catch (ExpressionBindingException ex) {
ex.printStackTrace(System.out);
DialogUtils.showErrorDialog(thisTable.getParent(), ex.getMessage() + "\n\nUse 'Ctrl-Space' to see a list of available names in your model or 'Esc' to revert to the original expression.");
bExpressionValid = false;
} catch (ExpressionException ex) {
ex.printStackTrace(System.out);
DialogUtils.showErrorDialog(thisTable.getParent(), ex.getMessage() + "\n\nUse 'Esc' to revert to the original expression.");
bExpressionValid = false;
}
}
}
}
if (!bExpressionValid) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
thisTable.requestFocus();
thisTable.setRowSelectionInterval(editingRow, editingRow);
((JComponent) getComponent()).setBorder(new LineBorder(Color.red));
textFieldAutoCompletion.requestFocus();
}
});
return false;
}
return super.stopCellEditing();
}
Aggregations