Search in sources :

Example 61 with Expression

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

the class CellQuanVCTranslator method processVariables.

protected void processVariables() {
    @SuppressWarnings("unchecked") List<Element> compElementList = sRoot.getChildren(CELLMLTags.COMPONENT, sNamespace);
    Iterator<Element> compElementIterator = compElementList.iterator();
    Element comp, var, varRef, temp;
    String compName, varName, publicIn, privateIn, mangledName;
    while (compElementIterator.hasNext()) {
        comp = compElementIterator.next();
        compName = comp.getAttributeValue(CELLMLTags.name, sAttNamespace);
        @SuppressWarnings("unchecked") List<Element> varElementList = comp.getChildren(CELLMLTags.VARIABLE, sNamespace);
        Iterator<Element> varElementIterator = varElementList.iterator();
        // clear const and volVars vectors for each component
        while (varElementIterator.hasNext()) {
            var = varElementIterator.next();
            publicIn = var.getAttributeValue(CELLMLTags.public_interface, sAttNamespace);
            privateIn = var.getAttributeValue(CELLMLTags.private_interface, sAttNamespace);
            if ((publicIn != null && publicIn.equals(CELLMLTags.inInterface)) || (privateIn != null && privateIn.equals(CELLMLTags.inInterface)))
                continue;
            varName = var.getAttributeValue(CELLMLTags.name, sAttNamespace);
            mangledName = nm.getMangledName(compName, varName);
            String uniqueVarName = getUniqueParamName(mangledName);
            System.out.println("Vars: " + compName + "." + varName + " " + mangledName + " " + uniqueVarName);
            // declare any differential free variables as VolumeVariable's
            temp = getMathElementIdentifier(comp, varName, MathMLTags.DIFFERENTIAL);
            try {
                if (temp != null) {
                    // formula ignored at this point
                    Domain domain = null;
                    VolVariable volVar = new VolVariable(mangledName, domain);
                    varsHash.addVariable(volVar);
                    continue;
                }
                // any LHS variables of an eq operator are functions
                temp = getMathElementIdentifier(comp, varName, MathMLTags.IDENTIFIER);
                if (temp != null) {
                    Function fn = addFunction(temp, comp, mangledName);
                    varsHash.addVariable(fn);
                    continue;
                }
                // Handle delta variables as a special case function
                varRef = getMatchingVarRef(comp, varName);
                if (varRef != null) {
                    // 2 functions with same name?
                    Function rateFn = addRateFunction(varRef, compName, uniqueVarName);
                    varsHash.addVariable(rateFn);
                    continue;
                }
                // check if it is the time variable, if not add as a constant. No handling for a specific value for the time var.
                if (!isTimeVar(varName)) {
                    Constant constant = new Constant(uniqueVarName, new Expression(getInitial(comp, varName)));
                    varsHash.addVariable(constant);
                }
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Error adding variable to variablesHash : " + e.getMessage());
            }
        }
    // end iteration for VARIABLES
    }
    // add the variables from varsHash to mathDescription
    try {
        mathDescription.setAllVariables(varsHash.getAlphabeticallyOrderedVariables());
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Error adding variables to mathDescription : " + e.getMessage());
    }
}
Also used : Function(cbit.vcell.math.Function) VolVariable(cbit.vcell.math.VolVariable) Expression(cbit.vcell.parser.Expression) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 62 with Expression

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

the class CellQuanVCTranslator method addFunction.

private Function addFunction(Element temp, Element comp, String mangledName) {
    String expStr = null;
    Expression exp = null;
    Element parent = (Element) temp.getParent();
    Element sibling = parent.getChild(MathMLTags.APPLY, mathns);
    if (sibling == null) {
        // check if its value is assigned to another variable (i.e. A = B)
        @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(parent.getChildren(MathMLTags.IDENTIFIER, mathns));
        if (list.size() == 2) {
            expStr = (list.get(1)).getTextTrim();
        }
        if (expStr == null || expStr.length() == 0) {
            expStr = parent.getChildText(MathMLTags.CONSTANT, mathns);
        }
        if (expStr == null || expStr.length() == 0) {
            // check if 'piecewise'
            sibling = parent.getChild(MathMLTags.PIECEWISE, mathns);
        }
        if (expStr != null) {
            try {
                exp = new Expression(expStr);
                exp = processMathExp(comp, exp);
                nl.mangleString(expStr);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException(e.getMessage());
            }
        }
    }
    if (sibling != null) {
        Element trimmedMath = new Element(CELLMLTags.MATH, mathns).addContent(sibling.detach());
        fixMathMLBug(trimmedMath);
        try {
            exp = (new ExpressionMathMLParser(null)).fromMathML(trimmedMath);
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException(e.getMessage());
        }
        exp = processMathExp(comp, exp);
        expStr = exp.infix();
        nl.mangleString(expStr);
    }
    Domain domain = null;
    return new Function(mangledName, exp, domain);
}
Also used : Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 63 with Expression

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

the class SBPAXHMMIrrevLawBuilder method addKinetics.

public void addKinetics(KineticContext context) {
    try {
        ReactionStep reaction = context.getReaction();
        HMM_IRRKinetics kinetics = new HMM_IRRKinetics((SimpleReaction) reaction);
        NameScope modelScope = reaction.getModel().getNameScope();
        ModelParameter kMichaelis = context.getParameter(SBOList.MICHAELIS_CONST_FORW);
        if (kMichaelis != null) {
            KineticsParameter kmParameter = kinetics.getKmParameter();
            kmParameter.setExpression(new Expression(kMichaelis, modelScope));
            kmParameter.setUnitDefinition(kMichaelis.getUnitDefinition());
        }
        ModelParameter kcat = context.getParameter(SBOList.CATALYTIC_RATE_CONST_FORW);
        if (kcat != null && context.getCatalysts().size() == 1) {
            KineticsParameter vmaxParameter = kinetics.getVmaxParameter();
            Catalyst catalyst = context.getCatalysts().iterator().next();
            vmaxParameter.setExpression(Expression.mult(new Expression(kcat, modelScope), new Expression(catalyst.getSpeciesContext(), modelScope)));
        // vmaxParameter.setUnitDefinition(vMax.getUnitDefinition());
        } else {
            ModelParameter vMax = context.getParameter(SBOList.MAXIMAL_VELOCITY_FORW);
            if (vMax != null) {
                KineticsParameter vmaxParameter = kinetics.getVmaxParameter();
                vmaxParameter.setExpression(new Expression(vMax, modelScope));
                vmaxParameter.setUnitDefinition(vMax.getUnitDefinition());
            }
        }
    } catch (ExpressionException e) {
        e.printStackTrace();
    }
}
Also used : ModelParameter(cbit.vcell.model.Model.ModelParameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) NameScope(cbit.vcell.parser.NameScope) Expression(cbit.vcell.parser.Expression) HMM_IRRKinetics(cbit.vcell.model.HMM_IRRKinetics) ReactionStep(cbit.vcell.model.ReactionStep) Catalyst(cbit.vcell.model.Catalyst) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 64 with Expression

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

the class SBPAXKineticsExtractor method setKForwardParam.

private static void setKForwardParam(ReactionStep reaction, SBOParam kForward, Kinetics kinetics) throws ExpressionException, PropertyVetoException {
    if (kForward != null) {
        KineticsParameter fwdParam = ((MassActionKinetics) kinetics).getForwardRateParameter();
        kinetics.setParameterValue(fwdParam, new Expression(kForward.getNumber()));
        String unitF = SBOParam.formatUnit(kForward.getUnit());
        VCUnitDefinition fwdUnit = reaction.getModel().getUnitSystem().getInstance(unitF);
        fwdParam.setUnitDefinition(fwdUnit);
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Expression(cbit.vcell.parser.Expression) MassActionKinetics(cbit.vcell.model.MassActionKinetics)

Example 65 with Expression

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

the class SBPAXKineticsExtractor method setKReverseParam.

private static void setKReverseParam(ReactionStep reaction, SBOParam kReverse, Kinetics kinetics) throws ExpressionException, PropertyVetoException {
    if (kReverse != null) {
        KineticsParameter revParam = ((MassActionKinetics) kinetics).getReverseRateParameter();
        kinetics.setParameterValue(revParam, new Expression(kReverse.getNumber()));
        String unitR = SBOParam.formatUnit(kReverse.getUnit());
        VCUnitDefinition revUnit = reaction.getModel().getUnitSystem().getInstance(unitR);
        revParam.setUnitDefinition(revUnit);
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Expression(cbit.vcell.parser.Expression) MassActionKinetics(cbit.vcell.model.MassActionKinetics)

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