use of cbit.vcell.parser.Expression in project vcell by virtualcell.
the class XmlReader method getMembraneRegionEquation.
/**
* This method returns a MembraneRegionEquation from a XML Element.
* Creation date: (5/17/2001 3:52:40 PM)
* @return cbit.vcell.math.MembraneRegionEquation
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private MembraneRegionEquation getMembraneRegionEquation(Element param, MathDescription mathDesc) throws XmlParseException {
// get attributes
String varname = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
// find reference in the dictionnary
// try a MembraneRegionVariable
MembraneRegionVariable varref = (MembraneRegionVariable) mathDesc.getVariable(varname);
if (varref == null) {
throw new XmlParseException("The reference to the MembraneRegion variable " + varname + " could not be resolved!");
}
// get Initial condition
String temp = param.getChildText(XMLTags.InitialTag, vcNamespace);
Expression exp;
exp = unMangleExpression(temp);
// ** Create the Equation **
MembraneRegionEquation memRegEq = new MembraneRegionEquation(varref, exp);
// set the Uniform Rate
temp = param.getChildText(XMLTags.UniformRateTag, vcNamespace);
exp = unMangleExpression(temp);
memRegEq.setUniformRateExpression(exp);
// set the Membrane Rate
temp = param.getChildText(XMLTags.MembraneRateTag, vcNamespace);
exp = unMangleExpression(temp);
memRegEq.setMembraneRateExpression(exp);
return memRegEq;
}
use of cbit.vcell.parser.Expression in project vcell by virtualcell.
the class XmlReader method readParameters.
private void readParameters(List<Element> parameterElements, ParameterContext parameterContext, HashMap<String, ParameterRoleEnum> roleHash, ParameterRoleEnum userDefinedRole, HashSet<String> xmlRolesTagsToIgnore, Model model) throws XmlParseException {
String contextName = parameterContext.getNameScope().getName();
try {
//
// prepopulate varHash with reserved symbols
//
VariableHash varHash = new VariableHash();
addResevedSymbols(varHash, model);
//
for (Element xmlParam : parameterElements) {
String parsedParamName = unMangle(xmlParam.getAttributeValue(XMLTags.NameAttrTag));
String parsedRoleString = xmlParam.getAttributeValue(XMLTags.ParamRoleAttrTag);
String parsedExpressionString = xmlParam.getText();
//
if (xmlRolesTagsToIgnore.contains(parsedRoleString)) {
varHash.removeVariable(parsedParamName);
continue;
}
Expression paramExp = null;
if (parsedExpressionString.trim().length() > 0) {
paramExp = unMangleExpression(parsedExpressionString);
}
if (varHash.getVariable(parsedParamName) == null) {
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
} else {
if (model.getReservedSymbolByName(parsedParamName) != null) {
varHash.removeVariable(parsedParamName);
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
}
}
//
// get the parameter for this xml role string
//
ParameterRoleEnum paramRole = roleHash.get(parsedRoleString);
if (paramRole == null) {
throw new XmlParseException("parameter '" + parsedParamName + "' has unexpected role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (paramRole != userDefinedRole) {
LocalParameter paramWithSameRole = parameterContext.getLocalParameterFromRole(paramRole);
if (paramWithSameRole == null) {
throw new XmlParseException("can't find parameter with role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (!paramWithSameRole.getName().equals(parsedParamName)) {
//
// first rename other parameters with same name
//
LocalParameter paramWithSameNameButDifferentRole = parameterContext.getLocalParameterFromName(parsedParamName);
if (paramWithSameNameButDifferentRole != null) {
//
// find available name
//
int n = 0;
String newName = parsedParamName + "_" + n++;
while (parameterContext.getEntry(newName) != null) {
newName = parsedParamName + "_" + n++;
}
parameterContext.renameLocalParameter(parsedParamName, newName);
}
//
// then rename parameter with correct role
//
parameterContext.renameLocalParameter(paramWithSameRole.getName(), parsedParamName);
}
}
}
//
// create unresolved parameters for all unresolved symbols
//
String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
while (unresolvedSymbol != null) {
try {
Domain domain = null;
// will turn into an UnresolvedParameter.
varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), domain));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e.getMessage());
}
parameterContext.addUnresolvedParameter(unresolvedSymbol);
unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
}
//
// in topological order, add parameters to model (getting units also).
// note that all pre-defined parameters already have the correct names
// here we set expressions on pre-defined parameters and add user-defined parameters
//
Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
ModelUnitSystem modelUnitSystem = model.getUnitSystem();
for (int i = sortedVariables.length - 1; i >= 0; i--) {
if (sortedVariables[i] instanceof Function) {
Function paramFunction = (Function) sortedVariables[i];
Element xmlParam = null;
for (int j = 0; j < parameterElements.size(); j++) {
Element tempParam = (Element) parameterElements.get(j);
if (paramFunction.getName().equals(unMangle(tempParam.getAttributeValue(XMLTags.NameAttrTag)))) {
xmlParam = tempParam;
break;
}
}
if (xmlParam == null) {
// must have been an unresolved parameter
continue;
}
String symbol = xmlParam.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
VCUnitDefinition unit = null;
if (symbol != null) {
unit = modelUnitSystem.getInstance(symbol);
}
LocalParameter tempParam = parameterContext.getLocalParameterFromName(paramFunction.getName());
if (tempParam == null) {
tempParam = parameterContext.addLocalParameter(paramFunction.getName(), new Expression(0.0), userDefinedRole, unit, userDefinedRole.getDescription());
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
} else {
if (tempParam.getExpression() != null) {
// if the expression is null, it should remain null.
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
}
tempParam.setUnitDefinition(unit);
}
}
}
} catch (PropertyVetoException | ExpressionException | MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while setting parameters for '" + contextName + "': " + e.getMessage(), e);
}
}
use of cbit.vcell.parser.Expression in project vcell by virtualcell.
the class XmlReader method getJumpCondition.
/**
* This method returns a JumpCondition object from a XML Element.
* Creation date: (5/18/2001 5:10:10 PM)
* @return cbit.vcell.math.JumpCondition
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private JumpCondition getJumpCondition(Element param, MathDescription mathDesc) throws XmlParseException {
// get VolVariable ref
String varname = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
Variable var = mathDesc.getVariable(varname);
if (var == null) {
throw new XmlParseException("The reference to the Variable " + varname + ", could not be resolved!");
}
JumpCondition jumpCondition = null;
if (var instanceof VolVariable) {
jumpCondition = new JumpCondition((VolVariable) var);
} else if (var instanceof VolumeRegionVariable) {
jumpCondition = new JumpCondition((VolumeRegionVariable) var);
} else {
throw new XmlParseException("unexpected variable type for jump condition");
}
// process InFlux
String temp = param.getChildText(XMLTags.InFluxTag, vcNamespace);
Expression exp = unMangleExpression(temp);
jumpCondition.setInFlux(exp);
// process OutFlux
temp = param.getChildText(XMLTags.OutFluxTag, vcNamespace);
exp = unMangleExpression(temp);
jumpCondition.setOutFlux(exp);
return jumpCondition;
}
use of cbit.vcell.parser.Expression in project vcell by virtualcell.
the class XmlReader method getParticleJumpProcess.
private ParticleJumpProcess getParticleJumpProcess(Element param, MathDescription md) throws XmlParseException {
// name
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
ProcessSymmetryFactor processSymmetryFactor = null;
Attribute symmetryFactorAttr = param.getAttribute(XMLTags.ProcessSymmetryFactorAttrTag);
if (symmetryFactorAttr != null) {
processSymmetryFactor = new ProcessSymmetryFactor(Double.parseDouble(symmetryFactorAttr.getValue()));
}
// selected particle
List<ParticleVariable> varList = new ArrayList<ParticleVariable>();
Iterator<Element> iterator = param.getChildren(XMLTags.SelectedParticleTag, vcNamespace).iterator();
while (iterator.hasNext()) {
Element tempelement = (Element) iterator.next();
String varname = unMangle(tempelement.getAttributeValue(XMLTags.NameAttrTag));
Variable var = md.getVariable(varname);
if (!(var instanceof ParticleVariable)) {
throw new XmlParseException("Not a ParticleVariable in ParticleJumpProcess.");
}
varList.add((ParticleVariable) var);
}
// probability rate
JumpProcessRateDefinition jprd = null;
// for old models
Element pb = param.getChild(XMLTags.ParticleProbabilityRateTag, vcNamespace);
if (pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new MacroscopicRateConstant(exp);
} else // for new models
{
pb = param.getChild(XMLTags.MacroscopicRateConstantTag, vcNamespace);
if (// jump process rate defined by macroscopic rate constant
pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new MacroscopicRateConstant(exp);
} else // jump process rate defined by binding radius
{
pb = param.getChild(XMLTags.InteractionRadiusTag, vcNamespace);
if (pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new InteractionRadius(exp);
}
}
}
// add actions
List<Action> actionList = new ArrayList<Action>();
iterator = param.getChildren(XMLTags.ActionTag, vcNamespace).iterator();
while (iterator.hasNext()) {
Element tempelement = (Element) iterator.next();
try {
actionList.add(getAction(tempelement, md));
} catch (MathException e) {
e.printStackTrace();
throw new XmlParseException(e);
} catch (ExpressionException e) {
e.printStackTrace();
throw new XmlParseException(e);
}
}
ParticleJumpProcess jump = new ParticleJumpProcess(name, varList, jprd, actionList, processSymmetryFactor);
return jump;
}
use of cbit.vcell.parser.Expression in project vcell by virtualcell.
the class XmlReader method getAction.
/**
* This method returns a Action object from a XML element.
* Creation date: (7/24/2006 5:56:36 PM)
* @return cbit.vcell.math.Action
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private Action getAction(Element param, MathDescription md) throws XmlParseException, MathException, ExpressionException {
// retrieve values
String operation = unMangle(param.getAttributeValue(XMLTags.OperationAttrTag));
String operand = param.getText();
Expression exp = null;
if (operand != null && operand.length() != 0) {
exp = unMangleExpression(operand);
}
String name = unMangle(param.getAttributeValue(XMLTags.VarNameAttrTag));
Variable var = md.getVariable(name);
if (var == null) {
throw new MathFormatException("variable " + name + " not defined");
}
if (!(var instanceof StochVolVariable) && !(var instanceof ParticleVariable)) {
throw new MathFormatException("variable " + name + " not a Stochastic Volume Variable");
}
try {
Action action = new Action(var, operation, exp);
return action;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations