use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class InitialConditionsPanel method jMenuItemPaste_ActionPerformed.
/**
* Comment
*/
private void jMenuItemPaste_ActionPerformed(final java.awt.event.ActionEvent actionEvent) {
final Vector<String> pasteDescriptionsV = new Vector<String>();
final Vector<Expression> newExpressionsV = new Vector<Expression>();
final Vector<SpeciesContextSpec.SpeciesContextSpecParameter> changedParametersV = new Vector<SpeciesContextSpec.SpeciesContextSpecParameter>();
AsynchClientTask task1 = new AsynchClientTask("validating", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
if (actionEvent.getSource() == getJMenuItemPaste() || actionEvent.getSource() == getJMenuItemPasteAll()) {
Object pasteThis = VCellTransferable.getFromClipboard(VCellTransferable.OBJECT_FLAVOR);
MathSymbolMapping msm = null;
Exception mathMappingException = null;
try {
MathMapping mm = null;
mm = getSimulationContext().createNewMathMapping();
msm = mm.getMathSymbolMapping();
} catch (Exception e) {
mathMappingException = e;
e.printStackTrace(System.out);
}
int[] rows = null;
if (actionEvent.getSource() == getJMenuItemPasteAll()) {
rows = new int[getScrollPaneTable().getRowCount()];
for (int i = 0; i < rows.length; i += 1) {
rows[i] = i;
}
} else {
rows = getScrollPaneTable().getSelectedRows();
}
//
// Check paste
//
StringBuffer errors = null;
for (int i = 0; i < rows.length; i += 1) {
SpeciesContextSpec scs = tableModel.getValueAt(rows[i]);
try {
if (pasteThis instanceof VCellTransferable.ResolvedValuesSelection) {
VCellTransferable.ResolvedValuesSelection rvs = (VCellTransferable.ResolvedValuesSelection) pasteThis;
for (int j = 0; j < rvs.getPrimarySymbolTableEntries().length; j += 1) {
SpeciesContextSpec.SpeciesContextSpecParameter pasteDestination = null;
SpeciesContextSpec.SpeciesContextSpecParameter clipboardBiologicalParameter = null;
if (rvs.getPrimarySymbolTableEntries()[j] instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
clipboardBiologicalParameter = (SpeciesContextSpec.SpeciesContextSpecParameter) rvs.getPrimarySymbolTableEntries()[j];
} else if (rvs.getAlternateSymbolTableEntries() != null && rvs.getAlternateSymbolTableEntries()[j] instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
clipboardBiologicalParameter = (SpeciesContextSpec.SpeciesContextSpecParameter) rvs.getAlternateSymbolTableEntries()[j];
}
if (clipboardBiologicalParameter == null) {
Variable pastedMathVariable = null;
if (rvs.getPrimarySymbolTableEntries()[j] instanceof Variable) {
pastedMathVariable = (Variable) rvs.getPrimarySymbolTableEntries()[j];
} else if (rvs.getAlternateSymbolTableEntries() != null && rvs.getAlternateSymbolTableEntries()[j] instanceof Variable) {
pastedMathVariable = (Variable) rvs.getAlternateSymbolTableEntries()[j];
}
if (pastedMathVariable != null) {
if (msm == null) {
throw mathMappingException;
}
Variable localMathVariable = msm.findVariableByName(pastedMathVariable.getName());
if (localMathVariable == null) {
// try if localMathVariable is a speciesContext init parameter
String initSuffix = DiffEquMathMapping.MATH_FUNC_SUFFIX_SPECIES_INIT_CONC_UNIT_PREFIX + TokenMangler.fixTokenStrict(scs.getInitialConcentrationParameter().getUnitDefinition().getSymbol());
localMathVariable = msm.findVariableByName(pastedMathVariable.getName() + initSuffix);
}
if (localMathVariable != null) {
SymbolTableEntry[] localBiologicalSymbolArr = msm.getBiologicalSymbol(localMathVariable);
for (int k = 0; k < localBiologicalSymbolArr.length; k += 1) {
if (localBiologicalSymbolArr[k] instanceof SpeciesContext && scs.getSpeciesContext() == localBiologicalSymbolArr[k]) {
// need to change
pasteDestination = scs.getInitialConditionParameter();
} else if (localBiologicalSymbolArr[k] instanceof SpeciesContextSpec.SpeciesContextSpecParameter) {
for (int l = 0; l < scs.getParameters().length; l += 1) {
if (scs.getParameters()[l] == localBiologicalSymbolArr[k]) {
pasteDestination = (SpeciesContextSpec.SpeciesContextSpecParameter) localBiologicalSymbolArr[k];
break;
}
}
}
if (pasteDestination != null) {
break;
}
}
}
}
} else {
for (int k = 0; k < scs.getParameters().length; k += 1) {
SpeciesContextSpec.SpeciesContextSpecParameter scsp = (SpeciesContextSpec.SpeciesContextSpecParameter) scs.getParameters()[k];
if (scsp.getRole() == clipboardBiologicalParameter.getRole() && scs.getSpeciesContext().compareEqual(((SpeciesContextSpec) clipboardBiologicalParameter.getNameScope().getScopedSymbolTable()).getSpeciesContext())) {
pasteDestination = (SpeciesContextSpec.SpeciesContextSpecParameter) scsp;
}
}
}
if (pasteDestination != null) {
changedParametersV.add(pasteDestination);
newExpressionsV.add(rvs.getExpressionValues()[j]);
pasteDescriptionsV.add(VCellCopyPasteHelper.formatPasteList(scs.getSpeciesContext().getName(), pasteDestination.getName(), pasteDestination.getExpression().infix(), rvs.getExpressionValues()[j].infix()));
}
}
}
} catch (Throwable e) {
if (errors == null) {
errors = new StringBuffer();
}
errors.append(scs.getSpeciesContext().getName() + " (" + e.getClass().getName() + ") " + e.getMessage() + "\n\n");
}
}
if (errors != null) {
throw new Exception(errors.toString());
}
}
}
};
AsynchClientTask task2 = new AsynchClientTask("pasting", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
// Do paste
if (pasteDescriptionsV.size() > 0) {
String[] pasteDescriptionArr = new String[pasteDescriptionsV.size()];
pasteDescriptionsV.copyInto(pasteDescriptionArr);
SpeciesContextSpec.SpeciesContextSpecParameter[] changedParametersArr = new SpeciesContextSpec.SpeciesContextSpecParameter[changedParametersV.size()];
changedParametersV.copyInto(changedParametersArr);
Expression[] newExpressionsArr = new Expression[newExpressionsV.size()];
newExpressionsV.copyInto(newExpressionsArr);
VCellCopyPasteHelper.chooseApplyPaste(InitialConditionsPanel.this, pasteDescriptionArr, changedParametersArr, newExpressionsArr);
} else {
PopupGenerator.showInfoDialog(InitialConditionsPanel.this, "No paste items match the destination (no changes made).");
}
}
};
ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), new AsynchClientTask[] { task1, task2 });
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class SpeciesContextSpecsTableModel method getComparator.
public Comparator<SpeciesContextSpec> getComparator(final int col, final boolean ascending) {
return new Comparator<SpeciesContextSpec>() {
/**
* Compares its two arguments for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than, equal
* to, or greater than the second.<p>
*/
public int compare(SpeciesContextSpec speciesContextSpec1, SpeciesContextSpec speciesContextSpec2) {
SpeciesContext speciesContext1 = speciesContextSpec1.getSpeciesContext();
SpeciesContext speciesContext2 = speciesContextSpec2.getSpeciesContext();
ColumnType columnType = columns.get(col);
switch(columnType) {
case COLUMN_SPECIESCONTEXT:
{
String name1 = speciesContext1.getName();
String name2 = speciesContext2.getName();
if (ascending) {
return name1.compareToIgnoreCase(name2);
} else {
return name2.compareToIgnoreCase(name1);
}
}
case COLUMN_STRUCTURE:
{
String name1 = speciesContext1.getStructure().getName();
String name2 = speciesContext2.getStructure().getName();
if (ascending) {
return name1.compareToIgnoreCase(name2);
} else {
return name2.compareToIgnoreCase(name1);
}
}
case COLUMN_CLAMPED:
{
Boolean bClamped1 = new Boolean(speciesContextSpec1.isConstant());
Boolean bClamped2 = new Boolean(speciesContextSpec2.isConstant());
if (ascending) {
return bClamped1.compareTo(bClamped2);
} else {
return bClamped2.compareTo(bClamped1);
}
}
case COLUMN_FORCECONTINUOUS:
{
Boolean bForceContinuous1 = new Boolean(speciesContextSpec1.isForceContinuous());
Boolean bForceContinuous2 = new Boolean(speciesContextSpec2.isForceContinuous());
if (ascending) {
return bForceContinuous1.compareTo(bForceContinuous2);
} else {
return bForceContinuous2.compareTo(bForceContinuous1);
}
}
case COLUMN_WELLMIXED:
{
Boolean bWellMixed1 = new Boolean(speciesContextSpec1.isWellMixed());
Boolean bWellMixed2 = new Boolean(speciesContextSpec2.isWellMixed());
if (ascending) {
return bWellMixed1.compareTo(bWellMixed2);
} else {
return bWellMixed2.compareTo(bWellMixed1);
}
}
case COLUMN_INITIAL:
{
Expression initExp1 = speciesContextSpec1.getInitialConditionParameter().getExpression();
Expression initExp2 = speciesContextSpec2.getInitialConditionParameter().getExpression();
return TableUtil.expressionCompare(initExp1, initExp2, ascending);
}
case COLUMN_DIFFUSION:
{
Expression diffExp1 = speciesContextSpec1.getDiffusionParameter().getExpression();
Expression diffExp2 = speciesContextSpec2.getDiffusionParameter().getExpression();
return TableUtil.expressionCompare(diffExp1, diffExp2, ascending);
}
}
return 1;
}
};
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class ParameterTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Parameter parameter = getValueAt(rowIndex);
// try {
switch(columnIndex) {
case COLUMN_NAME:
{
try {
if (aValue instanceof String) {
String newName = (String) aValue;
if (!parameter.getName().equals(newName)) {
if (parameter instanceof Kinetics.KineticsParameter) {
reactionStep.getKinetics().renameParameter(parameter.getName(), newName);
} else if (parameter instanceof Kinetics.KineticsProxyParameter) {
parameter.setName(newName);
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
}
break;
}
case COLUMN_IS_GLOBAL:
{
if (aValue.equals(Boolean.FALSE)) {
// check box has been <unset> (<true> to <false>) : change param from global to local
if ((parameter instanceof KineticsProxyParameter) && ((((KineticsProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((KineticsProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((KineticsProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((KineticsProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
} else {
try {
reactionStep.getKinetics().convertParameterType(parameter, false);
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + e.getMessage());
}
}
} else {
// check box has been <set> (<false> to <true>) : change param from local to global
if ((parameter instanceof KineticsParameter) && (((KineticsParameter) parameter).getRole() != Kinetics.ROLE_UserDefined)) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a pre-defined kinetics parameter (not user-defined); cannot convert it to a model level (global) parameter.");
} else {
ModelParameter mp = reactionStep.getKinetics().getReactionStep().getModel().getModelParameter(parameter.getName());
// model already had the model parameter 'param', but check if 'param' value is different from
// model parameter with same name. If it is, the local value will be overridden by global (model) param
// value, and user should be warned.
String choice = "Ok";
if (mp != null && !(mp.getExpression().compareEqual(parameter.getExpression()))) {
String msgStr = "Model already has a global parameter named : \'" + parameter.getName() + "\'; with value = \'" + mp.getExpression().infix() + "\'; This local parameter \'" + parameter.getName() + "\' with value = \'" + parameter.getExpression().infix() + "\' will be overridden by the global value. \nPress \'Ok' to override " + "local value with global value of \'" + parameter.getName() + "\'. \nPress \'Cancel\' to retain new local value.";
choice = PopupGenerator.showWarningDialog(ownerTable, msgStr, new String[] { "Ok", "Cancel" }, "Ok");
}
if (choice.equals("Ok")) {
try {
// Now 'parameter' is a local kinetic parameter. If it is not numeric, and if its expression
// contains other local kinetic parameters, warn user that 'parameter' cannot be promoted because
// of its expression containing other local parameters.
boolean bPromoteable = true;
if (!parameter.getExpression().isNumeric()) {
String[] symbols = parameter.getExpression().getSymbols();
for (int i = 0; i < symbols.length; i++) {
if (reactionStep.getKinetics().getKineticsParameter(symbols[i]) != null) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter \'" + parameter.getName() + "\' contains other local kinetic parameters; Cannot convert it to global until the referenced parameters are global.");
bPromoteable = false;
}
}
}
if (bPromoteable) {
reactionStep.getKinetics().convertParameterType(parameter, true);
}
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + e.getMessage());
}
}
}
}
fireTableRowsUpdated(rowIndex, rowIndex);
break;
}
case COLUMN_VALUE:
{
try {
if (aValue instanceof ScopedExpression) {
// }
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
if (parameter instanceof Kinetics.KineticsParameter) {
reactionStep.getKinetics().setParameterValue((Kinetics.KineticsParameter) parameter, new Expression(newExpressionString));
} else if (parameter instanceof Kinetics.KineticsProxyParameter) {
parameter.setExpression(new Expression(newExpressionString));
}
}
reactionStep.getKinetics().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error:\n" + e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_UNITS:
{
try {
if (aValue instanceof String && parameter instanceof Kinetics.KineticsParameter && ((Kinetics.KineticsParameter) parameter).getRole() == Kinetics.ROLE_UserDefined) {
String newUnitString = (String) aValue;
Kinetics.KineticsParameter kineticsParm = (Kinetics.KineticsParameter) parameter;
ModelUnitSystem modelUnitSystem = reactionStep.getModel().getUnitSystem();
if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
reactionStep.getKinetics().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (VCUnitException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter unit:\n" + e.getMessage());
}
break;
}
}
// }catch (java.beans.PropertyVetoException e){
// e.printStackTrace(System.out);
// }
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class GeneratedSpeciesTableRow method deriveSpecies.
private void deriveSpecies(String inputString, Model tempModel) {
if (owner != null && owner.getSimulationContext() != null) {
List<MolecularType> mtList = owner.getSimulationContext().getModel().getRbmModelContainer().getMolecularTypeList();
try {
tempModel.getRbmModelContainer().setMolecularTypeList(mtList);
} catch (PropertyVetoException e1) {
e1.printStackTrace();
throw new RuntimeException("Unexpected exception setting " + MolecularType.typeName + " list: " + e1.getMessage(), e1);
}
} else {
System.out.println("something is wrong, we just do nothing rather than crash");
return;
}
try {
String strStructure = null;
if (inputString.contains(RbmUtils.SiteStruct)) {
// we are in the mode where we emulate compartments by adding the compartment name as a fake site
Pair<List<String>, String> p = RbmUtils.extractCompartment(inputString);
strStructure = p.one.get(0);
inputString = p.two;
} else {
// should be the normal @comp:expression format - if it's not it will return null
strStructure = RbmUtils.parseCompartment(inputString, tempModel);
}
Structure structure;
if (strStructure != null) {
if (tempModel.getStructure(strStructure) == null) {
tempModel.addFeature(strStructure);
}
structure = tempModel.getStructure(strStructure);
} else {
structure = tempModel.getStructure(0);
}
SpeciesPattern sp = (SpeciesPattern) RbmUtils.parseSpeciesPattern(inputString, tempModel);
sp.resolveBonds();
// System.out.println(sp.toString());
species = new SpeciesContext(new Species("a", ""), structure, sp);
} catch (ParseException | PropertyVetoException | ModelException e1) {
e1.printStackTrace();
}
}
use of cbit.vcell.model.SpeciesContext in project vcell by virtualcell.
the class NetworkFreePanel method refreshInterface.
public void refreshInterface() {
int s1 = 0;
int s2 = 0;
for (SpeciesContext sc : fieldSimulationContext.getModel().getSpeciesContexts()) {
if (sc.hasSpeciesPattern()) {
s1++;
} else {
s2++;
}
}
int s3 = fieldSimulationContext.getModel().getNumSpeciesContexts();
int r1 = fieldSimulationContext.getModel().getNumReactions();
int r2 = fieldSimulationContext.getModel().getRbmModelContainer().getReactionRuleList().size();
int m1 = fieldSimulationContext.getModel().getRbmModelContainer().getMolecularTypeList().size();
int o1 = fieldSimulationContext.getModel().getRbmModelContainer().getObservableList().size();
speciesLabel.setText(s2 + "");
speciesMoleculesLabel.setText(s1 + "");
futureSpeciesLabel.setText(s1 + s2 + "");
reactionsLabel.setText(r1 + "");
rulesLabel.setText(r2 + "");
futureRulesLabel.setText((r1 + r2) + "");
molecularTypesLabel.setText(m1 + "");
observablesLabel.setText(o1 + "");
futureMolecularTypesLabel.setText((m1 + s2) + "");
futureObservablesLabel.setText((o1 + s2) + "");
// if(s1 > 0) {
rateWarningLabel.setText("<html><font color=#8C001A>" + SimulationContext.rateWarning + "</font></html>");
// } else {
// rateWarningLabel.setText("");
// }
createModelButton.setEnabled(true);
}
Aggregations