use of cbit.vcell.xml.XMLSource 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.vcell.xml.XMLSource in project vcell by virtualcell.
the class BioModelEditor method createNewBiomodelFromApp.
private void createNewBiomodelFromApp() {
SimulationContext simulationContext = getSelectedSimulationContext();
if (simulationContext == null) {
PopupGenerator.showErrorDialog(this, "Please select an application.");
return;
}
// bioModel.getVCMetaData().cleanupMetadata();
try {
String newBMXML = XmlHelper.bioModelToXML(bioModel);
BioModel newBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBMXML));
newBioModel.clearVersion();
SimulationContext[] newBMSimcontexts = newBioModel.getSimulationContexts();
for (int i = 0; i < newBMSimcontexts.length; i++) {
if (!newBMSimcontexts[i].getName().equals(simulationContext.getName())) {
// Remove sims before removing simcontext
Simulation[] newBMSims = newBMSimcontexts[i].getSimulations();
for (int j = 0; j < newBMSims.length; j++) {
newBMSimcontexts[i].removeSimulation(newBMSims[j]);
}
newBioModel.removeSimulationContext(newBMSimcontexts[i]);
}
}
VCDocument.DocumentCreationInfo newBMDocCreateInfo = new VCDocument.DocumentCreationInfo(VCDocumentType.BIOMODEL_DOC, VCDocument.BIO_OPTION_DEFAULT);
newBMDocCreateInfo.setPreCreatedDocument(newBioModel);
AsynchClientTask[] newBMTasks = getBioModelWindowManager().newDocument(newBMDocCreateInfo);
ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), newBMTasks);
} catch (Exception e) {
e.printStackTrace();
PopupGenerator.showErrorDialog(this, "Error creating new BioModel from Application '" + simulationContext.getName() + "'\n" + e.getMessage());
}
}
use of cbit.vcell.xml.XMLSource in project vcell by virtualcell.
the class RestDatabaseService method query.
public SimulationRepresentation query(BiomodelSimulationServerResource resource, User vcellUser) throws SQLException, DataAccessException, ExpressionException, XmlParseException, MappingException, MathException, MatrixException, ModelException {
if (vcellUser == null) {
vcellUser = VCellApiApplication.DUMMY_USER;
}
ArrayList<String> conditions = new ArrayList<String>();
String bioModelID = (String) resource.getRequestAttributes().get(VCellApiApplication.BIOMODELID);
if (bioModelID != null) {
conditions.add("(" + BioModelTable.table.id.getQualifiedColName() + " = " + bioModelID + ")");
} else {
throw new RuntimeException(VCellApiApplication.BIOMODELID + " not specified");
}
StringBuffer conditionsBuffer = new StringBuffer();
for (String condition : conditions) {
if (conditionsBuffer.length() > 0) {
conditionsBuffer.append(" AND ");
}
conditionsBuffer.append(condition);
}
int startRow = 1;
int maxRows = 1;
BioModelRep[] bioModelReps = databaseServerImpl.getBioModelReps(vcellUser, conditionsBuffer.toString(), null, startRow, maxRows);
for (BioModelRep bioModelRep : bioModelReps) {
KeyValue[] simContextKeys = bioModelRep.getSimContextKeyList();
for (KeyValue scKey : simContextKeys) {
SimContextRep scRep = getSimContextRep(scKey);
if (scRep != null) {
bioModelRep.addSimContextRep(scRep);
}
}
KeyValue[] simulationKeys = bioModelRep.getSimKeyList();
for (KeyValue simKey : simulationKeys) {
SimulationRep simulationRep = getSimulationRep(simKey);
if (simulationRep != null) {
bioModelRep.addSimulationRep(simulationRep);
}
}
}
if (bioModelReps == null || bioModelReps.length != 1) {
//
// try to determine if the current credentials are insufficient, try to fetch BioModel again with administrator privilege.
//
User adminUser = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
BioModelRep[] allBioModelReps = databaseServerImpl.getBioModelReps(adminUser, conditionsBuffer.toString(), null, startRow, 1);
if (allBioModelReps != null && allBioModelReps.length >= 0) {
throw new PermissionException("insufficient privilege to retrive BioModel " + bioModelID);
} else {
throw new RuntimeException("failed to get biomodel");
}
}
String simulationId = (String) resource.getRequestAttributes().get(VCellApiApplication.SIMULATIONID);
if (simulationId == null) {
throw new RuntimeException(VCellApiApplication.SIMULATIONID + " not specified");
}
SimulationRep simRep = getSimulationRep(new KeyValue(simulationId));
BigString bioModelXML = databaseServerImpl.getBioModelXML(vcellUser, bioModelReps[0].getBmKey());
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
return new SimulationRepresentation(simRep, bioModel);
}
use of cbit.vcell.xml.XMLSource 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.vcell.xml.XMLSource 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
}
}
}
}
Aggregations