Skip to content

Commit 380b4ad

Browse files
committed
Merge branch 'master' into validate
2 parents 374316a + 273bbad commit 380b4ad

File tree

4 files changed

+135
-39
lines changed

4 files changed

+135
-39
lines changed

vcell-client/src/main/java/cbit/vcell/client/ClientSimManager.java

Lines changed: 96 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
package cbit.vcell.client;
1212

1313
import java.awt.Dimension;
14+
import java.io.BufferedWriter;
1415
import java.io.File;
1516
import java.io.FileNotFoundException;
17+
import java.io.FileWriter;
1618
import java.io.IOException;
1719
import java.io.InputStream;
1820
import java.io.InputStreamReader;
@@ -23,6 +25,7 @@
2325
import java.util.EventObject;
2426
import java.util.Hashtable;
2527
import java.util.LinkedHashMap;
28+
import java.util.List;
2629
import java.util.Vector;
2730

2831
import javax.swing.SwingUtilities;
@@ -54,6 +57,7 @@
5457
import cbit.vcell.client.desktop.simulation.SimulationWindow.LocalState;
5558
import cbit.vcell.client.desktop.simulation.SimulationWorkspace;
5659
import cbit.vcell.client.pyvcellproxy.SimulationDataSetRef;
60+
import cbit.vcell.client.server.ClientServerManager;
5761
import cbit.vcell.client.server.UserPreferences;
5862
import cbit.vcell.client.task.AsynchClientTask;
5963
import cbit.vcell.client.task.AsynchClientTaskFunction;
@@ -63,10 +67,13 @@
6367
import cbit.vcell.field.FieldDataIdentifierSpec;
6468
import cbit.vcell.mapping.SimulationContext;
6569
import cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements;
70+
import cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException;
6671
import cbit.vcell.messaging.server.SimulationTask;
72+
import cbit.vcell.parser.ExpressionException;
6773
import cbit.vcell.resource.PropertyLoader;
6874
import cbit.vcell.resource.ResourceUtil;
6975
import cbit.vcell.resource.VCellConfiguration;
76+
import cbit.vcell.server.DataSetController;
7077
import cbit.vcell.server.SimulationStatus;
7178
import cbit.vcell.simdata.DataManager;
7279
import cbit.vcell.simdata.DataSetControllerImpl;
@@ -88,12 +95,15 @@
8895
import cbit.vcell.solver.TempSimulation;
8996
import cbit.vcell.solver.VCSimulationDataIdentifier;
9097
import cbit.vcell.solver.VCSimulationIdentifier;
98+
import cbit.vcell.solver.ode.ODESimData;
99+
import cbit.vcell.solver.ode.ODESolverResultSet;
91100
import cbit.vcell.solver.server.SimulationMessage;
92101
import cbit.vcell.solver.server.Solver;
93102
import cbit.vcell.solver.server.SolverEvent;
94103
import cbit.vcell.solver.server.SolverFactory;
95104
import cbit.vcell.solver.server.SolverListener;
96105
import cbit.vcell.solver.server.SolverStatus;
106+
import cbit.vcell.util.ColumnDescription;
97107

98108
/**
99109
* Insert the type's description here.
@@ -287,8 +297,9 @@ private static void saveFailure(Hashtable<String, Object>hashTable,Simulation si
287297
failures.put(sim, throwable);
288298
}
289299

290-
public void importBatchSimulations(OutputContext outputContext, Simulation simulation) throws java.beans.PropertyVetoException {
300+
public void getBatchSimulationsResults(OutputContext outputContext, Simulation simulation) throws java.beans.PropertyVetoException {
291301

302+
292303

293304
// simulation should be a template simulation
294305
if(simulation.getName().contains(SimulationContext.ReservedBatchExtensionString)) {
@@ -305,35 +316,82 @@ public void importBatchSimulations(OutputContext outputContext, Simulation simul
305316
throw new RuntimeException("Cannot add simulation, bioModel not set");
306317
}
307318

319+
File batchResultsDir = ResourceUtil.getLocalBatchDir();
320+
// File localSimDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
321+
322+
ArrayList<AsynchClientTask> taskList = new ArrayList<AsynchClientTask>();
323+
AsynchClientTask retrieveResultsTask = new AsynchClientTask("Retrieving results", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
324+
public void run(Hashtable<String, Object> hashTable) throws Exception {
325+
326+
// recover the list of batch simulations that belong to this template
327+
Simulation allSims[] = bioModel.getSimulations();
328+
LinkedHashMap<String, String> importsMap = new LinkedHashMap<>();
329+
LinkedHashMap <String, Boolean> successMap = new LinkedHashMap<>();
330+
String namePrefix = simulation.getName() + SimulationContext.ReservedBatchExtensionString;
331+
332+
for(Simulation simCandidate : allSims) {
333+
if(simCandidate.getName().startsWith(namePrefix) && simCandidate.getName().contains("_bat_")) {
334+
335+
int pos = simCandidate.getName().lastIndexOf("_");
336+
String indexName = simCandidate.getName().substring(pos+1);
337+
File currentSimulation = new File(batchResultsDir, indexName + ".txt");
338+
339+
ODESimData simData = null;
340+
importsMap.put(simCandidate.getName(), simCandidate.getSimulationID());
341+
successMap.put(simCandidate.getName(), true); // on failure we'll change to false
308342

309-
// recover the list of batch simulations that belong to this template
310-
Simulation allSims[] = bioModel.getSimulations();
311-
LinkedHashMap<String, String> importsMap = new LinkedHashMap<>();
312-
String namePrefix = simulation.getName() + SimulationContext.ReservedBatchExtensionString;
313-
for(Simulation simCandidate : allSims) {
314-
if(simCandidate.getName().startsWith(namePrefix)) {
315-
importsMap.put(simCandidate.getName(), simCandidate.getSimulationID());
316-
System.out.println(simCandidate.getName() + ": " + simCandidate.getSimulationID());
317-
SwingUtilities.invokeLater(new Runnable() {
318-
public void run() {
319-
// importBatchSimulation(outputContext, simCandidate);
343+
try {
344+
simData = importBatchSimulation(outputContext, simCandidate);
345+
} catch(Exception e) { // whatever fails, we keep going, this is a batch run
346+
System.out.println(simCandidate.getName() + ": failed to recover simulation results");
347+
348+
// TODO: also make a report with detailed exception text
349+
// e.printStackTrace();
350+
successMap.put(simCandidate.getName(), false);
351+
}
352+
if(simData != null) { // write the file
353+
// double[] res = osrs.extractColumn(4);
354+
355+
StringBuilder sb = new StringBuilder();
356+
for(ColumnDescription cd : simData.getDataColumnDescriptions()) {
357+
sb.append(cd.getName() + " ");
358+
}
359+
sb.append("\r\n");
360+
361+
for(double[] row : simData.getRows()) {
362+
for(double entry : row) {
363+
sb.append(entry);
364+
sb.append(" ");
365+
}
366+
sb.append("\r\n");
367+
}
368+
PrintWriter out = new PrintWriter(currentSimulation);
369+
out.print(sb.toString());
370+
out.flush();
371+
out.close();
372+
}
320373
}
321-
});
322-
}
323-
}
324-
// for(String name : importsMap.keySet()) {
325-
// String value = importsMap.get(name);
326-
// SwingUtilities.invokeLater(new Runnable() {
327-
// public void run() {
328-
// // write manifest
374+
}
375+
376+
// for(String name : importsMap.keySet()) {
377+
// String value = importsMap.get(name);
378+
// write some manifest?
379+
// // at least some basic info like the name of the simulation / biomodel / dat file
329380
// }
330-
// });
331-
// }
381+
System.out.println("Done !!!");
382+
} // --- end run()
383+
};
384+
taskList.add(retrieveResultsTask);
385+
AsynchClientTask[] taskArray = new AsynchClientTask[taskList.size()];
386+
taskList.toArray(taskArray);
387+
// knowProgress, cancelable, progressDialogListener
388+
ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), taskArray, false, false, null);
332389
}
333-
private void importBatchSimulation(OutputContext outputContext, Simulation sim) {
334390

335-
File localBatchDir = ResourceUtil.getLocalBatchDir();
336-
File localSimDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
391+
private ODESimData importBatchSimulation(OutputContext outputContext, Simulation sim) throws DataAccessException, RemoteProxyException, ExpressionException {
392+
393+
boolean success = false;
394+
ODESimData simData = null;
337395

338396
if(sim.getVersion() == null) {
339397
throw new RuntimeException("Missing Version.");
@@ -346,10 +404,18 @@ private void importBatchSimulation(OutputContext outputContext, Simulation sim)
346404
User usr = sim.getVersion().getOwner();
347405

348406

349-
// VCSimulationIdentifier authoritativeVCSimulationIdentifier = simInfo.getAuthoritativeVCSimulationIdentifier();
350-
// // sim.getScanCount() number of jobs - is 0
351-
// VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(authoritativeVCSimulationIdentifier, 0);
352-
// VCellClientTest.getVCellClient().getClientServerManager().getDataSetController().getODEData(vcSimulationDataIdentifier);
407+
VCSimulationIdentifier asi = simInfo.getAuthoritativeVCSimulationIdentifier();
408+
// sim.getScanCount() number of jobs must be one, so job index is 0
409+
VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(asi, 0);
410+
411+
ClientServerManager csm = VCellClientTest.getVCellClient().getClientServerManager();
412+
DataSetController dsc = csm.getDataSetController();
413+
simData = dsc.getODEData(vcSimulationDataIdentifier);
414+
415+
System.out.println(sim.getName() + ": simulation results recovered");
416+
return simData;
417+
418+
353419

354420
/*
355421
@@ -393,6 +459,7 @@ private void importBatchSimulation(OutputContext outputContext, Simulation sim)
393459
}
394460
395461
*/
462+
396463
}
397464

398465
private AsynchClientTask[] showSimulationResults0(final boolean isLocal, final ViewerType viewerType) {

vcell-client/src/main/java/cbit/vcell/client/desktop/simulation/SimulationListPanel.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void actionPerformed(java.awt.event.ActionEvent e) {
150150
stopSimulations();
151151
} else if (e.getSource() == exportBatchButton) {
152152
// DialogUtils.showInfoDialog(SimulationListPanel.this, "Under Construction");
153-
importBatchSimulations();
153+
getBatchSimulationsResults();
154154
} else if (e.getSource() == importBatchButton) {
155155
createBatchSimulations();
156156
} else if (e.getSource() == getNativeResultsButton()) {
@@ -278,7 +278,7 @@ private void createBatchSimulations() {
278278
getScrollPaneTable().scrollRectToVisible(getScrollPaneTable().getCellRect(index, 0, true));
279279
}
280280

281-
private void importBatchSimulations() {
281+
private void getBatchSimulationsResults() {
282282
int[] selections = getScrollPaneTable().getSelectedRows();
283283
if(selections.length != 1) {
284284
throw new RuntimeException("Exactly one template Simulation is required for Batch results Import");
@@ -289,8 +289,17 @@ private void importBatchSimulations() {
289289
Simulation[] toImport = (Simulation[])BeanUtils.getArray(v, Simulation.class);
290290
int index = -1;
291291

292+
// one way to choose dir for results
293+
// other way in ClientSimManager.getBatchSimulationResults()
294+
// UserPreferences up = getSimulationWorkspace().getClientSimManager().getUserPreferences();
295+
// File batchOutputDir = chooseBatchOutputDirectory(up);
296+
// if(batchOutputDir == null) {
297+
// System.out.println("Failed to create batch output directory or user canceled");
298+
// return;
299+
// }
300+
292301
try {
293-
index = getSimulationWorkspace().importBatchSimulations(toImport, this);
302+
index = getSimulationWorkspace().getBatchSimulationsResults(toImport, this);
294303
} catch (Throwable exc) {
295304
exc.printStackTrace(System.out);
296305
PopupGenerator.showErrorDialog(this, exc.getMessage(), exc);
@@ -300,6 +309,11 @@ private void importBatchSimulations() {
300309
getScrollPaneTable().scrollRectToVisible(getScrollPaneTable().getCellRect(index, 0, true));
301310
}
302311

312+
private File chooseBatchOutputDirectory(UserPreferences userPreferences) {
313+
File batchOutputDirectory = null;
314+
315+
return batchOutputDirectory;
316+
}
303317
private File parseBatchInputFile(UserPreferences userPreferences, Map<Integer, Map<String, String>> batchInputDataMap) {
304318

305319
StringBuffer stringBuffer = new StringBuffer();

vcell-client/src/main/java/cbit/vcell/client/desktop/simulation/SimulationWorkspace.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,15 @@ int createBatchSimulations(Simulation[] sims, Map<Integer, Map<String, String>>
389389
}
390390
return -1;
391391
}
392-
int importBatchSimulations(Simulation[] sims, Component requester) throws java.beans.PropertyVetoException {
392+
int getBatchSimulationsResults(Simulation[] sims, Component requester) throws java.beans.PropertyVetoException {
393393
if (sims == null || sims.length == 0) {
394394
return -1;
395395
}
396396

397397
// sims contains exactly one template simulation
398398
ArrayList<AnnotatedFunction> outputFunctionsList = getSimulationOwner().getOutputFunctionContext().getOutputFunctionsList();
399399
OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
400-
getClientSimManager().importBatchSimulations(outputContext, sims[0]);
400+
getClientSimManager().getBatchSimulationsResults(outputContext, sims[0]);
401401
// getSimulationOwner().importBatchSimulations(sims[0]); // was in simContext initially
402402
return -1;
403403
}

vcell-core/src/main/java/cbit/vcell/resource/ResourceUtil.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,30 @@ public static File getLocalRootDir()
276276
return localRootDir;
277277
}
278278

279+
private static void deleteRecursively(File f) throws IOException {
280+
if (f.isDirectory()) {
281+
for (File c : f.listFiles()) {
282+
deleteRecursively(c);
283+
}
284+
}
285+
if (!f.delete()) {
286+
throw new FileNotFoundException("Failed to delete file: " + f);
287+
}
288+
}
279289
public static File getLocalBatchDir()
280290
{
281-
if(localBatchDir == null)
282-
{
283-
localBatchDir = new File(getVcellHome(), "batchdata");
284-
if (!localBatchDir.exists()) {
285-
localBatchDir.mkdirs();
291+
File adir = new File(getVcellHome(), "batchResults");
292+
if(adir.exists()) {
293+
try {
294+
deleteRecursively(adir); // delete the output directory and all its content recursively
295+
} catch (IOException e) {
296+
System.err.println("Failed to empty " + adir.getName());
286297
}
287298
}
299+
if(!adir.exists()) {
300+
adir.mkdirs();
301+
}
302+
localBatchDir = adir;
288303
return localBatchDir;
289304
}
290305

0 commit comments

Comments
 (0)