Coverage Report - net.sourceforge.combean.graph.alg.traversal.FindPathByTraversalAlg
 
Classes in this File Line Coverage Branch Coverage Complexity
FindPathByTraversalAlg
97%
34/35
75%
6/8
1,75
 
 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 net.sourceforge.combean.graph.alg.AbstractGraphAlg;
 25  
 import net.sourceforge.combean.graph.containers.statics.PathFromPredMapBuilder;
 26  
 import net.sourceforge.combean.interfaces.graph.Node;
 27  
 import net.sourceforge.combean.interfaces.graph.alg.traverse.GraphTraversalAlg;
 28  
 import net.sourceforge.combean.interfaces.graph.containers.FixedPath;
 29  
 import net.sourceforge.combean.interfaces.graph.prop.NeighborhoodGraphProp;
 30  
 import net.sourceforge.combean.util.except.IllegalParameterException;
 31  
 
 32  
 /**
 33  
  * @author schickin
 34  
  *
 35  
  */
 36  
 public class FindPathByTraversalAlg extends AbstractGraphAlg {
 37  
     
 38  18
     NeighborhoodGraphProp neigh = null;
 39  
     
 40  18
     private GraphTraversalAlg traversalAlg = null;
 41  
     
 42  18
     private Node startNode = null;
 43  18
     private Node targetNode = null;
 44  
     
 45  18
     private boolean useOutgoingEdgesOnly = false;
 46  
     
 47  18
     private ConstructPredMapVisitor predMapVisitor = null;
 48  
 
 49  
     /**
 50  
      * constructor
 51  
      */
 52  
     public FindPathByTraversalAlg() {
 53  18
         super();
 54  18
     }
 55  
 
 56  
     /* (non-Javadoc)
 57  
      * @see net.sourceforge.combean.interfaces.graph.alg.GraphAlgorithm#run()
 58  
      */
 59  
     public void run() {
 60  27
         init();
 61  27
         this.traversalAlg.run();
 62  27
     }
 63  
 
 64  
     private void init() {
 65  27
         if (this.traversalAlg == null) {
 66  12
             this.traversalAlg = new BreadthFirstSearchImpl();
 67  
         }
 68  27
         this.traversalAlg.setGraph(this.neigh);
 69  27
         this.traversalAlg.setUseOnlyOutgoingEdges(this.useOutgoingEdgesOnly);
 70  
         
 71  27
         if (this.startNode == null) {
 72  0
             throw new IllegalParameterException("start node must be set");
 73  
         }
 74  27
         this.traversalAlg.setLocalStartNode(this.startNode);
 75  
         
 76  27
         this.predMapVisitor = new ConstructPredMapVisitor();
 77  27
         if (this.targetNode != null) {
 78  27
             this.predMapVisitor.setTarget(this.targetNode);
 79  
         }
 80  
         
 81  27
         this.traversalAlg.setVisitor(this.predMapVisitor);
 82  27
     }
 83  
     
 84  
     /**
 85  
      * @param traversalAlg The traversalAlg to set.
 86  
      */
 87  
     public final void setTraversalAlg(GraphTraversalAlg traversalAlg) {
 88  6
         this.traversalAlg = traversalAlg;
 89  6
     }
 90  
     
 91  
     /**
 92  
      * @param startNode The startNode to set.
 93  
      */
 94  
     public final void setStartNode(Node startNode) {
 95  18
         this.startNode = startNode;
 96  18
     }
 97  
     
 98  
     /**
 99  
      * @param targetNode The targetNode to set.
 100  
      */
 101  
     public final void setTargetNode(Node targetNode) {
 102  18
         this.targetNode = targetNode;
 103  18
     }
 104  
     
 105  
     /**
 106  
      * Return the path to t that has been found by the traversal algorithm
 107  
      * or null if the node is not reachable from the start node.
 108  
      * 
 109  
      * @param t the node to which the path shall be retrieved
 110  
      * @return the path to t
 111  
      */
 112  
     public FixedPath getPathTo(Node t) {
 113  27
         FixedPath result =
 114  
             PathFromPredMapBuilder.buildPathFromPredMap(this.neigh,
 115  
                     this.predMapVisitor.getPredMap(), t);
 116  27
         if (!result.getFirstNode().equals(this.startNode)) {
 117  9
             return null;
 118  
         }
 119  18
         return result;
 120  
     }
 121  
     
 122  
     /**
 123  
      * @param useOutgoingEdgesOnly The useOutgoingEdgesOnly to set.
 124  
      */
 125  
     public final void setUseOutgoingEdgesOnly(boolean useOutgoingEdgesOnly) {
 126  18
         this.useOutgoingEdgesOnly = useOutgoingEdgesOnly;
 127  18
     }
 128  
 }