Coverage Report - net.sourceforge.combean.graph.alg.traversal.AbstractGraphTraversalAlg
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractGraphTraversalAlg
100%
50/50
100%
12/12
1,5
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 20.08.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.graph.alg.traversal;
 23  
 
 24  
 import java.util.BitSet;
 25  
 
 26  
 import net.sourceforge.combean.graph.alg.AbstractGraphAlg;
 27  
 import net.sourceforge.combean.graph.containers.NodeSetAdvisor;
 28  
 import net.sourceforge.combean.interfaces.graph.EdgeIterator;
 29  
 import net.sourceforge.combean.interfaces.graph.Node;
 30  
 import net.sourceforge.combean.interfaces.graph.NodeIterator;
 31  
 import net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg;
 32  
 import net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor;
 33  
 import net.sourceforge.combean.interfaces.graph.containers.NodeSet;
 34  
 import net.sourceforge.combean.interfaces.graph.prop.GlobalNodesGraphProp;
 35  
 import net.sourceforge.combean.interfaces.graph.prop.NeighborhoodGraphProp;
 36  
 import net.sourceforge.combean.interfaces.graph.prop.OutgoingEdgeNeighborhoodGraphProp;
 37  
 
 38  
 /**
 39  
  * Abstract base class for DFS- and BFS-traversal. Holds common datastructures.
 40  
  * 
 41  
  * @author schickin
 42  
  *
 43  
  */
 44  
 public abstract class AbstractGraphTraversalAlg extends AbstractGraphAlg implements
 45  
         GraphTraversalAlg {
 46  
 
 47  78
     private TraversalVisitor visitor = null;
 48  
 
 49  78
     private Node startNode = null;
 50  
 
 51  78
     private GlobalNodesGraphProp globalNodes = null;
 52  78
     private NeighborhoodGraphProp graph = null;
 53  78
     private OutgoingEdgeNeighborhoodGraphProp outgoingNeigh = null;
 54  
 
 55  78
     private NodeSet visitedNodes = null;
 56  
     
 57  78
     private boolean useOnlyOutgoingEdges = false;
 58  
 
 59  
     /**
 60  
      * constructor
 61  
      */
 62  
     public AbstractGraphTraversalAlg() {
 63  78
         super();
 64  78
     }
 65  
 
 66  
     /**
 67  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg#setVisitor(net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor)
 68  
      */
 69  
     public void setVisitor(TraversalVisitor visitor) {
 70  93
             this.visitor = visitor;
 71  93
     }
 72  
 
 73  
     /**
 74  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg#getVisitor()
 75  
      */
 76  
     public final TraversalVisitor getVisitor() {
 77  1557
             return this.visitor;
 78  
     }
 79  
 
 80  
     /**
 81  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg#setLocalStartNode(net.sourceforge.combean.interfaces.graph.Node)
 82  
      */
 83  
     public final void setLocalStartNode(Node startNode) {
 84  45
             this.startNode = startNode;
 85  45
     }
 86  
 
 87  
     /**
 88  
      * @return Returns the startNode.
 89  
      */
 90  
     private final Node getStartNode() {
 91  354
         return this.startNode;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Set the NodeSet where the nodes are stored which have already been
 96  
      * visited during the traversal of the graph.
 97  
      * The NodeSetAdvisor will be consulted if left unset.
 98  
      * 
 99  
      * @param visitedNodes The visitedNodes to set.
 100  
      */
 101  
     public final void setVisitedNodes(NodeSet visitedNodes) {
 102  30
             this.visitedNodes = visitedNodes;
 103  30
     }
 104  
 
 105  
     /**
 106  
      * @return Returns the visitedNodes.
 107  
      */
 108  
     protected final NodeSet getVisitedNodes() {
 109  1059
         return this.visitedNodes;
 110  
     }
 111  
 
 112  
     /* (non-Javadoc)
 113  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg#setUseOnlyOutgoingEdges(boolean)
 114  
      */
 115  
     public void setUseOnlyOutgoingEdges(boolean useOnlyOutgoingEdges) {
 116  39
         this.useOnlyOutgoingEdges = useOnlyOutgoingEdges;
 117  39
     }
 118  
     
 119  
     /**
 120  
      * @return Returns the graph.
 121  
      */
 122  
     protected final NeighborhoodGraphProp getNeighborhood() {
 123  771
         return this.graph;
 124  
     }
 125  
 
 126  
     /**
 127  
      * Helper method for setting up the internal data structures.
 128  
      */
 129  
     protected void init() {
 130  99
         if (this.useOnlyOutgoingEdges) {
 131  27
             this.outgoingNeigh = (OutgoingEdgeNeighborhoodGraphProp) getGraph();
 132  
         }
 133  
         
 134  99
         if (this.visitedNodes == null) {
 135  48
             this.visitedNodes = NodeSetAdvisor.getFastestNodeSet(getNeighborhood(),
 136  
                     new BitSet(NodeSetAdvisor.NUM_PROPS));
 137  
         }
 138  99
         this.visitedNodes.clear();
 139  99
     }
 140  
 
 141  
     /**
 142  
      * Template method. Override with the implementation of a traversal
 143  
      * of all nodes reachable from v
 144  
      * 
 145  
      * @param v the node where the traversal shall start
 146  
      */
 147  
     protected abstract void runTraversalWithSingleStartNode(Node v);
 148  
     
 149  
     protected final EdgeIterator getNeighborIterator(Node v) {
 150  552
         if (this.useOnlyOutgoingEdges) {
 151  114
             return this.outgoingNeigh.getOutgoingEdges(v);
 152  
         }
 153  438
         return this.graph.getIncidentEdges(v);
 154  
     }
 155  
 
 156  
     /**
 157  
      * @see net.sourceforge.combean.interfaces.graph.alg.GraphAlgorithm#run()
 158  
      */
 159  
     public void run() {
 160  99
         init();
 161  
         
 162  99
             if (getStartNode() == null) {
 163  48
                     getVisitor().init(getNeighborhood());
 164  48
                     runTraversalForAllNodes();
 165  
             }
 166  
             else {
 167  51
                     getVisitor().initLocal(getNeighborhood(), getStartNode());
 168  51
                     getVisitor().visitComponent(getStartNode());
 169  51
                     getVisitedNodes().add(getStartNode());
 170  51
                     runTraversalWithSingleStartNode(getStartNode());
 171  51
                     getVisitor().leaveComponent(getStartNode());
 172  
             }
 173  99
     }
 174  
 
 175  
     /**
 176  
      * Execute the recursive DFS for all nodes in the graph.
 177  
      * 
 178  
      */
 179  
     private void runTraversalForAllNodes() {
 180  48
         NodeIterator allNodesIt = this.globalNodes.getAllNodesIterator();
 181  342
         while (allNodesIt.hasNext()) {
 182  294
             Node v = allNodesIt.next();
 183  294
             if (!getVisitedNodes().contains(v)) {
 184  90
                 getVisitor().visitComponent(v);
 185  90
                 getVisitedNodes().add(v);
 186  90
                 runTraversalWithSingleStartNode(v);
 187  90
                 getVisitor().leaveComponent(v);
 188  
             }
 189  294
         }
 190  48
     }
 191  
 }