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));
}
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;
}
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;
}
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;
}
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();
}
Aggregations