Search in sources :

Example 96 with SymbolTableEntry

use of cbit.vcell.parser.SymbolTableEntry 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 });
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) SimulationContext(cbit.vcell.mapping.SimulationContext) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) HashSet(java.util.HashSet)

Example 97 with SymbolTableEntry

use of cbit.vcell.parser.SymbolTableEntry in project vcell by virtualcell.

the class ParameterEstimationPanel method mapButton_ActionPerformed.

/**
 * Comment
 */
private void mapButton_ActionPerformed() {
    if (getParameterEstimationTask() == null) {
        return;
    }
    SymbolTableEntry[] symbolTableEntries = getParameterEstimationTask().getModelOptimizationSpec().calculateTimeDependentModelObjects(getParameterEstimationTask().getModelOptimizationSpec().getSimulationContext());
    java.util.Comparator<SymbolTableEntry> steComparator = new java.util.Comparator<SymbolTableEntry>() {

        private Class<?>[] classOrder = new Class<?>[] { Model.ReservedSymbol.class, SpeciesContext.class, Model.ModelParameter.class, Kinetics.KineticsParameter.class };

        public int compare(SymbolTableEntry ste1, SymbolTableEntry ste2) {
            int ste1Category = 100;
            int ste2Category = 100;
            for (int i = 0; i < classOrder.length; i++) {
                if (ste1.getClass().equals(classOrder[i])) {
                    ste1Category = i;
                }
                if (ste2.getClass().equals(classOrder[i])) {
                    ste2Category = i;
                }
            }
            if (ste1Category < ste2Category) {
                return 1;
            } else if (ste1Category > ste2Category) {
                return -1;
            } else {
                return ste1.getName().compareTo(ste2.getName());
            }
        }
    };
    java.util.Arrays.sort(symbolTableEntries, steComparator);
    SymbolTableEntry ste = (SymbolTableEntry) DialogUtils.showListDialog(this, symbolTableEntries, "Map Experimental Data", new SymbolTableEntryListCellRenderer());
    if (ste != null && getDataModelMappingTable().getSelectionModel().getMaxSelectionIndex() >= 0) {
        ReferenceDataMappingSpec refDataMappingSpec = getParameterEstimationTask().getModelOptimizationSpec().getReferenceDataMappingSpecs()[getDataModelMappingTable().getSelectionModel().getMaxSelectionIndex()];
        try {
            refDataMappingSpec.setModelObject(ste);
        } catch (java.beans.PropertyVetoException e) {
            e.printStackTrace(System.out);
            DialogUtils.showErrorDialog(this, e.getMessage());
        }
    }
    return;
}
Also used : ReferenceDataMappingSpec(cbit.vcell.modelopt.ReferenceDataMappingSpec) SymbolTableEntryListCellRenderer(cbit.vcell.modelopt.gui.SymbolTableEntryListCellRenderer) PropertyVetoException(java.beans.PropertyVetoException) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) ReferenceDataMappingSpecTableModel(org.vcell.optimization.gui.ReferenceDataMappingSpecTableModel) AnalysisTaskComboBoxModel(org.vcell.optimization.gui.AnalysisTaskComboBoxModel) Model(cbit.vcell.model.Model) Kinetics(cbit.vcell.model.Kinetics)

Example 98 with SymbolTableEntry

use of cbit.vcell.parser.SymbolTableEntry in project vcell by virtualcell.

the class SimulationRepresentation method getParameters.

private static ParameterRepresentation[] getParameters(BioModel bioModel, SimulationRep simulationRep) {
    SimulationContext simContext = null;
    for (SimulationContext sc : bioModel.getSimulationContexts()) {
        if (sc.getMathDescription().getKey().equals(simulationRep.getMathKey())) {
            simContext = sc;
            break;
        }
    }
    if (simContext == null) {
        return null;
    }
    // initialize to old mathDescription in case error generating math
    MathDescription mathDesc = simContext.getMathDescription();
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathSymbolMapping mathSymbolMapping = null;
    try {
        mathDesc = mathMapping.getMathDescription();
        mathSymbolMapping = mathMapping.getMathSymbolMapping();
    } catch (Exception e1) {
        System.err.println(e1.getMessage());
    }
    ArrayList<ParameterRepresentation> parameterReps = new ArrayList<ParameterRepresentation>();
    Enumeration<Constant> enumMath = mathDesc.getConstants();
    while (enumMath.hasMoreElements()) {
        Constant constant = enumMath.nextElement();
        if (constant.getExpression().isNumeric()) {
            SymbolTableEntry biologicalSymbolTableEntry = null;
            if (mathSymbolMapping != null) {
                SymbolTableEntry[] stes = mathSymbolMapping.getBiologicalSymbol(constant);
                if (stes != null && stes.length >= 1) {
                    biologicalSymbolTableEntry = stes[0];
                }
            }
            if (biologicalSymbolTableEntry instanceof ReservedSymbol) {
                continue;
            }
            try {
                parameterReps.add(new ParameterRepresentation(constant.getName(), constant.getExpression().evaluateConstant(), biologicalSymbolTableEntry));
            } catch (ExpressionException e) {
                // can't happen, because constant expression is numeric
                e.printStackTrace();
            }
        }
    }
    return parameterReps.toArray(new ParameterRepresentation[0]);
}
Also used : MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) ReservedSymbol(cbit.vcell.model.Model.ReservedSymbol) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) MathSymbolMapping(cbit.vcell.mapping.MathSymbolMapping) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MatrixException(cbit.vcell.matrix.MatrixException) MathException(cbit.vcell.math.MathException) ModelException(cbit.vcell.model.ModelException) ExpressionException(cbit.vcell.parser.ExpressionException) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) MathMapping(cbit.vcell.mapping.MathMapping)

Example 99 with SymbolTableEntry

use of cbit.vcell.parser.SymbolTableEntry in project vcell by virtualcell.

the class TableCellEditorAutoCompletion method stopCellEditing.

@Override
public boolean stopCellEditing() {
    if (thisTable.getCellEditor() == null) {
        return true;
    }
    if (textFieldAutoCompletion.getSelectedIndex() >= 0) {
        return false;
    }
    final int editingRow = thisTable.getEditingRow();
    final int editingColumn = thisTable.getEditingColumn();
    textFieldAutoCompletion.stopEditing();
    boolean bExpressionValid = true;
    if (thisTable.getColumnClass(editingColumn).equals(ScopedExpression.class)) {
        if (textFieldAutoCompletion.getSymbolTable() != null) {
            ScopedExpression scopedExpression = (ScopedExpression) thisTable.getValueAt(editingRow, editingColumn);
            String text = textFieldAutoCompletion.getText();
            if (text.trim().length() > 0) {
                try {
                    Expression exp = new Expression(text);
                    exp.validateUnscopedSymbols();
                    if (scopedExpression == null || scopedExpression.isValidateFunctionBinding()) {
                        FunctionInvocation[] functionInvocations = exp.getFunctionInvocations(null);
                        for (FunctionInvocation functionInvocation : functionInvocations) {
                            String formalDefinition = functionInvocation.getFormalDefinition();
                            if (functionInvocation.getFunctionId() == FunctionType.USERDEFINED) {
                                SymbolTableFunctionEntry stfe = (SymbolTableFunctionEntry) textFieldAutoCompletion.getSymbolTable().getEntry(formalDefinition);
                                if (stfe == null) {
                                    // 
                                    // check for wrong number of arguments
                                    // 
                                    Map<String, SymbolTableEntry> entries = new HashMap<String, SymbolTableEntry>();
                                    textFieldAutoCompletion.getSymbolTable().getEntries(entries);
                                    System.out.println("available symbols");
                                    for (String symbol : entries.keySet()) {
                                        System.out.print(symbol + ",");
                                    }
                                    System.out.println("");
                                    throw new ExpressionBindingException("unknown function " + formalDefinition, formalDefinition);
                                }
                            } else {
                                // built in function, check arguments
                                FunctionType functionType = functionInvocation.getFunctionId();
                                String formalDefinitionBuiltin = ASTFuncNode.getFormalDefinition(functionType.getName(), functionType.getArgTypes());
                                if (!formalDefinition.equals(formalDefinitionBuiltin)) {
                                    throw new ExpressionBindingException("expecting " + formalDefinitionBuiltin, formalDefinition);
                                }
                            }
                        }
                    }
                    if (scopedExpression == null || scopedExpression.isValidateIdentifierBinding()) {
                        exp.bindExpression(textFieldAutoCompletion.getSymbolTable());
                    }
                } catch (ExpressionBindingException ex) {
                    ex.printStackTrace(System.out);
                    DialogUtils.showErrorDialog(thisTable.getParent(), ex.getMessage() + "\n\nUse 'Ctrl-Space' to see a list of available names in your model or 'Esc' to revert to the original expression.");
                    bExpressionValid = false;
                } catch (ExpressionException ex) {
                    ex.printStackTrace(System.out);
                    DialogUtils.showErrorDialog(thisTable.getParent(), ex.getMessage() + "\n\nUse 'Esc' to revert to the original expression.");
                    bExpressionValid = false;
                }
            }
        }
    }
    if (!bExpressionValid) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                thisTable.requestFocus();
                thisTable.setRowSelectionInterval(editingRow, editingRow);
                ((JComponent) getComponent()).setBorder(new LineBorder(Color.red));
                textFieldAutoCompletion.requestFocus();
            }
        });
        return false;
    }
    return super.stopCellEditing();
}
Also used : FunctionInvocation(cbit.vcell.parser.FunctionInvocation) HashMap(java.util.HashMap) FunctionType(cbit.vcell.parser.ASTFuncNode.FunctionType) LineBorder(javax.swing.border.LineBorder) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) SymbolTableFunctionEntry(cbit.vcell.parser.SymbolTableFunctionEntry)

Example 100 with SymbolTableEntry

use of cbit.vcell.parser.SymbolTableEntry in project vcell by virtualcell.

the class PlotPane method updateLegend.

/**
 * Comment
 */
private void updateLegend() {
    Plot2D plot = getPlot2DPanel1().getPlot2D();
    SymbolTableEntry[] steList = plot.getSymbolTableEntries();
    DataSymbolMetadataResolver metadataResolver = plot.getDataSymbolMetadataResolver();
    String[] plotLabels = plot.getVisiblePlotColumnTitles();
    int[] plotIndices = plot.getVisiblePlotIndices();
    Component[] legends = getJPanelPlotLegends().getComponents();
    // add legends if necessarry
    if (ml == null) {
        ml = new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                String name = ((JLabel) evt.getSource()).getText();
                getPlot2DPanel1().setCurrentPlot(name);
            }
        };
    }
    for (int i = 0; i < plotIndices.length - legends.length / 2; i++) {
        JLabel line = new JLabel();
        JLabel text = new JLabel();
        line.setBorder(new EmptyBorder(6, 0, 0, 0));
        text.setBorder(new EmptyBorder(0, 8, 6, 0));
        getJPanelPlotLegends().add(line);
        getJPanelPlotLegends().add(text);
        text.addMouseListener(ml);
    }
    legends = getJPanelPlotLegends().getComponents();
    // update labels and show them,use reverse loop to generate non-repeatable colors
    for (int i = (plotIndices.length - 1); i >= 0; i--) {
        LineIcon icon = new LineIcon(getPlot2DPanel1().getVisiblePlotPaint(i));
        String plotLabel = null;
        if (plot instanceof SingleXPlot2D) {
            plotLabel = plotLabels[i + 1];
        } else {
            plotLabel = plotLabels[2 * i + 1];
        }
        ((JLabel) legends[2 * i]).setIcon(icon);
        final int head = 20;
        final int tail = 0;
        String tooltipString = "";
        String unitSymbol = "";
        if (metadataResolver != null) {
            DataSymbolMetadata metaData = metadataResolver.getDataSymbolMetadata(plotLabel);
            if (metaData != null && metaData.unit != null) {
                VCUnitDefinition ud = metaData.unit;
                if (ud != null) {
                    unitSymbol += ud.getSymbolUnicode();
                }
            }
            if (metaData != null && metaData.tooltipString != null) {
                tooltipString = metaData.tooltipString;
            }
        }
        String shortLabel = plotLabel;
        if (shortLabel.length() > head + 3 + tail) {
            shortLabel = shortLabel.substring(0, head) + "..." + shortLabel.substring(shortLabel.length() - tail, shortLabel.length());
        }
        // shortLabel = "<html>" + shortLabel + "<font color=\"red\">" + " [" + unitSymbol + "] " + "</font></html>";
        shortLabel = "<html>" + shortLabel + "<font color=\"#8B0000\">" + " [" + unitSymbol + "] " + "</font></html>";
        tooltipString = "<html>" + plotLabel + "<font color=\"#0000FF\">" + " " + tooltipString + " " + "</font></html>";
        ((JLabel) legends[2 * i + 1]).setText(shortLabel);
        ((JLabel) legends[2 * i + 1]).setToolTipText(tooltipString);
        legends[2 * i].setVisible(true);
        legends[2 * i + 1].setVisible(true);
    }
    // if extra ones, hide them
    for (int i = 2 * plotIndices.length; i < legends.length; i++) {
        legends[i].setVisible(false);
    }
}
Also used : DataSymbolMetadataResolver(cbit.vcell.solver.SimulationModelInfo.DataSymbolMetadataResolver) JLabel(javax.swing.JLabel) EnhancedJLabel(org.vcell.util.gui.EnhancedJLabel) Paint(java.awt.Paint) SingleXPlot2D(cbit.plot.SingleXPlot2D) DataSymbolMetadata(cbit.vcell.solver.DataSymbolMetadata) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SingleXPlot2D(cbit.plot.SingleXPlot2D) Plot2D(cbit.plot.Plot2D) Component(java.awt.Component) EmptyBorder(javax.swing.border.EmptyBorder)

Aggregations

SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)115 Expression (cbit.vcell.parser.Expression)50 ExpressionException (cbit.vcell.parser.ExpressionException)20 Vector (java.util.Vector)20 ArrayList (java.util.ArrayList)19 SpeciesContext (cbit.vcell.model.SpeciesContext)18 ModelParameter (cbit.vcell.model.Model.ModelParameter)14 PropertyVetoException (java.beans.PropertyVetoException)14 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)13 Model (cbit.vcell.model.Model)12 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)11 HashMap (java.util.HashMap)11 SimulationContext (cbit.vcell.mapping.SimulationContext)10 Variable (cbit.vcell.math.Variable)10 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)9 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)9 Parameter (cbit.vcell.model.Parameter)9 SingleXPlot2D (cbit.plot.SingleXPlot2D)8 MathException (cbit.vcell.math.MathException)8 ReservedVariable (cbit.vcell.math.ReservedVariable)8