Coverage Report - org.argouml.uml.cognitive.critics.CrNodesOverlap
 
Classes in this File Line Coverage Branch Coverage Complexity
CrNodesOverlap
15%
9/58
0%
0/42
5.167
 
 1  
 /* $Id: CrNodesOverlap.java 17849 2010-01-12 19:50:34Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2009 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.cognitive.critics;
 40  
 
 41  
 import java.awt.Rectangle;
 42  
 import java.util.HashSet;
 43  
 import java.util.List;
 44  
 import java.util.Set;
 45  
 
 46  
 import org.argouml.cognitive.Critic;
 47  
 import org.argouml.cognitive.Designer;
 48  
 import org.argouml.cognitive.ListSet;
 49  
 import org.argouml.cognitive.ToDoItem;
 50  
 import org.argouml.uml.cognitive.UMLDecision;
 51  
 import org.argouml.uml.diagram.SequenceDiagram;
 52  
 import org.argouml.uml.diagram.deployment.ui.FigObject;
 53  
 import org.argouml.uml.diagram.deployment.ui.UMLDeploymentDiagram;
 54  
 import org.argouml.uml.diagram.static_structure.ui.FigClass;
 55  
 import org.argouml.uml.diagram.static_structure.ui.FigInterface;
 56  
 import org.argouml.uml.diagram.ui.FigNodeModelElement;
 57  
 import org.tigris.gef.base.Diagram;
 58  
 import org.tigris.gef.presentation.FigNode;
 59  
 
 60  
 /**
 61  
  * A critic to detect when a modelelements overlap.
 62  
  *
 63  
  * @author jrobbins
 64  
  */
 65  
 public class CrNodesOverlap extends CrUML {
 66  
 
 67  
     /**
 68  
      * The constructor.
 69  
      */
 70  900
     public CrNodesOverlap() {
 71  
         // TODO: {name} is not expanded for diagram objects
 72  900
         setupHeadAndDesc();
 73  900
         addSupportedDecision(UMLDecision.CLASS_SELECTION);
 74  900
         addSupportedDecision(UMLDecision.EXPECTED_USAGE);
 75  900
         addSupportedDecision(UMLDecision.STATE_MACHINES);
 76  900
         setKnowledgeTypes(Critic.KT_PRESENTATION);
 77  900
     }
 78  
 
 79  
     ////////////////////////////////////////////////////////////////
 80  
     // critiquing API
 81  
 
 82  
     /*
 83  
      * @see org.argouml.uml.cognitive.critics.CrUML#predicate2(
 84  
      *      java.lang.Object, org.argouml.cognitive.Designer)
 85  
      */
 86  
     @Override
 87  
     public boolean predicate2(Object dm, Designer dsgr) {
 88  0
         if (!(dm instanceof Diagram)) {
 89  0
             return NO_PROBLEM;
 90  
         }
 91  0
         Diagram d = (Diagram) dm;
 92  
 
 93  
         // fixes bug #669. Sequencediagrams always overlap, so they shall
 94  
         // never report a problem
 95  0
         if (dm instanceof SequenceDiagram) {
 96  0
             return NO_PROBLEM;
 97  
         }
 98  
 
 99  0
         ListSet offs = computeOffenders(d);
 100  0
         if (offs == null) return NO_PROBLEM;
 101  0
         return PROBLEM_FOUND;
 102  
     }
 103  
 
 104  
 
 105  
     /*
 106  
      * @see org.argouml.cognitive.critics.Critic#toDoItem(java.lang.Object,
 107  
      *      org.argouml.cognitive.Designer)
 108  
      */
 109  
     @Override
 110  
     public ToDoItem toDoItem(Object dm, Designer dsgr) {
 111  0
         Diagram d = (Diagram) dm;
 112  0
         ListSet offs = computeOffenders(d);
 113  0
         return new ToDoItem(this, offs, dsgr);
 114  
     }
 115  
 
 116  
     /*
 117  
      * @see org.argouml.cognitive.Poster#stillValid(
 118  
      *      org.argouml.cognitive.ToDoItem, org.argouml.cognitive.Designer)
 119  
      */
 120  
     @Override
 121  
     public boolean stillValid(ToDoItem i, Designer dsgr) {
 122  0
         if (!isActive()) {
 123  0
             return false;
 124  
         }
 125  0
         ListSet offs = i.getOffenders();
 126  0
         Diagram d = (Diagram) offs.get(0);
 127  
         //if (!predicate(dm, dsgr)) return false;
 128  0
         ListSet newOffs = computeOffenders(d);
 129  0
         boolean res = offs.equals(newOffs);
 130  0
         return res;
 131  
     }
 132  
 
 133  
     /**
 134  
      * @param d the diagram
 135  
      * @return the set of offenders
 136  
      */
 137  
     public ListSet computeOffenders(Diagram d) {
 138  
         //TODO: algorithm is n^2 in number of nodes
 139  0
         List figs = d.getLayer().getContents();
 140  0
         int numFigs = figs.size();
 141  0
         ListSet offs = null;
 142  0
         for (int i = 0; i < numFigs - 1; i++) {
 143  0
             Object oi = figs.get(i);
 144  0
             if (!(oi instanceof FigNode)) {
 145  0
                 continue;
 146  
             }
 147  0
             FigNode fni = (FigNode) oi;
 148  0
             Rectangle boundsi = fni.getBounds();
 149  0
             for (int j = i + 1; j < numFigs; j++) {
 150  0
                 Object oj = figs.get(j);
 151  0
                 if (!(oj instanceof FigNode)) {
 152  0
                     continue;
 153  
                 }
 154  0
                 FigNode fnj = (FigNode) oj;
 155  0
                 if (fnj.intersects(boundsi)) {
 156  0
                     if (!(d instanceof UMLDeploymentDiagram)) {
 157  0
                         if (fni instanceof FigNodeModelElement) {
 158  0
                             if (((FigNodeModelElement) fni).getEnclosingFig()
 159  
                                 == fnj)
 160  0
                                 continue;
 161  
                         }
 162  0
                         if (fnj instanceof FigNodeModelElement) {
 163  0
                             if (((FigNodeModelElement) fnj).getEnclosingFig()
 164  
                                 == fni)
 165  0
                                 continue;
 166  
                         }
 167  
                     }
 168  
                     // In DeploymentDiagrams the situation is not the
 169  
                     // same as in other diagrams only classes,
 170  
                     // interfaces and objects can intersect each other
 171  
                     // while they are not the EnclosingFig, so you
 172  
                     // have to prouve only these elements.
 173  
                     else {
 174  0
                         if ((!((fni instanceof  FigClass)
 175  
                                || (fni instanceof FigInterface)
 176  
                                || (fni instanceof FigObject)))
 177  
                             || (!((fnj instanceof  FigClass)
 178  
                                   || (fnj instanceof FigInterface)
 179  
                                   || (fnj instanceof FigObject))))
 180  0
                             continue;
 181  
                     }
 182  0
                     if (offs == null) {
 183  0
                         offs = new ListSet();
 184  0
                         offs.add(d);
 185  
                     }
 186  0
                     offs.add(fni);
 187  0
                     offs.add(fnj);
 188  0
                     break;
 189  
                 }
 190  
             }
 191  
         }
 192  0
         return offs;
 193  
     }
 194  
 
 195  
     /*
 196  
      * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials()
 197  
      */
 198  
     public Set<Object> getCriticizedDesignMaterials() {
 199  900
         Set<Object> ret = new HashSet<Object>();
 200  900
         return ret;
 201  
     }
 202  
     
 203  
 } 
 204  
 
 205  
 
 206