Search in sources :

Example 16 with DefaultEdge

use of org.jgrapht.graph.DefaultEdge in project midpoint by Evolveum.

the class AbstractOrgClosureTest method checkClosureMatrix.

protected boolean checkClosureMatrix() throws SchemaException {
    try (Session session = openSession()) {
        // 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.createNativeQuery("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: {}", 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.Query) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) 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)

Example 17 with DefaultEdge

use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.

the class TypeGraph method assignRelativeNumbering.

private int assignRelativeNumbering(ClassInfo node, Map<ClassInfo, Pair<Integer, Integer>> rn, int low) {
    int next = low;
    for (DefaultEdge subEdge : outgoingEdgesOf(node)) {
        ClassInfo sub = getEdgeTarget(subEdge);
        if (sub.isInterface())
            continue;
        next = assignRelativeNumbering(sub, rn, next + 1);
    }
    rn.put(node, new Pair<Integer, Integer>(low, next));
    return next;
}
Also used : DefaultEdge(org.jgrapht.graph.DefaultEdge) ClassInfo(com.jopdesign.common.ClassInfo)

Example 18 with DefaultEdge

use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.

the class TypeGraph method getSupertypeSets.

/**
     * Compute, for each type, the set of subtypes, using a single
     * top-down traversal
     * @return
     */
public Map<ClassInfo, Set<ClassInfo>> getSupertypeSets() {
    Map<ClassInfo, Set<ClassInfo>> supertypeMap = new HashMap<ClassInfo, Set<ClassInfo>>();
    for (ClassInfo v : topDownTraversal()) {
        Set<ClassInfo> supertypes = new HashSet<ClassInfo>();
        for (DefaultEdge parentEdge : incomingEdgesOf(v)) {
            ClassInfo supertype = getEdgeSource(parentEdge);
            supertypes.addAll(supertypeMap.get(supertype));
            supertypes.add(supertype);
        }
        supertypeMap.put(v, supertypes);
    }
    return supertypeMap;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) HashMap(java.util.HashMap) DefaultEdge(org.jgrapht.graph.DefaultEdge) ClassInfo(com.jopdesign.common.ClassInfo) HashSet(java.util.HashSet)

Example 19 with DefaultEdge

use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.

the class TypeGraph method exportDOT.

/**
     * Write the type graph in DOT format.
     * @param w
     * @param classFilter If non-null, only classes matching this prefix will be exported
     * @throws IOException
     */
public void exportDOT(Writer w, String classFilter) throws IOException {
    Set<ClassInfo> subset = null;
    if (classFilter != null) {
        subset = new HashSet<ClassInfo>();
        subset.add(this.rootNode);
        for (ClassInfo ci : this.vertexSet()) {
            if (ci.getClassName().startsWith(classFilter)) {
                while (ci.getSuperClassInfo() != null) {
                    subset.add(ci);
                    ci = ci.getSuperClassInfo();
                }
            }
        }
    }
    Subgraph<ClassInfo, DefaultEdge, TypeGraph> subgraph = new Subgraph<ClassInfo, DefaultEdge, TypeGraph>(this, subset);
    AdvancedDOTExporter<ClassInfo, DefaultEdge> exporter = new AdvancedDOTExporter<ClassInfo, DefaultEdge>(new TgNodeLabeller(), null);
    exporter.exportDOTDiGraph(w, subgraph);
}
Also used : Subgraph(org.jgrapht.graph.Subgraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) ClassInfo(com.jopdesign.common.ClassInfo)

Example 20 with DefaultEdge

use of org.jgrapht.graph.DefaultEdge in project presto by prestodb.

the class QueryQueueRuleFactory method shortestCycle.

private static List<String> shortestCycle(DirectedGraph<String, DefaultEdge> graph) {
    FloydWarshallShortestPaths<String, DefaultEdge> floyd = new FloydWarshallShortestPaths<>(graph);
    int minDistance = Integer.MAX_VALUE;
    String minSource = null;
    String minDestination = null;
    for (DefaultEdge edge : graph.edgeSet()) {
        String src = graph.getEdgeSource(edge);
        String dst = graph.getEdgeTarget(edge);
        // from dst to src
        int dist = (int) Math.round(floyd.shortestDistance(dst, src));
        if (dist < 0) {
            continue;
        }
        if (dist < minDistance) {
            minDistance = dist;
            minSource = src;
            minDestination = dst;
        }
    }
    if (minSource == null) {
        return null;
    }
    GraphPath<String, DefaultEdge> shortestPath = floyd.getShortestPath(minDestination, minSource);
    List<String> pathVertexList = Graphs.getPathVertexList(shortestPath);
    // note: pathVertexList will be [a, a] instead of [a] when the shortest path is a loop edge
    if (!Objects.equals(shortestPath.getStartVertex(), shortestPath.getEndVertex())) {
        pathVertexList.add(pathVertexList.get(0));
    }
    return pathVertexList;
}
Also used : FloydWarshallShortestPaths(org.jgrapht.alg.FloydWarshallShortestPaths) DefaultEdge(org.jgrapht.graph.DefaultEdge)

Aggregations

DefaultEdge (org.jgrapht.graph.DefaultEdge)26 ClassInfo (com.jopdesign.common.ClassInfo)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)6 Set (java.util.Set)5 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)5 BitSet (java.util.BitSet)4 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TopologicalOrderIterator (org.jgrapht.traverse.TopologicalOrderIterator)3 File (java.io.File)2 StringWriter (java.io.StringWriter)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 ExportException (org.jgrapht.nio.ExportException)2 GraphExpr (org.matheclipse.core.expression.data.GraphExpr)2 IAST (org.matheclipse.core.interfaces.IAST)2 IASTDataset (org.matheclipse.core.interfaces.IASTDataset)2 IExpr (org.matheclipse.core.interfaces.IExpr)2