Search in sources :

Example 1 with BioEventParameterType

use of cbit.vcell.mapping.BioEvent.BioEventParameterType 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));
}
Also used : TriggerType(cbit.vcell.mapping.BioEvent.TriggerType) EventAssignment(cbit.vcell.math.Event.EventAssignment) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) BioEventParameterType(cbit.vcell.mapping.BioEvent.BioEventParameterType) Iterator(java.util.Iterator) BioEvent(cbit.vcell.mapping.BioEvent) Vector(java.util.Vector)

Aggregations

BioEvent (cbit.vcell.mapping.BioEvent)1 BioEventParameterType (cbit.vcell.mapping.BioEvent.BioEventParameterType)1 TriggerType (cbit.vcell.mapping.BioEvent.TriggerType)1 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)1 EventAssignment (cbit.vcell.math.Event.EventAssignment)1 Expression (cbit.vcell.parser.Expression)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)1 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)1 PropertyVetoException (java.beans.PropertyVetoException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1 Element (org.jdom.Element)1