Search in sources :

Example 31 with BioModelInfo

use of org.vcell.util.document.BioModelInfo in project vcell by virtualcell.

the class MathVerifier method testMathGeneration.

public MathGenerationResults testMathGeneration(KeyValue simContextKey) throws SQLException, ObjectNotFoundException, DataAccessException, XmlParseException, MappingException, MathException, MatrixException, ExpressionException, ModelException, PropertyVetoException {
    User adminUser = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new org.vcell.util.document.KeyValue(PropertyLoader.ADMINISTRATOR_ID));
    if (lg.isTraceEnabled())
        lg.trace("Testing SimContext with key '" + simContextKey + "'");
    // get biomodel refs
    java.sql.Connection con = null;
    java.sql.Statement stmt = null;
    con = conFactory.getConnection(new Object());
    cbit.vcell.modeldb.BioModelSimContextLinkTable bmscTable = cbit.vcell.modeldb.BioModelSimContextLinkTable.table;
    cbit.vcell.modeldb.BioModelTable bmTable = cbit.vcell.modeldb.BioModelTable.table;
    cbit.vcell.modeldb.UserTable userTable = cbit.vcell.modeldb.UserTable.table;
    String sql = "SELECT " + bmscTable.bioModelRef.getQualifiedColName() + "," + bmTable.ownerRef.getQualifiedColName() + "," + userTable.userid.getQualifiedColName() + " FROM " + bmscTable.getTableName() + "," + bmTable.getTableName() + "," + userTable.getTableName() + " WHERE " + bmscTable.simContextRef.getQualifiedColName() + " = " + simContextKey + " AND " + bmTable.id.getQualifiedColName() + " = " + bmscTable.bioModelRef.getQualifiedColName() + " AND " + bmTable.ownerRef.getQualifiedColName() + " = " + userTable.id.getQualifiedColName();
    ArrayList<KeyValue> bioModelKeys = new ArrayList<KeyValue>();
    stmt = con.createStatement();
    User owner = null;
    try {
        ResultSet rset = stmt.executeQuery(sql);
        while (rset.next()) {
            KeyValue key = new KeyValue(rset.getBigDecimal(bmscTable.bioModelRef.getUnqualifiedColName()));
            bioModelKeys.add(key);
            KeyValue ownerRef = new KeyValue(rset.getBigDecimal(bmTable.ownerRef.getUnqualifiedColName()));
            String userid = rset.getString(userTable.userid.getUnqualifiedColName());
            owner = new User(userid, ownerRef);
        }
    } finally {
        if (stmt != null) {
            stmt.close();
        }
        con.close();
    }
    // use the first biomodel...
    if (bioModelKeys.size() == 0) {
        throw new RuntimeException("zombie simContext ... no biomodels");
    }
    BioModelInfo bioModelInfo = dbServerImpl.getBioModelInfo(owner, bioModelKeys.get(0));
    // 
    // read in the BioModel from the database
    // 
    BigString bioModelXML = dbServerImpl.getBioModelXML(owner, bioModelInfo.getVersion().getVersionKey());
    BioModel bioModelFromDB = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
    BioModel bioModelNewMath = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
    bioModelFromDB.refreshDependencies();
    bioModelNewMath.refreshDependencies();
    // 
    // get all Simulations for this model
    // 
    Simulation[] modelSimsFromDB = bioModelFromDB.getSimulations();
    // 
    // ---> only for the SimContext we started with...
    // recompute mathDescription, and verify it is equivalent
    // then check each associated simulation to ensure math overrides are applied in an equivalent manner also.
    // 
    SimulationContext[] simContextsFromDB = bioModelFromDB.getSimulationContexts();
    SimulationContext[] simContextsNewMath = bioModelNewMath.getSimulationContexts();
    SimulationContext simContextFromDB = null;
    SimulationContext simContextNewMath = null;
    for (int k = 0; k < simContextsFromDB.length; k++) {
        // find it...
        if (simContextsFromDB[k].getKey().equals(simContextKey)) {
            simContextFromDB = simContextsFromDB[k];
            simContextNewMath = simContextsNewMath[k];
            break;
        }
    }
    if (simContextFromDB == null) {
        throw new RuntimeException("BioModel referred to by this SimContext does not contain this SimContext");
    } else {
        MathDescription origMathDesc = simContextFromDB.getMathDescription();
        // 
        try {
            if (simContextNewMath.getGeometry().getDimension() > 0 && simContextNewMath.getGeometry().getGeometrySurfaceDescription().getGeometricRegions() == null) {
                simContextNewMath.getGeometry().getGeometrySurfaceDescription().updateAll();
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        // 
        // updated mathDescription loaded into copy of bioModel, then test for equivalence.
        // 
        cbit.vcell.mapping.MathMapping mathMapping = simContextNewMath.createNewMathMapping();
        MathDescription mathDesc_latest = mathMapping.getMathDescription();
        MathMapping_4_8 mathMapping_4_8 = new MathMapping_4_8(simContextNewMath);
        MathDescription mathDesc_4_8 = mathMapping_4_8.getMathDescription();
        String issueString = null;
        org.vcell.util.Issue[] issues = mathMapping.getIssues();
        if (issues != null && issues.length > 0) {
            StringBuffer buffer = new StringBuffer("Issues(" + issues.length + "):\n");
            for (int l = 0; l < issues.length; l++) {
                buffer.append(" <<" + issues[l].toString() + ">>\n");
            }
            issueString = buffer.toString();
        }
        simContextNewMath.setMathDescription(mathDesc_latest);
        MathCompareResults mathCompareResults_latest = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, mathDesc_latest);
        MathCompareResults mathCompareResults_4_8 = null;
        try {
            mathCompareResults_4_8 = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, mathDesc_4_8);
        } catch (Exception e) {
            e.printStackTrace(System.out);
            mathCompareResults_4_8 = new MathCompareResults(Decision.MathDifferent_FAILURE_UNKNOWN, e.getMessage());
        }
        return new MathGenerationResults(bioModelFromDB, simContextFromDB, origMathDesc, mathDesc_latest, mathCompareResults_latest, mathDesc_4_8, mathCompareResults_4_8);
    }
}
Also used : User(org.vcell.util.document.User) Connection(java.sql.Connection) KeyValue(org.vcell.util.document.KeyValue) MathDescription(cbit.vcell.math.MathDescription) ArrayList(java.util.ArrayList) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) MathCompareResults(cbit.vcell.math.MathCompareResults) ResultSet(java.sql.ResultSet) BioModelInfo(org.vcell.util.document.BioModelInfo) MathMapping_4_8(cbit.vcell.mapping.vcell_4_8.MathMapping_4_8) SimulationContext(cbit.vcell.mapping.SimulationContext) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ModelException(cbit.vcell.model.ModelException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MathException(cbit.vcell.math.MathException) KeyValue(org.vcell.util.document.KeyValue) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource)

Example 32 with BioModelInfo

use of org.vcell.util.document.BioModelInfo in project vcell by virtualcell.

the class MathVerifier method scanSimContexts.

/**
 * Insert the method's description here.
 * Creation date: (2/2/01 3:40:29 PM)
 */
public void scanSimContexts(boolean bUpdateDatabase, KeyValue[] simContextKeys) throws MathException, MappingException, SQLException, DataAccessException, ModelException, ExpressionException {
    java.util.Calendar calendar = java.util.GregorianCalendar.getInstance();
    // calendar.set(2002,java.util.Calendar.MAY,7+1);
    calendar.set(2002, java.util.Calendar.JULY, 1);
    final java.util.Date fluxCorrectionOrDisablingBugFixDate = calendar.getTime();
    // calendar.set(2001,java.util.Calendar.JUNE,13+1);
    calendar.set(2002, java.util.Calendar.JANUARY, 1);
    final java.util.Date totalVolumeCorrectionFixDate = calendar.getTime();
    User user = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
    for (int i = 0; i < simContextKeys.length; i++) {
        if (lg.isTraceEnabled())
            lg.trace("Testing SimContext with key '" + simContextKeys[i] + "'");
        // get biomodel refs
        java.sql.Connection con = null;
        java.sql.Statement stmt = null;
        con = conFactory.getConnection(new Object());
        cbit.vcell.modeldb.BioModelSimContextLinkTable bmscTable = cbit.vcell.modeldb.BioModelSimContextLinkTable.table;
        String sql = "SELECT " + bmscTable.bioModelRef.getQualifiedColName() + " FROM " + bmscTable.getTableName() + " WHERE " + bmscTable.simContextRef.getQualifiedColName() + " = " + simContextKeys[i];
        java.util.Vector keys = new java.util.Vector();
        stmt = con.createStatement();
        try {
            ResultSet rset = stmt.executeQuery(sql);
            while (rset.next()) {
                KeyValue key = new KeyValue(rset.getBigDecimal(bmscTable.bioModelRef.getUnqualifiedColName()));
                keys.addElement(key);
            }
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            con.close();
        }
        KeyValue[] bmKeys = (org.vcell.util.document.KeyValue[]) org.vcell.util.BeanUtils.getArray(keys, org.vcell.util.document.KeyValue.class);
        try {
            // use the first biomodel...
            BioModelInfo bioModelInfo = dbServerImpl.getBioModelInfo(user, bmKeys[0]);
            // 
            // read in the BioModel from the database
            // 
            BigString bioModelXML = dbServerImpl.getBioModelXML(user, bioModelInfo.getVersion().getVersionKey());
            BioModel bioModelFromDB = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
            BioModel bioModelNewMath = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
            bioModelFromDB.refreshDependencies();
            bioModelNewMath.refreshDependencies();
            // 
            // get all Simulations for this model
            // 
            Simulation[] modelSimsFromDB = bioModelFromDB.getSimulations();
            // 
            // ---> only for the SimContext we started with...
            // recompute mathDescription, and verify it is equivalent
            // then check each associated simulation to ensure math overrides are applied in an equivalent manner also.
            // 
            SimulationContext[] simContextsFromDB = bioModelFromDB.getSimulationContexts();
            SimulationContext[] simContextsNewMath = bioModelNewMath.getSimulationContexts();
            SimulationContext simContextFromDB = null;
            SimulationContext simContextNewMath = null;
            for (int k = 0; k < simContextsFromDB.length; k++) {
                // find it...
                if (simContextsFromDB[k].getKey().equals(simContextKeys[i])) {
                    simContextFromDB = simContextsFromDB[k];
                    simContextNewMath = simContextsNewMath[k];
                    break;
                }
            }
            if (simContextFromDB == null) {
                throw new RuntimeException("BioModel referred to by this SimContext does not contain this SimContext");
            } else {
                Simulation[] appSimsFromDB = simContextFromDB.getSimulations();
                Simulation[] appSimsNewMath = simContextNewMath.getSimulations();
                MathCompareResults mathCompareResults = null;
                try {
                    MathDescription origMathDesc = simContextFromDB.getMathDescription();
                    // 
                    // find out if any simulation belonging to this Application has data
                    // 
                    boolean bApplicationHasData = false;
                    for (int l = 0; l < modelSimsFromDB.length; l++) {
                        SimulationStatusPersistent simulationStatus = dbServerImpl.getSimulationStatus(modelSimsFromDB[l].getKey());
                        if (simulationStatus != null && simulationStatus.getHasData()) {
                            bApplicationHasData = true;
                        }
                    }
                    // 
                    try {
                        if (simContextNewMath.getGeometry().getDimension() > 0 && simContextNewMath.getGeometry().getGeometrySurfaceDescription().getGeometricRegions() == null) {
                            simContextNewMath.getGeometry().getGeometrySurfaceDescription().updateAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace(System.out);
                    }
                    // 
                    // updated mathdescription loaded into copy of biomodel, then test for equivalence.
                    // 
                    cbit.vcell.mapping.MathMapping mathMapping = simContextNewMath.createNewMathMapping();
                    MathDescription newMathDesc = mathMapping.getMathDescription();
                    String issueString = null;
                    org.vcell.util.Issue[] issues = mathMapping.getIssues();
                    if (issues != null && issues.length > 0) {
                        StringBuffer buffer = new StringBuffer("Issues(" + issues.length + "):");
                        for (int l = 0; l < issues.length; l++) {
                            buffer.append(" <<" + issues[l].toString() + ">>");
                        }
                        issueString = buffer.toString();
                    }
                    simContextNewMath.setMathDescription(newMathDesc);
                    MathCompareResults testIfSameResults = cbit.vcell.math.MathUtilities.testIfSame(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
                    mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
                    StringBuffer buffer = new StringBuffer();
                    buffer.append(">>>BioModel(" + bioModelFromDB.getVersion().getVersionKey() + ") '" + bioModelFromDB.getName() + "':" + bioModelFromDB.getVersion().getDate() + ", Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "' <<EQUIV=" + mathCompareResults.isEquivalent() + ">>: " + mathCompareResults.toDatabaseStatus());
                    // 
                    if (bUpdateDatabase) {
                        con = null;
                        stmt = null;
                        try {
                            con = conFactory.getConnection(new Object());
                            stmt = con.createStatement();
                            // KeyValue mathKey = origMathDesc.getKey();
                            String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.hasData.getUnqualifiedColName() + " = " + ((bApplicationHasData) ? (1) : (0)) + ", " + SimContextStat2Table.table.equiv.getUnqualifiedColName() + " = " + (mathCompareResults.isEquivalent() ? (1) : (0)) + ", " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.toDatabaseStatus()) + "'" + ((issueString != null) ? (", " + SimContextStat2Table.table.comments.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(issueString, 255) + "'") : ("")) + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
                            int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                            if (numRowsChanged != 1) {
                                System.out.println("failed to update status");
                            }
                            con.commit();
                            if (lg.isTraceEnabled())
                                lg.trace("-------------- Update=true, saved 'newMath' for Application '" + simContextFromDB.getName() + "'");
                        } catch (SQLException e) {
                            lg.error(e.getMessage(), e);
                            if (lg.isWarnEnabled())
                                lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE MATH for Application '" + simContextFromDB.getName() + "'");
                        } finally {
                            if (stmt != null) {
                                stmt.close();
                            }
                            con.close();
                        }
                    }
                } catch (Throwable e) {
                    // exception in SimContext
                    lg.error(e.getMessage(), e);
                    if (bUpdateDatabase) {
                        con = null;
                        stmt = null;
                        try {
                            con = conFactory.getConnection(new Object());
                            stmt = con.createStatement();
                            String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
                            if (status.length() > 255)
                                status = status.substring(0, 254) + "'";
                            String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
                            int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                            if (numRowsChanged != 1) {
                                System.out.println("failed to update status with exception");
                            }
                            con.commit();
                            if (lg.isTraceEnabled())
                                lg.trace("-------------- Update=true, saved exception for Application '" + simContextFromDB.getName() + "'");
                        } catch (SQLException e2) {
                            lg.error(e2.getMessage(), e2);
                            if (lg.isWarnEnabled())
                                lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for Application '" + simContextFromDB.getName() + "'");
                        } finally {
                            if (stmt != null) {
                                stmt.close();
                            }
                            con.close();
                        }
                    }
                }
                // 
                for (int l = 0; l < appSimsFromDB.length; l++) {
                    try {
                        boolean bSimEquivalent = Simulation.testEquivalency(appSimsNewMath[l], appSimsFromDB[l], mathCompareResults);
                        if (lg.isTraceEnabled())
                            lg.trace("Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "', " + "Simulation(" + modelSimsFromDB[l].getKey() + ") '" + modelSimsFromDB[l].getName() + "':" + modelSimsFromDB[l].getVersion().getDate() + "mathEquivalency=" + mathCompareResults.isEquivalent() + ", simEquivalency=" + bSimEquivalent);
                        // 
                        if (bUpdateDatabase) {
                            con = null;
                            stmt = null;
                            try {
                                con = conFactory.getConnection(new Object());
                                stmt = con.createStatement();
                                String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.equiv.getUnqualifiedColName() + " = " + ((bSimEquivalent) ? (1) : (0)) + ", " + SimStatTable.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.toDatabaseStatus()) + "'" + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
                                int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                                if (numRowsChanged != 1) {
                                    System.out.println("failed to update status");
                                }
                                con.commit();
                                if (lg.isTraceEnabled())
                                    lg.trace("-------------- Update=true, saved 'simulation status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } catch (SQLException e) {
                                lg.error(e.getMessage(), e);
                                if (lg.isWarnEnabled())
                                    lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } finally {
                                if (stmt != null) {
                                    stmt.close();
                                }
                                con.close();
                            }
                        }
                    } catch (Throwable e) {
                        // exception in SimContext
                        lg.error(e.getMessage(), e);
                        if (bUpdateDatabase) {
                            con = null;
                            stmt = null;
                            try {
                                con = conFactory.getConnection(new Object());
                                stmt = con.createStatement();
                                String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
                                if (status.length() > 255)
                                    status = status.substring(0, 254) + "'";
                                String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
                                int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                                if (numRowsChanged != 1) {
                                    System.out.println("failed to update status with exception");
                                }
                                con.commit();
                                if (lg.isTraceEnabled())
                                    lg.trace("-------------- Update=true, saved exception for Simulation '" + appSimsFromDB[l].getName() + "'");
                            } catch (SQLException e2) {
                                lg.error(e2.getMessage(), e2);
                                if (lg.isWarnEnabled())
                                    lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } finally {
                                if (stmt != null) {
                                    stmt.close();
                                }
                                con.close();
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            // exception in whole BioModel
            lg.error(e.getMessage(), e);
            // update database, since we know the simcontext...
            if (bUpdateDatabase) {
                con = null;
                stmt = null;
                try {
                    con = conFactory.getConnection(new Object());
                    stmt = con.createStatement();
                    // KeyValue mathKey = origMathDesc.getKey();
                    String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = 'BIOMODEL EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'" + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextKeys[i];
                    int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                    if (numRowsChanged != 1) {
                        System.out.println("failed to update status with exception");
                    }
                    con.commit();
                    if (lg.isTraceEnabled())
                        lg.trace("-------------- Update=true, saved exception for Application with key '" + simContextKeys[i] + "'");
                } catch (SQLException e2) {
                    lg.error(e2.getMessage(), e2);
                    if (lg.isWarnEnabled())
                        lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for Application with key '" + simContextKeys[i] + "'");
                } finally {
                    if (stmt != null) {
                        stmt.close();
                    }
                    con.close();
                }
            }
        }
    }
}
Also used : User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Connection(java.sql.Connection) MathDescription(cbit.vcell.math.MathDescription) SQLException(java.sql.SQLException) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) Vector(java.util.Vector) MathCompareResults(cbit.vcell.math.MathCompareResults) ResultSet(java.sql.ResultSet) Vector(java.util.Vector) BioModelInfo(org.vcell.util.document.BioModelInfo) SimulationStatusPersistent(cbit.vcell.server.SimulationStatusPersistent) SimulationContext(cbit.vcell.mapping.SimulationContext) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ModelException(cbit.vcell.model.ModelException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MathException(cbit.vcell.math.MathException) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource)

Example 33 with BioModelInfo

use of org.vcell.util.document.BioModelInfo in project vcell by virtualcell.

the class MathVerifier method scanBioModels.

/**
 * Insert the method's description here.
 * Creation date: (2/2/01 3:40:29 PM)
 */
public void scanBioModels(boolean bUpdateDatabase, KeyValue[] bioModelKeys) throws MathException, MappingException, SQLException, DataAccessException, ModelException, ExpressionException {
    java.util.Calendar calendar = java.util.GregorianCalendar.getInstance();
    // calendar.set(2002,java.util.Calendar.MAY,7+1);
    calendar.set(2002, java.util.Calendar.JULY, 1);
    final java.util.Date fluxCorrectionOrDisablingBugFixDate = calendar.getTime();
    // calendar.set(2001,java.util.Calendar.JUNE,13+1);
    calendar.set(2002, java.util.Calendar.JANUARY, 1);
    final java.util.Date totalVolumeCorrectionFixDate = calendar.getTime();
    User user = new User(PropertyLoader.ADMINISTRATOR_ACCOUNT, new KeyValue(PropertyLoader.ADMINISTRATOR_ID));
    for (int i = 0; i < bioModelKeys.length; i++) {
        BioModelInfo bioModelInfo = dbServerImpl.getBioModelInfo(user, bioModelKeys[i]);
        if (lg.isTraceEnabled())
            lg.trace("Testing bioModel with key '" + bioModelKeys[i] + "'");
        java.sql.Connection con = null;
        java.sql.Statement stmt = null;
        try {
            // 
            // read in the BioModel from the database
            // 
            BigString bioModelXML = dbServerImpl.getBioModelXML(user, bioModelInfo.getVersion().getVersionKey());
            BioModel bioModelFromDB = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
            BioModel bioModelNewMath = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
            bioModelFromDB.refreshDependencies();
            bioModelNewMath.refreshDependencies();
            // 
            // get all Simulations for this model
            // 
            Simulation[] modelSimsFromDB = bioModelFromDB.getSimulations();
            // 
            // for each application, recompute mathDescription, and verify it is equivalent
            // then check each associated simulation to ensure math overrides are applied in an equivalent manner also.
            // 
            SimulationContext[] simContextsFromDB = bioModelFromDB.getSimulationContexts();
            SimulationContext[] simContextsNewMath = bioModelNewMath.getSimulationContexts();
            for (int k = 0; k < simContextsFromDB.length; k++) {
                SimulationContext simContextFromDB = simContextsFromDB[k];
                Simulation[] appSimsFromDB = simContextFromDB.getSimulations();
                SimulationContext simContextNewMath = simContextsNewMath[k];
                Simulation[] appSimsNewMath = simContextNewMath.getSimulations();
                MathCompareResults mathCompareResults = null;
                try {
                    MathDescription origMathDesc = simContextFromDB.getMathDescription();
                    // 
                    // find out if any simulation belonging to this Application has data
                    // 
                    boolean bApplicationHasData = false;
                    for (int l = 0; l < modelSimsFromDB.length; l++) {
                        SimulationStatusPersistent simulationStatus = dbServerImpl.getSimulationStatus(modelSimsFromDB[l].getKey());
                        if (simulationStatus != null && simulationStatus.getHasData()) {
                            bApplicationHasData = true;
                        }
                    }
                    // 
                    try {
                        if (simContextNewMath.getGeometry().getDimension() > 0 && simContextNewMath.getGeometry().getGeometrySurfaceDescription().getGeometricRegions() == null) {
                            simContextNewMath.getGeometry().getGeometrySurfaceDescription().updateAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace(System.out);
                    }
                    // 
                    // updated mathdescription loaded into copy of biomodel, then test for equivalence.
                    // 
                    cbit.vcell.mapping.MathMapping mathMapping = simContextNewMath.createNewMathMapping();
                    MathDescription newMathDesc = mathMapping.getMathDescription();
                    String issueString = null;
                    org.vcell.util.Issue[] issues = mathMapping.getIssues();
                    if (issues != null && issues.length > 0) {
                        StringBuffer buffer = new StringBuffer("Issues(" + issues.length + "):");
                        for (int l = 0; l < issues.length; l++) {
                            buffer.append(" <<" + issues[l].toString() + ">>");
                        }
                        issueString = buffer.toString();
                    }
                    simContextNewMath.setMathDescription(newMathDesc);
                    MathCompareResults testIfSameResults = cbit.vcell.math.MathUtilities.testIfSame(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
                    mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), origMathDesc, newMathDesc);
                    StringBuffer buffer = new StringBuffer();
                    buffer.append(">>>BioModel(" + bioModelFromDB.getVersion().getVersionKey() + ") '" + bioModelFromDB.getName() + "':" + bioModelFromDB.getVersion().getDate() + ", Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "' <<EQUIV=" + mathCompareResults.isEquivalent() + ">>: " + mathCompareResults.toDatabaseStatus());
                    // 
                    if (bUpdateDatabase) {
                        con = null;
                        stmt = null;
                        try {
                            con = conFactory.getConnection(new Object());
                            stmt = con.createStatement();
                            // KeyValue mathKey = origMathDesc.getKey();
                            String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.hasData.getUnqualifiedColName() + " = " + ((bApplicationHasData) ? (1) : (0)) + ", " + SimContextStat2Table.table.equiv.getUnqualifiedColName() + " = " + (mathCompareResults.isEquivalent() ? (1) : (0)) + ", " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.toDatabaseStatus()) + "'" + ((issueString != null) ? (", " + SimContextStat2Table.table.comments.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(issueString, 255) + "'") : ("")) + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
                            int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                            if (numRowsChanged != 1) {
                                System.out.println("failed to update status");
                            }
                            con.commit();
                            if (lg.isTraceEnabled())
                                lg.trace("-------------- Update=true, saved 'newMath' for Application '" + simContextFromDB.getName() + "'");
                        } catch (SQLException e) {
                            lg.error(e.getMessage(), e);
                            if (lg.isWarnEnabled())
                                lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE MATH for Application '" + simContextFromDB.getName() + "'");
                        } finally {
                            if (stmt != null) {
                                stmt.close();
                            }
                            con.close();
                        }
                    }
                } catch (Throwable e) {
                    // exception in SimContext
                    lg.error(e.getMessage(), e);
                    if (bUpdateDatabase) {
                        con = null;
                        stmt = null;
                        try {
                            con = conFactory.getConnection(new Object());
                            stmt = con.createStatement();
                            String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
                            if (status.length() > 255)
                                status = status.substring(0, 254) + "'";
                            String UPDATESTATUS = "UPDATE " + SimContextStat2Table.table.getTableName() + " SET " + SimContextStat2Table.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimContextStat2Table.table.simContextRef.getUnqualifiedColName() + " = " + simContextFromDB.getKey();
                            int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                            if (numRowsChanged != 1) {
                                System.out.println("failed to update status with exception");
                            }
                            con.commit();
                            if (lg.isTraceEnabled())
                                lg.trace("-------------- Update=true, saved exception for Application '" + simContextFromDB.getName() + "'");
                        } catch (SQLException e2) {
                            lg.error(e2.getMessage(), e2);
                            if (lg.isWarnEnabled())
                                lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for Application '" + simContextFromDB.getName() + "'");
                        } finally {
                            if (stmt != null) {
                                stmt.close();
                            }
                            con.close();
                        }
                    }
                }
                // 
                for (int l = 0; l < appSimsFromDB.length; l++) {
                    try {
                        boolean bSimEquivalent = Simulation.testEquivalency(appSimsNewMath[l], appSimsFromDB[l], mathCompareResults);
                        if (lg.isTraceEnabled())
                            lg.trace("Application(" + simContextFromDB.getKey() + ") '" + simContextFromDB.getName() + "', " + "Simulation(" + modelSimsFromDB[l].getKey() + ") '" + modelSimsFromDB[l].getName() + "':" + modelSimsFromDB[l].getVersion().getDate() + "mathEquivalency=" + mathCompareResults.isEquivalent() + ", simEquivalency=" + bSimEquivalent);
                        // 
                        if (bUpdateDatabase) {
                            con = null;
                            stmt = null;
                            try {
                                con = conFactory.getConnection(new Object());
                                stmt = con.createStatement();
                                String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.equiv.getUnqualifiedColName() + " = " + ((bSimEquivalent) ? (1) : (0)) + ", " + SimStatTable.table.status.getUnqualifiedColName() + " = '" + org.vcell.util.TokenMangler.getSQLEscapedString(mathCompareResults.decision.description) + "'" + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
                                int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                                if (numRowsChanged != 1) {
                                    System.out.println("failed to update status");
                                }
                                con.commit();
                                if (lg.isTraceEnabled())
                                    lg.trace("-------------- Update=true, saved 'simulation status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } catch (SQLException e) {
                                lg.error(e.getMessage(), e);
                                if (lg.isWarnEnabled())
                                    lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO UPDATE status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } finally {
                                if (stmt != null) {
                                    stmt.close();
                                }
                                con.close();
                            }
                        }
                    } catch (Throwable e) {
                        // exception in SimContext
                        lg.error(e.getMessage(), e);
                        if (bUpdateDatabase) {
                            con = null;
                            stmt = null;
                            try {
                                con = conFactory.getConnection(new Object());
                                stmt = con.createStatement();
                                String status = "'EXCEPTION: " + org.vcell.util.TokenMangler.getSQLEscapedString(e.toString()) + "'";
                                if (status.length() > 255)
                                    status = status.substring(0, 254) + "'";
                                String UPDATESTATUS = "UPDATE " + SimStatTable.table.getTableName() + " SET " + SimStatTable.table.status.getUnqualifiedColName() + " = " + status + " WHERE " + SimStatTable.table.simRef.getUnqualifiedColName() + " = " + appSimsFromDB[l].getKey();
                                int numRowsChanged = stmt.executeUpdate(UPDATESTATUS);
                                if (numRowsChanged != 1) {
                                    System.out.println("failed to update status with exception");
                                }
                                con.commit();
                                if (lg.isTraceEnabled())
                                    lg.trace("-------------- Update=true, saved exception for Simulation '" + appSimsFromDB[l].getName() + "'");
                            } catch (SQLException e2) {
                                lg.error(e2.getMessage(), e2);
                                if (lg.isWarnEnabled())
                                    lg.warn("*&*&*&*&*&*&*& Update=true, FAILED TO save exception status for simulation '" + appSimsFromDB[l].getName() + "'");
                            } finally {
                                if (stmt != null) {
                                    stmt.close();
                                }
                                con.close();
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            // exception in whole BioModel
            lg.error(e.getMessage(), e);
        // can't update anything in database, since we don't know what simcontexts are involved
        }
    }
}
Also used : User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Connection(java.sql.Connection) MathDescription(cbit.vcell.math.MathDescription) SQLException(java.sql.SQLException) BigString(org.vcell.util.BigString) BigString(org.vcell.util.BigString) MathCompareResults(cbit.vcell.math.MathCompareResults) BioModelInfo(org.vcell.util.document.BioModelInfo) SimulationStatusPersistent(cbit.vcell.server.SimulationStatusPersistent) SimulationContext(cbit.vcell.mapping.SimulationContext) PropertyVetoException(java.beans.PropertyVetoException) MatrixException(cbit.vcell.matrix.MatrixException) ModelException(cbit.vcell.model.ModelException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) SQLException(java.sql.SQLException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MathException(cbit.vcell.math.MathException) Simulation(cbit.vcell.solver.Simulation) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource)

Example 34 with BioModelInfo

use of org.vcell.util.document.BioModelInfo in project vcell by virtualcell.

the class DatabaseServerImpl method getVersionInfos.

/**
 * This method was created in VisualAge.
 * @return GeometryInfo
 * @param key KeyValue
 * @exception org.vcell.util.DataAccessException The exception description.
 * @exception java.rmi.RemoteException The exception description.
 */
private VersionInfo[] getVersionInfos(User user, KeyValue key, VersionableType vType, boolean bAll, boolean bCheckPermission) throws DataAccessException {
    try {
        if (lg.isTraceEnabled())
            lg.trace("DatabaseServerImpl.getVersionInfos(User=" + user + ",vType=" + vType + ",bAll=" + bAll + ")");
        Vector<VersionInfo> vector = dbTop.getVersionableInfos(user, key, vType, bAll, bCheckPermission, true);
        if (vType.equals(VersionableType.BioModelMetaData)) {
            BioModelInfo[] bioModelInfos = new BioModelInfo[vector.size()];
            vector.copyInto(bioModelInfos);
            return bioModelInfos;
        } else if (vType.equals(VersionableType.Geometry)) {
            GeometryInfo[] geoInfos = new GeometryInfo[vector.size()];
            vector.copyInto(geoInfos);
            return geoInfos;
        } else if (vType.equals(VersionableType.MathModelMetaData)) {
            MathModelInfo[] mathInfos = new MathModelInfo[vector.size()];
            vector.copyInto(mathInfos);
            return mathInfos;
        } else if (vType.equals(VersionableType.VCImage)) {
            VCImageInfo[] imgInfos = new VCImageInfo[vector.size()];
            vector.copyInto(imgInfos);
            return imgInfos;
        } else if (vType.equals(VersionableType.Simulation)) {
            SimulationInfo[] simInfos = new SimulationInfo[vector.size()];
            vector.copyInto(simInfos);
            return simInfos;
        } else {
            throw new IllegalArgumentException("Wrong VersinableType vType:" + vType);
        }
    } catch (SQLException e) {
        lg.error(e.getMessage(), e);
        throw new DataAccessException(e.getMessage());
    } catch (ObjectNotFoundException e) {
        lg.error(e.getMessage(), e);
        throw new ObjectNotFoundException(e.getMessage());
    } catch (Throwable e) {
        lg.error(e.getMessage(), e);
        throw new DataAccessException(e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) BioModelInfo(org.vcell.util.document.BioModelInfo) MathModelInfo(org.vcell.util.document.MathModelInfo) VersionInfo(org.vcell.util.document.VersionInfo) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) DataAccessException(org.vcell.util.DataAccessException) SimulationInfo(cbit.vcell.solver.SimulationInfo)

Example 35 with BioModelInfo

use of org.vcell.util.document.BioModelInfo in project vcell by virtualcell.

the class DbDriver method curate.

/**
 * Insert the method's description here.
 * Creation date: (5/23/2006 10:44:52 AM)
 */
public static VCDocumentInfo curate(CurateSpec curateSpec, Connection con, User user, DatabaseSyntax dbSyntax) throws DataAccessException, SQLException {
    VersionableType vType = null;
    if (curateSpec.getVCDocumentInfo() instanceof BioModelInfo) {
        vType = VersionableType.BioModelMetaData;
    } else if (curateSpec.getVCDocumentInfo() instanceof MathModelInfo) {
        vType = VersionableType.MathModelMetaData;
    } else {
        throw new DataAccessException("Expecting BioModelInfo or MathModelInfo but got type=" + curateSpec.getVCDocumentInfo().getClass().getName());
    }
    KeyValue vKey = curateSpec.getVCDocumentInfo().getVersion().getVersionKey();
    Version dbVersion = getVersionFromKeyValue(con, vType, vKey);
    // Must be owner to curate
    if (!dbVersion.getOwner().compareEqual(user)) {
        throw new PermissionException("Cannot curate " + vType.getTypeName() + " \"" + dbVersion.getName() + "\" (" + vKey + "), not owned by " + user.getName());
    }
    VersionFlag updatedVersionFlag = null;
    if (curateSpec.getCurateType() == CurateSpec.ARCHIVE) {
        if (!dbVersion.getFlag().compareEqual(VersionFlag.Current)) {
            throw new IllegalArgumentException("Only non-archived, non-published documents can be ARCHIVED");
        }
        updatedVersionFlag = VersionFlag.Archived;
    } else if (curateSpec.getCurateType() == CurateSpec.PUBLISH) {
        // Must have PUBLISH rights
        if (!dbVersion.getOwner().isPublisher()) {
            throw new PermissionException("Cannot curate " + vType.getTypeName() + " \"" + dbVersion.getName() + "\" (" + vKey + "), user " + user.getName() + " not granted PUBLISHING rights");
        }
        // Must be ARCHIVED and Public before PUBLISH is allowed
        if (!dbVersion.getFlag().compareEqual(VersionFlag.Archived) || !(dbVersion.getGroupAccess() instanceof GroupAccessAll)) {
            throw new IllegalArgumentException("Only ARCHIVED documents with PUBLIC permission can be PUBLISHED");
        }
        updatedVersionFlag = VersionFlag.Published;
    } else {
        throw new DataAccessException("Expecting CurateType " + CurateSpec.ARCHIVE + "(ARCHIVE) or " + CurateSpec.PUBLISH + "(PUBLISH) but got type=" + curateSpec.getCurateType());
    }
    VersionTable vTable = VersionTable.getVersionTable(vType);
    String set = vTable.versionFlag.getQualifiedColName() + " = " + updatedVersionFlag.getIntValue();
    String cond = vTable.id.getQualifiedColName() + " = " + vKey;
    String sql = DatabasePolicySQL.enforceOwnershipUpdate(user, vTable, set, cond);
    int numRowsProcessed = updateCleanSQL(con, sql);
    // Clear XML
    if (vType.equals(VersionableType.BioModelMetaData)) {
        updateCleanSQL(con, "DELETE FROM " + BioModelXMLTable.table.getTableName() + " WHERE " + BioModelXMLTable.table.bioModelRef.getQualifiedColName() + " = " + vKey.toString());
    } else if (vType.equals(VersionableType.MathModelMetaData)) {
        updateCleanSQL(con, "DELETE FROM " + MathModelXMLTable.table.getTableName() + " WHERE " + MathModelXMLTable.table.mathModelRef.getQualifiedColName() + " = " + vKey.toString());
    }
    VCDocumentInfo dbVCDocumentInfo = (VCDocumentInfo) getVersionableInfos(con, user, vType, false, vKey, false, dbSyntax).elementAt(0);
    return dbVCDocumentInfo;
}
Also used : PermissionException(org.vcell.util.PermissionException) KeyValue(org.vcell.util.document.KeyValue) BioModelInfo(org.vcell.util.document.BioModelInfo) MathModelInfo(org.vcell.util.document.MathModelInfo) VersionableType(org.vcell.util.document.VersionableType) VersionFlag(org.vcell.util.document.VersionFlag) GroupAccessAll(org.vcell.util.document.GroupAccessAll) Version(org.vcell.util.document.Version) VersionableTypeVersion(org.vcell.util.document.VersionableTypeVersion) VCDocumentInfo(org.vcell.util.document.VCDocumentInfo) DataAccessException(org.vcell.util.DataAccessException)

Aggregations

BioModelInfo (org.vcell.util.document.BioModelInfo)79 DataAccessException (org.vcell.util.DataAccessException)38 MathModelInfo (org.vcell.util.document.MathModelInfo)37 BioModel (cbit.vcell.biomodel.BioModel)25 GeometryInfo (cbit.vcell.geometry.GeometryInfo)22 KeyValue (org.vcell.util.document.KeyValue)18 SimulationContext (cbit.vcell.mapping.SimulationContext)17 VCDocumentInfo (org.vcell.util.document.VCDocumentInfo)15 BigString (org.vcell.util.BigString)14 User (org.vcell.util.document.User)14 Vector (java.util.Vector)13 Simulation (cbit.vcell.solver.Simulation)12 XmlParseException (cbit.vcell.xml.XmlParseException)12 SQLException (java.sql.SQLException)12 XMLSource (cbit.vcell.xml.XMLSource)10 MathModel (cbit.vcell.mathmodel.MathModel)9 Hashtable (java.util.Hashtable)9 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)9 RemoteProxyException (cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)8 Geometry (cbit.vcell.geometry.Geometry)7