Search in sources :

Example 1 with SparseDoubleMatrix2D

use of cern.colt.matrix.impl.SparseDoubleMatrix2D in project midpoint by Evolveum.

the class AbstractOrgClosureTest method checkClosureMatrix.

protected boolean checkClosureMatrix() throws SchemaException {
    Session session = getSession();
    // we compute the closure table "by hand" as 1 + A + A^2 + A^3 + ... + A^n where n is the greatest expected path length
    int vertices = getVertices().size();
    long start = System.currentTimeMillis();
    // used to give indices to vertices
    List<String> vertexList = new ArrayList<>(getVertices());
    if (DUMP_TC_MATRIX_DETAILS)
        LOGGER.info("Vertex list = {}", vertexList);
    DoubleMatrix2D a = new SparseDoubleMatrix2D(vertices, vertices);
    //        }
    for (DefaultEdge edge : orgGraph.edgeSet()) {
        a.set(vertexList.indexOf(orgGraph.getEdgeSource(edge)), vertexList.indexOf(orgGraph.getEdgeTarget(edge)), 1.0);
    }
    DoubleMatrix2D result = new SparseDoubleMatrix2D(vertices, vertices);
    for (int i = 0; i < vertices; i++) {
        result.setQuick(i, i, 1.0);
    }
    DoubleMatrix2D power = result.copy();
    Algebra alg = new Algebra();
    for (int level = 1; level <= maxLevel; level++) {
        power = alg.mult(power, a);
        result.assign(power, Functions.plus);
    //            System.out.println("a=" + a);
    //            System.out.println("a^"+level+"="+power);
    }
    LOGGER.info("TC matrix computed in {} ms", System.currentTimeMillis() - start);
    if (DUMP_TC_MATRIX_DETAILS)
        LOGGER.info("TC matrix expected = {}", result);
    Query q = session.createSQLQuery("select descendant_oid, ancestor_oid, val from m_org_closure").addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE).addScalar("val", LongType.INSTANCE);
    List<Object[]> list = q.list();
    LOGGER.info("OrgClosure has {} rows", list.size());
    DoubleMatrix2D closureInDatabase = new SparseDoubleMatrix2D(vertices, vertices);
    for (Object[] item : list) {
        int val = Integer.parseInt(item[2].toString());
        if (val == 0) {
            throw new IllegalStateException("Row with val == 0 in closure table: " + list);
        }
        closureInDatabase.set(vertexList.indexOf(item[0]), vertexList.indexOf(item[1]), val);
    }
    if (DUMP_TC_MATRIX_DETAILS)
        LOGGER.info("TC matrix fetched from db = {}", closureInDatabase);
    double zSumResultBefore = result.zSum();
    double zSumClosureInDb = closureInDatabase.zSum();
    result.assign(closureInDatabase, Functions.minus);
    double zSumResultAfter = result.zSum();
    LOGGER.info("Summary of items in closure computed: {}, in DB-stored closure: {}, delta: {}", new Object[] { zSumResultBefore, zSumClosureInDb, zSumResultAfter });
    if (DUMP_TC_MATRIX_DETAILS)
        LOGGER.info("Difference matrix = {}", result);
    boolean problem = false;
    for (int i = 0; i < vertices; i++) {
        for (int j = 0; j < vertices; j++) {
            double delta = result.get(i, j);
            if (Math.round(delta) != 0) {
                System.err.println("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta + " (closureInDB=" + closureInDatabase.get(i, j) + ", expected=" + (result.get(i, j) + closureInDatabase.get(i, j)) + ")");
                LOGGER.error("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta);
                problem = true;
            }
        }
    }
    if (problem) {
        checkOrgGraph();
    }
    return problem;
}
Also used : Query(org.hibernate.Query) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ArrayList(java.util.ArrayList) DefaultEdge(org.jgrapht.graph.DefaultEdge) Algebra(cern.colt.matrix.linalg.Algebra) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) SparseDoubleMatrix2D(cern.colt.matrix.impl.SparseDoubleMatrix2D) PrismObject(com.evolveum.midpoint.prism.PrismObject) SparseDoubleMatrix2D(cern.colt.matrix.impl.SparseDoubleMatrix2D) Session(org.hibernate.Session)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 SparseDoubleMatrix2D (cern.colt.matrix.impl.SparseDoubleMatrix2D)1 Algebra (cern.colt.matrix.linalg.Algebra)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)1 ArrayList (java.util.ArrayList)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1