Search in sources :

Example 11 with XmlParseException

use of cbit.vcell.xml.XmlParseException in project vcell by virtualcell.

the class SimulationData method getVarAndFunctionDataIdentifiers.

/**
 * This method was created in VisualAge.
 * @return java.lang.String[]
 */
public synchronized DataIdentifier[] getVarAndFunctionDataIdentifiers(OutputContext outputContext) throws IOException, DataAccessException {
    // Is this zip format?
    boolean bIsChombo = false;
    try {
        bIsChombo = isChombo();
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.out);
    }
    File zipFile1 = getZipFile(bIsChombo, null);
    File zipFile2 = getZipFile(bIsChombo, 0);
    bZipFormat1 = false;
    bZipFormat2 = false;
    if (zipFile1.exists()) {
        bZipFormat1 = true;
    } else if (zipFile2.exists()) {
        bZipFormat2 = true;
    }
    refreshLogFile();
    if (!isComsol()) {
        try {
            refreshMeshFile();
        } catch (MathException e) {
            e.printStackTrace(System.out);
            throw new DataAccessException(e.getMessage());
        }
    }
    if (!isRulesData && !getIsODEData() && !isComsol() && dataFilenames != null) {
        // read variables only when I have never read the file since variables don't change
        if (dataSetIdentifierList.size() == 0) {
            File file = getPDEDataFile(0.0);
            DataSet dataSet = getPDEDataSet(file, 0.0);
            String[] varNames = dataSet.getDataNames();
            int[] varTypeInts = dataSet.getVariableTypeIntegers();
            if (varNames == null) {
                return null;
            }
            dataSetIdentifierList.clear();
            for (int i = 0; i < varNames.length; i++) {
                VariableType varType = null;
                try {
                    varType = VariableType.getVariableTypeFromInteger(varTypeInts[i]);
                } catch (IllegalArgumentException e) {
                    if (LG.isEnabledFor(Level.WARN)) {
                        LG.warn("Exception typing " + varNames[i] + " has unsupported type " + varTypeInts[i] + ": " + e.getMessage());
                    }
                    varType = SimulationData.getVariableTypeFromLength(mesh, dataSet.getDataLength(varNames[i]));
                }
                Domain domain = Variable.getDomainFromCombinedIdentifier(varNames[i]);
                String varName = Variable.getNameFromCombinedIdentifier(varNames[i]);
                dataSetIdentifierList.addElement(new DataSetIdentifier(varName, varType, domain));
            }
            refreshDataProcessingOutputInfo(outputContext);
            if (dataProcessingOutputInfo != null) {
                for (int i = 0; i < dataProcessingOutputInfo.getVariableNames().length; i++) {
                    if (dataProcessingOutputInfo.getPostProcessDataType(dataProcessingOutputInfo.getVariableNames()[i]).equals(DataProcessingOutputInfo.PostProcessDataType.image)) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(dataProcessingOutputInfo.getVariableNames()[i], VariableType.POSTPROCESSING, null));
                    }
                }
            }
        }
        // always read functions file since functions might change
        getFunctionDataIdentifiers(outputContext);
    }
    if ((isRulesData || getIsODEData()) && dataSetIdentifierList.size() == 0) {
        ODEDataBlock odeDataBlock = getODEDataBlock();
        if (odeDataBlock == null) {
            throw new DataAccessException("Results are not availabe yet. Please try again later.");
        }
        ODESimData odeSimData = odeDataBlock.getODESimData();
        int colCount = odeSimData.getColumnDescriptionsCount();
        // assume index=0 is time "t"
        int DATA_OFFSET = 1;
        dataSetIdentifierList.clear();
        for (int i = 0; i < (colCount - DATA_OFFSET); i++) {
            String varName = odeSimData.getColumnDescriptions(i + DATA_OFFSET).getDisplayName();
            // TODO domain
            Domain domain = null;
            dataSetIdentifierList.addElement(new DataSetIdentifier(varName, VariableType.NONSPATIAL, domain));
        }
    }
    if (isComsol() && dataSetIdentifierList.size() == 0) {
        ComsolSimFiles comsolSimFiles = getComsolSimFiles();
        if (comsolSimFiles.simTaskXMLFile != null) {
            try {
                String xmlString = FileUtils.readFileToString(comsolSimFiles.simTaskXMLFile);
                SimulationTask simTask = XmlHelper.XMLToSimTask(xmlString);
                Enumeration<Variable> variablesEnum = simTask.getSimulation().getMathDescription().getVariables();
                while (variablesEnum.hasMoreElements()) {
                    Variable var = variablesEnum.nextElement();
                    if (var instanceof VolVariable) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), VariableType.VOLUME, var.getDomain()));
                    } else if (var instanceof MemVariable) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), VariableType.MEMBRANE, var.getDomain()));
                    } else if (var instanceof Function) {
                        VariableType varType = VariableType.UNKNOWN;
                        if (var.getDomain() != null && var.getDomain().getName() != null) {
                            SubDomain subDomain = simTask.getSimulation().getMathDescription().getSubDomain(var.getDomain().getName());
                            if (subDomain instanceof CompartmentSubDomain) {
                                varType = VariableType.VOLUME;
                            } else if (subDomain instanceof MembraneSubDomain) {
                                varType = VariableType.MEMBRANE;
                            } else if (subDomain instanceof FilamentSubDomain) {
                                throw new RuntimeException("filament subdomains not supported");
                            } else if (subDomain instanceof PointSubDomain) {
                                varType = VariableType.POINT_VARIABLE;
                            }
                        }
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), varType, var.getDomain()));
                    } else if (var instanceof Constant) {
                        System.out.println("ignoring Constant " + var.getName());
                    } else if (var instanceof InsideVariable) {
                        System.out.println("ignoring InsideVariable " + var.getName());
                    } else if (var instanceof OutsideVariable) {
                        System.out.println("ignoring OutsideVariable " + var.getName());
                    } else {
                        throw new RuntimeException("unexpected variable " + var.getName() + " of type " + var.getClass().getName());
                    }
                }
            } catch (XmlParseException | ExpressionException e) {
                e.printStackTrace();
                throw new RuntimeException("failed to read sim task file, msg: " + e.getMessage(), e);
            }
        }
    }
    DataIdentifier[] dis = new DataIdentifier[dataSetIdentifierList.size()];
    for (int i = 0; i < dataSetIdentifierList.size(); i++) {
        DataSetIdentifier dsi = (DataSetIdentifier) dataSetIdentifierList.elementAt(i);
        String displayName = dsi.getName();
        if (dsi.isFunction()) {
            AnnotatedFunction f = null;
            for (int j = 0; j < annotatedFunctionList.size(); j++) {
                AnnotatedFunction function = (AnnotatedFunction) annotatedFunctionList.elementAt(j);
                if (function.getName().equals(dsi.getName())) {
                    f = function;
                    break;
                }
            }
            if (f != null) {
                displayName = f.getDisplayName();
            }
        }
        dis[i] = new DataIdentifier(dsi.getName(), dsi.getVariableType(), dsi.getDomain(), dsi.isFunction(), displayName);
    }
    return dis;
}
Also used : MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) SimulationTask(cbit.vcell.messaging.server.SimulationTask) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) MemVariable(cbit.vcell.math.MemVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) Variable(cbit.vcell.math.Variable) VCDataIdentifier(org.vcell.util.document.VCDataIdentifier) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) Constant(cbit.vcell.math.Constant) FileNotFoundException(java.io.FileNotFoundException) PointSubDomain(cbit.vcell.math.PointSubDomain) ODESimData(cbit.vcell.solver.ode.ODESimData) InsideVariable(cbit.vcell.math.InsideVariable) ExpressionException(cbit.vcell.parser.ExpressionException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) MemVariable(cbit.vcell.math.MemVariable) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) ComsolSimFiles(org.vcell.vis.io.ComsolSimFiles) VariableType(cbit.vcell.math.VariableType) VolVariable(cbit.vcell.math.VolVariable) XmlParseException(cbit.vcell.xml.XmlParseException) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) MathException(cbit.vcell.math.MathException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) OutsideVariable(cbit.vcell.math.OutsideVariable) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 12 with XmlParseException

use of cbit.vcell.xml.XmlParseException in project vcell by virtualcell.

the class BatchTester method batchScanBioModels.

@SuppressWarnings("static-access")
public void batchScanBioModels(VCMultiBioVisitor databaseVisitor, String statusTable, int chunkSize) throws DataAccessException, XmlParseException, SQLException, IOException {
    PrintStream current = System.out;
    // System.setOut(new PrintStream(new NullStream()));
    try {
        String processHostId = ManagementFactory.getRuntimeMXBean().getName();
        String filename = processHostId + ".txt";
        FileOutputStream fos = new FileOutputStream(filename);
        System.setOut(new PrintStream(fos));
        OutputStreamWriter writer = new OutputStreamWriter(fos);
        // autoflush
        PrintWriter printWriter = new PrintWriter(writer, true);
        Connection conn = connFactory.getConnection(null);
        conn.setAutoCommit(true);
        printWriter.println("reserving slots");
        try (Statement statement = conn.createStatement()) {
            String query = "Update " + statusTable + " set scan_process = '" + processHostId + "', log_file = '" + filename + "' where scanned = 0 and scan_process is null and rownum <= " + chunkSize;
            int uCount = statement.executeUpdate(query);
            if (uCount > chunkSize) {
                throw new Error("logic / SQL bad");
            }
            if (uCount == 0) {
                printWriter.println("No models to scan, exiting");
                System.exit(100);
            }
        }
        printWriter.println("finding  ours");
        ArrayList<BioModelIdent> models = new ArrayList<BatchTester.BioModelIdent>();
        try (Statement statement = conn.createStatement()) {
            String query = "Select id, user_id, model_id from " + statusTable + " where scan_process ='" + processHostId + "' and scanned = 0";
            ResultSet rs = statement.executeQuery(query);
            while (rs.next()) {
                BioModelIdent mi = new BioModelIdent(rs);
                models.add(mi);
                printWriter.println("claiming " + mi.statusId);
            }
        }
        try {
            // start visiting models and writing log
            printWriter.println("Start scanning bio-models......");
            printWriter.println("\n");
            PreparedStatement ps = conn.prepareStatement("Update " + statusTable + " set scanned = 1, good = ? , exception_type = ?, exception = ?, scan_process = null where id = ?");
            for (BioModelIdent modelIdent : models) {
                ScanStatus scanStatus = ScanStatus.PASS;
                String exceptionMessage = null;
                String exceptionClass = null;
                try {
                    User user = new User("", convert(modelIdent.userId));
                    KeyValue modelKey = convert(modelIdent.modelId);
                    BigString bioModelXML = null;
                    // seconds
                    long dbSleepTime = 10;
                    while (bioModelXML == null) {
                        try {
                            bioModelXML = dbServerImpl.getBioModelXML(user, modelKey);
                        } catch (DataAccessException dae) {
                            Throwable cause = dae.getCause();
                            if (cause.getClass().getSimpleName().equals("UniversalConnectionPoolException")) {
                                printWriter.println("No db connection for  " + modelIdent.statusId + ", sleeping " + dbSleepTime + " seconds");
                                Thread.currentThread().sleep(dbSleepTime * 1000);
                                // wait a little longer next time
                                dbSleepTime *= 1.5;
                            } else {
                                // other exception, just rethrow
                                throw dae;
                            }
                        }
                    }
                    BioModel storedModel = cbit.vcell.xml.XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
                    if (databaseVisitor.filterBioModel(storedModel)) {
                        storedModel.refreshDependencies();
                        boolean goodModel = verifyMathDescriptionsUnchanged(storedModel, printWriter);
                        if (goodModel) {
                            printWriter.println("Model for " + modelIdent.statusId + " good");
                            databaseVisitor.setBioModel(storedModel, printWriter);
                            for (BioModel bioModel : databaseVisitor) {
                                SimulationContext[] simContexts = bioModel.getSimulationContexts();
                                for (SimulationContext sc : simContexts) {
                                    // try {
                                    // long start = System.currentTimeMillis();
                                    sc.createNewMathMapping().getMathDescription();
                                // long end = System.currentTimeMillis();
                                // printWriter.println("mapping took " + (end - start)/1000.0 + " sec ");
                                /*
									} catch (Exception e) {
										//printWriter.println("\t " + bioModel.getName() + " :: " + sc.getName() + " ----> math regeneration failed.s");
										// e.printStackTrace();
									}
											 */
                                }
                            }
                        } else {
                            throw new MathRegenFail();
                        }
                    } else {
                        scanStatus = ScanStatus.FILTERED;
                    }
                } catch (Exception e) {
                    lg.error(e.getMessage(), e);
                    scanStatus = ScanStatus.FAIL;
                    exceptionClass = e.getClass().getName();
                    exceptionMessage = e.getMessage();
                    printWriter.println("failed " + modelIdent.statusId);
                    e.printStackTrace(printWriter);
                }
                ps.setInt(1, scanStatus.code);
                ps.setString(2, exceptionClass);
                ps.setString(3, exceptionMessage);
                ps.setLong(4, modelIdent.statusId);
                boolean estat = ps.execute();
                if (estat) {
                    throw new Error("logic");
                }
                int uc = ps.getUpdateCount();
                if (uc != 1) {
                    throw new Error("logic / sql ");
                }
            }
        } catch (Exception e) {
            e.printStackTrace(printWriter);
        }
        printWriter.close();
    } finally {
        System.setOut(current);
    }
}
Also used : User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) ArrayList(java.util.ArrayList) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) ResultSet(java.sql.ResultSet) DataAccessException(org.vcell.util.DataAccessException) PrintWriter(java.io.PrintWriter) PrintStream(java.io.PrintStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SimulationContext(cbit.vcell.mapping.SimulationContext) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) FileOutputStream(java.io.FileOutputStream) BioModel(cbit.vcell.biomodel.BioModel) OutputStreamWriter(java.io.OutputStreamWriter) XMLSource(cbit.vcell.xml.XMLSource)

Example 13 with XmlParseException

use of cbit.vcell.xml.XmlParseException in project vcell by virtualcell.

the class BatchTester method batchScanMathModels.

@SuppressWarnings("static-access")
public void batchScanMathModels(BadMathVisitor databaseVisitor, String statusTable, int chunkSize) throws DataAccessException, XmlParseException, SQLException, IOException {
    PrintStream current = System.out;
    // System.setOut(new PrintStream(new NullStream()));
    try {
        String processHostId = ManagementFactory.getRuntimeMXBean().getName();
        String filename = processHostId + ".txt";
        FileOutputStream fos = new FileOutputStream(filename);
        System.setOut(new PrintStream(fos));
        OutputStreamWriter writer = new OutputStreamWriter(fos);
        // autoflush
        PrintWriter printWriter = new PrintWriter(writer, true);
        Connection conn = connFactory.getConnection(null);
        conn.setAutoCommit(true);
        printWriter.println("reserving slots");
        try (Statement statement = conn.createStatement()) {
            String query = "Update " + statusTable + " set scan_process = '" + processHostId + "', log_file = '" + filename + "' where scanned = 0 and scan_process is null and rownum <= " + chunkSize;
            int uCount = statement.executeUpdate(query);
            if (uCount > chunkSize) {
                throw new Error("logic / SQL bad");
            }
            if (uCount == 0) {
                printWriter.println("No models to scan, exiting");
                System.exit(100);
            }
        }
        printWriter.println("finding  ours");
        ArrayList<MathModelIdent> models = new ArrayList<BatchTester.MathModelIdent>();
        try (Statement statement = conn.createStatement()) {
            String query = "Select model_id from " + statusTable + " where scan_process ='" + processHostId + "' and scanned = 0";
            ResultSet rs = statement.executeQuery(query);
            while (rs.next()) {
                MathModelIdent mmi = new MathModelIdent(rs);
                models.add(mmi);
                printWriter.println("claiming " + mmi.id);
            }
        }
        try {
            // start visiting models and writing log
            printWriter.println("Start scanning math-models......");
            printWriter.println("\n");
            PreparedStatement ps = conn.prepareStatement("Update " + statusTable + " set scanned = 1, good = ? , exception_type = ?, exception = ?, scan_process = null where model_id = ?");
            for (MathModelIdent modelIdent : models) {
                ScanStatus scanStatus = ScanStatus.PASS;
                String exceptionMessage = null;
                String exceptionClass = null;
                try {
                    KeyValue modelKey = convert(modelIdent.id);
                    BigString mathModelXML = null;
                    // seconds
                    long dbSleepTime = 10;
                    while (mathModelXML == null) {
                        try {
                            mathModelXML = dbServerImpl.getMathModelXML(BatchTester.ADMINISTRATOR, modelKey);
                        } catch (DataAccessException dae) {
                            Throwable cause = dae.getCause();
                            if (cause.getClass().getSimpleName().equals("UniversalConnectionPoolException")) {
                                printWriter.println("No db connection for  " + modelIdent.id + ", sleeping " + dbSleepTime + " seconds");
                                Thread.currentThread().sleep(dbSleepTime * 1000);
                                // wait a little longer next time
                                dbSleepTime *= 1.5;
                            } else {
                                // other exception, just rethrow
                                throw dae;
                            }
                        }
                    }
                    MathModel storedModel = cbit.vcell.xml.XmlHelper.XMLToMathModel(new XMLSource(mathModelXML.toString()));
                    databaseVisitor.visitMathModel(storedModel, System.out);
                } catch (Exception e) {
                    lg.error(e.getMessage(), e);
                    scanStatus = ScanStatus.FAIL;
                    exceptionClass = e.getClass().getName();
                    exceptionMessage = e.getMessage();
                    printWriter.println("failed " + modelIdent.id);
                    e.printStackTrace(printWriter);
                }
                ps.setInt(1, scanStatus.code);
                ps.setString(2, exceptionClass);
                ps.setString(3, exceptionMessage);
                ps.setLong(4, modelIdent.id);
                boolean estat = ps.execute();
                if (estat) {
                    throw new Error("logic");
                }
                int uc = ps.getUpdateCount();
                if (uc != 1) {
                    throw new Error("logic / sql ");
                }
                printWriter.println("model " + modelIdent.id + " " + scanStatus);
            }
            printWriter.close();
        } finally {
            System.setOut(current);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) KeyValue(org.vcell.util.document.KeyValue) ArrayList(java.util.ArrayList) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) ResultSet(java.sql.ResultSet) DataAccessException(org.vcell.util.DataAccessException) PrintWriter(java.io.PrintWriter) PrintStream(java.io.PrintStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) XMLSource(cbit.vcell.xml.XMLSource)

Example 14 with XmlParseException

use of cbit.vcell.xml.XmlParseException in project vcell by virtualcell.

the class BioModelEditorApplicationsPanel method compareButtonPressed.

private void compareButtonPressed() {
    int[] rows = table.getSelectedRows();
    if (rows == null || rows.length != 2) {
        return;
    }
    try {
        SimulationContext simContext1 = tableModel.getValueAt(rows[0]);
        SimulationContext simContext2 = tableModel.getValueAt(rows[1]);
        BioModel bioModel = simContext1.getBioModel();
        MathMappingCallback callback = new MathMappingCallback() {

            @Override
            public void setProgressFraction(float fractionDone) {
                Thread.dumpStack();
                System.out.println("---> stdout mathMapping: progress = " + (fractionDone * 100.0) + "% done");
            }

            @Override
            public void setMessage(String message) {
                Thread.dumpStack();
                System.out.println("---> stdout mathMapping: message = " + message);
            }

            @Override
            public boolean isInterrupted() {
                return false;
            }
        };
        simContext1.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
        simContext2.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
        Xmlproducer xmlProducer = new Xmlproducer(false);
        String simContext1XML = XmlUtil.xmlToString(xmlProducer.getXML(simContext1, bioModel));
        String simContext2XML = XmlUtil.xmlToString(xmlProducer.getXML(simContext2, bioModel));
        DiffConfiguration comparisonSetting = DiffConfiguration.COMPARE_DOCS_OTHER;
        XmlTreeDiff diffTree = XmlHelper.compareMerge(simContext1XML, simContext2XML, comparisonSetting, true);
        String baselineDesc = "application " + simContext1.getName();
        String modifiedDesc = "application " + simContext2.getName();
        TMLPanel comparePanel = new TMLPanel();
        comparePanel.setXmlTreeDiff(diffTree);
        comparePanel.setBaselineVersionDescription(baselineDesc);
        comparePanel.setModifiedVersionDescription(modifiedDesc);
        ChildWindowManager childWindowManager = ChildWindowManager.findChildWindowManager(this);
        String title = "comparing application " + simContext1.getName() + " and " + simContext2.getName();
        ChildWindow childWindow = childWindowManager.addChildWindow(comparePanel, diffTree, title, true);
        childWindow.setSize(new Dimension(600, 600));
        childWindow.show();
    } catch (XmlParseException e) {
        e.printStackTrace();
        DialogUtils.showErrorDialog(this, "failed to compare applications: \n\n" + e.getMessage());
    }
}
Also used : DiffConfiguration(cbit.xml.merge.XmlTreeDiff.DiffConfiguration) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) Xmlproducer(cbit.vcell.xml.Xmlproducer) XmlTreeDiff(cbit.xml.merge.XmlTreeDiff) ChildWindowManager(cbit.vcell.client.ChildWindowManager) Dimension(java.awt.Dimension) XmlParseException(cbit.vcell.xml.XmlParseException) SimulationContext(cbit.vcell.mapping.SimulationContext) ChildWindow(cbit.vcell.client.ChildWindowManager.ChildWindow) TMLPanel(cbit.xml.merge.gui.TMLPanel) BioModel(cbit.vcell.biomodel.BioModel)

Example 15 with XmlParseException

use of cbit.vcell.xml.XmlParseException in project vcell by virtualcell.

the class MicroscopyXmlReader method getUShortImage.

/**
 * This method returns a VCIMage object from a XML representation.
 * Creation date: (3/16/2001 3:41:24 PM)
 * @param param org.jdom.Element
 * @return VCImage
 * @throws XmlParseException
 */
private UShortImage getUShortImage(Element param) throws XmlParseException {
    // get the attributes
    Element tempelement = param.getChild(XMLTags.ImageDataTag);
    int aNumX = Integer.parseInt(tempelement.getAttributeValue(XMLTags.XAttrTag));
    int aNumY = Integer.parseInt(tempelement.getAttributeValue(XMLTags.YAttrTag));
    int aNumZ = Integer.parseInt(tempelement.getAttributeValue(XMLTags.ZAttrTag));
    int compressSize = Integer.parseInt(tempelement.getAttributeValue(XMLTags.CompressedSizeTag));
    final int BYTES_PER_SHORT = 2;
    int UNCOMPRESSED_SIZE_BYTES = aNumX * aNumY * aNumZ * BYTES_PER_SHORT;
    // getpixels
    String hexEncodedBytes = tempelement.getText();
    byte[] rawBytes = org.vcell.util.Hex.toBytes(hexEncodedBytes);
    ByteArrayInputStream rawByteArrayInputStream = new ByteArrayInputStream(rawBytes);
    InputStream rawInputStream = rawByteArrayInputStream;
    if (compressSize != UNCOMPRESSED_SIZE_BYTES) {
        rawInputStream = new InflaterInputStream(rawByteArrayInputStream);
    }
    byte[] shortsAsBytes = new byte[UNCOMPRESSED_SIZE_BYTES];
    int readCount = 0;
    try {
        while ((readCount += rawInputStream.read(shortsAsBytes, readCount, shortsAsBytes.length - readCount)) != shortsAsBytes.length) {
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmlParseException("error reading image pixels: ", e);
    } finally {
        if (rawInputStream != null) {
            try {
                rawInputStream.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    ByteBuffer byteBuffer = ByteBuffer.wrap(shortsAsBytes);
    short[] shortPixels = new short[aNumX * aNumY * aNumZ];
    for (int i = 0; i < shortPixels.length; i++) {
        shortPixels[i] = byteBuffer.getShort();
    }
    Element extentElement = param.getChild(XMLTags.ExtentTag);
    Extent extent = null;
    if (extentElement != null) {
        extent = vcellXMLReader.getExtent(extentElement);
    }
    Element originElement = param.getChild(XMLTags.OriginTag);
    Origin origin = null;
    if (originElement != null) {
        origin = vcellXMLReader.getOrigin(originElement);
    }
    // //set attributes
    // String name = this.unMangle( param.getAttributeValue(XMLTags.NameAttrTag) );
    // String annotation = param.getChildText(XMLTags.AnnotationTag);
    UShortImage newimage;
    try {
        newimage = new UShortImage(shortPixels, origin, extent, aNumX, aNumY, aNumZ);
    } catch (ImageException e) {
        e.printStackTrace();
        throw new XmlParseException("error reading image: ", e);
    }
    return newimage;
}
Also used : Origin(org.vcell.util.Origin) ImageException(cbit.image.ImageException) Extent(org.vcell.util.Extent) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Element(org.jdom.Element) UShortImage(cbit.vcell.VirtualMicroscopy.UShortImage) XmlParseException(cbit.vcell.xml.XmlParseException) ByteBuffer(java.nio.ByteBuffer) XmlParseException(cbit.vcell.xml.XmlParseException) ImageException(cbit.image.ImageException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

XmlParseException (cbit.vcell.xml.XmlParseException)49 DataAccessException (org.vcell.util.DataAccessException)35 XMLSource (cbit.vcell.xml.XMLSource)19 BigString (org.vcell.util.BigString)18 BioModel (cbit.vcell.biomodel.BioModel)16 KeyValue (org.vcell.util.document.KeyValue)16 SQLException (java.sql.SQLException)14 ExpressionException (cbit.vcell.parser.ExpressionException)12 IOException (java.io.IOException)12 RemoteProxyException (cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)11 SimulationContext (cbit.vcell.mapping.SimulationContext)10 PrintWriter (java.io.PrintWriter)9 User (org.vcell.util.document.User)9 MathException (cbit.vcell.math.MathException)8 MathModel (cbit.vcell.mathmodel.MathModel)8 Element (org.jdom.Element)8 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)8 BioModelInfo (org.vcell.util.document.BioModelInfo)7 PropertyVetoException (java.beans.PropertyVetoException)5 File (java.io.File)5