Search in sources :

Example 31 with ExpressionBindingException

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

the class XmlReader method getSpatialProcesses.

public SpatialProcess[] getSpatialProcesses(SimulationContext simContext, Element spatialProcessesElement) throws XmlParseException {
    Iterator<Element> spatialProcessElementIterator = spatialProcessesElement.getChildren(XMLTags.SpatialProcessTag, vcNamespace).iterator();
    ArrayList<SpatialProcess> spatialProcessList = new ArrayList<SpatialProcess>();
    while (spatialProcessElementIterator.hasNext()) {
        Element spatialProcessElement = (Element) spatialProcessElementIterator.next();
        SpatialProcess spatialProcess = null;
        String name = unMangle(spatialProcessElement.getAttributeValue(XMLTags.NameAttrTag));
        String type = unMangle(spatialProcessElement.getAttributeValue(XMLTags.SpatialProcessTypeAttrTag));
        if (type.equals(XMLTags.SpatialProcessTypeAttrValue_PointKinematics)) {
            PointKinematics pointKinematics = new PointKinematics(name, simContext);
            String pointObjectName = spatialProcessElement.getAttributeValue(XMLTags.SpatialProcessPointObjectAttrTag);
            PointObject pointObject = (PointObject) simContext.getSpatialObject(pointObjectName);
            pointKinematics.setPointObject(pointObject);
            spatialProcess = pointKinematics;
        } else if (type.equals(XMLTags.SpatialProcessTypeAttrValue_PointLocation)) {
            PointLocation pointLocation = new PointLocation(name, simContext);
            String pointObjectName = spatialProcessElement.getAttributeValue(XMLTags.SpatialProcessPointObjectAttrTag);
            PointObject pointObject = (PointObject) simContext.getSpatialObject(pointObjectName);
            pointLocation.setPointObject(pointObject);
            spatialProcess = pointLocation;
        } else if (type.equals(XMLTags.SpatialProcessTypeAttrValue_SurfaceKinematics)) {
            SurfaceKinematics surfaceKinematics = new SurfaceKinematics(name, simContext);
            String surfaceRegionObjectName = spatialProcessElement.getAttributeValue(XMLTags.SpatialProcessSurfaceObjectAttrTag);
            SurfaceRegionObject surfaceRegionObject = (SurfaceRegionObject) simContext.getSpatialObject(surfaceRegionObjectName);
            surfaceKinematics.setSurfaceRegionObject(surfaceRegionObject);
            spatialProcess = surfaceKinematics;
        } else if (type.equals(XMLTags.SpatialProcessTypeAttrValue_VolumeKinematics)) {
            VolumeKinematics volumeKinematics = new VolumeKinematics(name, simContext);
            String volumeRegionObjectName = spatialProcessElement.getAttributeValue(XMLTags.SpatialProcessVolumeObjectAttrTag);
            VolumeRegionObject volumeRegionObject = (VolumeRegionObject) simContext.getSpatialObject(volumeRegionObjectName);
            volumeKinematics.setVolumeRegionObject(volumeRegionObject);
            spatialProcess = volumeKinematics;
        }
        // set parameters
        Iterator<Element> paramElementIter = spatialProcessElement.getChildren(XMLTags.ParameterTag, vcNamespace).iterator();
        ArrayList<LocalParameter> parameters = new ArrayList<LocalParameter>();
        while (paramElementIter.hasNext()) {
            Element paramElement = paramElementIter.next();
            // Get parameter attributes
            String paramName = paramElement.getAttributeValue(XMLTags.NameAttrTag);
            Expression exp = unMangleExpression(paramElement.getText());
            String roleStr = paramElement.getAttributeValue(XMLTags.ParamRoleAttrTag);
            SpatialProcessParameterType parameterType = SpatialProcessParameterType.fromRoleXmlName(roleStr);
            VCUnitDefinition unit = simContext.getModel().getUnitSystem().getInstance_TBD();
            String unitSymbol = paramElement.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
            if (unitSymbol != null) {
                unit = simContext.getModel().getUnitSystem().getInstance(unitSymbol);
            }
            parameters.add(spatialProcess.createNewParameter(paramName, parameterType, exp, unit));
        }
        try {
            spatialProcess.setParameters(parameters.toArray(new LocalParameter[0]));
        } catch (PropertyVetoException | ExpressionBindingException e) {
            e.printStackTrace();
            throw new XmlParseException("failed to read parameters in bioEvent " + name + ": " + e.getMessage(), e);
        }
        spatialProcessList.add(spatialProcess);
    }
    return spatialProcessList.toArray(new SpatialProcess[0]);
}
Also used : VolumeRegionObject(cbit.vcell.mapping.spatial.VolumeRegionObject) PointLocation(cbit.vcell.mapping.spatial.processes.PointLocation) SurfaceKinematics(cbit.vcell.mapping.spatial.processes.SurfaceKinematics) VolumeKinematics(cbit.vcell.mapping.spatial.processes.VolumeKinematics) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) PropertyVetoException(java.beans.PropertyVetoException) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) PointObject(cbit.vcell.mapping.spatial.PointObject) Expression(cbit.vcell.parser.Expression) SpatialProcess(cbit.vcell.mapping.spatial.processes.SpatialProcess) PointKinematics(cbit.vcell.mapping.spatial.processes.PointKinematics) SpatialProcessParameterType(cbit.vcell.mapping.spatial.processes.SpatialProcess.SpatialProcessParameterType) SurfaceRegionObject(cbit.vcell.mapping.spatial.SurfaceRegionObject)

Example 32 with ExpressionBindingException

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

the class ReactionRuleKineticsPropertiesPanel method setReversible.

private void setReversible(boolean bReversible) {
    reactionRule.setReversible(bReversible);
    if (!bReversible && reactionRule != null) {
        // we know for sure it must be MassAction
        LocalParameter lp = reactionRule.getKineticLaw().getLocalParameter(RbmKineticLawParameterType.MassActionReverseRate);
        try {
            lp.setExpression(new Expression(0.0d));
        } catch (ExpressionBindingException e) {
            e.printStackTrace();
        }
    }
    getReactionRulePropertiesTableModel().refreshData();
}
Also used : LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) Expression(cbit.vcell.parser.Expression) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException)

Example 33 with ExpressionBindingException

use of cbit.vcell.parser.ExpressionBindingException 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 34 with ExpressionBindingException

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

the class MathOverrides method verifyExpression.

/**
 * Insert the method's description here.
 * Creation date: (9/22/2005 3:24:36 PM)
 */
private void verifyExpression(Constant value, boolean checkScan) throws ExpressionBindingException {
    Expression exp = value.getExpression();
    String[] symbols = exp.getSymbols();
    MathDescription mathDescription = getSimulation().getMathDescription();
    exp.bindExpression(mathDescription);
    if (symbols != null) {
        for (int i = 0; i < symbols.length; i++) {
            // 
            // expression must be a function of another Simulation parameter
            // 
            Variable variable = mathDescription.getVariable(symbols[i]);
            if (!(variable != null && variable instanceof Constant)) {
                throw new ExpressionBindingException("identifier " + symbols[i] + " is not a constant. " + "Parameter overrides should only refer to constants.");
            }
            if (checkScan && isScan(symbols[i])) {
                throw new ExpressionBindingException("Parameter overrides cannot depend on another scanned parameter " + symbols[i]);
            }
            // 
            if (symbols[i].equals(value.getName())) {
                throw new ExpressionBindingException("Parameter overrides can not be recursive definition, can't use identifier " + value.getName() + " in expression for " + value.getName());
            }
        }
    }
}
Also used : Variable(cbit.vcell.math.Variable) Expression(cbit.vcell.parser.Expression) MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException)

Example 35 with ExpressionBindingException

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

the class ModelOptimizationMapping method computeOptimizationSpec.

/**
 * Insert the method's description here.
 * Creation date: (8/22/2005 9:26:52 AM)
 * @return cbit.vcell.opt.OptimizationSpec
 * @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
 */
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
    if (getModelOptimizationSpec().getReferenceData() == null) {
        System.out.println("no referenced data defined");
        return null;
    }
    OptimizationSpec optSpec = new OptimizationSpec();
    optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
    parameterMappings = null;
    // 
    // get original MathDescription (later to be substituted for local/global parameters).
    // 
    SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathDescription origMathDesc = null;
    mathSymbolMapping = null;
    try {
        origMathDesc = mathMapping.getMathDescription();
        mathSymbolMapping = mathMapping.getMathSymbolMapping();
    } catch (MatrixException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ModelException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    // create objective function (mathDesc and data)
    // 
    ReferenceData referenceData = getRemappedReferenceData(mathMapping);
    if (referenceData == null) {
        throw new RuntimeException("no referenced data defined");
    }
    // 
    // get parameter mappings
    // 
    ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
    Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
    Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
    for (int i = 0; i < parameterMappingSpecs.length; i++) {
        cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
        String mathSymbol = null;
        Variable mathVariable = null;
        if (mathSymbolMapping != null) {
            Variable variable = mathSymbolMapping.getVariable(modelParameter);
            if (variable != null) {
                mathSymbol = variable.getName();
            }
            if (mathSymbol != null) {
                mathVariable = origMathDesc.getVariable(mathSymbol);
            }
        }
        if (mathVariable != null) {
            if (parameterMappingSpecs[i].isSelected()) {
                if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getLow() < 0) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
                }
                if (parameterMappingSpecs[i].getHigh() <= 0) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
                }
            }
            double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
            double high = parameterMappingSpecs[i].getHigh();
            double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
            double current = parameterMappingSpecs[i].getCurrent();
            low = Math.min(low, current);
            high = Math.max(high, current);
            Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
            ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
            // 
            if (mathVariable instanceof Constant) {
                Constant origConstant = (Constant) mathVariable;
                for (int j = 0; j < allVars.length; j++) {
                    if (allVars[j].equals(origConstant)) {
                        if (parameterMappingSpecs[i].isSelected()) {
                            allVars[j] = new ParameterVariable(origConstant.getName());
                        } else {
                            allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
                        }
                        break;
                    }
                }
            }
            // 
            if (parameterMappingSpecs[i].isSelected()) {
                parameterMappingList.add(parameterMapping);
            }
        }
    }
    parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
    try {
        origMathDesc.setAllVariables(allVars);
    } catch (ExpressionBindingException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    for (int i = 0; i < parameterMappings.length; i++) {
        optSpec.addParameter(parameterMappings[i].getOptParameter());
    }
    Vector<Issue> issueList = new Vector<Issue>();
    IssueContext issueContext = new IssueContext();
    optSpec.gatherIssues(issueContext, issueList);
    for (int i = 0; i < issueList.size(); i++) {
        Issue issue = issueList.elementAt(i);
        if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
            throw new RuntimeException(issue.getMessage());
        }
    }
    // 
    // 
    // 
    optimizationSpec = optSpec;
    return mathSymbolMapping;
}
Also used : ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) Issue(org.vcell.util.Issue) MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) ParameterVariable(cbit.vcell.math.ParameterVariable) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MatrixException(cbit.vcell.matrix.MatrixException) IssueContext(org.vcell.util.IssueContext) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) Vector(java.util.Vector) ModelException(cbit.vcell.model.ModelException) SimulationContext(cbit.vcell.mapping.SimulationContext) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MathMapping(cbit.vcell.mapping.MathMapping) Parameter(cbit.vcell.opt.Parameter)

Aggregations

ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)37 Expression (cbit.vcell.parser.Expression)26 ExpressionException (cbit.vcell.parser.ExpressionException)16 PropertyVetoException (java.beans.PropertyVetoException)15 Vector (java.util.Vector)8 MathException (cbit.vcell.math.MathException)7 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)7 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)7 ArrayList (java.util.ArrayList)7 VCUnitException (cbit.vcell.units.VCUnitException)6 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)5 ModelParameter (cbit.vcell.model.Model.ModelParameter)5 SpeciesContext (cbit.vcell.model.SpeciesContext)5 Issue (org.vcell.util.Issue)5 ModelException (cbit.vcell.model.ModelException)4 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)4 Structure (cbit.vcell.model.Structure)4 Element (org.jdom.Element)4 ScopedExpression (cbit.gui.ScopedExpression)3 SubVolume (cbit.vcell.geometry.SubVolume)3