Search in sources :

Example 6 with XMLSource

use of cbit.vcell.xml.XMLSource 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 7 with XMLSource

use of cbit.vcell.xml.XMLSource 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 8 with XMLSource

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

the class StandaloneRuleBasedTest method checkNonspatialStochasticSimContext.

private static void checkNonspatialStochasticSimContext(SimulationContext srcSimContext, File baseDirectory, int numTrials, long bngTimeoutDuration) throws Exception {
    if (!srcSimContext.getApplicationType().equals(Application.NETWORK_STOCHASTIC) || srcSimContext.getGeometry().getDimension() != 0) {
        throw new RuntimeException("simContext is of type " + srcSimContext.getApplicationType() + " and geometry dimension of " + srcSimContext.getGeometry().getDimension() + ", expecting nonspatial stochastic");
    }
    BioModel origBioModel = srcSimContext.getBioModel();
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(XmlHelper.bioModelToXML(origBioModel)));
    bioModel.refreshDependencies();
    // create ODE and RuleBased
    SimulationContext newODEApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewODEApp", false, Application.NETWORK_DETERMINISTIC);
    SimulationContext newRuleBasedApp = SimulationContext.copySimulationContext(srcSimContext, "aUniqueNewRuleBasedApp", false, Application.RULE_BASED_STOCHASTIC);
    newODEApp.setBioModel(bioModel);
    newRuleBasedApp.setBioModel(bioModel);
    ArrayList<AnnotatedFunction> outputFunctionsList = srcSimContext.getOutputFunctionContext().getOutputFunctionsList();
    // OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
    newODEApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    newRuleBasedApp.getOutputFunctionContext().setOutputFunctions(outputFunctionsList);
    NetworkGenerationRequirements networkGenRequirements = NetworkGenerationRequirements.getComputeFull(bngTimeoutDuration);
    bioModel.addSimulationContext(newODEApp);
    newODEApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    bioModel.addSimulationContext(newRuleBasedApp);
    newRuleBasedApp.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    srcSimContext.refreshMathDescription(new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    // Create non-spatialStoch, ODE and RuleBased sims
    Simulation nonspatialStochAppNewSim = srcSimContext.addNewSimulation(STOCH_SIM_NAME, /*SimulationOwner.DEFAULT_SIM_NAME_PREFIX*/
    new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    Simulation newODEAppNewSim = newODEApp.addNewSimulation(ODE_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    Simulation newRuleBasedAppNewSim = newRuleBasedApp.addNewSimulation(NFS_SIM_NAME, new MathMappingCallbackTaskAdapter(null), networkGenRequirements);
    nonspatialStochAppNewSim.setSimulationOwner(srcSimContext);
    newODEAppNewSim.setSimulationOwner(newODEApp);
    newRuleBasedAppNewSim.setSimulationOwner(newRuleBasedApp);
    try {
        bioModel.getModel().getSpeciesContexts();
        ArrayList<String> varNameList = new ArrayList<String>();
        for (SpeciesContextSpec scs : srcSimContext.getReactionContext().getSpeciesContextSpecs()) {
            varNameList.add(scs.getSpeciesContext().getName());
        }
        String[] varNames = varNameList.toArray(new String[0]);
        OutputTimeSpec outputTimeSpec = nonspatialStochAppNewSim.getSolverTaskDescription().getOutputTimeSpec();
        ArrayList<Double> sampleTimeList = new ArrayList<Double>();
        if (outputTimeSpec instanceof UniformOutputTimeSpec) {
            double endingTime = nonspatialStochAppNewSim.getSolverTaskDescription().getTimeBounds().getEndingTime();
            double dT = ((UniformOutputTimeSpec) outputTimeSpec).getOutputTimeStep();
            int currTimeIndex = 0;
            while (currTimeIndex * dT <= (endingTime + 1e-8)) {
                sampleTimeList.add(currTimeIndex * dT);
                currTimeIndex++;
            }
        }
        double[] sampleTimes = new double[sampleTimeList.size()];
        for (int i = 0; i < sampleTimes.length; i++) {
            sampleTimes[i] = sampleTimeList.get(i);
        }
        TimeSeriesMultitrialData sampleDataStoch1 = new TimeSeriesMultitrialData("stochastic1", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataStoch2 = new TimeSeriesMultitrialData("stochastic2", varNames, sampleTimes, numTrials);
        TimeSeriesMultitrialData sampleDataDeterministic = new TimeSeriesMultitrialData("determinstic", varNames, sampleTimes, 1);
        runsolver(nonspatialStochAppNewSim, baseDirectory, numTrials, sampleDataStoch1);
        runsolver(newODEAppNewSim, baseDirectory, 1, sampleDataDeterministic);
        runsolver(newRuleBasedAppNewSim, baseDirectory, numTrials, sampleDataStoch2);
        StochtestFileUtils.writeVarDiffData(new File(baseDirectory, VARDIFF_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeKolmogorovSmirnovTest(new File(baseDirectory, KS_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeChiSquareTest(new File(baseDirectory, ChiSquared_TEST_FILE), sampleDataStoch1, sampleDataStoch2);
        StochtestFileUtils.writeData(sampleDataStoch1, new File(baseDirectory, "data." + sampleDataStoch1.datasetName + ".json"));
        StochtestFileUtils.writeData(sampleDataStoch2, new File(baseDirectory, "data." + sampleDataStoch2.datasetName + ".json"));
        StochtestFileUtils.writeData(sampleDataDeterministic, new File(baseDirectory, "data." + sampleDataDeterministic.datasetName + ".json"));
    } finally {
        srcSimContext.removeSimulation(nonspatialStochAppNewSim);
        newODEApp.removeSimulation(newODEAppNewSim);
        newRuleBasedApp.removeSimulation(newRuleBasedAppNewSim);
    }
}
Also used : MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) TimeSeriesMultitrialData(org.vcell.stochtest.TimeSeriesMultitrialData) ArrayList(java.util.ArrayList) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) NetworkGenerationRequirements(cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements) XMLSource(cbit.vcell.xml.XMLSource) File(java.io.File) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 9 with XMLSource

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

the class ClientRobot method run.

/**
 * Insert the method's description here.
 * Creation date: (3/8/01 3:04:15 PM)
 */
public void run() {
    try {
        String[] args = { host, userid, password };
        setManagerManager(cbit.vcell.client.test.ClientTester.mainInit(args, "ClientRobot", null));
        for (int i = 0; i < 10; i++) {
            log.print("Robot " + getName() + "starting loop : " + i);
            BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelFile));
            String newName = "bioModel_" + getName() + "_" + Math.random();
            log.print("Saving bioModel \"" + newName + "\"");
            getManagerManager().getDocumentManager().saveAsNew(bioModel, newName, null);
            BioModelInfo[] bioModelInfos = getManagerManager().getDocumentManager().getBioModelInfos();
            if (bioModelInfos != null && bioModelInfos.length > 0) {
                for (int j = 0; j < bioModelInfos.length; j++) {
                    log.print("bioModelInfo[" + j + "] = " + bioModelInfos[j]);
                }
            }
        }
    } catch (Throwable e) {
        log.exception(e);
    } finally {
        log.alert("Robot " + getName() + " exiting");
    }
}
Also used : BioModel(cbit.vcell.biomodel.BioModel) BioModelInfo(org.vcell.util.document.BioModelInfo) XMLSource(cbit.vcell.xml.XMLSource)

Example 10 with XMLSource

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

the class SBMLExportTest method getBiomodelByName.

static BioModel getBiomodelByName(VCellConnection vcn, String name) throws RemoteProxyException, DataAccessException, XmlParseException {
    EscapedName eName = new EscapedName(name);
    UserMetaDbServer dataServer = vcn.getUserMetaDbServer();
    for (BioModelInfo bmi : dataServer.getBioModelInfos(false)) {
        String mName = bmi.getVersion().getName();
        if (eName.matches(mName)) {
            KeyValue bioModelKey = bmi.getVersion().getVersionKey();
            BigString bioModelXML = dataServer.getBioModelXML(bioModelKey);
            BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
            return bioModel;
        }
    }
    // if not found, list what's available
    for (BioModelInfo bmi : dataServer.getBioModelInfos(false)) {
        String mName = bmi.getVersion().getName();
        System.out.println(mName);
    }
    return null;
}
Also used : UserMetaDbServer(cbit.vcell.server.UserMetaDbServer) KeyValue(org.vcell.util.document.KeyValue) BioModel(cbit.vcell.biomodel.BioModel) BioModelInfo(org.vcell.util.document.BioModelInfo) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) XMLSource(cbit.vcell.xml.XMLSource)

Aggregations

XMLSource (cbit.vcell.xml.XMLSource)50 BioModel (cbit.vcell.biomodel.BioModel)37 XmlParseException (cbit.vcell.xml.XmlParseException)31 DataAccessException (org.vcell.util.DataAccessException)27 SimulationContext (cbit.vcell.mapping.SimulationContext)22 BigString (org.vcell.util.BigString)22 KeyValue (org.vcell.util.document.KeyValue)21 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)19 Simulation (cbit.vcell.solver.Simulation)18 SQLException (java.sql.SQLException)15 User (org.vcell.util.document.User)15 BioModelInfo (org.vcell.util.document.BioModelInfo)13 MathException (cbit.vcell.math.MathException)12 ExpressionException (cbit.vcell.parser.ExpressionException)12 PropertyVetoException (java.beans.PropertyVetoException)12 MappingException (cbit.vcell.mapping.MappingException)11 MathModel (cbit.vcell.mathmodel.MathModel)11 IOException (java.io.IOException)11 MathDescription (cbit.vcell.math.MathDescription)10 File (java.io.File)10