Coverage Report - org.argouml.model.mdr.ExtensionMechanismsHelperMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtensionMechanismsHelperMDRImpl
7%
14/197
0%
0/138
5.3
 
 1  
 /* $Id: ExtensionMechanismsHelperMDRImpl.java 19046 2011-02-21 22:36:40Z thn $
 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  
  *    thn
 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.model.mdr;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.Collections;
 44  
 import java.util.HashMap;
 45  
 import java.util.HashSet;
 46  
 import java.util.Iterator;
 47  
 import java.util.List;
 48  
 import java.util.Map;
 49  
 
 50  
 import javax.jmi.model.MofClass;
 51  
 import javax.jmi.reflect.InvalidObjectException;
 52  
 
 53  
 import org.apache.log4j.Logger;
 54  
 import org.argouml.model.ExtensionMechanismsHelper;
 55  
 import org.argouml.model.InvalidElementException;
 56  
 import org.argouml.model.NotImplementedException;
 57  
 import org.omg.uml.foundation.core.ModelElement;
 58  
 import org.omg.uml.foundation.core.Namespace;
 59  
 import org.omg.uml.foundation.core.Stereotype;
 60  
 import org.omg.uml.foundation.core.TagDefinition;
 61  
 import org.omg.uml.foundation.core.TaggedValue;
 62  
 import org.omg.uml.modelmanagement.Model;
 63  
 import org.omg.uml.modelmanagement.UmlPackage;
 64  
 
 65  
 /**
 66  
  * Helper class for UML Foundation::ExtensionMechanisms Package.<p>
 67  
  *
 68  
  * @since ARGO0.19.5
 69  
  * @author Ludovic Ma&icirc;tre
 70  
  * @author Tom Morris
 71  
  * derived from NSUML implementation by:
 72  
  * @author Thierry Lach
 73  
  */
 74  0
 class ExtensionMechanismsHelperMDRImpl implements ExtensionMechanismsHelper {
 75  
 
 76  900
     private static final Logger LOG =
 77  
         Logger.getLogger(ExtensionMechanismsHelperMDRImpl.class);
 78  
 
 79  
     /**
 80  
      * The model implementation.
 81  
      */
 82  
     private MDRModelImplementation modelImpl;
 83  
 
 84  900
     private static Map<String, String> packageMap = 
 85  
         new HashMap<String, String>(16);
 86  
 
 87  
     /**
 88  
      * Don't allow instantiation.
 89  
      *
 90  
      * @param implementation
 91  
      *            To get other helpers and factories.
 92  
      */
 93  900
     ExtensionMechanismsHelperMDRImpl(MDRModelImplementation implementation) {
 94  900
         modelImpl = implementation;
 95  900
         packageMap.put("core", "Core");
 96  900
         packageMap.put("datatypes", "Data_Types");
 97  900
         packageMap.put("commonbehavior", "Common_Behavior");
 98  900
         packageMap.put("usecases", "Use_Cases");
 99  900
         packageMap.put("statemachines", "State_Machines");
 100  900
         packageMap.put("collaborations", "Collaborations");
 101  900
         packageMap.put("activitygraphs", "Activity_Graphs");
 102  900
         packageMap.put("modelmanagement", "Model_Management");
 103  900
     }
 104  
 
 105  
     
 106  
     public Collection<Stereotype> getStereotypes(Object ns) {
 107  0
         if (!(ns instanceof Namespace)) {
 108  0
             throw new IllegalArgumentException("A namespace was expected we got " + ns);
 109  
         }
 110  
         
 111  0
         List<Stereotype> l = new ArrayList<Stereotype>();
 112  
         // TODO: this could be a huge collection - find a more efficient way
 113  
         try {
 114  0
             for (Object o : ((Namespace) ns).getOwnedElement()) {
 115  0
                 if (o instanceof Stereotype) {
 116  0
                     l.add((Stereotype) o);
 117  0
                 } else if (o instanceof UmlPackage) {
 118  0
                     l.addAll(getStereotypes(o));
 119  
                 }
 120  
             }
 121  0
         } catch (InvalidObjectException e) {
 122  0
             throw new InvalidElementException(e);
 123  0
         }
 124  0
         return l;
 125  
     }
 126  
 
 127  
     /* TODO: Make this work when the given stereotype
 128  
      * has more than one baseclass.
 129  
      * TODO: Currently only works for stereotypes where the baseclass is 
 130  
      * equal to the given one - inheritance does not work.*/
 131  
     public Object getStereotype(Object ns, Object stereo) {
 132  0
         if (!(ns instanceof Namespace)) {
 133  0
             throw new IllegalArgumentException("namespace");
 134  
         }
 135  
 
 136  0
         if (!(stereo instanceof Stereotype)) {
 137  0
             throw new IllegalArgumentException("stereotype");
 138  
         }
 139  
 
 140  
         try {
 141  0
             String name = ((ModelElement) stereo).getName();
 142  0
             Collection<String> baseClasses = 
 143  
                 ((Stereotype) stereo).getBaseClass();
 144  0
             if (name == null || baseClasses.size() != 1) {
 145  0
                 return null;
 146  
             }
 147  0
             String baseClass = baseClasses.iterator().next();
 148  
             
 149  0
             for (Stereotype o : getStereotypes(ns)) {
 150  0
                 if (name.equals(o.getName())
 151  
                         && o.getBaseClass().contains(baseClass)) {
 152  0
                     return o;
 153  
                 }
 154  
             }
 155  0
         } catch (InvalidObjectException e) {
 156  0
             throw new InvalidElementException(e);
 157  0
         }
 158  0
         return null;
 159  
     }
 160  
 
 161  
 
 162  
     public Stereotype getStereotype(Collection models, Object stereo) {
 163  0
         if (stereo == null) {
 164  0
             throw new IllegalArgumentException("null argument");
 165  
         }
 166  0
         if (!(stereo instanceof Stereotype)) {
 167  0
             throw new IllegalArgumentException("stereotype");
 168  
         }
 169  
 
 170  
         try {
 171  0
             String name = ((Stereotype) stereo).getName();
 172  0
             Collection<String> baseClasses = 
 173  
                 ((Stereotype) stereo).getBaseClass();
 174  0
             if (name == null || baseClasses.size() != 1) {
 175  0
                 return null;
 176  
             }
 177  0
             String baseClass = baseClasses.iterator().next();
 178  
             
 179  0
             for (Model model : ((Collection<Model>) models)) {
 180  
                 // TODO: this should call the single namespace form
 181  
                 // getStereotype(it2.next(); stereo);
 182  0
                 for (Stereotype o : getStereotypes(model)) {
 183  0
                     if (name.equals(o.getName())
 184  
                             && o.getBaseClass().contains(baseClass)) {
 185  0
                         return o;
 186  
                     }
 187  
                 }
 188  
             }
 189  0
         } catch (InvalidObjectException e) {
 190  0
             throw new InvalidElementException(e);
 191  0
         }
 192  0
         return null;
 193  
     }
 194  
 
 195  
 
 196  
     @Deprecated
 197  
     public String getMetaModelName(Object m) {
 198  0
         return modelImpl.getMetaTypes().getName(m);
 199  
     }
 200  
 
 201  
     
 202  
     /*
 203  
      * @see org.argouml.model.ExtensionMechanismsHelper#getAllPossibleStereotypes(java.util.Collection, java.lang.Object)
 204  
      */
 205  
     public Collection<Stereotype> getAllPossibleStereotypes(Collection models,
 206  
             Object modelElement) {
 207  0
         if (modelElement == null) {
 208  0
             return Collections.emptySet();
 209  
         }
 210  0
         List<Stereotype> ret = new ArrayList<Stereotype>();
 211  
         try {
 212  0
             for (Stereotype stereo : getStereotypes(models)) {
 213  0
                 if (isApplicableStereo(modelElement, stereo)) {
 214  0
                     ret.add(stereo);
 215  
                 }
 216  
             }
 217  0
         } catch (InvalidObjectException e) {
 218  0
             throw new InvalidElementException(e);
 219  0
         }
 220  0
         return ret;
 221  
     }
 222  
 
 223  
     /*
 224  
      * @see org.argouml.model.ExtensionMechanismsHelper#getCommonTaggedValueTypes()
 225  
      */
 226  
     public Collection getCommonTaggedValueTypes() {
 227  
         // TODO: still not used, because in ArgoUML String is "hardwired"
 228  0
         return null;
 229  
     }
 230  
 
 231  
 
 232  
     /**
 233  
      * Can the given stereotype be applied the given class?
 234  
      *
 235  
      * @param element
 236  
      *            the UML element we want to apply the stereotype to
 237  
      * @param stereo
 238  
      *            the given stereotype
 239  
      * @return true if the stereotype may be applied
 240  
      */
 241  
     private boolean isApplicableStereo(Object element, Stereotype stereo) {
 242  0
         String metatypeName = modelImpl.getMetaTypes().getName(element);
 243  0
         MofClass metatype = ((FacadeMDRImpl) modelImpl.getFacade())
 244  
                 .getMofClass(metatypeName);
 245  0
         Collection<String> allTypes = getNames(metatype.allSupertypes());
 246  0
         allTypes.add(metatypeName);
 247  
 
 248  0
         for (String base : stereo.getBaseClass()) {
 249  0
             if (allTypes.contains(base)) {
 250  0
                 return true;
 251  
             }
 252  
         }
 253  0
         return false;
 254  
     }
 255  
 
 256  
 
 257  
     /**
 258  
      * Convert a collection of elements to a collection of names.
 259  
      *
 260  
      * @param elements The elements.
 261  
      * @return A Collection with {@link String}s.
 262  
      */
 263  
     private Collection<String> getNames(Collection<MofClass> elements) {
 264  0
         Collection<String> names = new HashSet<String>();
 265  0
         for (MofClass element : elements) {
 266  0
             names.add(element.getName());
 267  
         }
 268  0
         return names;
 269  
     }
 270  
 
 271  
 
 272  
     public boolean isValidStereotype(Object theModelElement,
 273  
             Object theStereotype) {
 274  0
         if (theModelElement == null) {
 275  0
             return false;
 276  
         }
 277  0
         return isApplicableStereo(theModelElement, (Stereotype) theStereotype);
 278  
     }
 279  
 
 280  
 
 281  
     public Collection<Stereotype> getStereotypes(Collection models) {
 282  0
         List<Stereotype> ret = new ArrayList<Stereotype>();
 283  
         try {
 284  0
             for (Object model : models) {
 285  0
                 if (!(model instanceof Model)) {
 286  0
                     throw new IllegalArgumentException(
 287  
                             "Expected to receive a collection of Models. "
 288  
                             + "The collection contained a "
 289  
                             + model.getClass().getName());
 290  
                 }
 291  0
                 ret.addAll(getStereotypes(model));
 292  
             }
 293  0
         } catch (InvalidObjectException e) {
 294  0
             throw new InvalidElementException(e);
 295  0
         }
 296  0
         return ret;
 297  
     }
 298  
 
 299  
 
 300  
     public void addCopyStereotype(Object modelElement, Object stereotype) {
 301  0
         modelImpl.getCoreHelper().addStereotype(modelElement, stereotype);
 302  0
     }
 303  
 
 304  
 
 305  
 
 306  
     public boolean isStereotype(Object object, String name, String base) {
 307  0
         if (!(object instanceof Stereotype)) {
 308  0
             return false;
 309  
         }
 310  
 
 311  0
         Stereotype st = (Stereotype) object;
 312  
         try {
 313  0
             if (name == null && st.getName() != null) {
 314  0
                 return false;
 315  
             }
 316  0
             if (base == null && !(st.getBaseClass().isEmpty())) {
 317  0
                 return false;
 318  
             }
 319  
             
 320  0
             return name.equals(st.getName()) 
 321  
                 && st.getBaseClass().contains(base);
 322  0
         } catch (InvalidObjectException e) {
 323  0
             throw new InvalidElementException(e);
 324  
         }
 325  
     }
 326  
 
 327  
 
 328  
     public boolean isStereotypeInh(Object object, String name, String base) {
 329  0
         if (!(object instanceof Stereotype)) {
 330  0
             return false;
 331  
         }
 332  
         try {
 333  0
             if (isStereotype(object, name, base)) {
 334  0
                 return true;
 335  
             }
 336  
             /* TODO: mvw: do we really look into super-types of the stereotype, 
 337  
              * or should we be looking into super-types of the baseclass? */
 338  0
             Iterator it = 
 339  
                 modelImpl.getCoreHelper().getSupertypes(object).iterator();
 340  0
             while (it.hasNext()) {
 341  0
                 if (isStereotypeInh(it.next(), name, base)) {
 342  0
                     return true;
 343  
                 }
 344  
             }
 345  0
         } catch (InvalidObjectException e) {
 346  0
             throw new InvalidElementException(e);
 347  0
         }
 348  0
         return false;
 349  
     }
 350  
 
 351  
 
 352  
     public void addExtendedElement(Object handle, Object extendedElement) {
 353  0
         if (handle instanceof Stereotype
 354  
                 && extendedElement instanceof ModelElement) {
 355  0
             ((ModelElement) extendedElement).getStereotype().add(
 356  
                     (Stereotype) handle);
 357  0
             return;
 358  
         }
 359  0
         throw new IllegalArgumentException("handle: " + handle
 360  
                 + " or extendedElement: " + extendedElement);
 361  
     }
 362  
 
 363  
 
 364  
     public void addBaseClass(Object handle, Object baseClass) {
 365  0
         if (handle instanceof Stereotype) {
 366  0
             if (baseClass instanceof String) {
 367  0
                 ((Stereotype) handle).getBaseClass().add((String) baseClass);
 368  0
                 return;
 369  
             }
 370  0
             if (baseClass instanceof ModelElement) {
 371  0
                 ((Stereotype) handle).getBaseClass().add(
 372  
                         modelImpl.getMetaTypes().getName(baseClass));
 373  0
                 return;
 374  
             }
 375  
         }
 376  0
         throw new IllegalArgumentException("handle: " + handle
 377  
                 + " or baseClass: " + baseClass);
 378  
     }
 379  
 
 380  
     public void applyProfile(Object handle, Object profile) {
 381  
         // do nothing
 382  1908
     }
 383  
 
 384  
     public void removeBaseClass(Object handle, Object baseClass) {
 385  
         try {
 386  0
             if (handle instanceof Stereotype) {
 387  0
                 if (baseClass instanceof String) {
 388  0
                     ((Stereotype) handle).getBaseClass().remove(baseClass);
 389  0
                     return;
 390  
                 }
 391  0
                 if (baseClass instanceof ModelElement) {
 392  0
                     ((Stereotype) handle).getBaseClass().remove(
 393  
                             modelImpl.getMetaTypes().getName(baseClass));
 394  0
                     return;
 395  
                 }
 396  
             }
 397  0
         } catch (InvalidObjectException e) {
 398  0
             throw new InvalidElementException(e);
 399  0
         }
 400  0
         throw new IllegalArgumentException("handle: " + handle
 401  
                 + " or baseClass: " + baseClass);
 402  
     }
 403  
 
 404  
 
 405  
     public void setIcon(Object handle, Object icon) {
 406  0
         if (handle instanceof Stereotype
 407  
                 && (icon == null || icon instanceof String)) {
 408  0
             ((Stereotype) handle).setIcon((String) icon);
 409  0
             return;
 410  
         }
 411  0
         throw new IllegalArgumentException("handle: " + handle + " or icon: "
 412  
                 + icon);
 413  
     }
 414  
 
 415  
 
 416  
     public void setValueOfTag(Object handle, String value) {
 417  0
         setDataValues(handle, new String[] {value});
 418  0
     }
 419  
     
 420  
     public void setDataValues(Object handle, String[] values) {
 421  0
         if (handle instanceof TaggedValue) {
 422  0
             TaggedValue tv = (TaggedValue) handle;
 423  0
             Collection<String> dataValues = tv.getDataValue();
 424  0
             dataValues.clear();
 425  0
             Collections.addAll(dataValues, values);
 426  
         }
 427  0
     }
 428  
 
 429  
     public void addTaggedValue(Object handle, Object taggedValue) {
 430  0
         if (handle instanceof ModelElement
 431  
                 && taggedValue instanceof TaggedValue) {
 432  0
             ((ModelElement) handle).getTaggedValue().add(
 433  
                     (TaggedValue) taggedValue);
 434  0
             return;
 435  
         }
 436  0
         throw new IllegalArgumentException("handle: " + handle
 437  
                 + " or taggedValue: " + taggedValue);
 438  
     }
 439  
 
 440  
 
 441  
     public void removeTaggedValue(Object handle, Object taggedValue) {
 442  0
         if (handle instanceof ModelElement
 443  
                 && taggedValue instanceof TaggedValue) {
 444  0
             ((ModelElement) handle).getTaggedValue().remove(taggedValue);
 445  0
             return;
 446  
         }
 447  0
         throw new IllegalArgumentException("handle: " + handle
 448  
                 + " or taggedValue: " + taggedValue);
 449  
     }
 450  
 
 451  
 
 452  
     public void setTaggedValue(Object handle, Collection taggedValues) {
 453  0
         if (handle instanceof ModelElement) {
 454  0
             Collection tv =
 455  
                 modelImpl.getFacade().getTaggedValuesCollection(handle);
 456  0
             if (!tv.isEmpty()) {
 457  0
                 Collection tvs = new ArrayList(tv);
 458  0
                 for (Object value : tvs) {
 459  0
                     if (!taggedValues.contains(value)) {
 460  0
                         tv.remove(value);
 461  
                     }
 462  
                 }
 463  
             }
 464  0
             for (Object value : taggedValues) {
 465  0
                 if (!tv.contains(value)) {
 466  0
                     tv.add(value);
 467  
                 }
 468  
             }
 469  0
             return;
 470  
         }
 471  0
         throw new IllegalArgumentException("handle: " + handle
 472  
                 + " or taggedValues: " + taggedValues);
 473  
     }
 474  
 
 475  
     public void setTaggedValue(Object handle, Object property, Object value) {
 476  0
         throw new NotImplementedException();
 477  
     }
 478  
  
 479  
     public void setTagType(Object handle, String tagType) {
 480  0
         if (handle instanceof TagDefinition) {
 481  
             // TODO: What type of validation can we do here on tagType?
 482  0
             ((TagDefinition) handle).setTagType(tagType);
 483  0
             return;
 484  
         }
 485  0
         throw new IllegalArgumentException("handle: " + handle
 486  
                 + " or tagType: " + tagType);
 487  
     }
 488  
 
 489  
     public void setType(Object handle, Object type) {
 490  0
         if (type == null || type instanceof TagDefinition) {
 491  0
             if (handle instanceof TaggedValue) {
 492  0
                 ((TaggedValue) handle).setType((TagDefinition) type);
 493  0
                 return;
 494  
             }
 495  
         }
 496  0
         throw new IllegalArgumentException("handle: " + handle + " or type: "
 497  
                 + type);
 498  
     }
 499  
 
 500  
     public void unapplyProfile(Object handle, Object profile) {
 501  
         // do nothing
 502  0
     }
 503  
 
 504  
     public boolean hasStereotype(Object handle, String name) {
 505  0
         if (name == null || !(handle instanceof ModelElement)) {
 506  0
             throw new IllegalArgumentException();
 507  
         }
 508  
         try {
 509  0
             ModelElement element = (ModelElement) handle;
 510  0
             for (Stereotype stereotype : element.getStereotype()) {
 511  0
                 if (name.equals(stereotype.getName())) {
 512  0
                     return true;
 513  
                 }
 514  
             }
 515  0
         } catch (InvalidObjectException e) {
 516  0
             throw new InvalidElementException(e);
 517  0
         }
 518  0
         return false;
 519  
     }
 520  
     
 521  
     public Object makeProfileApplicable(Object handle) {
 522  
         // there is nothing to do in UML1.4
 523  0
         return handle;
 524  
     }
 525  
 }