Coverage Report - net.sourceforge.combean.adapters.jgraph.JGraphModelAsGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
JGraphModelAsGraph
94%
46/49
92%
11/12
1,389
 
 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.02.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.adapters.jgraph;
 23  
 
 24  
 import org.jgraph.graph.GraphModel;
 25  
 
 26  
 import net.sourceforge.combean.graph.iterators.IncidentEdgeAsIncomingEdgeIterator;
 27  
 import net.sourceforge.combean.graph.iterators.IncidentEdgeAsOutgoingEdgeIterator;
 28  
 import net.sourceforge.combean.graph.iterators.IndexedEdgeIterator;
 29  
 import net.sourceforge.combean.graph.iterators.IndexedNodeIterator;
 30  
 import net.sourceforge.combean.interfaces.graph.Edge;
 31  
 import net.sourceforge.combean.interfaces.graph.EdgeIterator;
 32  
 import net.sourceforge.combean.interfaces.graph.Graph;
 33  
 import net.sourceforge.combean.interfaces.graph.Node;
 34  
 import net.sourceforge.combean.interfaces.graph.NodeIterator;
 35  
 import net.sourceforge.combean.interfaces.graph.prop.DirectedEdgeNeighborhoodGraphProp;
 36  
 import net.sourceforge.combean.interfaces.graph.prop.GlobalIndexedEdgesGraphProp;
 37  
 import net.sourceforge.combean.interfaces.graph.prop.GlobalIndexedNodesGraphProp;
 38  
 
 39  
 /**
 40  
  * Use a JGraph GraphModel as a Combean Graph
 41  
  * 
 42  
  * @author schickin
 43  
  *
 44  
  */
 45  
 public class JGraphModelAsGraph implements
 46  
         Graph,
 47  
         DirectedEdgeNeighborhoodGraphProp,
 48  
         GlobalIndexedNodesGraphProp,
 49  
         GlobalIndexedEdgesGraphProp {
 50  
     
 51  39
     private GraphModel graphModel = null;
 52  
     
 53  39
     private Node[] allNodes = null;
 54  39
     private Edge[] allEdges = null;
 55  
 
 56  
     /**
 57  
      * Constructor
 58  
      * 
 59  
      * @param jgraph the GraphModel to be wrapped
 60  
      */
 61  
     public JGraphModelAsGraph(GraphModel jgraph) {
 62  39
         super();
 63  39
         this.graphModel = jgraph;
 64  39
         int edgeCount = countEdgesInGraphModel();
 65  39
         this.allEdges = new Edge[edgeCount];
 66  39
         this.allNodes = new Node[this.graphModel.getRootCount()-edgeCount];
 67  39
         copyNodesAndEdgesFromGraphModel();
 68  39
     }
 69  
     
 70  
     /**
 71  
      * Helper method to count all edges in the GraphModel
 72  
      * 
 73  
      * @return number of edges
 74  
      */
 75  
     private final int countEdgesInGraphModel() {
 76  39
         int edgeCount = 0;
 77  222
         for (int i = 0; i < this.graphModel.getRootCount(); i++) {
 78  183
             Object o = this.graphModel.getRootAt(i);
 79  183
             if (this.graphModel.isEdge(o)) {
 80  81
                 edgeCount++;
 81  
             }
 82  
         }
 83  39
         return edgeCount;
 84  
     }
 85  
 
 86  
     /**
 87  
      * Copy nodes and edges from GraphModel into internal data structures
 88  
      * for easier and more efficient access.
 89  
      */
 90  
     private final void copyNodesAndEdgesFromGraphModel() {
 91  39
         int edgeCount = 0;
 92  39
         int nodeCount = 0;
 93  222
         for (int i = 0; i < this.graphModel.getRootCount(); i++) {
 94  183
             Object o = this.graphModel.getRootAt(i);
 95  183
             if (this.graphModel.isEdge(o)) {
 96  81
                 this.allEdges[edgeCount++] =
 97  
                     new JGraphEdgeAsEdge((org.jgraph.graph.Edge) o);
 98  
             }
 99  
             else {
 100  102
                 this.allNodes[nodeCount++] = new JGraphCellAsNode(o);
 101  
             }
 102  
         }
 103  39
     }
 104  
 
 105  
     /* (non-Javadoc)
 106  
      * @see net.sourceforge.combean.interfaces.graph.Graph#getNodeClass()
 107  
      */
 108  
     public Class getNodeClass() {
 109  0
          return JGraphCellAsNode.class;
 110  
     }
 111  
 
 112  
     /* (non-Javadoc)
 113  
      * @see net.sourceforge.combean.interfaces.graph.Graph#getEdgeClass()
 114  
      */
 115  
     public Class getEdgeClass() {
 116  0
         return JGraphEdgeAsEdge.class;
 117  
     }
 118  
 
 119  
     /* (non-Javadoc)
 120  
      * @see net.sourceforge.combean.interfaces.graph.NeighborhoodGraphProp#getIncidentEdges(net.sourceforge.combean.interfaces.graph.Node)
 121  
      */
 122  
     public EdgeIterator getIncidentEdges(Node v) {
 123  33
          return new JGraphEdgeIterator(this.graphModel,
 124  
                  ((JGraphCellAsNode)v).getInnerObject());
 125  
     }
 126  
 
 127  
     /* (non-Javadoc)
 128  
      * @see net.sourceforge.combean.interfaces.graph.OutgoingEdgeNeighborhoodGraphProp#getOutgoingEdges(net.sourceforge.combean.interfaces.graph.Node)
 129  
      */
 130  
     public EdgeIterator getOutgoingEdges(Node v) {
 131  15
          return new IncidentEdgeAsOutgoingEdgeIterator(
 132  
                  this, v, getIncidentEdges(v));
 133  
     }
 134  
     
 135  
     /* (non-Javadoc)
 136  
      * @see net.sourceforge.combean.interfaces.graph.IncomingEdgeNeighborhoodGraphProp#getIncomingEdges(net.sourceforge.combean.interfaces.graph.Node)
 137  
      */
 138  
     public EdgeIterator getIncomingEdges(Node v) {
 139  3
          return new IncidentEdgeAsIncomingEdgeIterator(
 140  
                  this, v, getIncidentEdges(v));
 141  
     }
 142  
     /* (non-Javadoc)
 143  
      * @see net.sourceforge.combean.interfaces.graph.NeighborhoodGraphProp#getOtherNode(net.sourceforge.combean.interfaces.graph.Edge, net.sourceforge.combean.interfaces.graph.Node)
 144  
      */
 145  
     public Node getOtherNode(Edge e, Node v) {
 146  6
         Node w = getFirstNode(e);
 147  6
         if (w.equals(v)) {
 148  0
             return getSecondNode(e);
 149  
         }
 150  6
         return w;
 151  
     }
 152  
     
 153  
     /**
 154  
      * Move up the tree in the GraphModel to the root.
 155  
      * 
 156  
      * @param o the starting object for which the root shall be found
 157  
      * @return the root of the tree containing o
 158  
      */
 159  
     private final Object findRootOf(Object o) {
 160  111
         Object result = o;
 161  222
         while (this.graphModel.getParent(result) != null) {
 162  111
             result = this.graphModel.getParent(result);
 163  
         }
 164  111
         return result;
 165  
     }
 166  
 
 167  
     /* (non-Javadoc)
 168  
      * @see net.sourceforge.combean.interfaces.graph.NeighborhoodGraphProp#getFirstNode(net.sourceforge.combean.interfaces.graph.Edge)
 169  
      */
 170  
     public Node getFirstNode(Edge e) {
 171  69
         org.jgraph.graph.Edge jgraphEdge =
 172  
             ((JGraphEdgeAsEdge)e).getJGraphEdge();
 173  69
         Object source = this.graphModel.getSource(jgraphEdge);
 174  69
         return new JGraphCellAsNode(findRootOf(source));
 175  
     }
 176  
 
 177  
     /* (non-Javadoc)
 178  
      * @see net.sourceforge.combean.interfaces.graph.NeighborhoodGraphProp#getSecondNode(net.sourceforge.combean.interfaces.graph.Edge)
 179  
      */
 180  
     public Node getSecondNode(Edge e) {
 181  42
         org.jgraph.graph.Edge jgraphEdge =
 182  
             ((JGraphEdgeAsEdge)e).getJGraphEdge();
 183  42
         Object target = this.graphModel.getTarget(jgraphEdge);
 184  42
         return new JGraphCellAsNode(findRootOf(target));
 185  
     }
 186  
 
 187  
     /* (non-Javadoc)
 188  
      * @see net.sourceforge.combean.interfaces.graph.GlobalNodesGraphProp#getAllNodesIterator()
 189  
      */
 190  
     public NodeIterator getAllNodesIterator() {
 191  6
          return new IndexedNodeIterator(this);
 192  
     }
 193  
     
 194  
     /* (non-Javadoc)
 195  
      * @see net.sourceforge.combean.interfaces.graph.GlobalNodesGraphProp#getNumNodes()
 196  
      */
 197  
     public int getNumNodes() {
 198  57
         return this.allNodes.length;
 199  
     }
 200  
     
 201  
     /* (non-Javadoc)
 202  
      * @see net.sourceforge.combean.interfaces.graph.GlobalIndexedNodesGraphProp#getNode(int)
 203  
      */
 204  
     public Node getNode(int index) {
 205  18
          return this.allNodes[index];
 206  
     }
 207  
     
 208  
     /* (non-Javadoc)
 209  
      * @see net.sourceforge.combean.interfaces.graph.GlobalEdgeGraphProp#getAllEdgesIterator()
 210  
      */
 211  
     public EdgeIterator getAllEdgesIterator() {
 212  3
          return new IndexedEdgeIterator(this);
 213  
     }
 214  
     
 215  
     /* (non-Javadoc)
 216  
      * @see net.sourceforge.combean.interfaces.graph.GlobalEdgeGraphProp#getNumEdges()
 217  
      */
 218  
     public int getNumEdges() {
 219  12
          return this.allEdges.length;
 220  
     }
 221  
     
 222  
     /* (non-Javadoc)
 223  
      * @see net.sourceforge.combean.interfaces.graph.GlobalIndexedEdgesGraphProp#getEdge(int)
 224  
      */
 225  
     public Edge getEdge(int index) {
 226  30
          return this.allEdges[index];
 227  
     }
 228  
 }