Search in sources :

Example 66 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class StructureMappingTableRenderer method getTableCellRendererComponent.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    setIcon(null);
    if (table.getModel() instanceof StructureMappingTableModel) {
        StructureMappingTableModel structureMappingTableModel = (StructureMappingTableModel) table.getModel();
        String toolTip = structureMappingTableModel.getToolTip(row, column);
        if (value instanceof Structure) {
            Structure structure = (Structure) value;
            setText(structure.getName());
        } else if (value instanceof Double && structureMappingTableModel.isNewSizeColumn(column)) {
            StructureMapping structureMapping = structureMappingTableModel.getStructureMapping(row);
            if (structureMappingTableModel.isNonSpatial()) {
                VCUnitDefinition unitDefinition = structureMapping.getStructure().getStructureSize().getUnitDefinition();
                TextIcon sizeIcon = unitIconHash.get(unitDefinition.getSymbol());
                if (sizeIcon == null) {
                    sizeIcon = new TextIcon("[ " + unitDefinition.getSymbolUnicode() + " ]");
                    unitIconHash.put(unitDefinition.getSymbol(), sizeIcon);
                }
                setIcon(sizeIcon);
            } else {
                // spatial
                if (structureMapping.getUnitSizeParameter() != null) {
                    VCUnitDefinition unitDefinition = structureMapping.getUnitSizeParameter().getUnitDefinition();
                    TextIcon sizeIcon = unitIconHash.get(unitDefinition.getSymbol());
                    if (sizeIcon == null) {
                        sizeIcon = new TextIcon("[ " + unitDefinition.getSymbolUnicode() + " ]");
                        unitIconHash.put(unitDefinition.getSymbol(), sizeIcon);
                    }
                    setIcon(sizeIcon);
                }
            }
        }
        if (structureMappingTableModel.isSubdomainColumn(column)) {
            // can be null
            if (value == null) {
                setText("Unmapped");
                setForeground(Color.red);
                setIcon(null);
            } else {
                if (value instanceof GeometryClass) {
                    setText(((GeometryClass) value).getName());
                    if (value instanceof SubVolume) {
                        SubVolume subVolume = (SubVolume) value;
                        java.awt.Color handleColor = new java.awt.Color(colormap[subVolume.getHandle()]);
                        // small square icon with subdomain color
                        Icon icon = new ColorIcon(10, 10, handleColor, true);
                        setHorizontalTextPosition(SwingConstants.RIGHT);
                        setIcon(icon);
                    } else if (value instanceof SurfaceClass) {
                        SurfaceClass sc = (SurfaceClass) value;
                        Set<SubVolume> sv = sc.getAdjacentSubvolumes();
                        Iterator<SubVolume> iterator = sv.iterator();
                        SubVolume sv1 = iterator.next();
                        SubVolume sv2 = iterator.next();
                        java.awt.Color c1 = new java.awt.Color(colormap[sv2.getHandle()]);
                        java.awt.Color c2 = new java.awt.Color(colormap[sv1.getHandle()]);
                        Icon icon = new ColorIconEx(10, 10, c1, c2);
                        setIcon(icon);
                        setHorizontalTextPosition(SwingConstants.RIGHT);
                    }
                } else {
                    setText(value.toString());
                    setIcon(null);
                }
            }
        }
        if (value instanceof BoundaryConditionType) {
            // we get here only for spatial
            Object candidate = structureMappingTableModel.getValueAt(row, StructureMappingTableModel.SPATIAL_COLUMN_SUBDOMAIN);
            if (candidate instanceof SurfaceClass) {
                SurfaceClass surfaceClass = (SurfaceClass) candidate;
                cbit.vcell.model.Model model = structureMappingTableModel.getGeometryContext().getModel();
                SimulationContext simContext = structureMappingTableModel.getGeometryContext().getSimulationContext();
                Pair<SubVolume, SubVolume> ret = DiffEquMathMapping.computeBoundaryConditionSource(model, simContext, surfaceClass);
                SubVolume innerSubVolume = ret.one;
                java.awt.Color handleColor = new java.awt.Color(colormap[innerSubVolume.getHandle()]);
                // small square icon with subdomain color
                Icon icon = new ColorIcon(8, 8, handleColor, true);
                setHorizontalTextPosition(SwingConstants.LEFT);
                setIcon(icon);
                setText("from");
                // override default tooltip
                toolTip = "Boundary condition inherited from Subdomain '" + innerSubVolume.getName() + "'";
                setToolTipText(toolTip);
            } else {
                setText(((BoundaryConditionType) value).boundaryTypeStringValue());
            }
        }
        List<Issue> issueList = structureMappingTableModel.getIssues(row, column, Issue.SEVERITY_ERROR);
        if (issueList.size() > 0) {
            // override default tooltip
            setToolTipText(Issue.getHtmlIssueMessage(issueList));
            if (column == 0) {
                setBorder(new MatteBorder(1, 1, 1, 0, Color.red));
            } else if (column == table.getColumnCount() - 1) {
                setBorder(new MatteBorder(1, 0, 1, 1, Color.red));
            } else {
                setBorder(new MatteBorder(1, 0, 1, 0, Color.red));
            }
        } else {
            setToolTipText(toolTip);
            setBorder(DEFAULT_GAP);
        }
    }
    return this;
}
Also used : Color(java.awt.Color) GeometryClass(cbit.vcell.geometry.GeometryClass) Set(java.util.Set) Issue(org.vcell.util.Issue) SurfaceClass(cbit.vcell.geometry.SurfaceClass) BoundaryConditionType(cbit.vcell.math.BoundaryConditionType) AttributedString(java.text.AttributedString) StructureMapping(cbit.vcell.mapping.StructureMapping) MatteBorder(javax.swing.border.MatteBorder) SubVolume(cbit.vcell.geometry.SubVolume) Iterator(java.util.Iterator) Structure(cbit.vcell.model.Structure) ColorIconEx(org.vcell.util.gui.ColorIconEx) ColorIcon(org.vcell.util.gui.ColorIcon) Color(java.awt.Color) SimulationContext(cbit.vcell.mapping.SimulationContext) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) ColorIcon(org.vcell.util.gui.ColorIcon) Icon(javax.swing.Icon)

Example 67 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class DataSymbolTable method populateDataSymbols.

public void populateDataSymbols(Connection con, KeyValue simContextRef, DataContext dataContext, User simcontextOwner, VCUnitSystem unitSystem) throws SQLException, DataAccessException {
    Statement stmt = null;
    try {
        HashMap<BigDecimal, ExternalDataIdentifier> extDataIDHashMap = new HashMap<BigDecimal, ExternalDataIdentifier>();
        stmt = con.createStatement();
        ResultSet rset = stmt.executeQuery("SELECT * FROM " + ExternalDataTable.table.getTableName() + " WHERE " + ExternalDataTable.table.ownerRef.getUnqualifiedColName() + " = " + simcontextOwner.getID().toString());
        while (rset.next()) {
            BigDecimal extdataIDBigDec = rset.getBigDecimal(ExternalDataTable.table.id.getUnqualifiedColName());
            ExternalDataIdentifier extdataID = new ExternalDataIdentifier(new KeyValue(extdataIDBigDec), simcontextOwner, rset.getString(ExternalDataTable.table.externalDataName.getUnqualifiedColName()));
            extDataIDHashMap.put(extdataIDBigDec, extdataID);
        }
        rset.close();
        rset = stmt.executeQuery("SELECT * FROM " + DataSymbolTable.table.getTableName() + " WHERE " + DataSymbolTable.table.simContextRef.getUnqualifiedColName() + " = " + simContextRef.toString());
        while (rset.next()) {
            String nextDataSymbolName = rset.getString(DataSymbolTable.table.dataSymbolName.toString());
            String nextDataSymbolTypeS = rset.getString(DataSymbolTable.table.dataSymbolType.toString());
            DataSymbolType nextDataSymbolType = DataSymbolType.valueOf(nextDataSymbolTypeS);
            String nextDataSymbolVCUnitDefS = rset.getString(DataSymbolTable.table.dataSymbolVCUnitDef.toString());
            nextDataSymbolVCUnitDefS = TokenMangler.getSQLRestoredString(nextDataSymbolVCUnitDefS);
            VCUnitDefinition nextVCUnitDefinition = unitSystem.getInstance(nextDataSymbolVCUnitDefS);
            // FieldDataSymbol (ExternalDataIdentifier) - (only data symbol defined right now)
            BigDecimal extDataBigDecimal = rset.getBigDecimal(DataSymbolTable.table.fieldDataRef.getUnqualifiedColName());
            ExternalDataIdentifier nextExtDataID = null;
            if (!rset.wasNull()) {
                nextExtDataID = extDataIDHashMap.get(extDataBigDecimal);
                if (nextExtDataID == null) {
                    throw new DataAccessException("PopulateDataSymbols extdataID " + extDataBigDecimal + " not found in database for user " + simcontextOwner.getName());
                }
                String nextFieldDataVarName = rset.getString(DataSymbolTable.table.fieldDataVarName.getUnqualifiedColName());
                String nextFieldDataVarType = rset.getString(DataSymbolTable.table.fieldDataVarType.getUnqualifiedColName());
                double nextFieldDataVarTime = rset.getDouble(DataSymbolTable.table.fieldDataVarTime.getUnqualifiedColName());
                FieldDataSymbol fieldDataSymbol = new FieldDataSymbol(nextDataSymbolName, nextDataSymbolType, dataContext, nextVCUnitDefinition, nextExtDataID, nextFieldDataVarName, nextFieldDataVarType, nextFieldDataVarTime);
                dataContext.addDataSymbol(fieldDataSymbol);
            } else {
                throw new DataAccessException("DataSymbol has no ExternalDataIdentifier");
            }
        }
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) HashMap(java.util.HashMap) Statement(java.sql.Statement) BigDecimal(java.math.BigDecimal) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) ResultSet(java.sql.ResultSet) DataSymbolType(cbit.vcell.data.DataSymbol.DataSymbolType) DataAccessException(org.vcell.util.DataAccessException) FieldDataSymbol(cbit.vcell.data.FieldDataSymbol)

Example 68 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class ModelUnitConverter method convertVarsWithUnitFactors.

private static void convertVarsWithUnitFactors(SymbolTable oldSymbolTable, SymbolTable newSymbolTable, Parameter parameter) throws ExpressionException {
    // get old unit
    VCUnitDefinition oldExprUnit = null;
    Parameter oldParameter = (Parameter) oldSymbolTable.getEntry(parameter.getName());
    if (oldParameter == null) {
        System.err.println("parameter " + parameter.getName() + " was not found in the old symbol table");
    } else if (oldParameter.getUnitDefinition() == null) {
        System.err.println("parameter " + parameter.getName() + " has a null unit in old model, can't convert");
    } else {
        oldExprUnit = oldParameter.getUnitDefinition();
    }
    // get new unit
    VCUnitDefinition newExprUnit = null;
    if (parameter.getUnitDefinition() == null) {
        System.err.println("parameter " + parameter.getName() + " has a null unit in new model, can't convert");
    } else {
        newExprUnit = parameter.getUnitDefinition();
    }
    convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldExprUnit, newExprUnit, parameter.getExpression());
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Parameter(cbit.vcell.model.Parameter)

Example 69 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class ModelUnitConverter method createBioModelWithNewUnitSystem.

public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
    // new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
    BioModel newBioModel = XmlHelper.cloneBioModelWithNewUnitSystem(oldBioModel, newUnitSystem);
    Model newModel = newBioModel.getModel();
    Model oldModel = oldBioModel.getModel();
    for (Parameter p : newBioModel.getModel().getModelParameters()) {
        convertVarsWithUnitFactors(oldBioModel.getModel(), newBioModel.getModel(), p);
    }
    for (ReactionStep reactionStep : newBioModel.getModel().getReactionSteps()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getReactionStep(reactionStep.getName());
        SymbolTable newSymbolTable = reactionStep;
        for (Parameter p : reactionStep.getKinetics().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionStep.getKinetics().getKineticsParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (ReactionRule reactionRule : newBioModel.getModel().getRbmModelContainer().getReactionRuleList()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getRbmModelContainer().getReactionRule(reactionRule.getName()).getKineticLaw().getScopedSymbolTable();
        SymbolTable newSymbolTable = reactionRule.getKineticLaw().getScopedSymbolTable();
        for (Parameter p : reactionRule.getKineticLaw().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionRule.getKineticLaw().getLocalParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (SimulationContext simContext : newBioModel.getSimulationContexts()) {
        SimulationContext oldSimContext = oldBioModel.getSimulationContext(simContext.getName());
        // ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
        for (StructureMapping mapping : simContext.getGeometryContext().getStructureMappings()) {
            Structure oldStructure = oldModel.getStructure(mapping.getStructure().getName());
            StructureMapping oldMapping = oldSimContext.getGeometryContext().getStructureMapping(oldStructure);
            for (Parameter p : mapping.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldMapping, mapping, p);
            }
        }
        for (SpeciesContextSpec spec : simContext.getReactionContext().getSpeciesContextSpecs()) {
            SpeciesContext oldSpeciesContext = oldModel.getSpeciesContext(spec.getSpeciesContext().getName());
            SpeciesContextSpec oldSpec = oldSimContext.getReactionContext().getSpeciesContextSpec(oldSpeciesContext);
            for (Parameter p : spec.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldSpec, spec, p);
            }
        }
        for (int i = 0; i < simContext.getElectricalStimuli().length; i++) {
            ElectricalStimulus newElectricalStimulus = simContext.getElectricalStimuli()[i];
            ElectricalStimulus oldElectricalStimulus = oldSimContext.getElectricalStimuli()[i];
            for (Parameter p : newElectricalStimulus.getParameters()) {
                convertVarsWithUnitFactors(oldElectricalStimulus.getNameScope().getScopedSymbolTable(), newElectricalStimulus.getNameScope().getScopedSymbolTable(), p);
            }
        }
        // convert events : trigger and delay parameters and event assignments
        for (int i = 0; simContext.getBioEvents() != null && oldSimContext.getBioEvents() != null && i < simContext.getBioEvents().length; i++) {
            BioEvent newBioEvent = simContext.getBioEvents()[i];
            BioEvent oldBioEvent = oldSimContext.getBioEvent(newBioEvent.getName());
            for (Parameter p : newBioEvent.getEventParameters()) {
                convertVarsWithUnitFactors(oldBioEvent.getNameScope().getScopedSymbolTable(), newBioEvent.getNameScope().getScopedSymbolTable(), p);
            }
            // for each event assignment expression
            for (int e = 0; e < newBioEvent.getEventAssignments().size(); e++) {
                ScopedSymbolTable newSymbolTable = newBioEvent.getNameScope().getScopedSymbolTable();
                ScopedSymbolTable oldSymbolTable = oldBioEvent.getNameScope().getScopedSymbolTable();
                EventAssignment newEventAssignment = newBioEvent.getEventAssignments().get(e);
                EventAssignment oldEventAssignment = oldBioEvent.getEventAssignments().get(e);
                VCUnitDefinition oldTargetUnit = oldEventAssignment.getTarget().getUnitDefinition();
                VCUnitDefinition newTargetUnit = newEventAssignment.getTarget().getUnitDefinition();
                Expression eventAssgnExpr = newEventAssignment.getAssignmentExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, eventAssgnExpr);
            }
        }
        /**
         * @TODO: If rate rule variable unit is TBD, we still need to handle the rate expression unit.
         */
        // convert rate rules
        RateRule[] rateRules = simContext.getRateRules();
        if (rateRules != null && rateRules.length > 0) {
            for (RateRule rateRule : rateRules) {
                RateRule oldRateRule = oldSimContext.getRateRule(rateRule.getName());
                ScopedSymbolTable oldSymbolTable = oldRateRule.getSimulationContext();
                ScopedSymbolTable newSymbolTable = rateRule.getSimulationContext();
                VCUnitDefinition oldTargetUnit = oldRateRule.getRateRuleVar().getUnitDefinition();
                VCUnitDefinition newTargetUnit = rateRule.getRateRuleVar().getUnitDefinition();
                Expression rateRuleExpr = rateRule.getRateRuleExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, rateRuleExpr);
            }
        }
    }
    // end  for - simulationContext
    return newBioModel;
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) EventAssignment(cbit.vcell.mapping.BioEvent.EventAssignment) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) SymbolTable(cbit.vcell.parser.SymbolTable) SpeciesContext(cbit.vcell.model.SpeciesContext) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) RateRule(cbit.vcell.mapping.RateRule) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) BioEvent(cbit.vcell.mapping.BioEvent) Structure(cbit.vcell.model.Structure)

Example 70 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class ScrollTable method setAutoResizeMode.

@Override
public void setAutoResizeMode(int mode) {
    super.setAutoResizeMode(mode);
    if (autoResizeMode == AUTO_RESIZE_OFF) {
        setDefaultRenderer(ScopedExpression.class, new ScopedExpressionTableCellRenderer());
        setDefaultRenderer(VCUnitDefinition.class, new DefaultScrollTableCellRenderer() {

            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (value instanceof VCUnitDefinition) {
                    setText(((VCUnitDefinition) value).getSymbolUnicode());
                }
                return this;
            }
        });
        if (componentListener == null) {
            componentListener = new ComponentAdapter() {

                public void componentResized(ComponentEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            ScopedExpressionTableCellRenderer.formatTableCellSizes(ScrollTable.this);
                        }
                    });
                }
            };
        }
        enclosingScrollPane.removeComponentListener(componentListener);
        enclosingScrollPane.addComponentListener(componentListener);
    } else {
        if (componentListener != null) {
            enclosingScrollPane.removeComponentListener(componentListener);
        }
    }
}
Also used : ScopedExpressionTableCellRenderer(cbit.vcell.model.gui.ScopedExpressionTableCellRenderer) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) JTable(javax.swing.JTable) EventObject(java.util.EventObject) ComponentEvent(java.awt.event.ComponentEvent) Component(java.awt.Component) JComponent(javax.swing.JComponent) Point(java.awt.Point) ComponentAdapter(java.awt.event.ComponentAdapter)

Aggregations

VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)113 Expression (cbit.vcell.parser.Expression)73 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)36 ExpressionException (cbit.vcell.parser.ExpressionException)26 PropertyVetoException (java.beans.PropertyVetoException)24 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)21 Element (org.jdom.Element)20 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)19 ArrayList (java.util.ArrayList)17 ModelParameter (cbit.vcell.model.Model.ModelParameter)16 Model (cbit.vcell.model.Model)14 Parameter (cbit.vcell.model.Parameter)14 SpeciesContext (cbit.vcell.model.SpeciesContext)13 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)13 Membrane (cbit.vcell.model.Membrane)12 Structure (cbit.vcell.model.Structure)12 StructureMapping (cbit.vcell.mapping.StructureMapping)11 MathException (cbit.vcell.math.MathException)10 ReactionStep (cbit.vcell.model.ReactionStep)10 Feature (cbit.vcell.model.Feature)9