Search in sources :

Example 1 with DirectedGraph

use of org._3pq.jgrapht.DirectedGraph in project OpenOLAT by OpenOLAT.

the class CourseEditorEnvImpl method listCycles.

/**
 * @see org.olat.course.editor.CourseEditorEnv#listCycles()
 */
@Override
public Set<String> listCycles() {
    /*
		 * convert nodeRefs datastructure to a directed graph 
		 */
    DirectedGraph dg = new DefaultDirectedGraph();
    DirectedEdgeFactory def = new EdgeFactories.DirectedEdgeFactory();
    /*
		 * add the course structure as directed graph, where 
		 */
    Visitor v = new Convert2DGVisitor(dg);
    (new TreeVisitor(v, cetm.getRootNode(), true)).visitAll();
    /*
		 * iterate over nodeRefs, add each not existing node id as vertex, for each
		 * key - child relation add an edge to the directed graph.
		 */
    Map<String, Set<String>> nodeSoftRefs = new HashMap<>();
    for (Iterator<String> iter = softRefs.keySet().iterator(); iter.hasNext(); ) {
        String nodeId = iter.next();
        List<ConditionExpression> conditionExprs = softRefs.get(nodeId);
        for (int i = 0; i < conditionExprs.size(); i++) {
            ConditionExpression ce = conditionExprs.get(i);
            Set<String> refs = ce.getSoftReferencesForCycleDetectorOf("courseNodeId");
            if (refs != null && refs.size() > 0) {
                if (nodeSoftRefs.containsKey(nodeId)) {
                    nodeSoftRefs.get(nodeId).addAll(refs);
                } else {
                    nodeSoftRefs.put(nodeId, refs);
                }
            }
        }
    }
    for (Iterator<String> keys = nodeSoftRefs.keySet().iterator(); keys.hasNext(); ) {
        // a node
        String key = keys.next();
        if (!dg.containsVertex(key)) {
            dg.addVertex(key);
        }
        // and its children
        Set<String> children = nodeSoftRefs.get(key);
        for (Iterator<String> childrenIt = children.iterator(); childrenIt.hasNext(); ) {
            String child = childrenIt.next();
            if (!dg.containsVertex(child)) {
                dg.addVertex(child);
            }
            // add edge, precondition: vertex key - child are already added to the graph
            Edge de = def.createEdge(key, child);
            dg.addEdge(de);
        }
    }
    /*
		 * find the id's participating in the cycle, and return the intersection
		 * with set of id's which actually produce references.
		 */
    CycleDetector cd = new CycleDetector(dg);
    Set<String> cycleIds = cd.findCycles();
    cycleIds.retainAll(nodeSoftRefs.keySet());
    return cycleIds;
}
Also used : Set(java.util.Set) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) DefaultDirectedGraph(org._3pq.jgrapht.graph.DefaultDirectedGraph) HashMap(java.util.HashMap) CycleDetector(org._3pq.jgrapht.alg.CycleDetector) TreeVisitor(org.olat.core.util.tree.TreeVisitor) DefaultDirectedGraph(org._3pq.jgrapht.graph.DefaultDirectedGraph) DirectedGraph(org._3pq.jgrapht.DirectedGraph) DirectedEdgeFactory(org._3pq.jgrapht.edge.EdgeFactories.DirectedEdgeFactory) ConditionExpression(org.olat.course.condition.interpreter.ConditionExpression) Edge(org._3pq.jgrapht.Edge)

Example 2 with DirectedGraph

use of org._3pq.jgrapht.DirectedGraph in project openolat by klemens.

the class CourseEditorEnvImpl method listCycles.

/**
 * @see org.olat.course.editor.CourseEditorEnv#listCycles()
 */
@Override
public Set<String> listCycles() {
    /*
		 * convert nodeRefs datastructure to a directed graph 
		 */
    DirectedGraph dg = new DefaultDirectedGraph();
    DirectedEdgeFactory def = new EdgeFactories.DirectedEdgeFactory();
    /*
		 * add the course structure as directed graph, where 
		 */
    Visitor v = new Convert2DGVisitor(dg);
    (new TreeVisitor(v, cetm.getRootNode(), true)).visitAll();
    /*
		 * iterate over nodeRefs, add each not existing node id as vertex, for each
		 * key - child relation add an edge to the directed graph.
		 */
    Map<String, Set<String>> nodeSoftRefs = new HashMap<>();
    for (Iterator<String> iter = softRefs.keySet().iterator(); iter.hasNext(); ) {
        String nodeId = iter.next();
        List<ConditionExpression> conditionExprs = softRefs.get(nodeId);
        for (int i = 0; i < conditionExprs.size(); i++) {
            ConditionExpression ce = conditionExprs.get(i);
            Set<String> refs = ce.getSoftReferencesForCycleDetectorOf("courseNodeId");
            if (refs != null && refs.size() > 0) {
                if (nodeSoftRefs.containsKey(nodeId)) {
                    nodeSoftRefs.get(nodeId).addAll(refs);
                } else {
                    nodeSoftRefs.put(nodeId, refs);
                }
            }
        }
    }
    for (Iterator<String> keys = nodeSoftRefs.keySet().iterator(); keys.hasNext(); ) {
        // a node
        String key = keys.next();
        if (!dg.containsVertex(key)) {
            dg.addVertex(key);
        }
        // and its children
        Set<String> children = nodeSoftRefs.get(key);
        for (Iterator<String> childrenIt = children.iterator(); childrenIt.hasNext(); ) {
            String child = childrenIt.next();
            if (!dg.containsVertex(child)) {
                dg.addVertex(child);
            }
            // add edge, precondition: vertex key - child are already added to the graph
            Edge de = def.createEdge(key, child);
            dg.addEdge(de);
        }
    }
    /*
		 * find the id's participating in the cycle, and return the intersection
		 * with set of id's which actually produce references.
		 */
    CycleDetector cd = new CycleDetector(dg);
    Set<String> cycleIds = cd.findCycles();
    cycleIds.retainAll(nodeSoftRefs.keySet());
    return cycleIds;
}
Also used : Set(java.util.Set) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) DefaultDirectedGraph(org._3pq.jgrapht.graph.DefaultDirectedGraph) HashMap(java.util.HashMap) CycleDetector(org._3pq.jgrapht.alg.CycleDetector) TreeVisitor(org.olat.core.util.tree.TreeVisitor) DefaultDirectedGraph(org._3pq.jgrapht.graph.DefaultDirectedGraph) DirectedGraph(org._3pq.jgrapht.DirectedGraph) DirectedEdgeFactory(org._3pq.jgrapht.edge.EdgeFactories.DirectedEdgeFactory) ConditionExpression(org.olat.course.condition.interpreter.ConditionExpression) Edge(org._3pq.jgrapht.Edge)

Aggregations

HashMap (java.util.HashMap)2 Set (java.util.Set)2 DirectedGraph (org._3pq.jgrapht.DirectedGraph)2 Edge (org._3pq.jgrapht.Edge)2 CycleDetector (org._3pq.jgrapht.alg.CycleDetector)2 DirectedEdgeFactory (org._3pq.jgrapht.edge.EdgeFactories.DirectedEdgeFactory)2 DefaultDirectedGraph (org._3pq.jgrapht.graph.DefaultDirectedGraph)2 TreeVisitor (org.olat.core.util.tree.TreeVisitor)2 Visitor (org.olat.core.util.tree.Visitor)2 ConditionExpression (org.olat.course.condition.interpreter.ConditionExpression)2