Coverage Report - org.argouml.uml.cognitive.critics.CrAssocNameConflict
 
Classes in this File Line Coverage Branch Coverage Complexity
CrAssocNameConflict
30%
16/53
15%
5/32
3.714
 
 1  
 /* $Id: CrAssocNameConflict.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-2006 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.Collection;
 42  
 import java.util.HashMap;
 43  
 import java.util.HashSet;
 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.model.Model;
 51  
 import org.argouml.uml.cognitive.UMLDecision;
 52  
 import org.argouml.uml.cognitive.UMLToDoItem;
 53  
 
 54  
 /**
 55  
  * Well-formedness rule [2] for Namespace. See section 2.5.3.26 of
 56  
  * UML 1.4 spec.  Rule [1] is checked by CrNameConfusion.
 57  
  * 
 58  
  * Well-formedness rule [1] for Namespace. See page 62 of UML 1.4
 59  
  * Semantics. OMG document UML 1.4.2 formal/04-07-02.
 60  
  * 
 61  
  * @author mkl
 62  
  */
 63  
 public class CrAssocNameConflict extends CrUML {
 64  
 
 65  
     /**
 66  
      * The constructor.
 67  
      * 
 68  
      */
 69  900
     public CrAssocNameConflict() {
 70  900
         setupHeadAndDesc();
 71  900
         addSupportedDecision(UMLDecision.NAMING);
 72  900
         setKnowledgeTypes(Critic.KT_SYNTAX);
 73  
         // no good trigger
 74  900
     }
 75  
 
 76  
     /*
 77  
      * @see org.argouml.uml.cognitive.critics.CrUML#predicate2(
 78  
      *      java.lang.Object, org.argouml.cognitive.Designer)
 79  
      */
 80  
     public boolean predicate2(Object dm, Designer dsgr) {
 81  35722
         return computeOffenders(dm).size() > 1;
 82  
     }
 83  
 
 84  
     /*
 85  
      * @see org.argouml.cognitive.critics.Critic#toDoItem( java.lang.Object,
 86  
      *      org.argouml.cognitive.Designer)
 87  
      */
 88  
     public ToDoItem toDoItem(Object dm, Designer dsgr) {
 89  0
         ListSet offs = computeOffenders(dm);
 90  0
         return new UMLToDoItem(this, offs, dsgr);
 91  
     }
 92  
 
 93  
     /**
 94  
      * @param dm
 95  
      *            the object to check
 96  
      * @return the set of offenders
 97  
      */
 98  
     protected ListSet computeOffenders(Object dm) {
 99  35722
         ListSet offenderResult = new ListSet();
 100  35722
         if (Model.getFacade().isANamespace(dm)) {
 101  35722
             HashMap<String, Object> names = new HashMap<String, Object>();
 102  35722
             for (Object name1Object : Model.getFacade().getOwnedElements(dm)) {
 103  941
                 if (!Model.getFacade().isAAssociation(name1Object)) {
 104  941
                     continue;
 105  
                 }
 106  0
                 String name = Model.getFacade().getName(name1Object);
 107  0
                 Collection typ1 = getAllTypes(name1Object);
 108  0
                 if (name == null || "".equals(name)) {
 109  0
                     continue;
 110  
                 }
 111  0
                 if (names.containsKey(name)) {
 112  0
                     Object offender = names.get(name);
 113  0
                     Collection typ2 = getAllTypes(offender);
 114  0
                     if (typ1.containsAll(typ2) && typ2.containsAll(typ1)) {
 115  0
                         if (!offenderResult.contains(offender)) {
 116  0
                             offenderResult.add(offender);
 117  
                         }
 118  0
                         offenderResult.add(name1Object);
 119  
                     }
 120  
                 }
 121  0
                 names.put(name, name1Object);
 122  0
             }
 123  
         }
 124  35722
         return offenderResult;
 125  
     }
 126  
 
 127  
     /*
 128  
      * @see org.argouml.cognitive.Poster#stillValid(
 129  
      *      org.argouml.cognitive.ToDoItem, org.argouml.cognitive.Designer)
 130  
      */
 131  
     @Override
 132  
     public boolean stillValid(ToDoItem i, Designer dsgr) {
 133  0
         if (!isActive()) {
 134  0
             return false;
 135  
         }
 136  0
         ListSet offs = i.getOffenders();
 137  
 
 138  
         // first element is e.g. the class, but we need to have its namespace
 139  
         // to recompute the offenders.
 140  0
         Object f = offs.get(0);
 141  0
         Object ns = Model.getFacade().getNamespace(f);
 142  0
         if (!predicate(ns, dsgr)) {
 143  0
             return false;
 144  
         }
 145  0
         ListSet newOffs = computeOffenders(ns);
 146  0
         boolean res = offs.equals(newOffs);
 147  0
         return res;
 148  
     }
 149  
 
 150  
     public Collection getAllTypes(Object assoc) {
 151  0
         Set list = new HashSet();
 152  0
         if (assoc == null) {
 153  0
             return list;
 154  
         }
 155  0
         Collection assocEnds = Model.getFacade().getConnections(assoc);
 156  0
         if (assocEnds == null) {
 157  0
             return list;
 158  
         }
 159  0
         for (Object element : assocEnds) {
 160  0
             if (Model.getFacade().isAAssociationEnd(element)) {
 161  0
                 Object type = Model.getFacade().getType(element);
 162  0
                 list.add(type);
 163  0
             }
 164  
         }
 165  0
         return list;
 166  
     }
 167  
     
 168  
     /*
 169  
      * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials()
 170  
      */
 171  
     public Set<Object> getCriticizedDesignMaterials() {
 172  900
         Set<Object> ret = new HashSet<Object>();
 173  900
         ret.add(Model.getMetaTypes().getNamespace());
 174  900
         return ret;
 175  
     }
 176  
     
 177  
 }