Coverage Report - org.argouml.uml.cognitive.critics.ChildGenUML
 
Classes in this File Line Coverage Branch Coverage Complexity
ChildGenUML
63%
37/58
50%
22/44
17.5
 
 1  
 /* $Id: ChildGenUML.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-2007 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.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.Collections;
 44  
 import java.util.Enumeration;
 45  
 import java.util.Iterator;
 46  
 import java.util.List;
 47  
 
 48  
 import org.apache.log4j.Logger;
 49  
 import org.argouml.kernel.Project;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.util.IteratorEnumeration;
 52  
 import org.argouml.util.SingleElementIterator;
 53  
 import org.tigris.gef.base.Diagram;
 54  
 import org.tigris.gef.util.ChildGenerator;
 55  
 
 56  
 /**
 57  
  * This class gives critics access to parts of the UML model of the
 58  
  * design.  It defines a gen() function that returns the "children"
 59  
  * of any given part of the UML model.  Basically, it goes from
 60  
  * Project, to Models, to ModelElements.  Argo's critic Agency uses
 61  
  * this to apply critics where appropriate.
 62  
  *
 63  
  * TODO: This thinks it knows all the composition associations of the
 64  
  * the UML metamodel, but it is a) incomplete and b) not updated for
 65  
  * UML 1.4.  This should be done using information from the metamodel
 66  
  * rather than hardwired code. - tfm - 20070205
 67  
  * 
 68  
  * @see org.argouml.cognitive.Agency
 69  
  * @see org.argouml.cognitive.Designer
 70  
  * @author jrobbins
 71  
  */
 72  900
 public class ChildGenUML implements ChildGenerator {
 73  
 
 74  900
     private static final Logger LOG = Logger.getLogger(ChildGenUML.class);
 75  
 
 76  
     /**
 77  
      * Reply a java.util.Enumeration of the children of the given Object
 78  
      * 
 79  
      * @param o the object to return the children of
 80  
      * @return an enumeration of the children of the given Object
 81  
      * @see org.tigris.gef.util.ChildGenerator#gen(java.lang.Object)
 82  
      * @deprecated for 0.25.4 by tfmorris. Only for use with legacy GEF
 83  
      *             interfaces. Use {@link #gen2(Object)} for new applications.
 84  
      */
 85  
     @Deprecated
 86  
     public Enumeration gen(Object o) {
 87  144629
         return new IteratorEnumeration(gen2(o));
 88  
     }
 89  
     
 90  
     /**
 91  
      * Return an Iterator of the children of the given Object
 92  
      *
 93  
      * @param o object to return the children of
 94  
      * @return an iterator over the children of the given object
 95  
      * @see org.tigris.gef.util.ChildGenerator#gen(java.lang.Object)
 96  
      */
 97  
     public Iterator gen2(Object o) {
 98  
 
 99  144629
         if (LOG.isDebugEnabled()) {
 100  0
             if (o == null) {
 101  0
                 LOG.debug("Object is null");
 102  
             } else {
 103  
 //                LOG.debug("Finding children for " + o.getClass());
 104  
             }
 105  
         }
 106  
 
 107  144629
         if (o instanceof Project) {
 108  35455
             Project p = (Project) o;
 109  35455
             Collection result = new ArrayList();
 110  35455
             result.addAll(p.getUserDefinedModelList());
 111  35455
             result.addAll(p.getDiagramList());
 112  35455
             return result.iterator();
 113  
         }
 114  
 
 115  109174
         if (o instanceof Diagram) {
 116  72225
             Collection figs = ((Diagram) o).getLayer().getContents();
 117  72225
             if (figs != null) {
 118  72225
                 return figs.iterator();
 119  
             }
 120  
         }
 121  
         
 122  
         // argument can be an instanceof a Fig which we ignore
 123  
 
 124  36949
         if (Model.getFacade().isAPackage(o)) {
 125  35447
             Collection ownedElements = 
 126  
                 Model.getFacade().getOwnedElements(o);
 127  35447
             if (ownedElements != null) {
 128  35447
                 return ownedElements.iterator();
 129  
             }
 130  
         }
 131  
 
 132  1502
         if (Model.getFacade().isAElementImport(o)) {
 133  0
             Object me = Model.getFacade().getModelElement(o);
 134  0
             if (me != null) {
 135  0
                 return new SingleElementIterator(me);
 136  
             }
 137  
         }
 138  
 
 139  
 
 140  
         // TODO: associationclasses fit both of the next 2 cases
 141  
 
 142  1502
         if (Model.getFacade().isAClassifier(o)) {
 143  0
             Collection result = new ArrayList();
 144  0
             result.addAll(Model.getFacade().getFeatures(o));
 145  
 
 146  0
             Collection sms = Model.getFacade().getBehaviors(o);
 147  
             //Object sm = null;
 148  
             //if (sms != null && sms.size() > 0)
 149  
                 //sm = sms.elementAt(0);
 150  
             //if (sm != null) res.addSub(new EnumerationSingle(sm));
 151  0
             if (sms != null) {
 152  0
                 result.addAll(sms);
 153  
             }
 154  0
             return result.iterator();
 155  
         }
 156  
 
 157  1502
         if (Model.getFacade().isAAssociation(o)) {
 158  0
             List assocEnds = (List) Model.getFacade().getConnections(o);
 159  0
             if (assocEnds != null) {
 160  0
                 return assocEnds.iterator();
 161  
             }
 162  
             //TODO: AssociationRole
 163  
         }
 164  
 
 165  
         // // needed?
 166  1502
         if (Model.getFacade().isAStateMachine(o)) {
 167  573
             Collection result = new ArrayList();
 168  573
             Object top = Model.getStateMachinesHelper().getTop(o);
 169  573
             if (top != null) {
 170  571
                 result.add(top);
 171  
             }
 172  573
             result.addAll(Model.getFacade().getTransitions(o));
 173  573
             return result.iterator();
 174  
         }
 175  
 
 176  
         // needed?
 177  929
         if (Model.getFacade().isACompositeState(o)) {
 178  570
             Collection substates = Model.getFacade().getSubvertices(o);
 179  570
             if (substates != null) {
 180  570
                 return substates.iterator();
 181  
             }
 182  
         }
 183  
 
 184  359
         if (Model.getFacade().isAOperation(o)) {
 185  0
             Collection params = Model.getFacade().getParameters(o);
 186  0
             if (params != null) {
 187  0
                 return params.iterator();
 188  
             }
 189  
         }
 190  
 
 191  359
         if (Model.getFacade().isAModelElement(o)) {
 192  359
             Collection behavior = Model.getFacade().getBehaviors(o);
 193  359
             if (behavior != null) {
 194  359
                 return behavior.iterator();
 195  
             }
 196  
         }
 197  
         
 198  
         // TODO: We can probably use this instead of all of the above
 199  
         // legacy UML 1.3 code - tfm - 20070915
 200  0
         if (Model.getFacade().isAUMLElement(o)) {
 201  0
             Collection result = Model.getFacade().getModelElementContents(o);
 202  0
             return result.iterator();
 203  
         }
 204  
         
 205  0
         return Collections.emptySet().iterator();
 206  
     }
 207  
 }