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]);
}
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();
}
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();
}
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());
}
}
}
}
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;
}
Aggregations