use of cbit.vcell.parser.ExpressionBindingException in project vcell by virtualcell.
the class XmlReader method getBioEvents.
public BioEvent[] getBioEvents(SimulationContext simContext, Element bioEventsElement) throws XmlParseException {
Iterator<Element> bioEventsIterator = bioEventsElement.getChildren(XMLTags.BioEventTag, vcNamespace).iterator();
Vector<BioEvent> bioEventsVector = new Vector<BioEvent>();
while (bioEventsIterator.hasNext()) {
Element bEventElement = (Element) bioEventsIterator.next();
BioEvent newBioEvent = null;
String name = unMangle(bEventElement.getAttributeValue(XMLTags.NameAttrTag));
Element triggerElement = bEventElement.getChild(XMLTags.TriggerTag, vcNamespace);
if (triggerElement != null && triggerElement.getText().length() > 0) {
//
// read legacy VCell 5.3 style trigger and delay elements
//
// <Trigger>(t>3.0)</Trigger>
// <Delay UseValuesFromTriggerTime="true">3.0</Delay> [optional]
//
Expression triggerExpression = unMangleExpression(triggerElement.getText());
// read <Delay>
Expression delayDurationExpression = null;
boolean useValuesFromTriggerTime = true;
Element delayElement = bEventElement.getChild(XMLTags.DelayTag, vcNamespace);
if (delayElement != null) {
useValuesFromTriggerTime = Boolean.valueOf(delayElement.getAttributeValue(XMLTags.UseValuesFromTriggerTimeAttrTag)).booleanValue();
delayDurationExpression = unMangleExpression((delayElement.getText()));
}
newBioEvent = new BioEvent(name, TriggerType.GeneralTrigger, useValuesFromTriggerTime, simContext);
try {
newBioEvent.setParameterValue(BioEventParameterType.GeneralTriggerFunction, triggerExpression);
if (delayDurationExpression != null) {
newBioEvent.setParameterValue(BioEventParameterType.TriggerDelay, delayDurationExpression);
}
} catch (ExpressionBindingException | PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException("failed to read trigger or delay expressions in bioEvent " + name + ": " + e.getMessage(), e);
}
} else if (triggerElement != null && triggerElement.getText().length() == 0) {
//
// read legacy first-pass VCell 5.4 style trigger and delay elements
//
// <Trigger>
// <TriggerParameters triggerClass="TriggerGeneral">
// (t > 500.0)
// </TriggerParameters>
// </Trigger>
// <Delay UseValuesFromTriggerTime="true">3.0</Delay> [optional]
//
final String TriggerParametersTag = "TriggerParameters";
final String TriggerClassAttrTag = "triggerClass";
final String TriggerClassAttrValue_TriggerGeneral = "TriggerGeneral";
Element triggerParametersElement = triggerElement.getChild(TriggerParametersTag, vcNamespace);
Expression triggerExpression = null;
String triggerClass = triggerParametersElement.getAttributeValue(TriggerClassAttrTag);
if (triggerClass.equals(TriggerClassAttrValue_TriggerGeneral)) {
triggerExpression = unMangleExpression(triggerParametersElement.getText());
} else {
// not general trigger (just make it never happen, user will have to edit "t > -1")
triggerExpression = Expression.relational(">", new Expression(simContext.getModel().getTIME(), simContext.getModel().getNameScope()), new Expression(-1.0));
}
// read <Delay>
Expression delayDurationExpression = null;
boolean useValuesFromTriggerTime = true;
Element delayElement = bEventElement.getChild(XMLTags.DelayTag, vcNamespace);
if (delayElement != null) {
useValuesFromTriggerTime = Boolean.valueOf(delayElement.getAttributeValue(XMLTags.UseValuesFromTriggerTimeAttrTag)).booleanValue();
delayDurationExpression = unMangleExpression((delayElement.getText()));
}
newBioEvent = new BioEvent(name, TriggerType.GeneralTrigger, useValuesFromTriggerTime, simContext);
try {
newBioEvent.setParameterValue(BioEventParameterType.GeneralTriggerFunction, triggerExpression);
if (delayDurationExpression != null) {
newBioEvent.setParameterValue(BioEventParameterType.TriggerDelay, delayDurationExpression);
}
} catch (ExpressionBindingException | PropertyVetoException e) {
e.printStackTrace();
throw new XmlParseException("failed to read trigger or delay expressions in bioEvent " + name + ": " + e.getMessage(), e);
}
} else {
//
// VCell 5.4 style bioevent parameters
//
//
TriggerType triggerType = TriggerType.fromXmlName(bEventElement.getAttributeValue(XMLTags.BioEventTriggerTypeAttrTag));
boolean bUseValuesFromTriggerTime = Boolean.parseBoolean(bEventElement.getAttributeValue(XMLTags.UseValuesFromTriggerTimeAttrTag));
newBioEvent = new BioEvent(name, triggerType, bUseValuesFromTriggerTime, simContext);
Iterator<Element> paramElementIter = bEventElement.getChildren(XMLTags.ParameterTag, vcNamespace).iterator();
ArrayList<LocalParameter> parameters = new ArrayList<LocalParameter>();
boolean bHasGeneralTriggerParam = false;
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);
BioEventParameterType parameterType = BioEventParameterType.fromRoleXmlName(roleStr);
if (parameterType == BioEventParameterType.GeneralTriggerFunction) {
bHasGeneralTriggerParam = true;
}
VCUnitDefinition unit = simContext.getModel().getUnitSystem().getInstance_TBD();
String unitSymbol = paramElement.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
if (unitSymbol != null) {
unit = simContext.getModel().getUnitSystem().getInstance(unitSymbol);
}
parameters.add(newBioEvent.createNewParameter(paramName, parameterType, exp, unit));
}
if (!bHasGeneralTriggerParam) {
parameters.add(newBioEvent.createNewParameter(BioEventParameterType.GeneralTriggerFunction.getDefaultName(), BioEventParameterType.GeneralTriggerFunction, // computed as needed
null, simContext.getModel().getUnitSystem().getInstance_DIMENSIONLESS()));
}
try {
newBioEvent.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);
}
}
ArrayList<BioEvent.EventAssignment> eventAssignmentList = new ArrayList<BioEvent.EventAssignment>();
Iterator<Element> iter = bEventElement.getChildren(XMLTags.EventAssignmentTag, vcNamespace).iterator();
while (iter.hasNext()) {
Element eventAssignmentElement = iter.next();
try {
String varname = eventAssignmentElement.getAttributeValue(XMLTags.EventAssignmentVariableAttrTag);
Expression assignExp = unMangleExpression(eventAssignmentElement.getText());
SymbolTableEntry target = simContext.getEntry(varname);
BioEvent.EventAssignment eventAssignment = newBioEvent.new EventAssignment(target, assignExp);
eventAssignmentList.add(eventAssignment);
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e);
}
}
try {
newBioEvent.setEventAssignmentsList(eventAssignmentList);
} catch (PropertyVetoException e1) {
e1.printStackTrace(System.out);
throw new XmlParseException(e1);
}
try {
newBioEvent.bind();
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e);
}
bioEventsVector.add(newBioEvent);
}
return ((BioEvent[]) BeanUtils.getArray(bioEventsVector, BioEvent.class));
}
use of cbit.vcell.parser.ExpressionBindingException in project vcell by virtualcell.
the class SpeciesContextSpec method initializeForSpatial.
public void initializeForSpatial() {
if (getDiffusionParameter() != null && getDiffusionParameter().getExpression() != null && getDiffusionParameter().getExpression().isZero()) {
Expression e = null;
ModelUnitSystem modelUnitSystem = getSimulationContext().getModel().getUnitSystem();
VCUnitDefinition micronsqpersecond = modelUnitSystem.getInstance("um2.s-1");
if (speciesContext.getStructure() instanceof Feature) {
RationalNumber rn = RationalNumber.getApproximateFraction(micronsqpersecond.convertTo(10, getDiffusionParameter().getUnitDefinition()));
e = new Expression(rn.doubleValue());
} else if (speciesContext.getStructure() instanceof Membrane) {
RationalNumber rn = RationalNumber.getApproximateFraction(micronsqpersecond.convertTo(0.1, getDiffusionParameter().getUnitDefinition()));
e = new Expression(rn.doubleValue());
} else {
RationalNumber rn = RationalNumber.getApproximateFraction(micronsqpersecond.convertTo(1.0, getDiffusionParameter().getUnitDefinition()));
e = new Expression(rn.doubleValue());
}
try {
getDiffusionParameter().setExpression(e);
} catch (ExpressionBindingException e1) {
e1.printStackTrace();
throw new RuntimeException("Error while initializing diffusion rate, " + e1.getMessage());
}
}
}
use of cbit.vcell.parser.ExpressionBindingException in project vcell by virtualcell.
the class MathMapping_4_8 method substituteGlobalParameters.
private Expression substituteGlobalParameters(Expression exp) throws ExpressionException {
Expression exp2 = new Expression(exp);
//
// do until no more globals to substitute
//
int count = 0;
ModelParameter[] modelParams = simContext.getModel().getModelParameters();
while (true) {
if (count++ > 30) {
throw new ExpressionBindingException("infinite loop in eliminating function nesting");
}
//
// get all symbols (identifiers), make list of globals used
//
String[] symbols = exp2.getSymbols();
Vector<ModelParameter> globalsVector = new Vector<ModelParameter>();
if (symbols != null) {
for (int i = 0; i < symbols.length; i++) {
for (int j = 0; j < modelParams.length; j++) {
if (symbols[i].equals(modelParams[j].getName())) {
globalsVector.addElement(modelParams[j]);
}
}
}
}
//
if (globalsVector.size() == 0) {
break;
}
//
for (int i = 0; i < globalsVector.size(); i++) {
ModelParameter mp = globalsVector.elementAt(i);
Expression mpExp = new Expression(mp.getName() + ";");
exp2.substituteInPlace(mpExp, new Expression(mp.getExpression()));
}
}
exp2.bindExpression(simContext.getModel());
return exp2;
}
use of cbit.vcell.parser.ExpressionBindingException in project vcell by virtualcell.
the class AbstractMathMapping method substituteGlobalParameters.
private Expression substituteGlobalParameters(Expression exp) throws ExpressionException {
Expression exp2 = new Expression(exp);
//
// do until no more globals to substitute
//
int count = 0;
ModelParameter[] modelParams = simContext.getModel().getModelParameters();
while (true) {
if (count++ > 30) {
throw new ExpressionBindingException("infinite loop in eliminating function nesting");
}
//
// get all symbols (identifiers), make list of globals used
//
String[] symbols = exp2.getSymbols();
Vector<ModelParameter> globalsVector = new Vector<ModelParameter>();
if (symbols != null) {
for (int i = 0; i < symbols.length; i++) {
for (int j = 0; j < modelParams.length; j++) {
if (symbols[i].equals(modelParams[j].getName())) {
globalsVector.addElement(modelParams[j]);
}
}
}
}
//
if (globalsVector.size() == 0) {
break;
}
//
for (int i = 0; i < globalsVector.size(); i++) {
ModelParameter mp = globalsVector.elementAt(i);
Expression mpExp = new Expression(mp.getName());
exp2.substituteInPlace(mpExp, mp.getExpression());
}
}
// exp2.bindExpression(simContext.getModel());
return exp2;
}
use of cbit.vcell.parser.ExpressionBindingException in project vcell by virtualcell.
the class BioEvent method refreshDependencies.
public void refreshDependencies() {
removePropertyChangeListener(this);
addPropertyChangeListener(this);
parameterContext.removePropertyChangeListener(this);
parameterContext.addPropertyChangeListener(this);
parameterContext.refreshDependencies();
for (EventAssignment eventAssignment : eventAssignmentList) {
try {
eventAssignment.rebind();
} catch (ExpressionBindingException e) {
e.printStackTrace();
throw new RuntimeException("failed to bind expression for assignment in event " + getName() + ": " + e.getMessage(), e);
}
}
}
Aggregations