Search in sources :

Example 31 with Expression

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;
}
Also used : MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) Expression(cbit.vcell.parser.Expression) MembraneRegionEquation(cbit.vcell.math.MembraneRegionEquation)

Example 32 with Expression

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);
    }
}
Also used : FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) VariableHash(cbit.vcell.math.VariableHash) Element(org.jdom.Element) ExpressionException(cbit.vcell.parser.ExpressionException) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) PropertyVetoException(java.beans.PropertyVetoException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Function(cbit.vcell.math.Function) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain) ParameterRoleEnum(cbit.vcell.mapping.ParameterContext.ParameterRoleEnum) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 33 with Expression

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;
}
Also used : JumpCondition(cbit.vcell.math.JumpCondition) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) StochVolVariable(cbit.vcell.math.StochVolVariable) VolVariable(cbit.vcell.math.VolVariable) Expression(cbit.vcell.parser.Expression)

Example 34 with Expression

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;
}
Also used : JumpProcessRateDefinition(cbit.vcell.math.JumpProcessRateDefinition) Action(cbit.vcell.math.Action) FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) InteractionRadius(cbit.vcell.math.InteractionRadius) Attribute(org.jdom.Attribute) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) Element(org.jdom.Element) ParticleJumpProcess(cbit.vcell.math.ParticleJumpProcess) ArrayList(java.util.ArrayList) ExpressionException(cbit.vcell.parser.ExpressionException) ProcessSymmetryFactor(cbit.vcell.math.ParticleJumpProcess.ProcessSymmetryFactor) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant)

Example 35 with Expression

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;
}
Also used : Action(cbit.vcell.math.Action) FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) Expression(cbit.vcell.parser.Expression) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MathFormatException(cbit.vcell.math.MathFormatException) StochVolVariable(cbit.vcell.math.StochVolVariable) GeometryException(cbit.vcell.geometry.GeometryException) MathFormatException(cbit.vcell.math.MathFormatException) MappingException(cbit.vcell.mapping.MappingException) PropertyVetoException(java.beans.PropertyVetoException) ImageException(cbit.image.ImageException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) DataConversionException(org.jdom.DataConversionException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException)

Aggregations

Expression (cbit.vcell.parser.Expression)549 ExpressionException (cbit.vcell.parser.ExpressionException)163 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)76 PropertyVetoException (java.beans.PropertyVetoException)73 Variable (cbit.vcell.math.Variable)69 ArrayList (java.util.ArrayList)58 Vector (java.util.Vector)56 MathException (cbit.vcell.math.MathException)55 VolVariable (cbit.vcell.math.VolVariable)53 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)51 SpeciesContext (cbit.vcell.model.SpeciesContext)50 Element (org.jdom.Element)47 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)45 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)45 Model (cbit.vcell.model.Model)43 Function (cbit.vcell.math.Function)42 Constant (cbit.vcell.math.Constant)41 ModelParameter (cbit.vcell.model.Model.ModelParameter)41 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)41 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)38