Coverage Report - net.sourceforge.combean.graph.alg.traversal.BreadthFirstSearchImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
BreadthFirstSearchImpl
87%
27/31
83%
10/12
2,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 org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 import net.sourceforge.combean.graph.containers.ListAsNodeQueue;
 28  
 import net.sourceforge.combean.interfaces.graph.Edge;
 29  
 import net.sourceforge.combean.interfaces.graph.EdgeIterator;
 30  
 import net.sourceforge.combean.interfaces.graph.Node;
 31  
 import net.sourceforge.combean.interfaces.graph.alg.traverse.BreadthFirstSearch;
 32  
 import net.sourceforge.combean.interfaces.graph.containers.NodeQueue;
 33  
 
 34  
 /**
 35  
  * @author schickin
 36  
  *
 37  
  */
 38  
 public class BreadthFirstSearchImpl extends AbstractGraphTraversalAlg
 39  
 implements BreadthFirstSearch {
 40  
 
 41  3
     private static Log log = LogFactory.getLog(BreadthFirstSearchImpl.class);
 42  
     
 43  33
     NodeQueue queue = null;
 44  
 
 45  
     /**
 46  
      * constructor 
 47  
      */
 48  
     public BreadthFirstSearchImpl() {
 49  33
         super();
 50  33
     }
 51  
 
 52  
     /* (non-Javadoc)
 53  
      * @see net.sourceforge.combean.graph.alg.traversal.AbstractGraphTraversalAlg#runTraversalWithSingleStartNode(net.sourceforge.combean.interfaces.graph.Node)
 54  
      */
 55  
     protected void runTraversalWithSingleStartNode(Node startNode) {
 56  54
         this.queue.enqueue(startNode);
 57  
         
 58  270
         while (!this.queue.isEmpty()) {
 59  216
             Node v = this.queue.dequeue();
 60  216
             if (log.isDebugEnabled()) {
 61  0
                 log.debug("visit: " + v);
 62  
             }
 63  216
             getVisitor().visitNode(v);
 64  216
                     EdgeIterator it = getNeighborIterator(v);
 65  495
                     while (it.hasNext()) {
 66  279
                             Edge e = it.next();
 67  279
                             Node w = getNeighborhood().getOtherNode(e, v);
 68  279
                             boolean notYetVisited = getVisitedNodes().add(w);
 69  279
                             if (notYetVisited) {
 70  162
                                     getVisitor().openNeighbor(e, v);
 71  162
                                     this.queue.enqueue(w);
 72  162
                                     if (log.isTraceEnabled()) {
 73  0
                                         log.debug("enqueue: " + e);
 74  
                                     }
 75  
                             }
 76  
                             else {
 77  117
                                     getVisitor().reopenNeighbor(e, v);
 78  
                             }
 79  279
                     }
 80  
             
 81  216
         }
 82  54
     }
 83  
     
 84  
     /**
 85  
      * @param queue The queue to set.
 86  
      */
 87  
     public final void setQueue(NodeQueue queue) {
 88  0
         this.queue = queue;
 89  0
     }
 90  
     
 91  
     /* (non-Javadoc)
 92  
      * @see net.sourceforge.combean.graph.alg.traversal.AbstractGraphTraversalAlg#init()
 93  
      */
 94  
     protected void init() {
 95  42
          super.init();
 96  
          
 97  42
          if (this.queue == null) {
 98  30
              this.queue = new ListAsNodeQueue();
 99  
          }
 100  42
          this.queue.clear();
 101  42
     }
 102  
 }