use of cbit.sql.QueryHashtable in project vcell by virtualcell.
the class RestDatabaseService method saveSimulation.
public SimulationSaveResponse saveSimulation(BiomodelSimulationSaveServerResource resource, User vcellUser, List<OverrideRepresentation> overrideRepresentations) throws PermissionException, ObjectNotFoundException, DataAccessException, SQLException, XmlParseException, PropertyVetoException, MappingException, ExpressionException {
String simId = resource.getAttribute(VCellApiApplication.SIMULATIONID);
KeyValue simKey = new KeyValue(simId);
SimulationRep simRep = getSimulationRep(simKey);
if (simRep == null) {
throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
}
boolean myModel = simRep.getOwner().compareEqual(vcellUser);
// get the bioModel
String biomodelId = resource.getAttribute(VCellApiApplication.BIOMODELID);
KeyValue biomodelKey = new KeyValue(biomodelId);
BigString bioModelXML = this.databaseServerImpl.getBioModelXML(vcellUser, biomodelKey);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
// copy the simulation as new
Simulation origSimulation = null;
for (Simulation sim : bioModel.getSimulations()) {
if (sim.getKey().equals(simKey)) {
origSimulation = sim;
}
}
if (origSimulation == null) {
throw new RuntimeException("cannot find original Simulation");
}
SimulationContext simContext = bioModel.getSimulationContext(origSimulation);
Simulation newUnsavedSimulation = simContext.copySimulation(origSimulation);
// make appropriate changes
// MATH OVERRIDES
MathOverrides mathOverrides = new MathOverrides(newUnsavedSimulation);
for (OverrideRepresentation overrideRep : overrideRepresentations) {
overrideRep.applyMathOverrides(mathOverrides);
}
newUnsavedSimulation.setMathOverrides(mathOverrides);
// save bioModel
String editedBioModelXML = XmlHelper.bioModelToXML(bioModel);
ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.databaseServerImpl);
String modelName = bioModel.getName();
if (!myModel) {
modelName = modelName + "_" + Math.abs(new Random().nextInt());
}
String newBioModelXML = serverDocumentManager.saveBioModel(new QueryHashtable(), vcellUser, editedBioModelXML, modelName, null);
BioModel savedBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBioModelXML));
Simulation savedSimulation = null;
for (Simulation sim : savedBioModel.getSimulations()) {
if (sim.getName().equals(newUnsavedSimulation.getName())) {
savedSimulation = sim;
}
}
if (savedSimulation == null) {
throw new RuntimeException("cannot find new Simulation");
}
return new SimulationSaveResponse(savedBioModel, savedSimulation);
}
use of cbit.sql.QueryHashtable in project vcell by virtualcell.
the class MathVerifier method testDocumentLoad.
private void testDocumentLoad(boolean bUpdateDatabase, User user, VCDocumentInfo documentInfo, VCDocument vcDocumentFromDBCache) {
KeyValue versionKey = documentInfo.getVersion().getVersionKey();
// try{
// String vcDocumentXMLFromDBCacheRoundtrip = null;
// Compare self same
Exception bSameCachedAndNotCachedXMLExc = null;
Exception bSameCachedAndNotCachedObjExc = null;
Exception bSameSelfXMLCachedRoundtripExc = null;
Boolean bSameCachedAndNotCachedXML = null;
Boolean bSameCachedAndNotCachedObj = null;
Boolean bSameSelfCachedRoundtrip = null;
// Boolean bSameSelfObjCachedRoundTrip = null;
long startTime = 0;
// Long compareXMLTime = null;
// Long compareObjTime = null;
// Long loadOriginalXMLTime = null;
Long loadUnresolvedTime = null;
if (documentInfo instanceof BioModelInfo) {
Level existingLogLevel = VCMLComparator.getLogLevel();
try {
String xmlRndTrip0 = XmlHelper.bioModelToXML((BioModel) vcDocumentFromDBCache);
BioModel bioModelRndTrip0 = XmlHelper.XMLToBioModel(new XMLSource(xmlRndTrip0));
String xmlRndTrip1 = XmlHelper.bioModelToXML((BioModel) bioModelRndTrip0);
BioModel bioModelRndTrip1 = XmlHelper.XMLToBioModel(new XMLSource(xmlRndTrip1));
if (Compare.logger != null) {
Compare.loggingEnabled = true;
VCMLComparator.setLogLevel(Level.DEBUG);
}
bSameSelfCachedRoundtrip = VCMLComparator.compareEquals(xmlRndTrip0, xmlRndTrip1, true);
System.out.println("----------XML same=" + bSameSelfCachedRoundtrip);
boolean objectSame = bioModelRndTrip0.compareEqual(bioModelRndTrip1);
System.out.println("----------Objects same=" + objectSame);
bSameSelfCachedRoundtrip = bSameSelfCachedRoundtrip && objectSame;
} catch (Exception e) {
bSameSelfCachedRoundtrip = null;
lg.error(e.getMessage(), e);
bSameSelfXMLCachedRoundtripExc = e;
} finally {
Compare.loggingEnabled = false;
VCMLComparator.setLogLevel(existingLogLevel);
}
String fromDBBioModelUnresolvedXML = null;
try {
startTime = System.currentTimeMillis();
fromDBBioModelUnresolvedXML = dbServerImpl.getServerDocumentManager().getBioModelUnresolved(new QueryHashtable(), user, versionKey);
BioModel vcDocumentFromDBNotCached = XmlHelper.XMLToBioModel(new XMLSource(fromDBBioModelUnresolvedXML));
loadUnresolvedTime = System.currentTimeMillis() - startTime;
bSameCachedAndNotCachedObj = vcDocumentFromDBCache.compareEqual(vcDocumentFromDBNotCached);
} catch (Exception e) {
lg.error(e.getMessage(), e);
bSameCachedAndNotCachedObjExc = e;
}
if (fromDBBioModelUnresolvedXML != null) {
try {
String vcDocumentXMLFromDBCacheRegenerate = XmlHelper.bioModelToXML((BioModel) vcDocumentFromDBCache);
bSameCachedAndNotCachedXML = VCMLComparator.compareEquals(vcDocumentXMLFromDBCacheRegenerate, fromDBBioModelUnresolvedXML, true);
} catch (Exception e) {
lg.error(e.getMessage(), e);
bSameCachedAndNotCachedXMLExc = e;
}
}
} else {
Level existingLogLevel = VCMLComparator.getLogLevel();
try {
String xmlRndTrip0 = XmlHelper.mathModelToXML((MathModel) vcDocumentFromDBCache);
MathModel mathModelRndTrip0 = XmlHelper.XMLToMathModel(new XMLSource(xmlRndTrip0));
String xmlRndTrip1 = XmlHelper.mathModelToXML((MathModel) mathModelRndTrip0);
MathModel mathModelRndTrip1 = XmlHelper.XMLToMathModel(new XMLSource(xmlRndTrip1));
if (Compare.logger != null) {
Compare.loggingEnabled = true;
VCMLComparator.setLogLevel(Level.DEBUG);
}
bSameSelfCachedRoundtrip = VCMLComparator.compareEquals(xmlRndTrip0, xmlRndTrip1, true);
bSameSelfCachedRoundtrip = bSameSelfCachedRoundtrip && mathModelRndTrip0.compareEqual(mathModelRndTrip1);
} catch (Exception e) {
bSameSelfCachedRoundtrip = null;
lg.error(e.getMessage(), e);
bSameSelfXMLCachedRoundtripExc = e;
} finally {
Compare.loggingEnabled = false;
VCMLComparator.setLogLevel(existingLogLevel);
}
String fromDBMathModelUnresolvedXML = null;
try {
startTime = System.currentTimeMillis();
MathModel vcDocumentFromDBNotCached = dbServerImpl.getServerDocumentManager().getMathModelUnresolved(new QueryHashtable(), user, versionKey);
loadUnresolvedTime = System.currentTimeMillis() - startTime;
fromDBMathModelUnresolvedXML = XmlHelper.mathModelToXML(vcDocumentFromDBNotCached);
bSameCachedAndNotCachedObj = vcDocumentFromDBCache.compareEqual(vcDocumentFromDBNotCached);
} catch (Exception e) {
lg.error(e.getMessage(), e);
bSameCachedAndNotCachedObjExc = e;
}
if (fromDBMathModelUnresolvedXML != null) {
try {
String vcDocumentXMLFromDBCacheRegenerate = XmlHelper.mathModelToXML((MathModel) vcDocumentFromDBCache);
bSameCachedAndNotCachedXML = VCMLComparator.compareEquals(vcDocumentXMLFromDBCacheRegenerate, fromDBMathModelUnresolvedXML, true);
} catch (Exception e) {
lg.error(e.getMessage(), e);
bSameCachedAndNotCachedXMLExc = e;
}
}
}
if (bUpdateDatabase) {
updateLoadModelsStatTable_CompareTest(versionKey, null, /*loadOriginalXMLTime*/
loadUnresolvedTime, bSameCachedAndNotCachedXML, bSameCachedAndNotCachedObj, bSameSelfCachedRoundtrip, bSameCachedAndNotCachedXMLExc, bSameCachedAndNotCachedObjExc, bSameSelfXMLCachedRoundtripExc);
} else {
System.out.println("loadOriginalXMLTime=" + null + /*loadOriginalXMLTime*/
" loadUnresolvedTime=" + loadUnresolvedTime);
System.out.println("bSameCachedAndNotCachedXML=" + bSameCachedAndNotCachedXML + " bSameCachedAndNotCachedObj=" + bSameCachedAndNotCachedObj + " bSameSelfXMLCachedRoundtrip=" + bSameSelfCachedRoundtrip);
System.out.println("bSameCachedAndNotCachedXMLExc=" + bSameCachedAndNotCachedXMLExc + "\nbSameCachedAndNotCachedObjExc=" + bSameCachedAndNotCachedObjExc + "\nbSameSelfXMLCachedRoundtripExc=" + bSameSelfXMLCachedRoundtripExc);
System.out.println();
}
}
use of cbit.sql.QueryHashtable in project vcell by virtualcell.
the class MathVerifier method scan.
/**
* Insert the method's description here.
* Creation date: (2/2/01 3:40:29 PM)
*/
public void scan(User[] users, boolean bUpdateDatabase, KeyValue[] bioAndMathModelKeys) throws MathException, MappingException, SQLException, DataAccessException, ModelException, ExpressionException {
// java.util.Calendar calendar = java.util.GregorianCalendar.getInstance();
// // calendar.set(2002,java.util.Calendar.MAY,7+1);
// calendar.set(2002,java.util.Calendar.JULY,1);
// final java.util.Date fluxCorrectionOrDisablingBugFixDate = calendar.getTime();
// // calendar.set(2001,java.util.Calendar.JUNE,13+1);
// calendar.set(2002,java.util.Calendar.JANUARY,1);
// final java.util.Date totalVolumeCorrectionFixDate = calendar.getTime();
KeyValue[] sortedBioAndMathModelKeys = null;
if (bioAndMathModelKeys != null) {
sortedBioAndMathModelKeys = bioAndMathModelKeys.clone();
Arrays.sort(sortedBioAndMathModelKeys, keyValueCpmparator);
}
for (int i = 0; i < users.length; i++) {
User user = users[i];
BioModelInfo[] bioModelInfos0 = dbServerImpl.getBioModelInfos(user, false);
MathModelInfo[] mathModelInfos0 = dbServerImpl.getMathModelInfos(user, false);
if (lg.isTraceEnabled())
lg.trace("Testing user '" + user + "'");
Vector<VCDocumentInfo> userBioAndMathModelInfoV = new Vector<VCDocumentInfo>();
userBioAndMathModelInfoV.addAll(Arrays.asList(bioModelInfos0));
userBioAndMathModelInfoV.addAll(Arrays.asList(mathModelInfos0));
//
for (int j = 0; j < userBioAndMathModelInfoV.size(); j++) {
//
// if certain Bio or Math models are requested, then filter all else out
//
VCDocumentInfo documentInfo = userBioAndMathModelInfoV.elementAt(j);
KeyValue versionKey = documentInfo.getVersion().getVersionKey();
if (sortedBioAndMathModelKeys != null) {
int srch = Arrays.binarySearch(sortedBioAndMathModelKeys, versionKey, keyValueCpmparator);
if (srch < 0) {
continue;
}
}
if (!(documentInfo instanceof BioModelInfo)) {
continue;
}
//
if (skipHash.contains(versionKey)) {
System.out.println("skipping " + (documentInfo instanceof BioModelInfo ? "BioModel" : "MathModel") + " with key '" + versionKey + "'");
continue;
}
try {
//
// read in the BioModel and MathModel from the database
//
VCDocument vcDocumentFromDBCache = null;
BigString vcDocumentXMLFromDBCache = null;
try {
long startTime = System.currentTimeMillis();
if (documentInfo instanceof BioModelInfo) {
vcDocumentXMLFromDBCache = new BigString(dbServerImpl.getServerDocumentManager().getBioModelXML(new QueryHashtable(), user, versionKey, false));
vcDocumentFromDBCache = XmlHelper.XMLToBioModel(new XMLSource(vcDocumentXMLFromDBCache.toString()));
} else {
vcDocumentXMLFromDBCache = new BigString(dbServerImpl.getServerDocumentManager().getMathModelXML(new QueryHashtable(), user, versionKey, false));
vcDocumentFromDBCache = XmlHelper.XMLToMathModel(new XMLSource(vcDocumentXMLFromDBCache.toString()));
}
if (bUpdateDatabase && testFlag.equals(MathVerifier.MV_LOAD_XML)) {
updateLoadModelsStatTable_LoadTest(System.currentTimeMillis() - startTime, versionKey, null);
}
} catch (Exception e) {
lg.error(e.getMessage(), e);
if (bUpdateDatabase && testFlag.equals(MathVerifier.MV_LOAD_XML)) {
updateLoadModelsStatTable_LoadTest(0, versionKey, e);
}
}
if (testFlag.equals(MathVerifier.MV_LOAD_XML)) {
//
if (vcDocumentXMLFromDBCache != null) {
testDocumentLoad(bUpdateDatabase, user, documentInfo, vcDocumentFromDBCache);
}
} else if (testFlag.equals(MathVerifier.MV_DEFAULT)) {
//
if (vcDocumentFromDBCache instanceof BioModel) {
BioModel bioModel = (BioModel) vcDocumentFromDBCache;
checkMathForBioModel(vcDocumentXMLFromDBCache, bioModel, user, bUpdateDatabase);
}
}
} catch (Throwable e) {
// exception in whole BioModel
lg.error(e.getMessage(), e);
// can't update anything in database, since we don't know what simcontexts are involved
}
}
}
}
use of cbit.sql.QueryHashtable in project vcell by virtualcell.
the class DatabaseServerImpl method getVCImageXML.
/**
* getVersionable method comment.
*/
public BigString getVCImageXML(User user, KeyValue key) throws DataAccessException, ObjectNotFoundException {
try {
if (lg.isTraceEnabled())
lg.trace("DatabaseServerImpl.getVCImageXML(user=" + user + ", Key=" + key + ")");
boolean bCheckPermission = true;
VCImage image = dbTop.getVCImage(new QueryHashtable(), user, key, bCheckPermission);
return new BigString(XmlHelper.imageToXML(image));
} catch (ObjectNotFoundException e) {
lg.error(e.getMessage(), e);
throw new ObjectNotFoundException(e.getMessage());
} catch (Throwable e) {
lg.error(e.getMessage(), e);
throw new DataAccessException(e.getMessage());
}
}
use of cbit.sql.QueryHashtable in project vcell by virtualcell.
the class StochtestRunService method runOne.
public void runOne() throws IllegalArgumentException, SQLException, DataAccessException, XmlParseException, PropertyVetoException, ExpressionException, MappingException, GeometryException, ImageException, IOException {
StochtestRun stochtestRun = StochtestDbUtils.acceptNextWaitingStochtestRun(conFactory);
String biomodelXML = null;
if (stochtestRun != null) {
String networkGenProbs = null;
try {
User user = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.dbServerImpl);
biomodelXML = serverDocumentManager.getBioModelXML(new QueryHashtable(), user, stochtestRun.stochtest.biomodelRef, true);
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(biomodelXML));
bioModel.refreshDependencies();
SimulationContext srcSimContext = null;
for (SimulationContext sc : bioModel.getSimulationContexts()) {
if (sc.getKey().equals(stochtestRun.stochtest.simContextRef)) {
srcSimContext = sc;
}
}
if (srcSimContext == null) {
throw new RuntimeException("cannot find simcontext with key=" + stochtestRun.stochtest.simContextRef);
}
//
for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
scs.setConstant(false);
}
SimulationContext simContext = srcSimContext;
StochtestMathType parentMathType = stochtestRun.parentMathType;
StochtestMathType mathType = stochtestRun.mathType;
if (parentMathType != mathType) {
if (parentMathType == StochtestMathType.nonspatialstochastic && mathType == StochtestMathType.rules) {
simContext = SimulationContext.copySimulationContext(srcSimContext, "generatedRules", false, Application.RULE_BASED_STOCHASTIC);
} else if (parentMathType == StochtestMathType.rules && mathType == StochtestMathType.nonspatialstochastic) {
simContext = SimulationContext.copySimulationContext(srcSimContext, "generatedSSA", false, Application.NETWORK_STOCHASTIC);
} else {
throw new RuntimeException("unexpected copy of simcontext from " + parentMathType + " to " + mathType);
}
bioModel.addSimulationContext(simContext);
}
MathMappingCallback mathMappingCallback = new MathMappingCallback() {
@Override
public void setProgressFraction(float fractionDone) {
}
@Override
public void setMessage(String message) {
}
@Override
public boolean isInterrupted() {
return false;
}
};
MathMapping mathMapping = simContext.createNewMathMapping(mathMappingCallback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
MathDescription mathDesc = mathMapping.getMathDescription(mathMappingCallback);
simContext.setMathDescription(mathDesc);
if (simContext.isInsufficientIterations()) {
networkGenProbs = "insufficientIterations";
} else if (simContext.isInsufficientMaxMolecules()) {
networkGenProbs = "insufficientMaxMolecules";
}
File baseDirectory = StochtestFileUtils.createDirFile(baseDir, stochtestRun);
try {
OutputTimeSpec outputTimeSpec = new UniformOutputTimeSpec(0.5);
double endTime = 10.0;
computeTrials(simContext, stochtestRun, baseDirectory, outputTimeSpec, endTime, numTrials);
StochtestDbUtils.finalizeAcceptedStochtestRun(conFactory, stochtestRun, StochtestRun.StochtestRunStatus.complete, null, networkGenProbs);
} finally {
StochtestFileUtils.clearDir(baseDirectory);
}
} catch (Exception e) {
StochtestDbUtils.finalizeAcceptedStochtestRun(conFactory, stochtestRun, StochtestRun.StochtestRunStatus.failed, e.getMessage(), networkGenProbs);
//
if (biomodelXML != null) {
XmlUtil.writeXMLStringToFile(biomodelXML, new File(baseDir, "stochtestrun_" + stochtestRun.stochtest.key + ".vcml").getPath(), false);
}
//
// write exception trace to .txt file
//
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
printWriter.flush();
System.out.println(stringWriter.getBuffer().toString());
XmlUtil.writeXMLStringToFile(stringWriter.getBuffer().toString(), new File(baseDir, "stochtestrun_" + stochtestRun.stochtest.key + "_error.txt").getPath(), false);
}
} else {
System.out.println("no jobs waiting");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aggregations