use of cbit.vcell.mapping.MathMappingCallbackTaskAdapter in project vcell by virtualcell.
the class BioModelEditor method popupMenuActionPerformed.
@Override
protected void popupMenuActionPerformed(DocumentEditorPopupMenuAction action, String actionCommand) {
Model model = bioModel.getModel();
final SimulationContext selectedSimulationContext = getSelectedSimulationContext();
switch(action) {
case add_new:
try {
Object obj = documentEditorTree.getLastSelectedPathComponent();
if (obj == null || !(obj instanceof BioModelNode)) {
return;
}
BioModelNode selectedNode = (BioModelNode) obj;
Object userObject = selectedNode.getUserObject();
if (userObject instanceof DocumentEditorTreeFolderNode) {
DocumentEditorTreeFolderClass folderClass = ((DocumentEditorTreeFolderNode) userObject).getFolderClass();
Object newObject = null;
switch(folderClass) {
case REACTIONS_NODE:
// TODO: should add a Add New Rule menu item
newObject = model.createSimpleReaction(model.getStructure(0));
break;
case STRUCTURES_NODE:
newObject = model.createFeature();
break;
case SPECIES_NODE:
newObject = model.createSpeciesContext(model.getStructure(0));
break;
case MOLECULAR_TYPES_NODE:
MolecularType mt = model.getRbmModelContainer().createMolecularType();
model.getRbmModelContainer().addMolecularType(mt, true);
newObject = mt;
break;
case OBSERVABLES_NODE:
if (bioModel.getModel().getRbmModelContainer().getMolecularTypeList().isEmpty()) {
PopupGenerator.showInfoDialog(this, VCellErrorMessages.MustBeRuleBased);
return;
}
RbmObservable o = model.getRbmModelContainer().createObservable(RbmObservable.ObservableType.Molecules);
model.getRbmModelContainer().addObservable(o);
SpeciesPattern sp = new SpeciesPattern();
o.addSpeciesPattern(sp);
newObject = o;
break;
case SIMULATIONS_NODE:
if (selectedSimulationContext != null) {
AsynchClientTask task1 = new AsynchClientTask("new simulation", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
selectedSimulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
}
};
AsynchClientTask task2 = new AsynchClientTask("new simulation", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
Object newsim = selectedSimulationContext.addNewSimulation(SimulationOwner.DEFAULT_SIM_NAME_PREFIX, callback, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
selectionManager.setSelectedObjects(new Object[] { newsim });
}
};
ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 });
}
break;
default:
break;
}
if (newObject != null) {
selectionManager.setSelectedObjects(new Object[] { newObject });
}
}
} catch (Exception ex) {
DialogUtils.showErrorDialog(this, ex.getMessage());
}
break;
case add_new_app_deterministic:
newApplication(Application.NETWORK_DETERMINISTIC);
break;
case add_new_app_stochastic:
newApplication(Application.NETWORK_STOCHASTIC);
break;
case add_new_app_rulebased:
{
// if(model.getStructures().length > 1) {
// DialogUtils.showErrorDialog(this, VCellErrorMessages.NFSimAppNotAllowedForMultipleStructures);
// return;
// }
newApplication(Application.RULE_BASED_STOCHASTIC);
break;
}
case copy_app:
ApplicationActionCommand acc = ApplicationActionCommand.lookup(actionCommand);
switch(acc.actionType()) {
case COPY_AS_IS:
copyApplication();
break;
case COPY_CHANGE:
boolean bothSpatial = acc.isSourceSpatial() && acc.isDestSpatial();
// if(acc.getAppType().equals(SimulationContext.Application.RULE_BASED_STOCHASTIC) && model.getStructures().length > 1) {
// DialogUtils.showErrorDialog(this, VCellErrorMessages.NFSimAppNotAllowedForMultipleStructures);
// return;
// }
copyApplication(bothSpatial, acc.getAppType());
break;
case CREATE:
// not used in this menu
throw new UnsupportedOperationException();
}
break;
case app_new_biomodel:
if (actionCommand.equals(GuiConstants.MENU_TEXT_APP_NEWBIOMODEL)) {
createNewBiomodelFromApp();
}
break;
case delete:
try {
if (selectedSimulationContext != null) {
String confirm = PopupGenerator.showOKCancelWarningDialog(this, "Deleting application", "You are going to delete the Application '" + selectedSimulationContext.getName() + "'. Continue?");
if (confirm.equals(UserMessage.OPTION_CANCEL)) {
return;
}
deleteSimulationcontexts(new SimulationContext[] { selectedSimulationContext });
}
} catch (Exception ex) {
DialogUtils.showErrorDialog(this, ex.getMessage());
}
break;
case deleteChoose:
try {
SimulationContext[] allSimContexts = Arrays.copyOf(getBioModelWindowManager().getVCDocument().getSimulationContexts(), getBioModelWindowManager().getVCDocument().getSimulationContexts().length);
Arrays.sort(allSimContexts, new Comparator<SimulationContext>() {
@Override
public int compare(SimulationContext o1, SimulationContext o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
String[][] rowDataOrig = new String[allSimContexts.length][2];
for (int i = 0; i < allSimContexts.length; i++) {
rowDataOrig[i][0] = allSimContexts[i].getName();
rowDataOrig[i][1] = allSimContexts[i].getSimulations().length + "";
}
final String DELETE = "Delete";
final String CANCEL = "Cancel";
TableListResult result = DialogUtils.showComponentOptionsTableList(this, "Select Applications (and associated Simulations) to delete.", new String[] { "Application", "# of Sims" }, rowDataOrig, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, null, new String[] { DELETE, CANCEL }, CANCEL, null);
if (result != null && result.selectedOption != null && result.selectedOption.equals(DELETE) && result.selectedTableRows != null && result.selectedTableRows.length > 0) {
ArrayList<SimulationContext> deleteTheseSimcontexts = new ArrayList<SimulationContext>();
for (int i = 0; i < result.selectedTableRows.length; i++) {
deleteTheseSimcontexts.add(allSimContexts[result.selectedTableRows[i]]);
}
deleteSimulationcontexts(deleteTheseSimcontexts.toArray(new SimulationContext[0]));
}
} catch (Exception ex) {
DialogUtils.showErrorDialog(this, ex.getMessage());
}
break;
default:
break;
}
}
use of cbit.vcell.mapping.MathMappingCallbackTaskAdapter in project vcell by virtualcell.
the class SimulationListPanel method newSimulation.
/**
* Comment
*/
private void newSimulation(final NetworkGenerationRequirements networkGenerationRequirements) {
AsynchClientTask task1 = new AsynchClientTask("new simulation", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
MathMappingCallback mathMappingCallback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
if (getSimulationWorkspace().getSimulationOwner() instanceof SimulationContext) {
SimulationContext simulationContext = (SimulationContext) getSimulationWorkspace().getSimulationOwner();
simulationContext.refreshMathDescription(mathMappingCallback, networkGenerationRequirements);
}
}
};
AsynchClientTask task2 = new AsynchClientTask("new simulation", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
MathMappingCallback mathMappingCallback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
int newSimIndex = getSimulationWorkspace().newSimulation(SimulationListPanel.this, mathMappingCallback, networkGenerationRequirements);
getScrollPaneTable().getSelectionModel().setSelectionInterval(newSimIndex, newSimIndex);
getScrollPaneTable().scrollRectToVisible(getScrollPaneTable().getCellRect(newSimIndex, 0, true));
}
};
ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 });
}
use of cbit.vcell.mapping.MathMappingCallbackTaskAdapter in project vcell by virtualcell.
the class ClientTaskManager method copyApplication.
public static AsynchClientTask[] copyApplication(final JComponent requester, final BioModel bioModel, final SimulationContext simulationContext, final boolean bSpatial, final SimulationContext.Application appType) {
// get valid application name
String newApplicationName = null;
String baseName = "Copy of " + simulationContext.getName();
int count = 0;
while (true) {
if (count == 0) {
newApplicationName = baseName;
} else {
newApplicationName = baseName + " " + count;
}
if (bioModel.getSimulationContext(newApplicationName) == null) {
break;
}
count++;
}
final String newName = newApplicationName;
AsynchClientTask task1 = new AsynchClientTask("preparing to copy", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
SimulationContext newSimulationContext = SimulationContext.copySimulationContext(simulationContext, newName, bSpatial, appType);
newSimulationContext.getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT());
if (newSimulationContext.isSameTypeAs(simulationContext)) {
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
String oldHash = simulationContext.getMd5hash();
BNGOutputSpec oldSpec = simulationContext.getMostRecentlyCreatedOutputSpec();
if (oldHash != null && oldSpec != null) {
// Warning: the results from "Edit / Test Constraints" are never cached because we want to repeatedly run it
// even if nothing changed
newSimulationContext.setMd5hash(oldHash);
newSimulationContext.setMostRecentlyCreatedOutputSpec(oldSpec);
newSimulationContext.setInsufficientIterations(simulationContext.isInsufficientIterations());
newSimulationContext.setInsufficientMaxMolecules(simulationContext.isInsufficientMaxMolecules());
}
newSimulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
}
hashTable.put("newSimulationContext", newSimulationContext);
}
};
AsynchClientTask task2 = new AsynchClientTask("copying application and simulations", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
SimulationContext newSimulationContext = (SimulationContext) hashTable.get("newSimulationContext");
bioModel.addSimulationContext(newSimulationContext);
if (newSimulationContext.isSameTypeAs(simulationContext)) {
// copy simulations to new simContext
for (Simulation sim : simulationContext.getSimulations()) {
Simulation clonedSimulation = new Simulation(sim, false);
clonedSimulation.setMathDescription(newSimulationContext.getMathDescription());
clonedSimulation.setName(simulationContext.getBioModel().getFreeSimulationName());
newSimulationContext.addSimulation(clonedSimulation);
}
// copy output functions to new simContext
ArrayList<AnnotatedFunction> outputFunctions = simulationContext.getOutputFunctionContext().getOutputFunctionsList();
ArrayList<AnnotatedFunction> newOutputFunctions = new ArrayList<AnnotatedFunction>();
for (AnnotatedFunction afn : outputFunctions) {
newOutputFunctions.add(new AnnotatedFunction(afn));
}
newSimulationContext.getOutputFunctionContext().setOutputFunctions(newOutputFunctions);
} else {
if (simulationContext.getSimulations().length > 0) {
DialogUtils.showWarningDialog(requester, "Simulations are not copied because new application is of different type.");
}
}
}
};
return new AsynchClientTask[] { task1, task2 };
}
use of cbit.vcell.mapping.MathMappingCallbackTaskAdapter in project vcell by virtualcell.
the class RuleBasedTest method checkNonspatialStochasticSimContext.
private static void checkNonspatialStochasticSimContext(SimulationContext srcSimContext, File baseDirectory, int numTrials) throws Exception {
if (!srcSimContext.getApplicationType().equals(Application.NETWORK_STOCHASTIC) || srcSimContext.getGeometry().getDimension() != 0) {
throw new RuntimeException("simContext is of type " + srcSimContext.getApplicationType() + " and geometry dimension of " + srcSimContext.getGeometry().getDimension() + ", expecting nonspatial stochastic");
}
BioModel origBioModel = srcSimContext.getBioModel();
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(XmlHelper.bioModelToXML(origBioModel)));
bioModel.refreshDependencies();
// create ODE and RuleBased
SimulationContext newODEApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewODEApp", false, Application.NETWORK_DETERMINISTIC);
SimulationContext newRuleBasedApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewRuleBasedApp", false, Application.RULE_BASED_STOCHASTIC);
newODEApp.setBioModel(bioModel);
newRuleBasedApp.setBioModel(bioModel);
ArrayList<AnnotatedFunction> outputFunctionsList = srcSimContext.getOutputFunctionContext().getOutputFunctionsList();
// OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
newODEApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
newRuleBasedApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
NetworkGenerationRequirements networkGenerationRequirements = NetworkGenerationRequirements.AllowTruncatedStandardTimeout;
bioModel.addSimulationContext(newODEApp);
newODEApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
bioModel.addSimulationContext(newRuleBasedApp);
newRuleBasedApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
srcSimContext.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
// Create non-spatialStoch, ODE and RuleBased sims
Simulation nonspatialStochAppNewSim = srcSimContext.addNewSimulation(STOCH_SIM_NAME, /*SimulationOwner.DEFAULT_SIM_NAME_PREFIX*/
new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
Simulation newODEAppNewSim = newODEApp.addNewSimulation(ODE_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
Simulation newRuleBasedAppNewSim = newRuleBasedApp.addNewSimulation(NFS_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenerationRequirements);
nonspatialStochAppNewSim.setSimulationOwner(srcSimContext);
newODEAppNewSim.setSimulationOwner(newODEApp);
newRuleBasedAppNewSim.setSimulationOwner(newRuleBasedApp);
try {
bioModel.getModel().getSpeciesContexts();
ArrayList<String> varNameList = new ArrayList<String>();
for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
varNameList.add(scs.getSpeciesContext().getName());
}
String[] varNames = varNameList.toArray(new String[0]);
OutputTimeSpec outputTimeSpec = nonspatialStochAppNewSim.getSolverTaskDescription().getOutputTimeSpec();
ArrayList<Double> sampleTimeList = new ArrayList<Double>();
if (outputTimeSpec instanceof UniformOutputTimeSpec) {
double endingTime = nonspatialStochAppNewSim.getSolverTaskDescription().getTimeBounds().getEndingTime();
double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
int currTimeIndex = 0;
while (currTimeIndex * dT <= (endingTime + 1e-8)) {
sampleTimeList.add(currTimeIndex * dT);
currTimeIndex++;
}
}
double[] sampleTimes = new double[sampleTimeList.size()];
for (int i = 0; i < sampleTimes.length; i++) {
sampleTimes[i] = sampleTimeList.get(i);
}
TimeSeriesMultitrialData sampleDataStoch1 = new TimeSeriesMultitrialData("stochastic1", varNames, sampleTimes, numTrials);
TimeSeriesMultitrialData sampleDataStoch2 = new TimeSeriesMultitrialData("stochastic2", varNames, sampleTimes, numTrials);
TimeSeriesMultitrialData sampleDataDeterministic = new TimeSeriesMultitrialData("determinstic", varNames, sampleTimes, 1);
runsolver(nonspatialStochAppNewSim, baseDirectory, numTrials, sampleDataStoch1);
runsolver(newODEAppNewSim, baseDirectory, 1, sampleDataDeterministic);
runsolver(newRuleBasedAppNewSim, baseDirectory, numTrials, sampleDataStoch2);
writeVarDiffData(baseDirectory, sampleDataStoch1, sampleDataStoch2);
writeKolmogorovSmirnovTest(baseDirectory, sampleDataStoch1, sampleDataStoch2);
writeChiSquareTest(baseDirectory, sampleDataStoch1, sampleDataStoch2);
writeData(baseDirectory, sampleDataStoch1);
writeData(baseDirectory, sampleDataStoch2);
writeData(baseDirectory, sampleDataDeterministic);
} finally {
srcSimContext.removeSimulation(nonspatialStochAppNewSim);
newODEApp.removeSimulation(newODEAppNewSim);
newRuleBasedApp.removeSimulation(newRuleBasedAppNewSim);
}
}
use of cbit.vcell.mapping.MathMappingCallbackTaskAdapter in project vcell by virtualcell.
the class OutputFunctionsPanel method addOutputFunction.
private void addOutputFunction() {
if (simulationWorkspace == null) {
return;
}
AsynchClientTask task1 = new AsynchClientTask("refresh math description", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
MathMappingCallback mathMappingCallback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
if (simulationWorkspace.getSimulationOwner() instanceof SimulationContext) {
SimulationContext simulationContext = (SimulationContext) simulationWorkspace.getSimulationOwner();
simulationContext.refreshMathDescription(mathMappingCallback, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
}
// else, for mathModels, nothing to refresh.
}
};
AsynchClientTask task2 = new AsynchClientTask("show dialog", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
ArrayList<AnnotatedFunction> outputFunctionList = outputFunctionContext.getOutputFunctionsList();
String defaultName = null;
int count = 0;
while (true) {
boolean nameUsed = false;
count++;
defaultName = "func" + count;
for (AnnotatedFunction function : outputFunctionList) {
if (function.getName().equals(defaultName)) {
nameUsed = true;
}
}
if (!nameUsed) {
break;
}
}
final boolean bSpatial = simulationWorkspace.getSimulationOwner().getGeometry().getDimension() > 0;
// for non-spatial application, set 'Next' to 'Finish'.
if (!bSpatial) {
getNextButton().setText("Finish");
} else {
getNextButton().setText("Next >>");
}
getFunctionNameTextField().setText(defaultName);
getFunctionExpressionTextField().setText("0.0");
Set<String> autoCompList = new HashSet<String>();
Map<String, SymbolTableEntry> entryMap = new HashMap<String, SymbolTableEntry>();
outputFunctionContext.getEntries(entryMap);
autoCompList = entryMap.keySet();
getFunctionExpressionTextField().setAutoCompletionWords(autoCompList);
getFunctionExpressionTextField().setSymbolTable(outputFunctionContext);
//
// Show the editor with a default name and default expression for the function
// If the OK option is chosen, get the new name and expression for the function and create a new
// function, add it to the list of output functions in simulationOwner
// Else, pop-up an error dialog indicating that function cannot be added.
//
cardLayout.show(getAddFunctionPanel(), funcNameAndExprPanel.getName());
DialogUtils.showModalJDialogOnTop(getAddFunctionDialog(), OutputFunctionsPanel.this);
}
};
ClientTaskDispatcher.dispatch(OutputFunctionsPanel.this, new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 });
}
Aggregations