Coverage Report - org.argouml.uml.cognitive.critics.CrDupRoleNames
 
Classes in this File Line Coverage Branch Coverage Complexity
CrDupRoleNames
37%
9/24
0%
0/12
4
 
 1  
 /* $Id: CrDupRoleNames.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  
  *    maurelio1234
 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.HashSet;
 44  
 import java.util.Iterator;
 45  
 import java.util.Set;
 46  
 
 47  
 import org.argouml.cognitive.Designer;
 48  
 import org.argouml.model.Model;
 49  
 import org.argouml.uml.cognitive.UMLDecision;
 50  
 
 51  
 
 52  
 // Use Model through Facade
 53  
 
 54  
 /**
 55  
  * A critic to check that the ends of an association all have distinct
 56  
  * names.<p>
 57  
  *
 58  
  * This is the first well-formedness rule for associations in the UML 1.3
 59  
  * standard (see section 2.5.3 of the standard).<p>
 60  
  *
 61  
  * Well-formedness rule [1] for Association. See page 51 of UML 1.4
 62  
  * Semantics. OMG document UML 1.4.2 formal/04-07-02.
 63  
  * 
 64  
  * See ArgoUML User Manual: Duplicate end (role) names for &lt;association&gt;
 65  
  *
 66  
  * @author Jason Robbins
 67  
  */
 68  
 public class CrDupRoleNames extends CrUML {
 69  
 
 70  
     /**
 71  
      * <p>Constructor for the critic.</p>
 72  
      *
 73  
      * <p>Sets up the resource name, which will allow headline and description
 74  
      * to found for the current locale. Provides a design issue category
 75  
      * (NAMING) and add triggers for "connection" and "end_name".</p>
 76  
      */
 77  
 
 78  900
     public CrDupRoleNames() {
 79  900
         setupHeadAndDesc();
 80  900
         addSupportedDecision(UMLDecision.NAMING);
 81  
 
 82  
         // These may not actually make any difference at present (the code
 83  
         // behind addTrigger needs more work).
 84  
 
 85  900
         addTrigger("connection");
 86  900
         addTrigger("end_name");
 87  900
     }
 88  
 
 89  
 
 90  
     /**
 91  
      * <p>The trigger for the critic.</p>
 92  
      *
 93  
      * <p>We do not handle association roles, which are a subclass of
 94  
      *   association. An association role should be fine, if its parent is OK,
 95  
      *   since it must have the same or fewer ends than its parent.</p>
 96  
      *
 97  
      * <p><em>Note</em>. ArgoUML does not currently have a constructor to check
 98  
      *   that an association role is more tightly constrained than its
 99  
      *   parent.</p>
 100  
      *
 101  
      * <p>Then loop through the ends, building a list of end names that we
 102  
      *   have seen, and looking to see if the current end is already in that
 103  
      *   list. We ignore any ends that are unnamed, or have the empty string
 104  
      *   as name.</p>
 105  
      *
 106  
      * <p>Whilst this is an O(n^2) algorithm, most associations have only two
 107  
      *   ends, so this is unlikely to cause difficulty.</p>
 108  
      *
 109  
      * @param  dm    the {@link java.lang.Object Object} to be checked against
 110  
      *               the critic.
 111  
      *
 112  
      * @param  dsgr  the {@link org.argouml.cognitive.Designer Designer}
 113  
      *               creating the model. Not used, this is for future
 114  
      *               development of ArgoUML.
 115  
      *
 116  
      * @return       {@link #PROBLEM_FOUND PROBLEM_FOUND} if the critic is
 117  
      *               triggered, otherwise {@link #NO_PROBLEM NO_PROBLEM}.
 118  
      */
 119  
 
 120  
     public boolean predicate2(Object dm, Designer dsgr) {
 121  
 
 122  
         // Only work for associations
 123  
 
 124  0
         if (!(Model.getFacade().isAAssociation(dm))) {
 125  0
             return NO_PROBLEM;
 126  
         }
 127  
 
 128  
         // No problem if this is an association role.
 129  0
         if (Model.getFacade().isAAssociationRole(dm)) {
 130  0
             return NO_PROBLEM;
 131  
         }
 132  
 
 133  
         // Loop through all the ends, comparing the name against those already
 134  
         // seen (ignoring any with no name).
 135  
         // No problem if there are no connections defined, we will fall
 136  
         // through immediately.
 137  
 
 138  0
         Collection<String>   namesSeen = new ArrayList<String>();
 139  
 
 140  0
         Iterator conns = Model.getFacade().getConnections(dm).iterator();
 141  0
         while (conns.hasNext()) {
 142  0
             String name = Model.getFacade().getName(conns.next());
 143  
 
 144  
             // Ignore non-existent and empty names
 145  
 
 146  0
             if ((name == null) || name.equals("")) {
 147  0
                 continue;
 148  
             }
 149  
 
 150  
             // Is the name already in the list of those seen, if not add it
 151  
             // and go on round.
 152  
 
 153  0
             if (namesSeen.contains(name)) {
 154  0
                 return PROBLEM_FOUND;
 155  
             }
 156  
 
 157  0
             namesSeen.add(name);
 158  0
         }
 159  
 
 160  
         // If we drop out there were no clashes
 161  
 
 162  0
         return NO_PROBLEM;
 163  
     }
 164  
 
 165  
     /*
 166  
      * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials()
 167  
      */
 168  
     public Set<Object> getCriticizedDesignMaterials() {
 169  900
         Set<Object> ret = new HashSet<Object>();
 170  900
         ret.add(Model.getMetaTypes().getAssociationClass());
 171  900
         return ret;
 172  
     }    
 173  
 }
 174