use of cbit.vcell.field.io.FieldDataFileOperationResults in project vcell by virtualcell.
the class FieldDataGUIPanel method jButtonFDFromSim_ActionPerformed.
private void jButtonFDFromSim_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
try {
final RequestManager clientRequestManager = fieldDataWindowManager.getLocalRequestManager();
final FieldDataWindowManager.OpenModelInfoHolder simInfoHolder = FieldDataWindowManager.selectOpenModelsFromDesktop(this, fieldDataWindowManager.getRequestManager(), true, "Select Simulation for Field Data", true);
if (simInfoHolder == null) {
PopupGenerator.showErrorDialog(this, "Please open a Bio or Math model containing the spatial (non-compartmental) simulation you wish to use to create a new Field Data");
return;
}
// Check that the sim is in a state that can be copied
final SimulationInfo simInfo = simInfoHolder.getSimInfo();
if (simInfo == null) {
throw new Exception("Selected sim '" + simInfoHolder.getSimName() + "' has no simInfo (save your model and retry).");
}
SimulationStatus simStatus = clientRequestManager.getServerSimulationStatus(simInfo);
if (simStatus != null && (simStatus.isRunning() || simStatus.isStartRequested())) {
throw new Exception("Can't copy 'running' simulation data from sim '" + simInfo.getName() + "'");
}
final FieldDataFileOperationSpec fdos = FieldDataFileOperationSpec.createCopySimFieldDataFileOperationSpec(null, (simInfo.getParentSimulationReference() != null ? simInfo.getParentSimulationReference() : simInfo.getSimulationVersion().getVersionKey()), simInfo.getOwner(), simInfoHolder.getJobIndex(), clientRequestManager.getDocumentManager().getUser());
AsynchClientTask[] addTasks = addNewExternalData(this, this, true);
AsynchClientTask[] taskArray = new AsynchClientTask[1 + addTasks.length];
// add to the end
System.arraycopy(addTasks, 0, taskArray, 1, addTasks.length);
taskArray[0] = new AsynchClientTask("Create Field Data from Simulation", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {
FieldDataFileOperationResults fdor = clientRequestManager.getDocumentManager().fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec((simInfo.getParentSimulationReference() != null ? simInfo.getParentSimulationReference() : simInfo.getSimulationVersion().getVersionKey()), simInfo.getOwner(), simInfoHolder.getJobIndex()));
// Create (non-editable) info for display
fdos.origin = fdor.origin;
fdos.extent = fdor.extent;
fdos.isize = fdor.iSize;
fdos.times = fdor.times;
fdos.varNames = new String[fdor.dataIdentifierArr.length];
for (int i = 0; i < fdor.dataIdentifierArr.length; i += 1) {
fdos.varNames[i] = (fdor.dataIdentifierArr[i].getVariableType().equals(VariableType.VOLUME) ? "(vol) " : "") + (fdor.dataIdentifierArr[i].getVariableType().equals(VariableType.MEMBRANE) ? "(mbr) " : "") + fdor.dataIdentifierArr[i].getName();
}
hashTable.put("fdos", fdos);
hashTable.put("initFDName", simInfo.getName());
// addNewExternalData(clientRequestManager, fdos, simInfoHolder.simInfo.getName(), true);
}
};
Hashtable<String, Object> hash = new Hashtable<String, Object>();
ClientTaskDispatcher.dispatch(this, hash, taskArray, false);
} catch (UserCancelException e) {
return;
} catch (Exception e) {
PopupGenerator.showErrorDialog(this, "Error creating Field Data from simulation\n" + e.getMessage(), e);
}
}
use of cbit.vcell.field.io.FieldDataFileOperationResults in project vcell by virtualcell.
the class FieldDataGUIPanel method refreshMainNode.
private void refreshMainNode(final DefaultMutableTreeNode mainNode) {
final boolean isMainExpanded = getJTree1().isExpanded(new TreePath(mainNode.getPath()));
final boolean isVarExpanded = getJTree1().isExpanded(new TreePath(((DefaultMutableTreeNode) mainNode.getLastChild()).getPath()));
// Remove all children from Main node in a Tree safe way
DefaultMutableTreeNode root = (DefaultMutableTreeNode) getJTree1().getModel().getRoot();
int mainNodeIndex = ((DefaultTreeModel) getJTree1().getModel()).getIndexOfChild(root, mainNode);
((DefaultTreeModel) getJTree1().getModel()).removeNodeFromParent(mainNode);
mainNode.removeAllChildren();
final DefaultMutableTreeNode varNode = new DefaultMutableTreeNode(new FieldDataVarMainList());
mainNode.add(varNode);
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(mainNode, root, mainNodeIndex);
//
// Create thread-safe tasks to get Field Data Info and update JTree
//
final String FDOR_INFO = "FDOR_INFO";
final RequestManager clientRequestManager = fieldDataWindowManager.getLocalRequestManager();
AsynchClientTask FieldDataInfoTask = new AsynchClientTask("Gather Field Data info", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hash) throws Exception {
FieldDataMainList fieldDataMainList = (FieldDataMainList) mainNode.getUserObject();
final FieldDataFileOperationResults fieldDataFileOperationResults = clientRequestManager.getDocumentManager().fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec(fieldDataMainList.externalDataIdentifier.getKey(), clientRequestManager.getDocumentManager().getUser(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT));
hash.put(FDOR_INFO, fieldDataFileOperationResults);
}
};
AsynchClientTask FieldDataInfoTreeUpdate = new AsynchClientTask("Update Field Data GUI", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
public void run(Hashtable<String, Object> hash) {
try {
FieldDataFileOperationResults fieldDataFileOperationResults = (FieldDataFileOperationResults) hash.get(FDOR_INFO);
Arrays.sort(fieldDataFileOperationResults.dataIdentifierArr, new Comparator<DataIdentifier>() {
public int compare(DataIdentifier o1, DataIdentifier o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
FieldDataMainList fieldDataMainList = (FieldDataMainList) mainNode.getUserObject();
final DefaultMutableTreeNode isizeNode = new DefaultMutableTreeNode(new FieldDataISizeList(fieldDataFileOperationResults.iSize));
final DefaultMutableTreeNode originNode = new DefaultMutableTreeNode(new FieldDataOriginList(fieldDataFileOperationResults.origin));
final DefaultMutableTreeNode extentNode = new DefaultMutableTreeNode(new FieldDataExtentList(fieldDataFileOperationResults.extent));
final DefaultMutableTreeNode timeNode = new DefaultMutableTreeNode(new FieldDataTimeList(fieldDataFileOperationResults.times));
final DefaultMutableTreeNode idNode = new DefaultMutableTreeNode(new FieldDataIDList(fieldDataMainList.externalDataIdentifier.getKey()));
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(isizeNode, mainNode, 0);
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(originNode, mainNode, 1);
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(extentNode, mainNode, 2);
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(timeNode, mainNode, 3);
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(idNode, mainNode, 4);
for (int i = 0; i < fieldDataFileOperationResults.dataIdentifierArr.length; i += 1) {
((DefaultTreeModel) getJTree1().getModel()).insertNodeInto(new DefaultMutableTreeNode(new FieldDataVarList(fieldDataFileOperationResults.dataIdentifierArr[i])), varNode, varNode.getChildCount());
}
if (isMainExpanded) {
getJTree1().expandPath(new TreePath(mainNode.getPath()));
}
if (isVarExpanded) {
getJTree1().expandPath(new TreePath(varNode.getPath()));
}
} catch (Throwable e) {
hash.put(ClientTaskDispatcher.TASK_ABORTED_BY_ERROR, e);
}
}
};
//
// Execute Field Data Info - JTree tasks
//
AsynchClientTask[] tasks = new AsynchClientTask[] { FieldDataInfoTask, FieldDataInfoTreeUpdate };
Hashtable<String, Object> hash = new Hashtable<String, Object>();
ClientTaskDispatcher.dispatch(this, hash, tasks, false);
}
use of cbit.vcell.field.io.FieldDataFileOperationResults in project vcell by virtualcell.
the class ClientDocumentManager method saveFieldData.
public ExternalDataIdentifier saveFieldData(FieldDataFileOperationSpec fdos, String fieldDataBaseName) throws DataAccessException {
FieldDataDBOperationResults fieldDataResults = fieldDataDBOperation(FieldDataDBOperationSpec.createGetExtDataIDsSpec(getUser()));
ExternalDataIdentifier[] existingExternalIDs = fieldDataResults.extDataIDArr;
// find available field data name (starting from init).
for (ExternalDataIdentifier externalID : existingExternalIDs) {
if (externalID.getName().equals(fieldDataBaseName)) {
fieldDataBaseName = TokenMangler.getNextEnumeratedToken(fieldDataBaseName);
}
}
FieldDataDBOperationSpec newExtDataIDSpec = FieldDataDBOperationSpec.createSaveNewExtDataIDSpec(getUser(), fieldDataBaseName, "");
ExternalDataIdentifier eid = fieldDataDBOperation(newExtDataIDSpec).extDataID;
fdos.specEDI = eid;
fdos.annotation = "";
try {
// Add to Server Disk
FieldDataFileOperationResults fdor = fieldDataFileOperation(fdos);
LG.debug(fdor);
} catch (DataAccessException e) {
try {
// try to cleanup new ExtDataID
fieldDataDBOperation(FieldDataDBOperationSpec.createDeleteExtDataIDSpec(fdos.specEDI));
} catch (Exception e2) {
// ignore
}
fdos.specEDI = null;
throw e;
}
return eid;
}
use of cbit.vcell.field.io.FieldDataFileOperationResults in project vcell by virtualcell.
the class DataSetControllerImpl method fieldDataFileOperation.
public FieldDataFileOperationResults fieldDataFileOperation(FieldDataFileOperationSpec fieldDataFileOperationSpec) throws ObjectNotFoundException {
if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_COPYSIM) {
Vector<File> removeFilesIfErrorV = new Vector<File>();
try {
int simJobIndex = fieldDataFileOperationSpec.sourceSimParamScanJobIndex;
// Determine style so file names can be constructed properly
VCSimulationDataIdentifier sourceSimDataID = new VCSimulationDataIdentifier(new VCSimulationIdentifier(fieldDataFileOperationSpec.sourceSimDataKey, fieldDataFileOperationSpec.sourceOwner), simJobIndex);
SimulationData simulationData = (SimulationData) getVCData(sourceSimDataID);
boolean isOldStyle = (simulationData.getResultsInfoObject() instanceof VCSimulationDataIdentifierOldStyle);
//
// log,mesh,zip,func
//
KeyValue origSimKey = fieldDataFileOperationSpec.sourceSimDataKey;
File meshFile_orig = simulationData.getMeshFile(false);
File funcFile_orig = simulationData.getFunctionsFile(false);
File subdomainFile_orig = simulationData.getSubdomainFile();
File fdLogFile_orig = simulationData.getLogFile();
File zipFile_orig = simulationData.getZipFile(false, 0);
boolean bCopySubdomainFile = subdomainFile_orig.exists();
// Dont' check subdomainFile_orig
if (!(meshFile_orig.exists() && funcFile_orig.exists() && fdLogFile_orig.exists() && zipFile_orig.exists())) {
throw new RuntimeException("Couldn't find all of the files required to copy sim");
}
File userDir = getPrimaryUserDir(fieldDataFileOperationSpec.owner, true);
File meshFile_new = new File(userDir, SimulationData.createCanonicalMeshFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File funcFile_new = new File(userDir, SimulationData.createCanonicalFunctionsFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File subdomainFile_new = new File(userDir, SimulationData.createCanonicalSubdomainFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File fdLogFile_new = new File(userDir, SimulationData.createCanonicalSimLogFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File zipFile_new = new File(userDir, SimulationData.createCanonicalSimZipFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, 0, false, false));
if (meshFile_new.exists() || funcFile_new.exists() || fdLogFile_new.exists() || zipFile_new.exists() || (bCopySubdomainFile && subdomainFile_new.exists())) {
throw new RuntimeException("File names required for new Field Data already exist on server");
}
removeFilesIfErrorV.add(funcFile_new);
removeFilesIfErrorV.add(meshFile_new);
removeFilesIfErrorV.add(fdLogFile_new);
// Simple copy of mesh and funcfile because they do not have to be changed
FileUtils.copyFile(meshFile_orig, meshFile_new, false, false, 8 * 1024);
FileUtils.copyFile(funcFile_orig, funcFile_new, false, false, 8 * 1024);
if (bCopySubdomainFile) {
FileUtils.copyFile(subdomainFile_orig, subdomainFile_new, false, false, 8 * 1024);
}
// Copy Log file and replace original simID with ExternalDataIdentifier id
BufferedWriter writer = null;
try {
String origLog = FileUtils.readFileToString(fdLogFile_orig);
String newLogStr;
String replace_new = SimulationData.createSimIDWithJobIndex(fieldDataFileOperationSpec.specEDI.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
if (isOldStyle) {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, 0, true);
newLogStr = origLog.replaceAll(replace_orig, replace_new);
} else {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, fieldDataFileOperationSpec.sourceSimParamScanJobIndex, false);
newLogStr = origLog.replaceAll(replace_orig, replace_new);
}
writer = new BufferedWriter(new FileWriter(fdLogFile_new));
writer.write(newLogStr);
writer.close();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (Exception e) {
/*ignore*/
}
;
}
//
// Copy zip file and rename entries
//
int zipIndex = 0;
while (true) {
// Loop because there might be more than 1 zip file for large datasets
zipFile_orig = simulationData.getZipFile(false, zipIndex);
if (!zipFile_orig.exists()) {
// done
break;
}
zipFile_new = new File(userDir, SimulationData.createCanonicalSimZipFileName(fieldDataFileOperationSpec.specEDI.getKey(), zipIndex, 0, false, false));
if (zipFile_new.exists()) {
throw new DataAccessException("new zipfile name " + zipFile_new.getAbsolutePath() + " already exists");
}
removeFilesIfErrorV.add(zipFile_new);
ZipFile inZipFile = null;
InputStream zis = null;
ZipOutputStream zos = null;
try {
inZipFile = new ZipFile(zipFile_orig);
;
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile_new)));
Enumeration<? extends ZipEntry> zipEntryEnum = inZipFile.getEntries();
while (zipEntryEnum.hasMoreElements()) {
ZipEntry zeIN = zipEntryEnum.nextElement();
byte[] zdataIN = new byte[(int) zeIN.getSize()];
int num = 0;
int numTotal = 0;
zis = new BufferedInputStream(inZipFile.getInputStream((ZipArchiveEntry) zeIN));
// long startTime = System.currentTimeMillis();
while ((num = zis.read(zdataIN, numTotal, zdataIN.length - numTotal)) != -1 && numTotal != zdataIN.length) {
numTotal += num;
}
// System.out.println("zipread time="+((System.currentTimeMillis()-startTime)/1000.0));
zis.close();
String newName;
String replace_new = SimulationData.createSimIDWithJobIndex(fieldDataFileOperationSpec.specEDI.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
if (isOldStyle) {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, 0, true);
newName = zeIN.getName().replaceAll(replace_orig, replace_new);
} else {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, fieldDataFileOperationSpec.sourceSimParamScanJobIndex, false);
newName = zeIN.getName().replaceAll(replace_orig, replace_new);
}
ZipEntry zeOUT = new ZipEntry(newName);
zeOUT.setComment(zeIN.getComment());
zeOUT.setCompressedSize(zeIN.getCompressedSize());
zeOUT.setCrc(zeIN.getCrc());
zeOUT.setExtra(zeIN.getExtra());
zeOUT.setMethod(zeIN.getMethod());
zeOUT.setSize(zeIN.getSize());
zeOUT.setTime(zeIN.getTime());
// startTime = System.currentTimeMillis();
zos.putNextEntry(zeOUT);
zos.write(zdataIN, 0, zdataIN.length);
// System.out.println("zipwrite time="+((System.currentTimeMillis()-startTime)/1000.0)+"\n");
}
} finally {
try {
if (zis != null) {
zis.close();
}
} catch (Exception e) {
/*ignore*/
}
;
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
/*ignore*/
}
;
}
zipIndex += 1;
}
// Now see if we can read what we just wrote
return fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.owner, FieldDataFileOperationSpec.JOBINDEX_DEFAULT));
} catch (Exception e) {
e.printStackTrace();
try {
for (int i = 0; i < removeFilesIfErrorV.size(); i += 1) {
removeFilesIfErrorV.elementAt(i).delete();
}
} catch (Throwable e2) {
// ignore, we tried to cleanup
}
throw new RuntimeException("Error copying sim data to new Field Data\n" + e.getMessage());
}
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_ADD) {
if (fieldDataFileOperationSpec.cartesianMesh == null) {
throw new RuntimeException("Field Data Operation 'ADD' cartesianMesh cannot be null");
}
if (fieldDataFileOperationSpec.times == null || fieldDataFileOperationSpec.times.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' times cannot be null");
}
if (fieldDataFileOperationSpec.times[0] != 0) {
throw new RuntimeException("Field Data Operation 'ADD' first time must be 0.0");
}
if (fieldDataFileOperationSpec.varNames == null || fieldDataFileOperationSpec.varNames.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' variable names cannot be null");
}
if ((fieldDataFileOperationSpec.shortSpecData != null && fieldDataFileOperationSpec.doubleSpecData != null) || (fieldDataFileOperationSpec.shortSpecData == null && fieldDataFileOperationSpec.doubleSpecData == null)) {
throw new RuntimeException("Field Data Operation 'ADD' must have ONLY 1 data specifier, short or double");
}
if (fieldDataFileOperationSpec.shortSpecData != null && (fieldDataFileOperationSpec.shortSpecData.length != fieldDataFileOperationSpec.times.length || fieldDataFileOperationSpec.shortSpecData[0].length != fieldDataFileOperationSpec.varNames.length)) {
throw new RuntimeException("Field Data Operation 'ADD' 'short' data dimension does not match\n" + "times and variable names array lengths");
}
if (fieldDataFileOperationSpec.doubleSpecData != null && (fieldDataFileOperationSpec.doubleSpecData.length != fieldDataFileOperationSpec.times.length || fieldDataFileOperationSpec.doubleSpecData[0].length != fieldDataFileOperationSpec.varNames.length)) {
throw new RuntimeException("Field Data Operation 'ADD' 'double' data dimension does not match\n" + "times and variable names array lengths");
}
if (fieldDataFileOperationSpec.variableTypes == null || fieldDataFileOperationSpec.variableTypes.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' variable types cannot be null");
}
if (fieldDataFileOperationSpec.variableTypes.length != fieldDataFileOperationSpec.varNames.length) {
throw new RuntimeException("Field Data Operation 'ADD' variable types count does not match variable names count");
}
// byte[][][] allData = fieldDataFileOperationSpec.specData;
double[] times = fieldDataFileOperationSpec.times;
ExternalDataIdentifier dataset = fieldDataFileOperationSpec.specEDI;
String[] vars = fieldDataFileOperationSpec.varNames;
VariableType[] varTypes = fieldDataFileOperationSpec.variableTypes;
File userDir = null;
try {
userDir = getPrimaryUserDir(fieldDataFileOperationSpec.owner, true);
} catch (FileNotFoundException e) {
throw new RuntimeException("Couldn't create new user directory on server");
}
double[][][] convertedData = null;
if (fieldDataFileOperationSpec.doubleSpecData != null) {
convertedData = fieldDataFileOperationSpec.doubleSpecData;
} else {
// convert short to double
convertedData = new double[times.length][vars.length][];
for (int i = 0; i < times.length; i += 1) {
for (int j = 0; j < vars.length; j += 1) {
if (fieldDataFileOperationSpec.shortSpecData != null) {
convertedData[i][j] = new double[fieldDataFileOperationSpec.shortSpecData[i][j].length];
for (int k = 0; k < fieldDataFileOperationSpec.shortSpecData[i][j].length; k += 1) {
convertedData[i][j][k] = (double) (((int) fieldDataFileOperationSpec.shortSpecData[i][j][k]) & 0x0000FFFF);
}
} else {
throw new RuntimeException("no pixel data found");
}
}
}
}
// Write Log file
File fdLogFile = new File(userDir, SimulationData.createCanonicalSimLogFileName(dataset.getKey(), 0, false));
PrintStream ps = null;
File zipFile = new File(userDir, SimulationData.createCanonicalSimZipFileName(dataset.getKey(), 0, 0, false, false));
Vector<String> simFileNamesV = new Vector<String>();
try {
if (!fdLogFile.createNewFile()) {
throw new Exception("File.createNewFile() returned null");
}
ps = new PrintStream(fdLogFile);
for (int i = 0; i < times.length; i += 1) {
String simFilename = SimulationData.createCanonicalSimFilePathName(dataset.getKey(), i, 0, false);
simFileNamesV.add(simFilename);
ps.println(i + "\t" + simFilename + "\t" + zipFile.getName() + "\t" + times[i] + "");
}
ps.flush();
} catch (Exception e) {
throw new RuntimeException("Couldn't create log file " + fdLogFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
if (ps != null) {
ps.close();
}
}
// Write zipFile
java.util.zip.ZipOutputStream zipOut = null;
try {
java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(new java.io.FileOutputStream(zipFile));
zipOut = new java.util.zip.ZipOutputStream(bout);
for (int t = 0; t < times.length; t += 1) {
java.io.File temp = java.io.File.createTempFile("temp", null);
DataSet.writeNew(temp, vars, varTypes, fieldDataFileOperationSpec.isize, convertedData[t]);
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(simFileNamesV.get(t));
zipOut.putNextEntry(zipEntry);
// ----------------------------
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.io.FileInputStream(temp));
byte[] bytes = new byte[65536];
try {
int b = in.read(bytes);
while (b != -1) {
zipOut.write(bytes, 0, b);
b = in.read(bytes);
}
} catch (IOException e) {
throw new Exception("Error writing zip file bytes");
} finally {
// cleanup
in.close();
temp.delete();
}
// ----------------------------
}
} catch (Exception e) {
throw new RuntimeException("Couldn't create zip file " + zipFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
try {
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
// ignore
}
}
// Write Mesh file
FileOutputStream fos = null;
File meshFile = null;
try {
CartesianMesh mesh = fieldDataFileOperationSpec.cartesianMesh;
meshFile = new File(userDir, SimulationData.createCanonicalMeshFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
fos = new FileOutputStream(meshFile);
mesh.write(new PrintStream(fos));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error writing mesh file " + meshFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
// ignore
}
}
// Write Functionfile file
PrintStream ps2 = null;
File funcFile = null;
try {
funcFile = new File(userDir, SimulationData.createCanonicalFunctionsFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
FileOutputStream fos2 = new FileOutputStream(funcFile);
ps2 = new PrintStream(fos2);
ps2.println("##---------------------------------------------" + "\n" + "## " + funcFile.getAbsolutePath() + "\n" + "##---------------------------------------------" + "\n");
ps2.flush();
ps2.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error writing function file " + funcFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
if (ps2 != null) {
ps2.close();
}
}
try {
// return Info
return fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.owner, FieldDataFileOperationSpec.JOBINDEX_DEFAULT));
} catch (Exception e) {
e.printStackTrace();
// ignore
}
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_DEPENDANTFUNCS) {
throw new RuntimeException("This function is not currently used");
// //
// //Check for references to FieldData from users User Defined Functions
// //
// HashMap<String, KeyValue> dbFuncFileNamesAndSimKeys = null;
// try{
// dbFuncFileNamesAndSimKeys =
// FieldDataDBOperationDriver.getFunctionFileNamesAndSimKeys(
// fieldDataFileOperationSpec.specEDI.getOwner());
// }catch(Exception e){
// e.printStackTrace();
// throw new RuntimeException("couldn't get Function File names from Database\n"+e.getMessage());
// }
// //String regex = "^.*"+MathMLTags.FIELD+"\\s*\\(\\s*"+fieldDataFileOperationSpec.specEDI.getName()+"\\s*,.*$";
// String regex = "^.*?field\\s*\\(\\s*"+fieldDataFileOperationSpec.specEDI.getName()+"\\s*,.*?$";
// java.util.regex.Pattern pattern =
// java.util.regex.Pattern.compile(regex);//,java.util.regex.Pattern.MULTILINE|java.util.regex.Pattern.DOTALL);
// Matcher matcher = pattern.matcher("");
// Set<Map.Entry<String,KeyValue>> funcAndSimsES = dbFuncFileNamesAndSimKeys.entrySet();
// Vector<FieldDataFileOperationResults.FieldDataReferenceInfo> referencingFuncFileDescription =
// new Vector<FieldDataFileOperationResults.FieldDataReferenceInfo>();
// boolean bSearchSecondary =
// secondaryRootDirectory != null &&
// !primaryRootDirectory.equals(secondaryRootDirectory);
// TreeSet<String> searchedFuncFilesTS = new TreeSet<String>();
// Iterator<Map.Entry<String,KeyValue>> iter = funcAndSimsES.iterator();
// FunctionFileGenerator.FuncFileLineInfo funcfileInfo = null;
// while(iter.hasNext()){
// Map.Entry<String,KeyValue> currentEntry = iter.next();
// File currentFile = null;
// for (int i = 0; i < (bSearchSecondary?2:1); i++) {
// if(searchedFuncFilesTS.contains(currentEntry.getKey())){
// continue;
// }
// currentFile = new File(
// getUserDirectoryName(
// (i==0?primaryRootDirectory:secondaryRootDirectory),
// fieldDataFileOperationSpec.specEDI.getOwner()),currentEntry.getKey());
// if(!currentFile.exists()){
// continue;
// }
// searchedFuncFilesTS.add(currentEntry.getKey());
// LineNumberReader lineNumberReader = null;
// Vector<String> referringFieldfunctionNamesV = new Vector<String>();
// try{
// lineNumberReader = new LineNumberReader(new FileReader(currentFile));
// String funcFileLine = null;
// while((funcFileLine = lineNumberReader.readLine()) != null){
// funcfileInfo = FunctionFileGenerator.readFunctionLine(funcFileLine);
// if(funcfileInfo != null && funcfileInfo.functionExpr != null){
// matcher.reset(funcfileInfo.functionExpr);
// if(matcher.matches()){
// referringFieldfunctionNamesV.add(funcfileInfo.functionName);
// }
// }
// }
// lineNumberReader.close();
// if(referringFieldfunctionNamesV.size() > 0){
// FieldDataFileOperationResults.FieldDataReferenceInfo fieldDataReferenceInfo =
// FieldDataDBOperationDriver.getModelDescriptionForSimulation(
// fieldDataFileOperationSpec.specEDI.getOwner(), currentEntry.getValue());
// fieldDataReferenceInfo.funcNames = referringFieldfunctionNamesV.toArray(new String[0]);
// referencingFuncFileDescription.add(fieldDataReferenceInfo);
// // for (int j = 0; j < referringFieldfunctionNamesV.size(); j++) {
// // referencingFuncFileDescription.add(new String[][] {
// // referringFieldfunctionNamesV.elementAt(j),modelDescription});
// // }
// }
// }catch(Exception e){
// e.printStackTrace();
// throw new RuntimeException(e.getMessage(),e);
// }finally{
// if(lineNumberReader != null){try{lineNumberReader.close();}catch(Exception e){e.printStackTrace();}}
// }
// }
// }
// if(referencingFuncFileDescription.size() > 0){
// FieldDataFileOperationResults fdfor = new FieldDataFileOperationResults();
// fdfor.dependantFunctionInfo =
// referencingFuncFileDescription.toArray(new FieldDataFileOperationResults.FieldDataReferenceInfo[0]);
// return fdfor;
// }
// return null;
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_DELETE) {
//
if (cacheTable0 != null) {
VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.specEDI.getOwner());
VCSimulationDataIdentifier simDataID = new VCSimulationDataIdentifier(vcSimID, FieldDataFileOperationSpec.JOBINDEX_DEFAULT);
cacheTable0.removeAll(simDataID);
cacheTable0.removeAll(fieldDataFileOperationSpec.specEDI);
}
if (userExtDataIDH != null) {
userExtDataIDH.remove(fieldDataFileOperationSpec.specEDI.getOwner());
}
SimulationData simulationData = null;
try {
simulationData = (SimulationData) getVCData(fieldDataFileOperationSpec.specEDI);
} catch (Exception e) {
throw new ObjectNotFoundException(e.getMessage(), e);
}
File fdLogFile = simulationData.getLogFile();
File fdMeshFile = simulationData.getMeshFile(false);
File fdFunctionFile = simulationData.getFunctionsFile(true);
File fdSubdomainFile = simulationData.getSubdomainFile();
if (!fdLogFile.delete()) {
System.out.println("Couldn't delete log file " + fdLogFile.getAbsolutePath());
}
if (!fdMeshFile.delete()) {
System.out.println("Couldn't delete Mesh file " + fdMeshFile.getAbsolutePath());
}
if (!fdFunctionFile.delete()) {
System.out.println("Couldn't delete Functions file " + fdFunctionFile.getAbsolutePath());
}
if (fdSubdomainFile.exists() && fdSubdomainFile.delete()) {
System.out.println("Couldn't delete Subdomains file " + fdSubdomainFile.getAbsolutePath());
}
int index = 0;
while (true) {
File fdZipFile = simulationData.getZipFile(false, index);
if (index != 0 && !fdZipFile.exists()) {
break;
}
if (!fdZipFile.delete()) {
System.out.println("Couldn't delete zip file " + fdZipFile.getAbsolutePath());
}
index += 1;
}
return null;
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_INFO) {
try {
FieldDataFileOperationResults fdor = new FieldDataFileOperationResults();
VCDataIdentifier sourceSimDataID = new VCSimulationDataIdentifier(new VCSimulationIdentifier(fieldDataFileOperationSpec.sourceSimDataKey, fieldDataFileOperationSpec.sourceOwner), fieldDataFileOperationSpec.sourceSimParamScanJobIndex);
fdor.dataIdentifierArr = getDataIdentifiers(null, sourceSimDataID);
CartesianMesh mesh = getMesh(sourceSimDataID);
fdor.extent = mesh.getExtent();
fdor.origin = mesh.getOrigin();
fdor.iSize = new ISize(mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ());
fdor.times = getDataSetTimes(sourceSimDataID);
return fdor;
} catch (FileNotFoundException e) {
throw new ObjectNotFoundException("Error FieldDataOp get INFO", e);
} catch (Exception e) {
throw new RuntimeException("Error FieldDataFileOperationSpec INFO Operation\n" + e.getMessage());
}
}
throw new RuntimeException("Field data operation " + fieldDataFileOperationSpec.opType + " unknown.");
}
use of cbit.vcell.field.io.FieldDataFileOperationResults in project vcell by virtualcell.
the class FieldDataWindowManager method findReferencingModels.
public boolean findReferencingModels(final ExternalDataIdentifier targetExtDataID, boolean bShowReferencingModelsList) throws DataAccessException, UserCancelException {
ReferenceQuerySpec rqs = new ReferenceQuerySpec(targetExtDataID);
ReferenceQueryResult rqr = getRequestManager().getDocumentManager().findReferences(rqs);
VersionableTypeVersion[] dependants = null;
Hashtable<VersionableTypeVersion, String[]> choices = new Hashtable<VersionableTypeVersion, String[]>();
boolean bDanglingReferences = false;
VersionableType bioModelType = VersionableType.BioModelMetaData;
VersionableType mathModelType = VersionableType.MathModelMetaData;
if (rqr != null) {
dependants = (rqr.getVersionableFamily().bDependants() ? rqr.getVersionableFamily().getUniqueDependants() : null);
if (dependants != null) {
for (int i = 0; i < dependants.length; i += 1) {
boolean isBioModel = dependants[i].getVType().equals(bioModelType);
boolean isTop = isBioModel || dependants[i].getVType().equals(mathModelType);
if (isTop) {
VersionableRelationship[] vrArr2 = rqr.getVersionableFamily().getDependantRelationships();
for (int j = 0; j < vrArr2.length; j += 1) {
if ((vrArr2[j].from() == dependants[i]) && vrArr2[j].to().getVType().equals((isBioModel ? VersionableType.SimulationContext : VersionableType.MathDescription))) {
for (int k = 0; k < vrArr2.length; k += 1) {
boolean bAdd = false;
if (k == j && vrArr2[k].from().getVType().equals(mathModelType)) {
bAdd = true;
}
if ((vrArr2[k].from() == vrArr2[j].to()) && vrArr2[k].to().getVType().equals(VersionableType.MathDescription)) {
bAdd = true;
}
if (bAdd) {
choices.put(dependants[i], new String[] { dependants[i].getVersion().getName(), (isBioModel ? bioModelType.getTypeName() : mathModelType.getTypeName()), (isBioModel ? vrArr2[k].from().getVersion().getName() : ""), dependants[i].getVersion().getVersionKey().toString() });
}
}
}
}
}
}
bDanglingReferences = (choices.size() == 0);
} else {
bDanglingReferences = true;
}
}
// FieldDataFileOperationResults fdfor = getRequestManager().getDocumentManager().fieldDataFileOperation(
// FieldDataFileOperationSpec.createDependantFuncsFieldDataFileOperationSpec(targetExtDataID));
FieldDataFileOperationResults fdfor = null;
boolean bHasReferences = false;
if (choices.size() > 0 || fdfor != null) {
bHasReferences = true;
if (bShowReferencingModelsList) {
String[] columnNames = new String[] { "Model", "Type", "Description" };
Vector<VersionableType> varTypeV = new Vector<VersionableType>();
Vector<KeyValue> keyValV = new Vector<KeyValue>();
Vector<String[]> choicesV = new Vector<String[]>();
String[][] modelListData = choices.values().toArray(new String[0][0]);
for (int i = 0; i < modelListData.length; i++) {
choicesV.add(new String[] { modelListData[i][0], modelListData[i][1], "Model Variable - " + (modelListData[i][2].length() == 0 ? "" : "App='" + modelListData[i][2] + "'") + " version[" + modelListData[i][3] + "]" });
varTypeV.add((modelListData[i][1].equals(bioModelType.getTypeName()) ? bioModelType : mathModelType));
keyValV.add(new KeyValue(modelListData[i][3]));
}
for (int i = 0; fdfor != null && i < fdfor.dependantFunctionInfo.length; i++) {
String functionNames = "";
for (int j = 0; j < fdfor.dependantFunctionInfo[i].funcNames.length; j++) {
functionNames += (j > 0 ? "," : "") + fdfor.dependantFunctionInfo[i].funcNames[j];
}
choicesV.add(new String[] { fdfor.dependantFunctionInfo[i].referenceSourceName, fdfor.dependantFunctionInfo[i].referenceSourceType, "Data Viewer Function(s) '" + functionNames + "' - " + (fdfor.dependantFunctionInfo[i].applicationName == null ? "" : "App='" + fdfor.dependantFunctionInfo[i].applicationName + "' ") + (fdfor.dependantFunctionInfo[i].simulationName == null ? "" : "Sim='" + fdfor.dependantFunctionInfo[i].simulationName + "' ") + "version[" + fdfor.dependantFunctionInfo[i].refSourceVersionDate + "]" });
if (fdfor.dependantFunctionInfo[i].referenceSourceType.equals(FieldDataFileOperationResults.FieldDataReferenceInfo.FIELDDATATYPENAME)) {
varTypeV.add(null);
} else if (fdfor.dependantFunctionInfo[i].referenceSourceType.equals(bioModelType.getTypeName())) {
varTypeV.add(bioModelType);
} else if (fdfor.dependantFunctionInfo[i].referenceSourceType.equals(mathModelType.getTypeName())) {
varTypeV.add(mathModelType);
} else {
throw new IllegalArgumentException("Unknown reference source type " + fdfor.dependantFunctionInfo[i].referenceSourceType);
}
keyValV.add(fdfor.dependantFunctionInfo[i].refSourceVersionKey);
}
int[] selectionArr = PopupGenerator.showComponentOKCancelTableList(getComponent(), "References to Field Data (Select To Open) " + targetExtDataID.getName(), columnNames, choicesV.toArray(new String[0][0]), ListSelectionModel.SINGLE_SELECTION);
if (selectionArr != null && selectionArr.length > 0) {
if (varTypeV.elementAt(selectionArr[0]) != null) {
if (varTypeV.elementAt(selectionArr[0]).equals(bioModelType)) {
BioModelInfo bmi = getRequestManager().getDocumentManager().getBioModelInfo(keyValV.elementAt(selectionArr[0]));
getRequestManager().openDocument(bmi, FieldDataWindowManager.this, true);
} else if (varTypeV.elementAt(selectionArr[0]).equals(mathModelType)) {
MathModelInfo mmi = getRequestManager().getDocumentManager().getMathModelInfo(keyValV.elementAt(selectionArr[0]));
getRequestManager().openDocument(mmi, FieldDataWindowManager.this, true);
} else {
throw new IllegalArgumentException("Not expecting varType " + varTypeV.elementAt(selectionArr[0]));
}
} else {
PopupGenerator.showInfoDialog(this, "use FiledDataManager to view FieldData '" + choicesV.elementAt(selectionArr[0])[0] + "'");
}
}
}
} else {
if (!bDanglingReferences) {
bHasReferences = false;
if (bShowReferencingModelsList) {
PopupGenerator.showInfoDialog(this, "No Model references found for Field Data " + targetExtDataID.getName());
}
} else {
bHasReferences = true;
if (bShowReferencingModelsList) {
PopupGenerator.showInfoDialog(this, "No current Model references found.\n" + "Field Data has internal database references from\n" + "previously linked Model(s) that have been deleted.\n" + "Note: Field Data '" + targetExtDataID.getName() + "' is not deletable\n" + "until database is culled (daily).");
}
}
}
return bHasReferences;
}
Aggregations