Coverage Report - org.argouml.uml.cognitive.critics.CrNameConflict
 
Classes in this File Line Coverage Branch Coverage Complexity
CrNameConflict
57%
24/42
45%
9/20
2.833
 
 1  
 /* $Id: CrNameConflict.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-2008 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.HashMap;
 42  
 import java.util.HashSet;
 43  
 import java.util.Set;
 44  
 
 45  
 import org.argouml.cognitive.Critic;
 46  
 import org.argouml.cognitive.Designer;
 47  
 import org.argouml.cognitive.ListSet;
 48  
 import org.argouml.cognitive.ToDoItem;
 49  
 import org.argouml.model.Model;
 50  
 import org.argouml.uml.cognitive.UMLDecision;
 51  
 import org.argouml.uml.cognitive.UMLToDoItem;
 52  
 
 53  
 /**
 54  
  * Well-formedness rule [1] for Namespace. See page 33 of UML 1.1 Semantics.
 55  
  * OMG document ad/97-08-04. <p>
 56  
  *
 57  
  * Well-formedness rule [1] for Namespace. See page 62 of UML 1.4
 58  
  * Semantics. OMG document UML 1.4.2 formal/04-07-02.
 59  
  *
 60  
  * Names of contained elements in a namespace (i.e. the design material) 
 61  
  * must be unique. 
 62  
  * This condition does not apply to names of Generalizations, 
 63  
  * or elements without name.
 64  
  */
 65  
 public class CrNameConflict extends CrUML {
 66  
 
 67  
     /**
 68  
      * The constructor.
 69  
      */
 70  900
     public CrNameConflict() {
 71  900
         setupHeadAndDesc();
 72  900
         addSupportedDecision(UMLDecision.NAMING);
 73  900
         setKnowledgeTypes(Critic.KT_SYNTAX);
 74  900
         addTrigger("name");
 75  900
         addTrigger("feature_name");
 76  900
     }
 77  
 
 78  
     /*
 79  
      * @see org.argouml.uml.cognitive.critics.CrUML#predicate2(
 80  
      *      java.lang.Object, org.argouml.cognitive.Designer)
 81  
      */
 82  
     @Override
 83  
     public boolean predicate2(Object dm, Designer dsgr) {
 84  35625
         return computeOffenders(dm).size() > 1;
 85  
     }
 86  
 
 87  
     /*
 88  
      * @see org.argouml.cognitive.critics.Critic#toDoItem( java.lang.Object,
 89  
      *      org.argouml.cognitive.Designer)
 90  
      */
 91  
     @Override
 92  
     public ToDoItem toDoItem(Object dm, Designer dsgr) {
 93  0
         ListSet offs = computeOffenders(dm);
 94  0
         return new UMLToDoItem(this, offs, dsgr);
 95  
     }
 96  
 
 97  
     /**
 98  
      * @param dm
 99  
      *            the object to check
 100  
      * @return the set of offenders
 101  
      */
 102  
     protected ListSet computeOffenders(Object dm) {
 103  35625
         ListSet offenderResult = new ListSet();
 104  35625
         if (Model.getFacade().isANamespace(dm)) {
 105  35625
             HashMap<String, Object> names = new HashMap<String, Object>();
 106  35625
             for (Object name1Object :  Model.getFacade().getOwnedElements(dm)) {
 107  841
                 if (Model.getFacade().isAGeneralization(name1Object))
 108  0
                     continue;
 109  841
                 String name = Model.getFacade().getName(name1Object);
 110  841
                 if (name == null)
 111  573
                     continue;
 112  268
                 if ("".equals(name))
 113  0
                     continue;
 114  268
                 if (names.containsKey(name)) {
 115  0
                     Object offender = names.get(name);
 116  0
                     if (!offenderResult.contains(offender)) {
 117  0
                         offenderResult.add(offender);
 118  
                     }
 119  0
                     offenderResult.add(name1Object);
 120  
                 }
 121  268
                 names.put(name, name1Object);
 122  268
             }
 123  
         }
 124  35625
         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  
     /*
 151  
      * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials()
 152  
      */
 153  
     public Set<Object> getCriticizedDesignMaterials() {
 154  900
         Set<Object> ret = new HashSet<Object>();
 155  900
         ret.add(Model.getMetaTypes().getNamespace());
 156  900
         return ret;
 157  
     }
 158  
     
 159  
 }