use of cbit.vcell.mapping.MappingException in project vcell by virtualcell.
the class MathMapping_4_8 method refreshVariables.
/**
* This method was created in VisualAge.
*/
private void refreshVariables() throws MappingException {
// System.out.println("MathMapping.refreshVariables()");
//
// non-constant dependent variables require a function
//
Enumeration<SpeciesContextMapping> enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = enum1.nextElement();
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
if (scm.getDependencyExpression() != null && !scs.isConstant()) {
// scm.setVariable(new Function(scm.getSpeciesContext().getName(),scm.getDependencyExpression()));
scm.setVariable(null);
}
}
enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = enum1.nextElement();
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
if (getSimulationContext().hasEventAssignment(scs.getSpeciesContext())) {
scm.setDependencyExpression(null);
}
}
//
// non-constant independent variables require either a membrane or volume variable
//
enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = (SpeciesContextMapping) enum1.nextElement();
SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
if (scm.getDependencyExpression() == null && (!scs.isConstant() || getSimulationContext().hasEventAssignment(scs.getSpeciesContext()))) {
StructureMapping sm = simContext.getGeometryContext().getStructureMapping(scm.getSpeciesContext().getStructure());
Structure struct = scm.getSpeciesContext().getStructure();
if (struct instanceof Feature) {
if (getResolved(sm)) {
scm.setVariable(getResolvedVolVariable(scm.getSpeciesContext().getSpecies()));
} else {
scm.setVariable(new VolVariable(scm.getSpeciesContext().getName(), nullDomain));
}
} else if (struct instanceof Membrane) {
if (getResolved(sm)) {
scm.setVariable(new MemVariable(scm.getSpeciesContext().getName(), nullDomain));
} else {
scm.setVariable(new VolVariable(scm.getSpeciesContext().getName(), nullDomain));
}
} else {
throw new MappingException("class " + scm.getSpeciesContext().getStructure().getClass() + " not supported");
}
mathSymbolMapping.put(scm.getSpeciesContext(), scm.getVariable().getName());
}
}
}
use of cbit.vcell.mapping.MappingException in project vcell by virtualcell.
the class PotentialMapping method determineLumpedEquations.
/**
* Insert the method's description here.
* Creation date: (2/20/2002 9:02:42 AM)
* @return cbit.vcell.mathmodel.MathModel
* @param circuitGraph cbit.vcell.mapping.potential.Graph
*/
private void determineLumpedEquations(Graph graph, double temperatureKelvin) throws ExpressionException, MatrixException, MappingException, MathException {
//
// traverses graph and calculates RHS expressions for all capacitive devices (dV/dt)
//
// calculate dependent voltages as functions of independent voltages.
//
//
Graph[] spanningTrees = graph.getSpanningForest();
if (!bSilent) {
System.out.println("spanning tree(s):");
for (int i = 0; i < spanningTrees.length; i++) {
System.out.println(i + ") " + spanningTrees[i]);
}
}
Path[] fundamentalCycles = graph.getFundamentalCycles();
if (!bSilent) {
System.out.println("fundamental cycles:");
for (int i = 0; i < fundamentalCycles.length; i++) {
System.out.println(" " + fundamentalCycles[i]);
}
}
//
// print out basic device information
//
fieldEdges = graph.getEdges();
//
if (!bSilent) {
System.out.println("\n\n applying KVL to <<ALL>> fundamental cycles\n");
}
Path[] graphCycles = graph.getFundamentalCycles();
RationalExpMatrix voltageMatrix = new RationalExpMatrix(graphCycles.length, graph.getNumEdges());
for (int i = 0; i < graphCycles.length; i++) {
Edge[] cycleEdges = graphCycles[i].getEdges();
Node[] nodesTraversed = graphCycles[i].getNodesTraversed();
Expression exp = new Expression(0.0);
//
for (int j = 0; j < cycleEdges.length; j++) {
int edgeIndex = graph.getIndex(cycleEdges[j]);
Expression voltage = new Expression(((ElectricalDevice) cycleEdges[j].getData()).getVoltageSymbol(), fieldMathMapping.getNameScope());
if (cycleEdges[j].getNode1().equals(nodesTraversed[j])) {
// going same direction
exp = Expression.add(exp, voltage);
voltageMatrix.set_elem(i, edgeIndex, 1);
} else {
// going opposite direction
exp = Expression.add(exp, Expression.negate(voltage));
voltageMatrix.set_elem(i, edgeIndex, -1);
}
}
if (!bSilent) {
System.out.println(exp.flatten().infix() + " = 0.0");
}
}
if (!bSilent) {
voltageMatrix.show();
}
if (voltageMatrix.getNumRows() > 0) {
RationalExpMatrix vPermutationMatrix = new RationalExpMatrix(voltageMatrix.getNumRows(), voltageMatrix.getNumRows());
voltageMatrix.gaussianElimination(vPermutationMatrix);
if (!bSilent) {
System.out.println("reduced matrix");
voltageMatrix.show();
}
} else {
voltageMatrix = null;
}
//
// declare dependent voltages as functions of independent voltages
//
// 1) Always use Voltage-Clamps as independent voltages
// 2) Try to use Capacitive devices as independent voltages
//
// solve for current-clamp voltages and redundant/constrained capacitive voltages as function of (1) and (2).
//
Expression[] dependentVoltageExpressions = new Expression[fieldEdges.length];
//
// make sure assumptions hold regarding edge ordering, otherwise wrong dependent voltages will be selected
//
verifyEdgeOrdering();
for (int i = 0; voltageMatrix != null && i < voltageMatrix.getNumRows(); i++) {
//
// find first '1.0' element, this column is the next 'dependent' voltage
//
int column = -1;
for (int j = i; j < voltageMatrix.getNumCols(); j++) {
RationalExp elem = voltageMatrix.get(i, j);
if (elem.isConstant() && elem.getConstant().doubleValue() == 1.0) {
column = j;
break;
}
}
if (column != -1) {
//
// get electrical device of dependent voltage
//
ElectricalDevice device = (ElectricalDevice) fieldEdges[column].getData();
//
// form dependency expression
//
StringBuffer buffer = new StringBuffer();
for (int j = column + 1; j < graph.getNumEdges(); j++) {
if (!voltageMatrix.get_elem(i, j).isZero()) {
ElectricalDevice colDevice = (ElectricalDevice) fieldEdges[j].getData();
Expression voltage = new Expression(colDevice.getVoltageSymbol(), fieldMathMapping.getNameScope());
buffer.append(" + " + voltageMatrix.get(i, j).minus().infixString() + '*' + voltage.infix());
}
}
dependentVoltageExpressions[column] = (new Expression(buffer.toString())).flatten();
dependentVoltageExpressions[column].bindExpression(device);
device.setDependentVoltageExpression(dependentVoltageExpressions[column]);
}
}
if (!bSilent) {
for (int i = 0; i < dependentVoltageExpressions.length; i++) {
System.out.println("dependentVoltageExpressions[" + i + "] = " + dependentVoltageExpressions[i]);
}
}
//
if (!bSilent) {
System.out.println("\n\nSOLVE FOR TOTAL CURRENTS IN TERMS OF APPLIED CURRENTS");
System.out.println("\n\n 1) applying KVL to all fundamental \"capacitive\" and Voltage-clamp cycles (after current-clamp edges are removed)\n");
}
Graph capacitorGraph = new Graph();
for (int i = 0; i < fieldEdges.length; i++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[i].getData();
if (device.hasCapacitance() || device.isVoltageSource()) {
capacitorGraph.addEdge(fieldEdges[i]);
}
}
Path[] capacitorGraphCycles = capacitorGraph.getFundamentalCycles();
RationalExpMatrix capacitorGraphVoltageMatrix = new RationalExpMatrix(capacitorGraphCycles.length, graph.getNumEdges());
for (int i = 0; i < capacitorGraphCycles.length; i++) {
Edge[] cycleEdges = capacitorGraphCycles[i].getEdges();
Node[] nodesTraversed = capacitorGraphCycles[i].getNodesTraversed();
Expression exp = new Expression(0.0);
//
for (int j = 0; j < cycleEdges.length; j++) {
int edgeIndex = graph.getIndex(cycleEdges[j]);
Expression voltage = new Expression(((ElectricalDevice) cycleEdges[j].getData()).getVoltageSymbol(), fieldMathMapping.getNameScope());
if (cycleEdges[j].getNode1().equals(nodesTraversed[j])) {
// going same direction
exp = Expression.add(exp, voltage);
capacitorGraphVoltageMatrix.set_elem(i, edgeIndex, 1);
} else {
// going opposite direction
exp = Expression.add(exp, Expression.negate(voltage));
capacitorGraphVoltageMatrix.set_elem(i, edgeIndex, -1);
}
}
if (!bSilent) {
System.out.println(exp.flatten().infix() + " = 0.0");
}
}
if (!bSilent) {
capacitorGraphVoltageMatrix.show();
}
//
if (!bSilent) {
System.out.println("\n\n 2) applying KCL to all nodes (except one) .. n-1 nodes of full graph --- CONSERVATION OF TOTAL CURRENT\n");
}
Node[] nodes = graph.getNodes();
RationalExpMatrix kclMatrix = new RationalExpMatrix(graph.getNumNodes() - 1, graph.getNumEdges());
for (int i = 0; i < nodes.length - 1; i++) {
if (graph.getDegree(nodes[i]) > 0) {
Edge[] adjacentEdges = graph.getAdjacentEdges(nodes[i]);
Expression exp = new Expression(0.0);
//
for (int j = 0; j < adjacentEdges.length; j++) {
int edgeIndex = graph.getIndex(adjacentEdges[j]);
Expression totalCurrent = new Expression(((ElectricalDevice) adjacentEdges[j].getData()).getTotalCurrentSymbol(), fieldMathMapping.getNameScope());
if (adjacentEdges[j].getNode1().equals(nodes[i])) {
exp = Expression.add(exp, Expression.negate(totalCurrent));
kclMatrix.set_elem(i, edgeIndex, -1);
} else {
exp = Expression.add(exp, totalCurrent);
kclMatrix.set_elem(i, edgeIndex, 1);
}
}
if (!bSilent) {
System.out.println(exp.flatten().infix() + " = 0.0");
}
} else {
throw new MappingException("compartment node is isolated in PotentialMapping, not supported");
}
}
if (!bSilent) {
kclMatrix.show();
}
//
if (!bSilent) {
System.out.println("\n\n 3) form total 'current' matrix\n");
}
int numNonCapacitiveEdges = 0;
for (int i = 0; i < fieldEdges.length; i++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[i].getData();
if (!device.hasCapacitance()) {
numNonCapacitiveEdges++;
}
}
int cmat_rows = kclMatrix.getNumRows() + capacitorGraphVoltageMatrix.getNumRows() + numNonCapacitiveEdges;
RationalExpMatrix currentMatrix = new RationalExpMatrix(cmat_rows, 3 * graph.getNumEdges());
//
// order edges for current elimination (unknown voltage-clamp currents as well as all total currents).
//
int[] cIndex = new int[fieldEdges.length];
int ci = 0;
for (int i = 0; i < fieldEdges.length; i++) {
if (((ElectricalDevice) fieldEdges[i].getData()).isVoltageSource()) {
cIndex[ci++] = i;
}
}
for (int i = 0; i < fieldEdges.length; i++) {
if (!((ElectricalDevice) fieldEdges[i].getData()).isVoltageSource()) {
cIndex[ci++] = i;
}
}
if (ci != fieldEdges.length) {
throw new RuntimeException("error computing current indexes");
}
if (!bSilent) {
System.out.println("reordered devices for current matrix elimination");
for (int i = 0; i < cIndex.length; i++) {
Expression voltage = new Expression(((ElectricalDevice) fieldEdges[cIndex[i]].getData()).getVoltageSymbol(), fieldMathMapping.getNameScope());
System.out.println(i + ") = device[" + cIndex[i] + "] = " + voltage.infix());
}
}
int row = 0;
//
for (int i = 0; i < kclMatrix.getNumRows(); i++) {
for (int j = 0; j < kclMatrix.getNumCols(); j++) {
// entry for i's
currentMatrix.set_elem(row, j, kclMatrix.get(i, cIndex[j]));
}
row++;
}
//
for (int i = 0; i < fieldEdges.length; i++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[cIndex[i]].getData();
if (!device.hasCapacitance()) {
currentMatrix.set_elem(row, i, 1);
currentMatrix.set_elem(row, i + graph.getNumEdges(), -1);
row++;
}
}
//
for (int i = 0; i < capacitorGraphVoltageMatrix.getNumRows(); i++) {
for (int j = 0; j < capacitorGraphVoltageMatrix.getNumCols(); j++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[cIndex[j]].getData();
RationalExp coefficient = capacitorGraphVoltageMatrix.get(i, cIndex[j]);
if (device.hasCapacitance()) {
//
// replace dVi/dt with 1000/Ci * Ii + 1000/Ci * Fi
//
Expression capacitance = new Expression(((MembraneElectricalDevice) device).getCapacitanceParameter(), fieldMathMapping.getNameScope());
String Cname = capacitance.infix();
// entry for i's
currentMatrix.set_elem(row, j, coefficient.mult(new RationalExp(BigInteger.valueOf(1000))).div(new RationalExp(Cname)));
// entry for F's
currentMatrix.set_elem(row, j + graph.getNumEdges(), coefficient.minus().mult(new RationalExp(BigInteger.valueOf(1000))).div(new RationalExp(Cname)));
} else if (device.isVoltageSource()) {
//
// directly insert "symbolic" dVi/dt into the new matrix
//
currentMatrix.set_elem(row, j + 2 * graph.getNumEdges(), coefficient);
}
}
row++;
}
if (!bSilent) {
currentMatrix.show();
}
if (currentMatrix.getNumRows() > 0) {
RationalExpMatrix cPermutationMatrix = new RationalExpMatrix(currentMatrix.getNumRows(), currentMatrix.getNumRows());
currentMatrix.gaussianElimination(cPermutationMatrix);
if (!bSilent) {
System.out.println("reduced matrix");
currentMatrix.show();
}
}
//
if (!bSilent) {
System.out.println("\n\n total currents for each device\n");
}
Expression[] totalCurrents = new Expression[fieldEdges.length];
for (int i = 0; i < fieldEdges.length; i++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[cIndex[i]].getData();
StringBuffer buffer = new StringBuffer("0.0");
//
for (int j = 0; j < graph.getNumEdges(); j++) {
RationalExp coefficient = currentMatrix.get(i, j + graph.getNumEdges());
if (!coefficient.isZero()) {
ElectricalDevice colDevice = (ElectricalDevice) fieldEdges[cIndex[j]].getData();
Expression source = new Expression(colDevice.getSourceSymbol(), fieldMathMapping.getNameScope());
buffer.append(" + " + coefficient.minus() + "*" + source.infix());
}
}
//
for (int j = 0; j < graph.getNumEdges(); j++) {
RationalExp coefficient = currentMatrix.get(i, j + 2 * graph.getNumEdges());
if (!coefficient.isZero()) {
VoltageClampElectricalDevice colDevice = (VoltageClampElectricalDevice) fieldEdges[cIndex[j]].getData();
Expression timeDeriv = colDevice.getVoltageClampStimulus().getVoltageParameter().getExpression().differentiate("t");
timeDeriv = timeDeriv.flatten();
timeDeriv.bindExpression(colDevice);
timeDeriv.renameBoundSymbols(colDevice.getNameScope().getParent());
buffer.append(" + " + coefficient.minus() + "*" + timeDeriv.infix());
}
}
totalCurrents[cIndex[i]] = (new Expression(buffer.toString())).flatten();
totalCurrents[cIndex[i]].bindExpression(device.getNameScope().getParent().getScopedSymbolTable());
device.getParameterFromRole(ElectricalDevice.ROLE_TotalCurrent).setExpression(totalCurrents[cIndex[i]]);
}
if (!bSilent) {
for (int i = 0; i < totalCurrents.length; i++) {
System.out.println("totalCurrents[" + i + "] = " + totalCurrents[cIndex[i]].toString());
}
}
//
if (!bSilent) {
System.out.println("\n\n capacitive currents for each device\n");
}
Expression[] capacitiveCurrents = new Expression[fieldEdges.length];
for (int i = 0; i < fieldEdges.length; i++) {
ElectricalDevice device = (ElectricalDevice) fieldEdges[i].getData();
if (device instanceof MembraneElectricalDevice) {
MembraneElectricalDevice membraneElectricalDevice = (MembraneElectricalDevice) device;
Expression source = new Expression(membraneElectricalDevice.getSourceSymbol(), fieldMathMapping.getNameScope());
capacitiveCurrents[i] = Expression.add(totalCurrents[i], Expression.negate(source));
capacitiveCurrents[i].bindExpression(membraneElectricalDevice);
// membraneElectricalDevice.setCapacitiveCurrentExpression(capacitiveCurrents[i]);
} else {
// device.setCapacitiveCurrentExpression(new Expression(0.0));
}
}
if (!bSilent) {
for (int i = 0; i < capacitiveCurrents.length; i++) {
System.out.println("capacitiveCurrents[" + i + "] = " + ((capacitiveCurrents[cIndex[i]] == null) ? "0.0" : capacitiveCurrents[cIndex[i]].infix()));
}
}
//
// display equations for independent voltages.
//
// if (!bSilent){
// for (int i = 0; i < graph.getNumEdges(); i++){
// ElectricalDevice device = (ElectricalDevice)graph.getEdges()[i].getData();
// //
// // membrane ode
// //
// if (device.hasCapacitance() && dependentVoltageExpressions[i]==null){
// Expression initExp = new Expression(0.0);
// System.out.println(device.getInitialVoltageFunction().getVCML());
// System.out.println((new cbit.vcell.math.OdeEquation(new cbit.vcell.math.VolVariable(device.getVName()),new Expression(device.getInitialVoltageFunction().getName()),new Expression(fieldCapacitiveCurrent[i].flatten().toString()+"*"+cbit.vcell.model.ReservedSymbol.KMILLIVOLTS.getName()+"/"+device.getCapName()))).getVCML());
// }
// //
// // membrane forced potential
// //
// if (device.hasCapacitance() && dependentVoltageExpressions[i]!=null){
// System.out.println((new Function(device.getVName(),dependentVoltageExpressions[i])).getVCML());
// System.out.println((new Function(device.getSourceName(),device.getCurrentSourceExpression()).getVCML()));
// }
// //
// // current clamp
// //
// if (!device.hasCapacitance() && !device.isVoltageSource()){
// System.out.println((new Function(device.getSourceName(),device.getCurrentSourceExpression()).getVCML()));
// System.out.println((new Function(device.getVName(),dependentVoltageExpressions[i])).getVCML());
// }
// //
// // voltage clamp
// //
// if (!device.hasCapacitance() && device.isVoltageSource()){
// System.out.println((new Function(device.getIName(),totalCurrents[i])).getVCML());
// System.out.println((new Function(device.getVName(),device.getInitialVoltageFunction().getExpression())).getVCML());
// }
// }
// }
}
use of cbit.vcell.mapping.MappingException in project vcell by virtualcell.
the class ModelOptimizationMapping method computeOptimizationSpec.
/**
* Insert the method's description here.
* Creation date: (8/22/2005 9:26:52 AM)
* @return cbit.vcell.opt.OptimizationSpec
* @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
*/
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
if (getModelOptimizationSpec().getReferenceData() == null) {
System.out.println("no referenced data defined");
return null;
}
OptimizationSpec optSpec = new OptimizationSpec();
optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
parameterMappings = null;
//
// get original MathDescription (later to be substituted for local/global parameters).
//
SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription origMathDesc = null;
mathSymbolMapping = null;
try {
origMathDesc = mathMapping.getMathDescription();
mathSymbolMapping = mathMapping.getMathSymbolMapping();
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ModelException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
// create objective function (mathDesc and data)
//
ReferenceData referenceData = getRemappedReferenceData(mathMapping);
if (referenceData == null) {
throw new RuntimeException("no referenced data defined");
}
//
// get parameter mappings
//
ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
for (int i = 0; i < parameterMappingSpecs.length; i++) {
cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
String mathSymbol = null;
Variable mathVariable = null;
if (mathSymbolMapping != null) {
Variable variable = mathSymbolMapping.getVariable(modelParameter);
if (variable != null) {
mathSymbol = variable.getName();
}
if (mathSymbol != null) {
mathVariable = origMathDesc.getVariable(mathSymbol);
}
}
if (mathVariable != null) {
if (parameterMappingSpecs[i].isSelected()) {
if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
}
if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getLow() < 0) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
}
if (parameterMappingSpecs[i].getHigh() <= 0) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
}
}
double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
double high = parameterMappingSpecs[i].getHigh();
double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
double current = parameterMappingSpecs[i].getCurrent();
low = Math.min(low, current);
high = Math.max(high, current);
Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
if (parameterMappingSpecs[i].isSelected()) {
allVars[j] = new ParameterVariable(origConstant.getName());
} else {
allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
}
break;
}
}
}
//
if (parameterMappingSpecs[i].isSelected()) {
parameterMappingList.add(parameterMapping);
}
}
}
parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
try {
origMathDesc.setAllVariables(allVars);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
for (int i = 0; i < parameterMappings.length; i++) {
optSpec.addParameter(parameterMappings[i].getOptParameter());
}
Vector<Issue> issueList = new Vector<Issue>();
IssueContext issueContext = new IssueContext();
optSpec.gatherIssues(issueContext, issueList);
for (int i = 0; i < issueList.size(); i++) {
Issue issue = issueList.elementAt(i);
if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
throw new RuntimeException(issue.getMessage());
}
}
//
//
//
optimizationSpec = optSpec;
return mathSymbolMapping;
}
use of cbit.vcell.mapping.MappingException in project vcell by virtualcell.
the class SBMLExporter method addSpecies.
/**
* addSpecies comment.
* @throws XMLStreamException
* @throws SbmlException
*/
protected void addSpecies() throws XMLStreamException, SbmlException {
Model vcModel = vcBioModel.getModel();
SpeciesContext[] vcSpeciesContexts = vcModel.getSpeciesContexts();
for (int i = 0; i < vcSpeciesContexts.length; i++) {
org.sbml.jsbml.Species sbmlSpecies = sbmlModel.createSpecies();
sbmlSpecies.setId(vcSpeciesContexts[i].getName());
if (vcSpeciesContexts[i].getSbmlName() != null) {
sbmlSpecies.setName(vcSpeciesContexts[i].getSbmlName());
}
// Assuming that at this point, the compartment(s) for the model are already filled in.
Compartment compartment = sbmlModel.getCompartment(TokenMangler.mangleToSName(vcSpeciesContexts[i].getStructure().getName()));
if (compartment != null) {
sbmlSpecies.setCompartment(compartment.getId());
}
// 'hasSubstanceOnly' field will be 'true', since export to SBML is done by converting to initial amounts.
sbmlSpecies.setHasOnlySubstanceUnits(true);
// Get (and set) the initial concentration value
if (getSelectedSimContext() == null) {
throw new RuntimeException("No simcontext (application) specified; Cannot proceed.");
}
// Get the speciesContextSpec in the simContext corresponding to the 'speciesContext'; and extract its initial concentration value.
SpeciesContextSpec vcSpeciesContextsSpec = getSelectedSimContext().getReactionContext().getSpeciesContextSpec(vcSpeciesContexts[i]);
// since we are setting the substance units for species to 'molecule' or 'item', a unit that is originally in uM (or molecules/um2),
// we need to convert concentration from uM -> molecules/um3; this can be achieved by dividing by KMOLE.
// for now we don't do this here and defer to the mechanisms built into the SimContext to convert and set amount instead of concentration
// TO-DO: change to export either concentrations or amounts depending on the type of SimContext and setting
SpeciesContextSpecParameter initCount = vcSpeciesContextsSpec.getInitialCountParameter();
if (initCount.getExpression() == null) {
try {
getSelectedSimContext().convertSpeciesIniCondition(false);
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage());
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
Expression initCountExpr = initCount.getExpression();
try {
sbmlSpecies.setInitialAmount(initCountExpr.evaluateConstant());
} catch (cbit.vcell.parser.ExpressionException e) {
// If exporting to L2V3, if species concentration is not an expr with x, y, z or other species, add as InitialAssignment, else complain.
if (initCountExpr != null) {
if ((sbmlLevel == 2 && sbmlVersion >= 3) || (sbmlLevel > 2)) {
// L2V3 and above - add expression as init assignment
cbit.vcell.mapping.AssignmentRule vcellAs = getSelectedSimContext().getAssignmentRule(vcSpeciesContexts[i]);
if (vcellAs == null) {
// we don't create InitialAssignment for an AssignmentRule variable (Reference: L3V1 Section 4.8)
ASTNode initAssgnMathNode = getFormulaFromExpression(initCountExpr);
InitialAssignment initAssignment = sbmlModel.createInitialAssignment();
initAssignment.setSymbol(vcSpeciesContexts[i].getName());
initAssignment.setMath(initAssgnMathNode);
}
} else {
// L2V1 (or L1V2 also??)
// do nothing - we no longer support export to level <3
// // L2V1 (and L1V2?) and species is 'fixed' (constant), and not fn of x,y,z, other sp, add expr as assgn rule
// ASTNode assgnRuleMathNode = getFormulaFromExpression(initCountExpr);
// AssignmentRule assgnRule = sbmlModel.createAssignmentRule();
// assgnRule.setVariable(vcSpeciesContexts[i].getName());
// assgnRule.setMath(assgnRuleMathNode);
}
}
}
// Get (and set) the boundary condition value
boolean bBoundaryCondition = getBoundaryCondition(vcSpeciesContexts[i]);
sbmlSpecies.setBoundaryCondition(bBoundaryCondition);
// mandatory for L3, optional for L2
sbmlSpecies.setConstant(false);
// set species substance units as 'molecules' - same as defined in the model; irrespective of it is in surface or volume.
UnitDefinition unitDefn = getOrCreateSBMLUnit(sbmlExportSpec.getSubstanceUnits());
sbmlSpecies.setSubstanceUnits(unitDefn);
// need to do the following if exporting to SBML spatial
if (bSpatial) {
// Required for setting BoundaryConditions : structureMapping for vcSpeciesContext[i] & sbmlGeometry.coordinateComponents
StructureMapping sm = getSelectedSimContext().getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure());
SpatialModelPlugin mplugin = (SpatialModelPlugin) sbmlModel.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
org.sbml.jsbml.ext.spatial.Geometry sbmlGeometry = mplugin.getGeometry();
CoordinateComponent ccX = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getX().getName());
CoordinateComponent ccY = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getY().getName());
CoordinateComponent ccZ = sbmlGeometry.getListOfCoordinateComponents().get(vcModel.getZ().getName());
// add diffusion, advection, boundary condition parameters for species, if they exist
Parameter[] scsParams = vcSpeciesContextsSpec.getParameters();
if (scsParams != null) {
for (int j = 0; j < scsParams.length; j++) {
if (scsParams[j] != null) {
SpeciesContextSpecParameter scsParam = (SpeciesContextSpecParameter) scsParams[j];
// no need to add parameters in SBML for init conc or init count
int role = scsParam.getRole();
switch(role) {
case SpeciesContextSpec.ROLE_BoundaryValueXm:
{
break;
}
case SpeciesContextSpec.ROLE_BoundaryValueXp:
{
break;
}
case SpeciesContextSpec.ROLE_BoundaryValueYm:
{
break;
}
case SpeciesContextSpec.ROLE_BoundaryValueYp:
{
break;
}
case SpeciesContextSpec.ROLE_BoundaryValueZm:
{
break;
}
case SpeciesContextSpec.ROLE_BoundaryValueZp:
{
break;
}
case SpeciesContextSpec.ROLE_DiffusionRate:
{
break;
}
case SpeciesContextSpec.ROLE_InitialConcentration:
{
// done elsewhere??
continue;
// break;
}
case SpeciesContextSpec.ROLE_InitialCount:
{
// done elsewhere??
continue;
// break;
}
case SpeciesContextSpec.ROLE_VelocityX:
{
break;
}
case SpeciesContextSpec.ROLE_VelocityY:
{
break;
}
case SpeciesContextSpec.ROLE_VelocityZ:
{
break;
}
default:
{
throw new RuntimeException("SpeciesContext Specification parameter with role " + SpeciesContextSpec.RoleNames[role] + " not yet supported for SBML export");
}
}
// if diffusion is 0 && vel terms are not specified, boundary condition not present
if (vcSpeciesContextsSpec.isAdvecting() || vcSpeciesContextsSpec.isDiffusing()) {
Expression diffExpr = vcSpeciesContextsSpec.getDiffusionParameter().getExpression();
boolean bDiffExprNull = (diffExpr == null);
boolean bDiffExprIsZero = false;
if (!bDiffExprNull && diffExpr.isNumeric()) {
try {
bDiffExprIsZero = (diffExpr.evaluateConstant() == 0.0);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Unable to evalute numeric value of diffusion parameter for speciesContext '" + vcSpeciesContexts[i] + "'.");
}
}
boolean bDiffusionZero = (bDiffExprNull || bDiffExprIsZero);
Expression velX_Expr = vcSpeciesContextsSpec.getVelocityXParameter().getExpression();
SpatialQuantity[] velX_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.X);
boolean bVelX_ExprIsNull = (velX_Expr == null && velX_Quantities.length == 0);
Expression velY_Expr = vcSpeciesContextsSpec.getVelocityYParameter().getExpression();
SpatialQuantity[] velY_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.Y);
boolean bVelY_ExprIsNull = (velY_Expr == null && velY_Quantities.length == 0);
Expression velZ_Expr = vcSpeciesContextsSpec.getVelocityZParameter().getExpression();
SpatialQuantity[] velZ_Quantities = vcSpeciesContextsSpec.getVelocityQuantities(QuantityComponent.Z);
boolean bVelZ_ExprIsNull = (velZ_Expr == null && velZ_Quantities.length == 0);
boolean bAdvectionNull = (bVelX_ExprIsNull && bVelY_ExprIsNull && bVelZ_ExprIsNull);
if (bDiffusionZero && bAdvectionNull) {
continue;
}
}
// for example, if scsParam is BC_Zm and if coordinateComponent 'ccZ' is null, no SBML parameter should be created for BC_Zm
if ((((role == SpeciesContextSpec.ROLE_BoundaryValueXm) || (role == SpeciesContextSpec.ROLE_BoundaryValueXp)) && (ccX == null)) || (((role == SpeciesContextSpec.ROLE_BoundaryValueYm) || (role == SpeciesContextSpec.ROLE_BoundaryValueYp)) && (ccY == null)) || (((role == SpeciesContextSpec.ROLE_BoundaryValueZm) || (role == SpeciesContextSpec.ROLE_BoundaryValueZp)) && (ccZ == null))) {
continue;
}
org.sbml.jsbml.Parameter sbmlParam = createSBMLParamFromSpeciesParam(vcSpeciesContexts[i], (SpeciesContextSpecParameter) scsParams[j]);
if (sbmlParam != null) {
BoundaryConditionType vcBCType_Xm = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeXm();
BoundaryConditionType vcBCType_Xp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeXp();
BoundaryConditionType vcBCType_Ym = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeYm();
BoundaryConditionType vcBCType_Yp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeYp();
BoundaryConditionType vcBCType_Zm = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeZm();
BoundaryConditionType vcBCType_Zp = vcSelectedSimContext.getGeometryContext().getStructureMapping(vcSpeciesContexts[i].getStructure()).getBoundaryConditionTypeZp();
SpatialParameterPlugin spplugin = (SpatialParameterPlugin) sbmlParam.getPlugin(SBMLUtils.SBML_SPATIAL_NS_PREFIX);
if (role == SpeciesContextSpec.ROLE_DiffusionRate) {
// set diffusionCoefficient element in SpatialParameterPlugin for param
DiffusionCoefficient sbmlDiffCoeff = new DiffusionCoefficient();
sbmlDiffCoeff.setVariable(vcSpeciesContexts[i].getName());
sbmlDiffCoeff.setDiffusionKind(DiffusionKind.isotropic);
sbmlDiffCoeff.setSpeciesRef(vcSpeciesContexts[i].getName());
spplugin.setParamType(sbmlDiffCoeff);
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueXm) && (ccX != null)) {
// set BoundaryCondn Xm element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCXm = new BoundaryCondition();
spplugin.setParamType(sbmlBCXm);
sbmlBCXm.setType(getBoundaryConditionKind(vcBCType_Xm));
sbmlBCXm.setVariable(vcSpeciesContexts[i].getName());
sbmlBCXm.setCoordinateBoundary(ccX.getBoundaryMinimum().getId());
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueXp) && (ccX != null)) {
// set BoundaryCondn Xp element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCXp = new BoundaryCondition();
spplugin.setParamType(sbmlBCXp);
sbmlBCXp.setType(getBoundaryConditionKind(vcBCType_Xp));
sbmlBCXp.setVariable(vcSpeciesContexts[i].getName());
// sbmlBCXp.setType(sm.getBoundaryConditionTypeXp().boundaryTypeStringValue());
sbmlBCXp.setCoordinateBoundary(ccX.getBoundaryMaximum().getId());
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueYm) && (ccY != null)) {
// set BoundaryCondn Ym element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCYm = new BoundaryCondition();
spplugin.setParamType(sbmlBCYm);
sbmlBCYm.setType(getBoundaryConditionKind(vcBCType_Yp));
sbmlBCYm.setVariable(vcSpeciesContexts[i].getName());
// sbmlBCYm.setType(sm.getBoundaryConditionTypeYm().boundaryTypeStringValue());
sbmlBCYm.setCoordinateBoundary(ccY.getBoundaryMinimum().getId());
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueYp) && (ccY != null)) {
// set BoundaryCondn Yp element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCYp = new BoundaryCondition();
spplugin.setParamType(sbmlBCYp);
sbmlBCYp.setType(getBoundaryConditionKind(vcBCType_Yp));
sbmlBCYp.setVariable(vcSpeciesContexts[i].getName());
// sbmlBCYp.setType(sm.getBoundaryConditionTypeYp().boundaryTypeStringValue());
sbmlBCYp.setCoordinateBoundary(ccY.getBoundaryMaximum().getId());
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueZm) && (ccZ != null)) {
// set BoundaryCondn Zm element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCZm = new BoundaryCondition();
spplugin.setParamType(sbmlBCZm);
sbmlBCZm.setType(getBoundaryConditionKind(vcBCType_Zm));
sbmlBCZm.setVariable(vcSpeciesContexts[i].getName());
// sbmlBCZm.setType(sm.getBoundaryConditionTypeZm().boundaryTypeStringValue());
sbmlBCZm.setCoordinateBoundary(ccZ.getBoundaryMinimum().getId());
}
if ((role == SpeciesContextSpec.ROLE_BoundaryValueZp) && (ccZ != null)) {
// set BoundaryCondn Zp element in SpatialParameterPlugin for param
BoundaryCondition sbmlBCZp = new BoundaryCondition();
spplugin.setParamType(sbmlBCZp);
sbmlBCZp.setType(getBoundaryConditionKind(vcBCType_Zp));
sbmlBCZp.setVariable(vcSpeciesContexts[i].getName());
// sbmlBCZp.setType(sm.getBoundaryConditionTypeZp().boundaryTypeStringValue());
sbmlBCZp.setCoordinateBoundary(ccZ.getBoundaryMaximum().getId());
}
if (role == SpeciesContextSpec.ROLE_VelocityX) {
// set advectionCoeff X element in SpatialParameterPlugin for param
AdvectionCoefficient sbmlAdvCoeffX = new AdvectionCoefficient();
spplugin.setParamType(sbmlAdvCoeffX);
sbmlAdvCoeffX.setVariable(vcSpeciesContexts[i].getName());
sbmlAdvCoeffX.setCoordinate(CoordinateKind.cartesianX);
}
if (role == SpeciesContextSpec.ROLE_VelocityY) {
// set advectionCoeff Y element in SpatialParameterPlugin for param
AdvectionCoefficient sbmlAdvCoeffY = new AdvectionCoefficient();
spplugin.setParamType(sbmlAdvCoeffY);
sbmlAdvCoeffY.setVariable(vcSpeciesContexts[i].getName());
sbmlAdvCoeffY.setCoordinate(CoordinateKind.cartesianY);
}
if (role == SpeciesContextSpec.ROLE_VelocityZ) {
// set advectionCoeff Z element in SpatialParameterPlugin for param
AdvectionCoefficient sbmlAdvCoeffZ = new AdvectionCoefficient();
spplugin.setParamType(sbmlAdvCoeffZ);
sbmlAdvCoeffZ.setVariable(vcSpeciesContexts[i].getName());
sbmlAdvCoeffZ.setCoordinate(CoordinateKind.cartesianZ);
}
}
// if sbmlParam != null
}
// if scsParams[j] != null
}
// end for scsParams
}
// end scsParams != null
}
// end if (bSpatial)
// Add the common name of species to annotation, and add an annotation element to the species.
// This is required later while trying to read in fluxes ...
// new Element(XMLTags.VCellRelatedInfoTag, sbml_vcml_ns);
Element sbmlImportRelatedElement = null;
// Element speciesElement = new Element(XMLTags.SpeciesTag, sbml_vcml_ns);
// speciesElement.setAttribute(XMLTags.NameAttrTag, TokenMangler.mangleToSName(vcSpeciesContexts[i].getSpecies().getCommonName()));
// sbmlImportRelatedElement.addContent(speciesElement);
// Get RDF annotation for species from SBMLAnnotationUtils
sbmlAnnotationUtil.writeAnnotation(vcSpeciesContexts[i].getSpecies(), sbmlSpecies, sbmlImportRelatedElement);
// Now set notes,
sbmlAnnotationUtil.writeNotes(vcSpeciesContexts[i].getSpecies(), sbmlSpecies);
}
}
use of cbit.vcell.mapping.MappingException in project vcell by virtualcell.
the class ServerDocumentManager method saveBioModel.
/**
* Insert the method's description here.
* Creation date: (10/28/00 12:08:30 AM)
*/
public String saveBioModel(QueryHashtable dbc, User user, String bioModelXML, String newName, String[] independentSims) throws DataAccessException, java.sql.SQLException, java.beans.PropertyVetoException, MappingException, cbit.vcell.xml.XmlParseException {
long start = System.currentTimeMillis();
//
// this invokes "update" on the database layer
//
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML));
forceDeepDirtyIfForeign(user, bioModel);
boolean isSaveAsNew = true;
//
if (newName != null) {
try {
bioModel.setName(newName);
} catch (java.beans.PropertyVetoException e) {
lg.error(e.getLocalizedMessage(), e);
throw new DataAccessException("couldn't set new name for BioModel: " + e.getMessage());
}
} else {
isSaveAsNew = false;
}
Version oldVersion = bioModel.getVersion();
BioModel origBioModel = null;
if (oldVersion != null) {
try {
String origBioModelXML = getBioModelXML(dbc, user, oldVersion.getVersionKey(), false);
origBioModel = XmlHelper.XMLToBioModel(new XMLSource(origBioModelXML));
} catch (ObjectNotFoundException nfe) {
if (isSaveAsNew) {
User foceClearVersionUser = new User("foceClearVersionUser", new KeyValue("0"));
forceDeepDirtyIfForeign(foceClearVersionUser, bioModel);
} else {
throw new DataAccessException("Stored model has been changed or removed, please use 'Save As..'");
}
}
}
boolean bSomethingChanged = false;
//
// verify that there are no orphaned Simulations (that belonged to Applications that have null mathDescriptions ... incomplete mappings)
//
// the workspace is responsible for cleaning up Simulations
//
{
Simulation[] sims = bioModel.getSimulations();
SimulationContext[] scs = bioModel.getSimulationContexts();
for (int i = 0; sims != null && i < sims.length; i++) {
boolean bFound = false;
for (int j = 0; scs != null && j < scs.length; j++) {
if (scs[j].getMathDescription() == sims[i].getMathDescription()) {
bFound = true;
}
}
if (!bFound) {
throw new RuntimeException("Error: Simulation " + sims[i].getName() + " cannot be saved, no Application exists with same MathDescription");
}
}
}
//
// UPDATE AND SUBSTITUTE FROM BOTTOM UP
//
// Image->Geometry
// Geometry->SimContext,MathDescription
// MathDescription->Simulation,SimulationContext
// Model->BioModel
// Simulation->BioModel
// SimContext->BioModel
// VCMetaData->BioModel
//
Simulation[] simArray = bioModel.getSimulations();
SimulationContext[] scArray = bioModel.getSimulationContexts();
// Hashtable mathEquivHash = new Hashtable();
long roundtripTimer = 0;
long l1 = 0;
long l2 = 0;
//
// for each image (anywhere in document):
// save if necessary (only once) and store saved instance in hashTable
//
Hashtable<Versionable, Versionable> memoryToDatabaseHash = new Hashtable<Versionable, Versionable>();
for (int i = 0; scArray != null && i < scArray.length; i++) {
VCImage memoryImage = scArray[i].getGeometry().getGeometrySpec().getImage();
if (memoryImage != null) {
if (!memoryToDatabaseHash.containsKey(memoryImage)) {
//
// didn't evaluate this image yet.
//
// defaults to unchanged
memoryToDatabaseHash.put(memoryImage, memoryImage);
if (memoryImage.getKey() != null && memoryImage.getVersion().getName().equals(memoryImage.getName())) {
//
// if image had previously been saved, not been forced 'dirty', and name not changed
// compare with original image to see if "update" is required.
//
VCImage databaseImage = null;
if (origBioModel != null) {
for (int j = 0; j < origBioModel.getNumSimulationContexts(); j++) {
VCImage origImage = origBioModel.getSimulationContext(j).getGeometry().getGeometrySpec().getImage();
if (origImage != null && origImage.getKey().equals(memoryImage.getKey())) {
databaseImage = origImage;
}
}
}
if (databaseImage == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseImage = dbServer.getDBTopLevel().getVCImage(dbc, user, memoryImage.getKey(), false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
if (databaseImage != null && !databaseImage.compareEqual(memoryImage)) {
KeyValue updatedImageKey = dbServer.getDBTopLevel().updateVersionable(user, memoryImage, false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryImage, updatedImage);
bSomethingChanged = true;
}
} else {
//
// Image hasn't been saved, has been renamed, or has been forced 'dirty'
// insert it with a unique name
//
int count = 0;
fixNullImageName(memoryImage);
while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.VCImage, memoryImage.getName(), true)) {
try {
memoryImage.setName(TokenMangler.getNextRandomToken(memoryImage.getName()));
} catch (java.beans.PropertyVetoException e) {
lg.error(e.getLocalizedMessage(), e);
}
if (count++ > 5) {
throw new DataAccessException("failed to find unique image name '" + memoryImage.getName() + "' is last name tried");
}
}
KeyValue updatedImageKey = dbServer.getDBTopLevel().insertVersionable(user, memoryImage, memoryImage.getName(), false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryImage, updatedImage);
bSomethingChanged = true;
}
}
}
}
//
for (int i = 0; scArray != null && i < scArray.length; i++) {
Geometry memoryGeometry = scArray[i].getGeometry();
if (!memoryToDatabaseHash.containsKey(memoryGeometry)) {
//
// didn't evaluate this geometry yet.
//
// defaults to unchanged
memoryToDatabaseHash.put(memoryGeometry, memoryGeometry);
boolean bMustSaveGeometry = false;
VCImage geometryImage = memoryGeometry.getGeometrySpec().getImage();
if (geometryImage != null && memoryToDatabaseHash.get(geometryImage) != geometryImage) {
//
// image had changed and was saved, load saved image into geometry and force a save of this geometry.
//
memoryGeometry.getGeometrySpec().setImage((VCImage) memoryToDatabaseHash.get(geometryImage));
geometryImage = (VCImage) memoryToDatabaseHash.get(geometryImage);
bMustSaveGeometry = true;
}
if (memoryGeometry.getKey() != null && memoryGeometry.getVersion().getName().equals(memoryGeometry.getName())) {
if (!bMustSaveGeometry) {
//
// if geometry had previously been saved, not been forced 'dirty', and name not changed
// compare with original geometry to see if "update" is required.
//
Geometry databaseGeometry = null;
if (origBioModel != null) {
for (int j = 0; j < origBioModel.getNumSimulationContexts(); j++) {
Geometry origGeometry = origBioModel.getSimulationContext(j).getGeometry();
if (origGeometry != null && origGeometry.getKey().equals(memoryGeometry.getKey())) {
databaseGeometry = origGeometry;
}
}
}
if (databaseGeometry == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, memoryGeometry.getKey(), false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
if (databaseGeometry != null && !databaseGeometry.compareEqual(memoryGeometry)) {
bMustSaveGeometry = true;
}
if (!bMustSaveGeometry && memoryGeometry.getDimension() > 0) {
GeometrySurfaceDescription geomSurfDescr = memoryGeometry.getGeometrySurfaceDescription();
SurfaceClass[] surfClassArr = geomSurfDescr.getSurfaceClasses();
for (int j = 0; surfClassArr != null && j < surfClassArr.length; j++) {
if (surfClassArr[j].getKey() == null) {
bMustSaveGeometry = true;
break;
}
}
}
}
if (bMustSaveGeometry) {
KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
KeyValue updatedGeometryKey = dbServer.getDBTopLevel().updateVersionable(dbc, user, memoryGeometry, updatedImageKey, false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
bSomethingChanged = true;
}
} else {
//
// Geometry hasn't been saved, has been renamed, or has been forced 'dirty'
// insert it with a unique name
//
int count = 0;
while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.Geometry, memoryGeometry.getName(), true)) {
try {
memoryGeometry.setName(TokenMangler.getNextRandomToken(memoryGeometry.getName()));
} catch (java.beans.PropertyVetoException e) {
lg.error(e.getLocalizedMessage(), e);
}
if (count++ > 5) {
throw new DataAccessException("failed to find unique geometry name '" + memoryGeometry.getName() + "' is last name tried");
}
}
KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
KeyValue updatedGeometryKey = dbServer.getDBTopLevel().insertVersionable(dbc, user, memoryGeometry, updatedImageKey, memoryGeometry.getName(), false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
bSomethingChanged = true;
}
}
}
//
// for each MathDescription in document:
// substitute saved geometry's into SimulationContext and
// save SimulationContext if necessary (only once) and store saved instance in hashtable.
//
Hashtable<MathDescription, MathCompareResults> mathEquivalencyHash = new Hashtable<MathDescription, MathCompareResults>();
for (int i = 0; scArray != null && i < scArray.length; i++) {
MathDescription memoryMathDescription = scArray[i].getMathDescription();
if (!memoryToDatabaseHash.containsKey(memoryMathDescription)) {
//
// didn't evaluate this SimulationContext yet.
//
// defaults to unchanged
memoryToDatabaseHash.put(memoryMathDescription, memoryMathDescription);
boolean bMustSaveMathDescription = false;
Geometry scGeometry = memoryMathDescription.getGeometry();
if (scGeometry != null && memoryToDatabaseHash.get(scGeometry) != scGeometry) {
//
// geometry had changed and was saved, load saved geometry into SimulationContext (and it's MathDescription) and force a save of this SimulationContext.
//
memoryMathDescription.setGeometry((Geometry) memoryToDatabaseHash.get(scGeometry));
bMustSaveMathDescription = true;
}
MathDescription databaseMathDescription = null;
if (memoryMathDescription.getKey() != null) {
//
if (origBioModel != null) {
for (int j = 0; j < origBioModel.getNumSimulationContexts(); j++) {
MathDescription math = origBioModel.getSimulationContext(j).getMathDescription();
if (math.getKey().equals(memoryMathDescription.getKey())) {
databaseMathDescription = math;
}
}
}
if (databaseMathDescription == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, memoryMathDescription.getKey());
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
MathDescription.updateReservedSymbols(databaseMathDescription, (origBioModel == null ? bioModel : origBioModel).getModel().getReservedSymbols());
if (databaseMathDescription != null && !databaseMathDescription.compareEqual(memoryMathDescription)) {
bMustSaveMathDescription = true;
}
} else {
bMustSaveMathDescription = true;
}
if (bMustSaveMathDescription) {
MathCompareResults mathCompareResults = null;
if (databaseMathDescription != null) {
try {
mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), memoryMathDescription, databaseMathDescription);
if (mathCompareResults != null && !mathCompareResults.isEquivalent() && (mathCompareResults.decision.equals(Decision.MathDifferent_DIFFERENT_NUMBER_OF_VARIABLES) || mathCompareResults.decision.equals(Decision.MathDifferent_VARIABLE_NOT_FOUND_AS_FUNCTION))) {
//
// if there is a different number of variables or cannot find variables by name (even considering change of state variables)
// then try the VCell 4.8 generated math.
//
MathDescription mathDesc_4_8 = new MathMapping_4_8(scArray[i]).getMathDescription();
mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), mathDesc_4_8, databaseMathDescription);
}
} catch (Exception e) {
lg.error(e.getLocalizedMessage(), e);
mathCompareResults = new MathCompareResults(Decision.MathDifferent_FAILURE_UNKNOWN, "Exception: '" + e.getMessage() + "'");
if (lg.isTraceEnabled()) {
lg.trace("FAILED TO COMPARE THE FOLLOWING MATH DESCRIPTIONS");
try {
lg.trace("MemoryMathDescription:\n" + ((memoryMathDescription != null) ? (memoryMathDescription.getVCML_database()) : ("null")));
lg.trace("DatabaseMathDescription:\n" + ((databaseMathDescription != null) ? (databaseMathDescription.getVCML_database()) : ("null")));
} catch (Exception e2) {
lg.error(e2.getLocalizedMessage(), e2);
}
}
}
} else {
mathCompareResults = new MathCompareResults(Decision.MathDifferent_NOT_SAVED);
}
//
// MathDescription hasn't been saved, has been renamed, or has been forced 'dirty'
// insert it with a any name (doens't have to be unique ... mathDescription is not a top-level versionable).
//
KeyValue updatedGeometryKey = memoryMathDescription.getGeometry().getKey();
KeyValue updatedMathDescriptionKey = null;
if (memoryMathDescription.getVersion() != null && memoryMathDescription.getVersion().getName().equals(memoryMathDescription.getName())) {
updatedMathDescriptionKey = dbServer.getDBTopLevel().updateVersionable(user, memoryMathDescription, updatedGeometryKey, false, true);
} else {
updatedMathDescriptionKey = dbServer.getDBTopLevel().insertVersionable(user, memoryMathDescription, updatedGeometryKey, memoryMathDescription.getName(), false, true);
}
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
MathDescription updatedMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, updatedMathDescriptionKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryMathDescription, updatedMathDescription);
mathEquivalencyHash.put(updatedMathDescription, mathCompareResults);
bSomethingChanged = true;
} else {
mathEquivalencyHash.put(memoryMathDescription, new MathCompareResults(Decision.MathEquivalent_SAME_MATHDESC_AS_IN_DB));
}
}
}
//
// update physiology
//
{
Model memoryModel = bioModel.getModel();
// preload with unchanged.
memoryToDatabaseHash.put(memoryModel, memoryModel);
if (memoryModel.getKey() != null && memoryModel.getVersion().getName().equals(memoryModel.getName())) {
//
// if Model had previously been saved, not been forced 'dirty', and name not changed
// compare with original Model to see if "update" is required.
//
Model databaseModel = null;
if (origBioModel != null) {
if (origBioModel.getModel().getKey().equals(memoryModel.getKey())) {
databaseModel = origBioModel.getModel();
}
}
if (databaseModel == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseModel = dbServer.getDBTopLevel().getModel(dbc, user, memoryModel.getKey());
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
if (databaseModel != null && !databaseModel.compareEqual(memoryModel)) {
KeyValue updatedModelKey = dbServer.getDBTopLevel().updateVersionable(user, memoryModel, false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
Model updatedModel = dbServer.getDBTopLevel().getModel(dbc, user, updatedModelKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryModel, updatedModel);
bSomethingChanged = true;
}
} else {
//
// Model hasn't been saved, has been renamed, or has been forced 'dirty'
// insert it with a any name (doens't have to be unique ... mathDescription is not a top-level versionable).
//
KeyValue updatedModelKey = dbServer.getDBTopLevel().insertVersionable(user, memoryModel, memoryModel.getName(), false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
Model updatedModel = dbServer.getDBTopLevel().getModel(dbc, user, updatedModelKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
memoryToDatabaseHash.put(memoryModel, updatedModel);
bSomethingChanged = true;
}
}
//
for (int i = 0; scArray != null && i < scArray.length; i++) {
SimulationContext memorySimContext = scArray[i];
if (!memoryToDatabaseHash.containsKey(memorySimContext)) {
//
// didn't evaluate this SimulationContext yet.
//
// defaults to unchanged
memoryToDatabaseHash.put(memorySimContext, memorySimContext);
boolean bMustSaveSimContext = false;
Geometry scGeometry = memorySimContext.getGeometry();
if (scGeometry != null && memoryToDatabaseHash.get(scGeometry) != scGeometry) {
//
// geometry had changed and was saved, load saved geometry into SimulationContext (and force a save)
//
memorySimContext.setGeometry((Geometry) memoryToDatabaseHash.get(scGeometry));
bMustSaveSimContext = true;
}
MathDescription scMathDescription = memorySimContext.getMathDescription();
if (scMathDescription != null && memoryToDatabaseHash.get(scMathDescription) != scMathDescription) {
//
// mathDescription had changed and was saved, load saved mathDescription into SimulationContext (and force a save)
//
memorySimContext.setMathDescription((MathDescription) memoryToDatabaseHash.get(scMathDescription));
bMustSaveSimContext = true;
}
Model scModel = memorySimContext.getModel();
if (scModel != null && memoryToDatabaseHash.get(scModel) != scModel) {
//
// model had changed and was saved, load saved model into SimulationContext (and force a save)
//
memorySimContext.setModel((Model) memoryToDatabaseHash.get(scModel));
bMustSaveSimContext = true;
}
if (memorySimContext.getKey() != null && memorySimContext.getVersion().getName().equals(memorySimContext.getName())) {
if (!bMustSaveSimContext) {
//
// if SimulationContext had previously been saved, not been forced 'dirty', and name not changed
// compare with original SimulationContext to see if "update" is required.
//
SimulationContext databaseSimContext = null;
if (origBioModel != null) {
for (int j = 0; j < origBioModel.getNumSimulationContexts(); j++) {
if (origBioModel.getSimulationContext(j).getKey().equals(memorySimContext.getKey())) {
databaseSimContext = origBioModel.getSimulationContext(j);
}
}
}
if (databaseSimContext == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseSimContext = dbServer.getDBTopLevel().getSimulationContext(dbc, user, memorySimContext.getKey());
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
if (databaseSimContext != null && !databaseSimContext.compareEqual(memorySimContext)) {
bMustSaveSimContext = true;
}
}
if (bMustSaveSimContext) {
KeyValue updatedGeometryKey = memorySimContext.getGeometry().getKey();
KeyValue updatedMathDescriptionKey = memorySimContext.getMathDescription().getKey();
Model updatedModel = memorySimContext.getModel();
KeyValue updatedSimContextKey = dbServer.getDBTopLevel().updateVersionable(user, memorySimContext, updatedMathDescriptionKey, updatedModel, updatedGeometryKey, false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
SimulationContext updatedSimContext = dbServer.getDBTopLevel().getSimulationContext(dbc, user, updatedSimContextKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
//
// make sure mathDescription is a single reference (for this app and all of it's Simulations).
//
updatedSimContext.setMathDescription((MathDescription) memorySimContext.getMathDescription());
memoryToDatabaseHash.put(memorySimContext, updatedSimContext);
bSomethingChanged = true;
}
} else {
//
// SimulationContext hasn't been saved, has been renamed, or has been forced 'dirty'
//
KeyValue updatedGeometryKey = memorySimContext.getGeometry().getKey();
KeyValue updatedMathDescriptionKey = memorySimContext.getMathDescription().getKey();
Model updatedModel = memorySimContext.getModel();
KeyValue updatedSimContextKey = dbServer.getDBTopLevel().insertVersionable(user, memorySimContext, updatedMathDescriptionKey, updatedModel, updatedGeometryKey, memorySimContext.getName(), false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
SimulationContext updatedSimContext = dbServer.getDBTopLevel().getSimulationContext(dbc, user, updatedSimContextKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
//
// make sure mathDescription is a single reference (for this app and all of it's Simulations).
//
updatedSimContext.setMathDescription((MathDescription) memorySimContext.getMathDescription());
memoryToDatabaseHash.put(memorySimContext, updatedSimContext);
bSomethingChanged = true;
}
}
}
//
for (int i = 0; simArray != null && i < simArray.length; i++) {
Simulation memorySimulation = simArray[i];
if (!memoryToDatabaseHash.containsKey(memorySimulation)) {
//
// didn't evaluate this Simulation yet.
//
// defaults to unchanged
memoryToDatabaseHash.put(memorySimulation, memorySimulation);
boolean bMustSaveSimulation = false;
MathDescription simMathDescription = memorySimulation.getMathDescription();
if (simMathDescription != null && memoryToDatabaseHash.get(simMathDescription) != simMathDescription) {
if (memoryToDatabaseHash.get(simMathDescription) != null) {
// make sure mathDescription hasn't already propagated (newer math won't be in hashtable)
//
// mathDescription had changed and was saved, load saved mathDescription into Simulation (and force a save)
//
memorySimulation.setMathDescription((MathDescription) memoryToDatabaseHash.get(simMathDescription));
bMustSaveSimulation = true;
}
}
Simulation databaseSimulation = null;
//
if (memorySimulation.getKey() != null) {
if (origBioModel != null) {
for (int j = 0; j < origBioModel.getNumSimulations(); j++) {
if (origBioModel.getSimulation(j).getKey().equals(memorySimulation.getKey())) {
databaseSimulation = origBioModel.getSimulation(j);
}
}
}
if (databaseSimulation == null) {
//
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
databaseSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, memorySimulation.getKey());
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
if (databaseSimulation != null) {
if (!memorySimulation.compareEqual(databaseSimulation)) {
bMustSaveSimulation = true;
}
}
if (!memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
// name was changed.
bMustSaveSimulation = true;
}
} else {
// never been saved.
bMustSaveSimulation = true;
}
if (bMustSaveSimulation) {
boolean bMathematicallyEquivalent = false;
if (databaseSimulation != null) {
//
// if to be forced "independent", then set equivalent to false
//
boolean bForceIndependent = false;
for (int j = 0; independentSims != null && j < independentSims.length; j++) {
if (independentSims[j].equals(memorySimulation.getName())) {
bForceIndependent = true;
}
}
//
// check for math equivalency first
//
MathCompareResults mathCompareResults = mathEquivalencyHash.get(memorySimulation.getMathDescription());
bMathematicallyEquivalent = !bForceIndependent && Simulation.testEquivalency(memorySimulation, databaseSimulation, mathCompareResults);
//
if (bMathematicallyEquivalent) {
VCSimulationIdentifier vcSimulationIdentifier = databaseSimulation.getSimulationInfo().getAuthoritativeVCSimulationIdentifier();
SimulationStatusPersistent simStatus = dbServer.getSimulationStatus(vcSimulationIdentifier.getSimulationKey());
if (simStatus == null || !simStatus.getHasData()) {
bMathematicallyEquivalent = false;
}
}
}
KeyValue updatedMathDescriptionKey = memorySimulation.getMathDescription().getKey();
KeyValue updatedSimulationKey = null;
if (memorySimulation.getKey() != null && memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
// name not changed, update simulation (but pass in database Simulation to check for parent-equivalence)
updatedSimulationKey = dbServer.getDBTopLevel().updateVersionable(user, memorySimulation, updatedMathDescriptionKey, false, bMathematicallyEquivalent, true);
} else {
// name changed, insert simulation (but pass in database Simulation to check for parent-equivalence)
updatedSimulationKey = dbServer.getDBTopLevel().insertVersionable(user, memorySimulation, updatedMathDescriptionKey, memorySimulation.getName(), false, bMathematicallyEquivalent, true);
}
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
Simulation updatedSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, updatedSimulationKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
//
// make sure mathDescription is a single reference (for an app and all of it's Simulations).
//
updatedSimulation.setMathDescription((MathDescription) memorySimulation.getMathDescription());
memoryToDatabaseHash.put(memorySimulation, updatedSimulation);
bSomethingChanged = true;
}
}
}
boolean bMustSaveVCMetaData = false;
if (origBioModel != null) {
//
// for the VCMetaData in the document:
// save VCMetaData if necessary (only once) and store saved instance in hashtable.
//
// The persisted VCMetaData doesn't have any foreign keys
// (when annotating a simulation ... we don't point to the simulation,
// we use the text-based VCID that is stored in URIBindingList in the XML serialization
//
// Therefore, there are no additional dependencies that we have to update during the
// incremental save and force propagation to save the VCMetaData.
//
VCMetaData memoryVCMetaData = bioModel.getVCMetaData();
VCMetaData databaseVCMetaData = origBioModel.getVCMetaData();
//
if (databaseVCMetaData == null || !databaseVCMetaData.compareEquals(memoryVCMetaData)) {
bMustSaveVCMetaData = true;
bSomethingChanged = true;
}
}
if (bSomethingChanged || origBioModel == null || !bioModel.compareEqual(origBioModel)) {
//
// create new BioModelMetaData and save to server
//
KeyValue modelKey = ((Model) memoryToDatabaseHash.get(bioModel.getModel())).getKey();
KeyValue[] scKeys = new KeyValue[bioModel.getNumSimulationContexts()];
for (int i = 0; i < bioModel.getNumSimulationContexts(); i++) {
scKeys[i] = ((SimulationContext) memoryToDatabaseHash.get(bioModel.getSimulationContext(i))).getKey();
}
KeyValue[] simKeys = new KeyValue[bioModel.getNumSimulations()];
for (int i = 0; i < bioModel.getNumSimulations(); i++) {
simKeys[i] = ((Simulation) memoryToDatabaseHash.get(bioModel.getSimulation(i))).getKey();
}
// @TODO Add VC_METADATA table ... pointed to by VC_BIOMODEL (metadataref on delete cascade)
// @TODO Write script to populate VC_METADATA from VC_MIRIAM
// @TODO save VCMetaData from this BioModel into VC_METADATA .. stick in memoryToDatabaseHash
//
BioModelMetaData bioModelMetaData = null;
String vcMetaDataXML = XmlHelper.vcMetaDataToXML(bioModel.getVCMetaData(), bioModel);
if (oldVersion == null) {
bioModelMetaData = new BioModelMetaData(modelKey, scKeys, simKeys, vcMetaDataXML, bioModel.getName(), bioModel.getDescription());
} else {
bioModelMetaData = new BioModelMetaData(oldVersion, modelKey, scKeys, simKeys, vcMetaDataXML);
if (!bioModel.getDescription().equals(oldVersion.getAnnot())) {
try {
bioModelMetaData.setDescription(bioModel.getDescription());
} catch (java.beans.PropertyVetoException e) {
lg.error(e.getLocalizedMessage(), e);
}
}
}
// bioModelMetaData.setMIRIAMAnnotation(bioModel.getMIRIAMAnnotation());
BioModelMetaData updatedBioModelMetaData = null;
if (bioModel.getVersion() == null || !bioModel.getVersion().getName().equals(bioModel.getName())) {
KeyValue updatedBioModelKey = dbServer.getDBTopLevel().insertVersionable(user, bioModelMetaData, null, /*hack*/
bioModel.getName(), false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
updatedBioModelMetaData = dbServer.getDBTopLevel().getBioModelMetaData(dbc, user, updatedBioModelKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
} else {
KeyValue updatedBioModelKey = dbServer.getDBTopLevel().updateVersionable(user, bioModelMetaData, null, /*hack*/
false, true);
if (lg.isTraceEnabled()) {
l1 = System.currentTimeMillis();
}
updatedBioModelMetaData = dbServer.getDBTopLevel().getBioModelMetaData(dbc, user, updatedBioModelKey);
if (lg.isTraceEnabled()) {
l2 = System.currentTimeMillis();
roundtripTimer += l2 - l1;
}
}
//
// (THIS IS THE REALLY SCAREY PART...NOT GETTING A FRESH VIEW OF EVERYTING FROM THE DATABASE FOR CREATING THE XML)
//
// bioModelXML = getBioModelXML(user,updatedBioModelMetaData.getVersion().getVersionKey());
BioModel updatedBioModel = new BioModel(updatedBioModelMetaData.getVersion());
// updatedBioModel.setMIRIAMAnnotation(updatedBioModelMetaData.getMIRIAMAnnotation());
updatedBioModel.setModel((Model) memoryToDatabaseHash.get(bioModel.getModel()));
for (int i = 0; i < bioModel.getNumSimulationContexts(); i++) {
updatedBioModel.addSimulationContext((SimulationContext) memoryToDatabaseHash.get(bioModel.getSimulationContext(i)));
}
for (int i = 0; i < bioModel.getNumSimulations(); i++) {
updatedBioModel.addSimulation((Simulation) memoryToDatabaseHash.get(bioModel.getSimulation(i)));
}
updatedBioModel.setVCMetaData(XmlHelper.xmlToVCMetaData(updatedBioModel.getVCMetaData(), updatedBioModel, vcMetaDataXML));
// TODO must replace this with proper persistance.
updatedBioModel.getPathwayModel().merge(bioModel.getPathwayModel());
updatedBioModel.getRelationshipModel().merge(bioModel.getRelationshipModel());
bioModelXML = cbit.vcell.xml.XmlHelper.bioModelToXML(updatedBioModel);
dbServer.insertVersionableChildSummary(user, VersionableType.BioModelMetaData, updatedBioModel.getVersion().getVersionKey(), updatedBioModel.createBioModelChildSummary().toDatabaseSerialization());
dbServer.insertVersionableXML(user, VersionableType.BioModelMetaData, updatedBioModel.getVersion().getVersionKey(), bioModelXML);
if (lg.isTraceEnabled()) {
lg.trace("Total time: " + ((double) (System.currentTimeMillis() - start)) / 1000 + " roundtrip: " + ((double) roundtripTimer) / 1000);
}
return bioModelXML;
} else {
if (lg.isTraceEnabled()) {
lg.trace("Total time: " + ((double) (System.currentTimeMillis() - start)) / 1000 + " roundtrip: " + ((double) roundtripTimer) / 1000);
}
// If we got here were were doing 'save' or 'save as new version' but no changes were detected
boolean bError = !bSomethingChanged && !isSaveAsNew;
if (bError) {
throw new ServerRejectedSaveException(origBioModel.getVersion().getVersionKey().toString());
}
return bioModelXML;
}
}
Aggregations