Search in sources :

Example 6 with FunctionInvocation

use of cbit.vcell.parser.FunctionInvocation 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;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) Issue(org.vcell.util.Issue) ModelFunction(cbit.vcell.model.Model.ModelFunction) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) VCUnitException(cbit.vcell.units.VCUnitException) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression)

Example 7 with FunctionInvocation

use of cbit.vcell.parser.FunctionInvocation 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;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) FunctionFilter(cbit.vcell.parser.Expression.FunctionFilter) Expression(cbit.vcell.parser.Expression) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 8 with FunctionInvocation

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

the class FiniteVolumeFileWriter method subsituteExpression.

/**
 * Insert the method's description here.
 * Creation date: (5/9/2005 2:52:48 PM)
 * @throws ExpressionException
 */
private Expression subsituteExpression(Expression exp, SymbolTable symbolTable, VariableDomain variableDomain) throws ExpressionException {
    Expression newExp = MathUtilities.substituteFunctions(exp, symbolTable).flatten();
    FieldFunctionArguments[] fieldFunctionArguments = FieldUtilities.getFieldFunctionArguments(newExp);
    if (fieldFunctionArguments != null && fieldFunctionArguments.length > 0) {
        if (uniqueFieldDataNSet != null && uniqueFieldDataNSet.size() > 0) {
            for (FieldDataNumerics fdn : uniqueFieldDataNSet) {
                newExp.substituteInPlace(new Expression(fdn.getFieldFunction()), new Expression(fdn.getNumericsSubsitute()));
            }
        } else {
            throw new RuntimeException("Didn't find field functions in simulation when preprocessing, but expression [" + exp.infix() + "] has field function " + newExp.infix() + " in it");
        }
    }
    if (bChomboSolver) {
        Set<FunctionInvocation> sizeFunctions = SolverUtilities.getSizeFunctionInvocations(newExp);
        if (sizeFunctions.size() > 0) {
            throw new ExpressionException("Size functions are not supported by " + simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel());
        }
    } else {
        newExp = SolverUtilities.substituteSizeAndNormalFunctions(newExp, variableDomain);
    }
    return newExp;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) Expression(cbit.vcell.parser.Expression) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 9 with FunctionInvocation

use of cbit.vcell.parser.FunctionInvocation 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();
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) HashMap(java.util.HashMap) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) LineBorder(javax.swing.border.LineBorder) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 10 with FunctionInvocation

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

the class DataSetControllerImpl method hasGradient.

private boolean hasGradient(Expression exp) {
    boolean hasGradient = false;
    FunctionInvocation[] functionInvocations = exp.getFunctionInvocations(null);
    for (FunctionInvocation fi : functionInvocations) {
        if (fi.getSymbolTableFunctionEntry() instanceof GradientFunctionDefinition) {
            hasGradient = true;
        }
    }
    return hasGradient;
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) GradientFunctionDefinition(cbit.vcell.math.GradientFunctionDefinition)

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