Search in sources :

Example 1 with EventAssignment

use of cbit.vcell.math.Event.EventAssignment 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)

Example 2 with EventAssignment

use of cbit.vcell.math.Event.EventAssignment in project vcell by virtualcell.

the class Xmlproducer method getXML.

// For events in SimulationContext - XML is very similar to math events
public Element getXML(BioEvent[] bioEvents) throws XmlParseException {
    Element bioEventsElement = new Element(XMLTags.BioEventsTag);
    for (int i = 0; i < bioEvents.length; i++) {
        Element eventElement = new Element(XMLTags.BioEventTag);
        eventElement.setAttribute(XMLTags.NameAttrTag, mangle(bioEvents[i].getName()));
        String triggerType = bioEvents[i].getTriggerType().getXmlName();
        // Add atributes
        eventElement.setAttribute(XMLTags.BioEventTriggerTypeAttrTag, triggerType);
        eventElement.setAttribute(XMLTags.UseValuesFromTriggerTimeAttrTag, Boolean.toString(bioEvents[i].getUseValuesFromTriggerTime()));
        // Add BioEvent Parameters
        LocalParameter[] parameters = bioEvents[i].getEventParameters();
        for (LocalParameter parm : parameters) {
            if (parm.getExpression() != null) {
                Element tempparameter = new Element(XMLTags.ParameterTag);
                // Get parameter attributes
                tempparameter.setAttribute(XMLTags.NameAttrTag, mangle(parm.getName()));
                tempparameter.setAttribute(XMLTags.ParamRoleAttrTag, bioEvents[i].getParameterType(parm).getRoleXmlName());
                VCUnitDefinition unit = parm.getUnitDefinition();
                if (unit != null) {
                    tempparameter.setAttribute(XMLTags.VCUnitDefinitionAttrTag, unit.getSymbol());
                }
                tempparameter.addContent(mangleExpression(parm.getExpression()));
                // Add the parameter to the general kinetics object
                eventElement.addContent(tempparameter);
            }
        }
        ArrayList<BioEvent.EventAssignment> eventAssignmentsList = bioEvents[i].getEventAssignments();
        if (eventAssignmentsList != null) {
            for (BioEvent.EventAssignment eventAssignment : eventAssignmentsList) {
                Element eventAssignmentElement = new Element(XMLTags.EventAssignmentTag);
                eventAssignmentElement.setAttribute(XMLTags.EventAssignmentVariableAttrTag, eventAssignment.getTarget().getName());
                eventAssignmentElement.addContent(mangleExpression(eventAssignment.getAssignmentExpression()));
                eventElement.addContent(eventAssignmentElement);
            }
        }
        bioEventsElement.addContent(eventElement);
    }
    System.out.println(XmlUtil.xmlToString(bioEventsElement));
    return bioEventsElement;
}
Also used : LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) EventAssignment(cbit.vcell.math.Event.EventAssignment) Element(org.jdom.Element) BioEvent(cbit.vcell.mapping.BioEvent)

Example 3 with EventAssignment

use of cbit.vcell.math.Event.EventAssignment in project vcell by virtualcell.

the class Xmlproducer method getXML.

private Element getXML(Event event) throws XmlParseException {
    Element eventElement = new Element(XMLTags.EventTag);
    eventElement.setAttribute(XMLTags.NameAttrTag, mangle(event.getName()));
    Element element = new Element(XMLTags.TriggerTag);
    element.addContent(mangleExpression(event.getTriggerExpression()));
    eventElement.addContent(element);
    Delay delay = event.getDelay();
    if (delay != null) {
        element = new Element(XMLTags.DelayTag);
        element.setAttribute(XMLTags.UseValuesFromTriggerTimeAttrTag, delay.useValuesFromTriggerTime() + "");
        element.addContent(mangleExpression(delay.getDurationExpression()));
        eventElement.addContent(element);
    }
    Iterator<EventAssignment> iter = event.getEventAssignments();
    while (iter.hasNext()) {
        EventAssignment eventAssignment = iter.next();
        element = new Element(XMLTags.EventAssignmentTag);
        element.setAttribute(XMLTags.EventAssignmentVariableAttrTag, eventAssignment.getVariable().getName());
        element.addContent(mangleExpression(eventAssignment.getAssignmentExpression()));
        eventElement.addContent(element);
    }
    transcribeComments(event, eventElement);
    return eventElement;
}
Also used : EventAssignment(cbit.vcell.math.Event.EventAssignment) Element(org.jdom.Element) Delay(cbit.vcell.math.Event.Delay)

Example 4 with EventAssignment

use of cbit.vcell.math.Event.EventAssignment in project vcell by virtualcell.

the class XmlReader method getEvent.

private Event getEvent(MathDescription mathdesc, Element eventElement) throws XmlParseException {
    String name = unMangle(eventElement.getAttributeValue(XMLTags.NameAttrTag));
    Element element = eventElement.getChild(XMLTags.TriggerTag, vcNamespace);
    Expression triggerExp = unMangleExpression(element.getText());
    element = eventElement.getChild(XMLTags.DelayTag, vcNamespace);
    Delay delay = null;
    if (element != null) {
        boolean useValuesFromTriggerTime = Boolean.valueOf(element.getAttributeValue(XMLTags.UseValuesFromTriggerTimeAttrTag)).booleanValue();
        Expression durationExp = unMangleExpression(element.getText());
        delay = new Delay(useValuesFromTriggerTime, durationExp);
    }
    ArrayList<EventAssignment> eventAssignmentList = new ArrayList<EventAssignment>();
    Iterator<Element> iter = eventElement.getChildren(XMLTags.EventAssignmentTag, vcNamespace).iterator();
    while (iter.hasNext()) {
        element = iter.next();
        String varname = element.getAttributeValue(XMLTags.EventAssignmentVariableAttrTag);
        Expression assignExp = unMangleExpression(element.getText());
        Variable var = mathdesc.getVariable(varname);
        EventAssignment eventAssignment = new EventAssignment(var, assignExp);
        eventAssignmentList.add(eventAssignment);
    }
    Event event = new Event(name, triggerExp, delay, eventAssignmentList);
    transcribeComments(eventElement, event);
    return event;
}
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) EventAssignment(cbit.vcell.math.Event.EventAssignment) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) ArrayList(java.util.ArrayList) Event(cbit.vcell.math.Event) BioEvent(cbit.vcell.mapping.BioEvent) Delay(cbit.vcell.math.Event.Delay)

Example 5 with EventAssignment

use of cbit.vcell.math.Event.EventAssignment in project vcell by virtualcell.

the class OdeFileWriter method writeEvents.

private String writeEvents(HashMap<Discontinuity, String> discontinuityNameMap) throws ExpressionException {
    Simulation simulation = simTask.getSimulation();
    StringBuffer sb = new StringBuffer();
    MathDescription mathDescription = simulation.getMathDescription();
    Iterator<Event> iter = mathDescription.getEvents();
    sb.append("EVENTS " + mathDescription.getNumEvents() + "\n");
    while (iter.hasNext()) {
        Event event = iter.next();
        sb.append("EVENT " + event.getName() + "\n");
        Expression triggerExpression = event.getTriggerExpression();
        triggerExpression = MathUtilities.substituteFunctions(triggerExpression, varsSymbolTable).flatten();
        Vector<Discontinuity> v = triggerExpression.getDiscontinuities();
        for (Discontinuity od : v) {
            od = getSubsitutedAndFlattened(od, varsSymbolTable);
            String dname = discontinuityNameMap.get(od);
            if (dname == null) {
                dname = ROOT_VARIABLE_PREFIX + discontinuityNameMap.size();
                discontinuityNameMap.put(od, dname);
            }
            triggerExpression.substituteInPlace(od.getDiscontinuityExp(), new Expression("(" + dname + "==1)"));
        }
        sb.append("TRIGGER " + triggerExpression.infix() + ";\n");
        Delay delay = event.getDelay();
        if (delay != null) {
            Expression durationExpression = delay.getDurationExpression();
            durationExpression = MathUtilities.substituteFunctions(durationExpression, varsSymbolTable).flatten();
            sb.append("DELAY " + delay.useValuesFromTriggerTime() + " " + durationExpression.infix() + ";\n");
        }
        sb.append("EVENTASSIGNMENTS " + event.getNumEventAssignments() + "\n");
        Iterator<EventAssignment> iter2 = event.getEventAssignments();
        while (iter2.hasNext()) {
            EventAssignment eventAssignment = iter2.next();
            Expression assignmentExpression = eventAssignment.getAssignmentExpression();
            assignmentExpression = MathUtilities.substituteFunctions(assignmentExpression, varsSymbolTable).flatten();
            Variable assignmentTarget = eventAssignment.getVariable();
            for (int i = 0; i < fieldStateVariables.size(); i++) {
                if (assignmentTarget.getName().equals(fieldStateVariables.get(i).getVariable().getName())) {
                    sb.append(i + " " + assignmentExpression.infix() + ";\n");
                    break;
                }
            }
        }
    }
    return sb.toString();
}
Also used : Discontinuity(cbit.vcell.parser.Discontinuity) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) EventAssignment(cbit.vcell.math.Event.EventAssignment) MathDescription(cbit.vcell.math.MathDescription) Delay(cbit.vcell.math.Event.Delay) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) Event(cbit.vcell.math.Event)

Aggregations

EventAssignment (cbit.vcell.math.Event.EventAssignment)6 Expression (cbit.vcell.parser.Expression)4 Element (org.jdom.Element)4 BioEvent (cbit.vcell.mapping.BioEvent)3 Event (cbit.vcell.math.Event)3 Delay (cbit.vcell.math.Event.Delay)3 Variable (cbit.vcell.math.Variable)3 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)2 ReservedVariable (cbit.vcell.math.ReservedVariable)2 VolVariable (cbit.vcell.math.VolVariable)2 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)2 ArrayList (java.util.ArrayList)2 BioEventParameterType (cbit.vcell.mapping.BioEvent.BioEventParameterType)1 TriggerType (cbit.vcell.mapping.BioEvent.TriggerType)1 FilamentRegionVariable (cbit.vcell.math.FilamentRegionVariable)1 FilamentVariable (cbit.vcell.math.FilamentVariable)1 InsideVariable (cbit.vcell.math.InsideVariable)1 MathDescription (cbit.vcell.math.MathDescription)1 MemVariable (cbit.vcell.math.MemVariable)1 MembraneParticleVariable (cbit.vcell.math.MembraneParticleVariable)1