Coverage Report - org.argouml.model.mdr.CollaborationsHelperMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CollaborationsHelperMDRImpl
1%
5/391
0%
0/328
8.132
 
 1  
 /* $Id: CollaborationsHelperMDRImpl.java 18402 2010-05-19 16:30:45Z bobtarling $
 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  
  *    bobtarling
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2009 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.HashSet;
 45  
 import java.util.List;
 46  
 import java.util.Set;
 47  
 
 48  
 import javax.jmi.reflect.InvalidObjectException;
 49  
 
 50  
 import org.apache.log4j.Logger;
 51  
 import org.argouml.model.CollaborationsHelper;
 52  
 import org.argouml.model.CoreHelper;
 53  
 import org.argouml.model.InvalidElementException;
 54  
 import org.argouml.model.Model;
 55  
 import org.argouml.model.ModelManagementHelper;
 56  
 import org.omg.uml.behavioralelements.collaborations.AssociationEndRole;
 57  
 import org.omg.uml.behavioralelements.collaborations.AssociationRole;
 58  
 import org.omg.uml.behavioralelements.collaborations.ClassifierRole;
 59  
 import org.omg.uml.behavioralelements.collaborations.Collaboration;
 60  
 import org.omg.uml.behavioralelements.collaborations.Interaction;
 61  
 import org.omg.uml.behavioralelements.collaborations.Message;
 62  
 import org.omg.uml.behavioralelements.commonbehavior.Action;
 63  
 import org.omg.uml.behavioralelements.commonbehavior.Instance;
 64  
 import org.omg.uml.behavioralelements.commonbehavior.Stimulus;
 65  
 import org.omg.uml.foundation.core.AssociationEnd;
 66  
 import org.omg.uml.foundation.core.Classifier;
 67  
 import org.omg.uml.foundation.core.Feature;
 68  
 import org.omg.uml.foundation.core.ModelElement;
 69  
 import org.omg.uml.foundation.core.Namespace;
 70  
 import org.omg.uml.foundation.core.Operation;
 71  
 import org.omg.uml.foundation.core.UmlAssociation;
 72  
 import org.omg.uml.modelmanagement.UmlPackage;
 73  
 
 74  
 /**
 75  
  * Helper class for UML BehavioralElements::Collaborations Package.
 76  
  * <p>
 77  
  * @since ARGO0.19.5
 78  
  * @author Ludovic Ma&icirc;tre
 79  
  * @author Tom Morris
 80  
  * Derived from NSUML implementation by: 
 81  
  * @author Thierry Lach
 82  
  */
 83  900
 class CollaborationsHelperMDRImpl implements CollaborationsHelper {
 84  
 
 85  
     /**
 86  
      * The model implementation.
 87  
      */
 88  
     private MDRModelImplementation modelImpl;
 89  
 
 90  900
     private static final Logger LOG = Logger.getLogger(CollaborationsHelperMDRImpl.class);
 91  
     
 92  
     /**
 93  
      * Constructor.
 94  
      * 
 95  
      * @param implementation
 96  
      *            To get other helpers and factories.
 97  
      */
 98  900
     CollaborationsHelperMDRImpl(MDRModelImplementation implementation) {
 99  900
         modelImpl = implementation;
 100  900
     }
 101  
 
 102  
 
 103  
     public Collection<ClassifierRole> getAllClassifierRoles(Object ns) {
 104  0
         if (!(ns instanceof Namespace)) {
 105  0
             throw new IllegalArgumentException();
 106  
         }
 107  
 
 108  
         try {
 109  0
             List<ClassifierRole> list = new ArrayList<ClassifierRole>();
 110  0
             for (Object o : ((Namespace) ns).getOwnedElement()) {
 111  0
                 if (o instanceof Namespace) {
 112  0
                     list.addAll(getAllClassifierRoles(o));
 113  
                 }
 114  0
                 if (o instanceof ClassifierRole) {
 115  0
                     list.add((ClassifierRole) o);
 116  
                 }
 117  
             }
 118  0
             return list;
 119  0
         } catch (InvalidObjectException e) {
 120  0
             throw new InvalidElementException(e);
 121  
         }
 122  
     }
 123  
 
 124  
 
 125  
     public Collection getAllPossibleAssociationRoles(Object roleArg) {
 126  0
         if (!(roleArg instanceof ClassifierRole)) {
 127  0
             throw new IllegalArgumentException();
 128  
         }
 129  
 
 130  0
         ClassifierRole role = (ClassifierRole) roleArg;
 131  
 
 132  
         try {
 133  0
             if (role.getBase().isEmpty()) {
 134  0
                 return Collections.emptyList();
 135  
             }
 136  0
             Set associations = new HashSet();
 137  0
             for (Classifier base : role.getBase()) {
 138  0
                 associations.addAll(
 139  
                         modelImpl.getCoreHelper().getAssociations(base));
 140  
             }
 141  0
             return associations;
 142  0
         } catch (InvalidObjectException e) {
 143  0
             throw new InvalidElementException(e);
 144  
         }
 145  
     }
 146  
 
 147  
 
 148  
     public Collection<Classifier> getClassifierRoles(Object role) {
 149  0
         if (role == null) {
 150  0
             return Collections.emptySet();
 151  
         }
 152  
 
 153  0
         if (!(role instanceof ClassifierRole)) {
 154  0
             throw new IllegalArgumentException();
 155  
         }
 156  
 
 157  0
         List<Classifier> roles = new ArrayList<Classifier>();
 158  
         try {
 159  0
             Collection<AssociationEnd> associationEnds = 
 160  
                 Model.getFacade().getAssociationEnds(role);
 161  0
             if (!associationEnds.isEmpty()) {
 162  0
                 for (AssociationEnd end : associationEnds) {
 163  0
                     if (end instanceof AssociationEndRole) {
 164  0
                         UmlAssociation assoc = end.getAssociation();
 165  0
                         for (AssociationEnd end2 : assoc.getConnection()) {
 166  0
                             Classifier classifier = end2.getParticipant();
 167  0
                             if (classifier != role
 168  
                                     && classifier instanceof ClassifierRole) {
 169  0
                                 roles.add(classifier);
 170  
                             }
 171  0
                         }
 172  0
                     }
 173  
                 }
 174  
             }
 175  0
         } catch (InvalidObjectException e) {
 176  0
             throw new InvalidElementException(e);
 177  0
         }
 178  0
         return roles;
 179  
     }
 180  
 
 181  
 
 182  
     public Object getAssociationRole(Object afrom, Object ato) {
 183  0
         if (afrom == null || ato == null) {
 184  0
             throw new IllegalArgumentException();
 185  
         }
 186  0
         ClassifierRole from = (ClassifierRole) afrom;
 187  0
         ClassifierRole to = (ClassifierRole) ato;
 188  
 
 189  
         try {
 190  0
             Collection<AssociationEnd> ends = 
 191  
                 Model.getFacade().getAssociationEnds(from);
 192  0
             for (AssociationEnd end : ends) {
 193  0
                 if (end instanceof AssociationEndRole) {
 194  0
                     UmlAssociation assoc = end.getAssociation();
 195  0
                     for (AssociationEnd end2 : assoc.getConnection()) {
 196  0
                         Classifier classifier = end2.getParticipant();
 197  0
                         if (classifier == to) {
 198  0
                             return assoc;
 199  
                         }
 200  0
                     }
 201  0
                 }
 202  
             }
 203  0
         } catch (InvalidObjectException e) {
 204  0
             throw new InvalidElementException(e);
 205  0
         }
 206  0
         return null;
 207  
     }
 208  
     
 209  
 
 210  
     public Collection<Message> getAllPossibleActivators(Object ames) {
 211  0
         Message mes = (Message) ames;
 212  0
         if (mes == null || mes.getInteraction() == null) {
 213  0
             return Collections.unmodifiableCollection(Collections.EMPTY_LIST);
 214  
         }
 215  
         
 216  
         try {
 217  0
             Interaction inter = mes.getInteraction();
 218  0
             Collection<Message> predecessors = mes.getPredecessor();
 219  0
             Collection<Message> allMessages = inter.getMessage();
 220  0
             List<Message> list = new ArrayList<Message>();
 221  0
             for (Message m : allMessages) {
 222  0
                 if (!predecessors.contains(m) && mes != m
 223  
                         && !hasAsActivator(m, mes)
 224  
                         && !m.getPredecessor().contains(mes)) {
 225  0
                     list.add(m);
 226  
                 }
 227  
             }
 228  0
             return list;
 229  0
         } catch (InvalidObjectException e) {
 230  0
             throw new InvalidElementException(e);
 231  
         }
 232  
     }
 233  
 
 234  
 
 235  
     public boolean hasAsActivator(Object message, Object activator) {
 236  0
         if (!(message instanceof Message)) {
 237  0
             throw new IllegalArgumentException();
 238  
         }
 239  0
         if (!(activator instanceof Message)) {
 240  0
             throw new IllegalArgumentException();
 241  
         }
 242  
 
 243  
         try {
 244  0
             Message messActivator = ((Message) message).getActivator();
 245  0
             if (messActivator == null) {
 246  0
                 return false;
 247  
             }
 248  0
             if (messActivator == activator
 249  
                     || messActivator.getPredecessor().contains(activator)) {
 250  0
                 return true;
 251  
             }
 252  0
             return hasAsActivator(messActivator, activator);
 253  0
         } catch (InvalidObjectException e) {
 254  0
             throw new InvalidElementException(e);
 255  
         }
 256  
     }
 257  
 
 258  
 
 259  
     public void setActivator(Object ames, Object anactivator) {
 260  0
         if (ames == null) {
 261  0
             throw new IllegalArgumentException("message is null");
 262  
         }
 263  0
         if (!(ames instanceof Message)) {
 264  0
             throw new IllegalArgumentException("message");
 265  
         }
 266  0
         if (anactivator != null && !(anactivator instanceof Message)) {
 267  0
             throw new IllegalArgumentException(
 268  
                     "An activator must be a message");
 269  
         }
 270  0
         Message mes = (Message) ames;
 271  0
         Message activator = (Message) anactivator;
 272  0
         if (mes == activator) {
 273  0
             throw new IllegalArgumentException("In setActivator: message may "
 274  
                     + "not be equal to activator");
 275  
         }
 276  
 
 277  0
         if (activator != null) {
 278  0
             if (mes.getInteraction() != activator.getInteraction()) {
 279  0
                 throw new IllegalArgumentException(
 280  
                         "In setActivator: interaction "
 281  
                                 + "of message should equal "
 282  
                                 + "interaction of activator");
 283  
             }
 284  
             // we must find out if the activator itself does not have
 285  
             // message as it's activator
 286  0
             if (hasAsActivator(activator, mes)) {
 287  0
                 throw new IllegalArgumentException(
 288  
                         "In setActivator: message may "
 289  
                                 + "not be the activator for "
 290  
                                 + "the original activator");
 291  
             }
 292  
             // An activator can't also be a predecessor of a message
 293  0
             if (mes.getPredecessor().contains(activator)) {
 294  0
                 mes.getPredecessor().remove(activator);
 295  
             }
 296  
         }
 297  0
         List<Message> listToChange = new ArrayList<Message>();
 298  0
         Collection<Message> predecessors = mes.getPredecessor();
 299  0
         listToChange.addAll(predecessors);
 300  0
         listToChange.add(mes);
 301  0
         Interaction inter = mes.getInteraction();
 302  0
         for (Message mes2 : inter.getMessage()) {
 303  0
             if (mes2.getPredecessor().contains(mes)) {
 304  0
                 listToChange.add(mes2);
 305  
             }
 306  
         }
 307  
         // This causes problems. It can make multiple return messages
 308  
         // refer to the same activator even if not returning to the same
 309  
         // classifier role as the activator emenates from.
 310  
         // I'm not sure that changing the activator of one message
 311  
         // should amend any other messages but this is certainly changing
 312  
         // too many - Bob.
 313  0
         for (Message mes2 : listToChange) {
 314  0
             mes2.setActivator(activator);
 315  
         }
 316  
 
 317  0
     }
 318  
 
 319  
 
 320  
     public Collection<Message> getAllPossiblePredecessors(Object amessage) {
 321  0
         Message message = (Message) amessage;
 322  0
         if (message == null) {
 323  0
             throw new IllegalArgumentException(
 324  
                     "In getAllPossiblePredecessors: "
 325  
                             + "argument message is null");
 326  
         }
 327  
         
 328  
         try {
 329  0
             Interaction inter = message.getInteraction();
 330  0
             List<Message> list = new ArrayList<Message>();
 331  0
             for (Message mes : inter.getMessage()) {
 332  0
                 if (mes.getActivator() == message.getActivator() 
 333  
                         && message != mes
 334  
                         && !mes.getPredecessor().contains(message)
 335  
                         && !message.getPredecessor().contains(message)) {
 336  0
                     list.add(mes);
 337  
                 }
 338  
             }
 339  0
             return list;
 340  0
         } catch (InvalidObjectException e) {
 341  0
             throw new InvalidElementException(e);
 342  
         }
 343  
     }
 344  
 
 345  
 
 346  
     public void addBase(Object arole, Object abase) {
 347  0
         ClassifierRole role = (ClassifierRole) arole;
 348  0
         Classifier base = (Classifier) abase;
 349  0
         if (role == null || base == null) {
 350  0
             throw new IllegalArgumentException("In addBase: either the role "
 351  
                     + "or the base is null");
 352  
         }
 353  0
         role.getBase().add(base);
 354  0
         if (modelImpl.getFacade().getBases(role).size() == 1) {
 355  0
             role.getAvailableContents().clear();
 356  0
             role.getAvailableContents().addAll(base.getOwnedElement());
 357  0
             role.getAvailableFeature().clear();
 358  0
             role.getAvailableFeature().addAll(base.getFeature());
 359  
         } else {
 360  0
             for (ModelElement elem : base.getOwnedElement()) {
 361  0
                 if (!role.getAvailableContents().contains(elem)) {
 362  0
                     role.getAvailableContents().add(elem);
 363  
                 }
 364  
             }
 365  0
             for (Feature feature : base.getFeature()) {
 366  0
                 if (!role.getAvailableFeature().contains(feature)) {
 367  0
                     role.getAvailableFeature().add(feature);
 368  
                 }
 369  
             }
 370  
         }
 371  0
     }
 372  
 
 373  
     public void setBases(Object role, Collection bases) {
 374  0
         if (role == null || bases == null) {
 375  0
             throw new IllegalArgumentException("In setBases: either the role "
 376  
                     + "or the collection bases is " + "null");
 377  
         }
 378  0
         CollectionHelper.update(((ClassifierRole) role).getBase(), bases);
 379  0
     }
 380  
 
 381  
 
 382  
     public Collection<Feature> allAvailableFeatures(Object arole) {
 383  0
         LOG.info("allAvailableFeatures start");
 384  
         
 385  0
         if (arole instanceof ClassifierRole) {
 386  
             try {
 387  0
                 List<Feature> returnList = new ArrayList<Feature>();
 388  0
                 ClassifierRole role = (ClassifierRole) arole;
 389  
                 for (ModelElement genElem 
 390  0
                         : CoreHelperMDRImpl.getAllParents(role)) {
 391  0
                     if (genElem instanceof ClassifierRole) {
 392  0
                         returnList.addAll(allAvailableFeatures(genElem));
 393  
                     }
 394  
                 }
 395  0
                 for (Classifier classifier : role.getBase()) {
 396  0
                     returnList.addAll(classifier.getFeature());
 397  
                 }
 398  0
                 LOG.info("allAvailableFeatures " + returnList.size());
 399  0
                 return returnList;
 400  0
             } catch (InvalidObjectException e) {
 401  0
                 throw new InvalidElementException(e);
 402  
             }
 403  
         }
 404  0
         throw new IllegalArgumentException("Cannot get available features on "
 405  
                 + arole);
 406  
     }
 407  
 
 408  
 
 409  
     public Collection allAvailableContents(Object arole) {
 410  0
         LOG.info("allAvailableContents start");
 411  
         try {
 412  0
             if (arole instanceof ClassifierRole) {
 413  0
                 List returnList = new ArrayList();
 414  0
                 ClassifierRole role = (ClassifierRole) arole;
 415  
                 for (ModelElement genElem 
 416  0
                         : CoreHelperMDRImpl.getAllParents(role)) {
 417  0
                     if (genElem instanceof ClassifierRole) {
 418  0
                         returnList.addAll(allAvailableContents(genElem));
 419  
                     }
 420  
                 }
 421  0
                 for (Classifier baseClassifier : role.getBase()) {
 422  0
                     returnList.addAll(baseClassifier.getOwnedElement());
 423  
                 }
 424  0
                 LOG.info("allAvailableContents " + returnList.size());
 425  0
                 return returnList;
 426  
             }
 427  0
         } catch (InvalidObjectException e) {
 428  0
             throw new InvalidElementException(e);
 429  0
         }
 430  0
         throw new IllegalArgumentException("Cannot get available contents on "
 431  
                 + arole);
 432  
     }
 433  
 
 434  
 
 435  
     public Collection getAllPossibleBases(Object role) {
 436  
         try {
 437  0
             if (role instanceof ClassifierRole) {
 438  0
                 return getAllPossibleBases((ClassifierRole) role);
 439  0
             } else if (role instanceof AssociationRole) {
 440  0
                 return getAllPossibleBases((AssociationRole) role);
 441  
             } else {
 442  0
                 throw new IllegalArgumentException("Illegal type " + role);
 443  
             }
 444  0
         } catch (InvalidObjectException e) {
 445  0
             throw new InvalidElementException(e);
 446  
         }
 447  
     }
 448  
 
 449  
     /**
 450  
      * Returns all possible bases for some AssociationRole taking into account
 451  
      * the wellformednessrules as defined in section 2.10.3 of the UML 1.3 spec.
 452  
      * <p>
 453  
      * Beware: this function does not return the actual base! 
 454  
      * Which is by design; there are easier ways to retrieve the actual base.
 455  
      * 
 456  
      * @param aRole
 457  
      *            the given associationrole
 458  
      * @return Collection all possible bases
 459  
      */
 460  
     private Collection getAllPossibleBases(AssociationRole aRole) {
 461  0
         Set<UmlAssociation> ret = new HashSet<UmlAssociation>();
 462  0
         if (aRole == null || aRole.getNamespace() == null) {
 463  0
             return ret;
 464  
         }
 465  
 
 466  
         // find the bases of the connected classifierroles so that we can see
 467  
         // what associations are between them. If there are bases then the
 468  
         // associations between those bases form the possible bases. Otherwise
 469  
         // the bases are formed by all associations in the namespace of the
 470  
         // collaboration
 471  0
         Set<Classifier> bases = new HashSet<Classifier>();
 472  0
         for (AssociationEnd end : aRole.getConnection()) {
 473  0
             assert end instanceof AssociationEndRole;
 474  0
             ClassifierRole type = (ClassifierRole) end.getParticipant();
 475  0
             if (type != null) {
 476  0
                 bases.addAll(type.getBase());
 477  
             }
 478  0
         }
 479  0
         if (bases.isEmpty()) {
 480  0
             ModelManagementHelper mmh = modelImpl.getModelManagementHelper();
 481  0
             Namespace ns = 
 482  
                 ((Collaboration) aRole.getNamespace()).getNamespace();
 483  0
             ret.addAll(
 484  
                     mmh.getAllModelElementsOfKind(ns, UmlAssociation.class));
 485  0
             ret.removeAll(mmh.getAllModelElementsOfKind(ns,
 486  
                     AssociationRole.class));
 487  0
         } else {
 488  0
             CoreHelper ch = modelImpl.getCoreHelper();
 489  
             /* This double 'for' loop may be optimised as follows:
 490  
              * - Use arrays in stead of a Set for bases
 491  
              * - Have the second loop start from the element after base1 
 492  
              * ... but I chose not to do this, since the gain is small,
 493  
              * and this is only used for a lazily filled combo.
 494  
              * BTW: This is also used by Notation.*/
 495  0
             for (Classifier base1 : bases)  {
 496  0
                 for (Classifier base2 : bases) {
 497  
                     // include associations to self - see issue 5602
 498  0
                     ret.addAll(ch.getAssociations(base1, base2));
 499  
                 }
 500  
             }
 501  
         }
 502  
         // An Association can only have a single unnamed ClassifierRole, so
 503  0
         Collection<UmlAssociation> listToRemove = new ArrayList<UmlAssociation>();
 504  0
         for (UmlAssociation association : ret) {
 505  0
             Collection<AssociationRole> associationRoles = 
 506  
                 ((org.omg.uml.UmlPackage) (association)
 507  
                     .refOutermostPackage()).getCollaborations()
 508  
                     .getABaseAssociationRole().getAssociationRole(association);
 509  0
             if (associationRoles.isEmpty()) {
 510  0
                 continue;
 511  
             }
 512  
             // if we are unnamed eliminate all classifiers which are already 
 513  
             // the base of some role
 514  0
             if (aRole.getName() == null || aRole.getName().equals("")) {
 515  0
                 listToRemove.add(association);
 516  
             } else {
 517  
                 // eliminate Classifiers which already have an unnamed role
 518  0
                 for (AssociationRole ar : associationRoles) {
 519  0
                     if (ar.getName() == null || ar.getName().equals("")) {
 520  0
                         listToRemove.add(association);
 521  
                     }
 522  
                 }
 523  
             }
 524  0
         }
 525  0
         ret.removeAll(listToRemove);
 526  
 
 527  0
         return ret;
 528  
     }
 529  
 
 530  
     /**
 531  
      * Returns all possible bases for some classifierrole taking into account
 532  
      * the wellformednessrules as defined in section 2.10.3 of the UML 1.4 spec.
 533  
      * <p>
 534  
      * 
 535  
      * @param role
 536  
      *            the given classifierrole
 537  
      * @return Collection all possible bases
 538  
      */
 539  
     private Collection getAllPossibleBases(ClassifierRole role) {
 540  0
         if (role == null || modelImpl.getFacade().getNamespace(role) == null) {
 541  0
             return Collections.EMPTY_SET;
 542  
         }
 543  0
         Collaboration collaboration = (Collaboration) role.getNamespace();
 544  0
         Namespace ns = collaboration.getNamespace();
 545  0
         ModelManagementHelper mmh = modelImpl.getModelManagementHelper();
 546  0
         Collection<Classifier> returnList = mmh.getAllModelElementsOfKind(ns,
 547  
                 Classifier.class);
 548  
         // WFR 2.10.3.3 #4
 549  0
         returnList.removeAll(mmh.getAllModelElementsOfKind(ns,
 550  
                 ClassifierRole.class));
 551  
 
 552  
         // A Classifier can only have a single unnamed ClassifierRole, so
 553  
         // TODO: This probably belongs in a critic instead of here
 554  0
         Collection<Classifier> listToRemove = new ArrayList<Classifier>();
 555  0
         for (Classifier classifier : returnList) {
 556  0
             Collection<ClassifierRole> classifierRoles = 
 557  
                 ((org.omg.uml.UmlPackage) (classifier)
 558  
                     .refOutermostPackage()).getCollaborations()
 559  
                     .getAClassifierRoleBase().getClassifierRole(classifier);
 560  0
             if (classifierRoles.isEmpty()) {
 561  0
                 continue;
 562  
             }
 563  
             // if we are unnamed eliminate all classifiers which are already 
 564  
             // the base of some role
 565  0
             if (role.getName() == null || role.getName().equals("")) {
 566  0
                 listToRemove.add(classifier);
 567  
             } else {
 568  
                 // eliminate Classifiers which already have an unnamed role
 569  0
                 for (ClassifierRole cr : classifierRoles) {
 570  0
                     if (cr.getName() == null || cr.getName().equals("")) {
 571  0
                         listToRemove.add(classifier);
 572  
                     }
 573  
                 }
 574  
             }
 575  0
         }
 576  0
         returnList.removeAll(listToRemove);
 577  
 
 578  
         /* We need to verify that ns is a Package,
 579  
          * if not - find its parent package! 
 580  
          * Otherwise this causes an exception when creating 
 581  
          * a sequence diagram for a ClassifierRole.*/
 582  0
         if (!(ns instanceof UmlPackage)) {
 583  0
             while (ns != null) {
 584  0
                 ns = ns.getNamespace();
 585  0
                 if (ns instanceof UmlPackage) {
 586  0
                     break;
 587  
                 }
 588  
             }
 589  
         }
 590  
         // now get all classifiers imported from other packages
 591  
         // TODO: This should probably happen automatically in 
 592  
         // getAllModelElementsOfKind() - tfm
 593  0
         if (modelImpl.getFacade().isAPackage(ns)) {
 594  0
             returnList.addAll(getAllImportedClassifiers(ns));
 595  
         }
 596  
 
 597  0
         return returnList;
 598  
     }
 599  
     
 600  
     /**
 601  
      * Return a collection of classifiers that are imported from other packages
 602  
      * into the given namespace.
 603  
      *  
 604  
      * @param obj the given namespace
 605  
      * @return a collection of classifiers
 606  
      */
 607  
     private Collection<Classifier> getAllImportedClassifiers(Object obj) {
 608  0
         Collection c = modelImpl.getModelManagementHelper()
 609  
                         .getAllImportedElements(obj);
 610  0
         return filterClassifiers(c);
 611  
     }
 612  
     
 613  
     private Collection<Classifier> filterClassifiers(Collection in) {
 614  0
         Collection<Classifier> out = new ArrayList<Classifier>();
 615  0
         for (Object o : in) {
 616  0
             if (o instanceof Classifier && !(o instanceof ClassifierRole)) 
 617  0
                 out.add((Classifier) o);
 618  
         }
 619  0
         return out;
 620  
     }
 621  
     
 622  
 
 623  
     public void setBase(Object arole, Object abase) {
 624  0
         if (arole == null) {
 625  0
             throw new IllegalArgumentException("role is null");
 626  
         }
 627  0
         if (arole instanceof AssociationRole) {
 628  0
             AssociationRole role = (AssociationRole) arole;
 629  0
             UmlAssociation base = (UmlAssociation) abase;
 630  
 
 631  
             // TODO: Must we calculate the whole list?
 632  0
             if (base != null && !getAllPossibleBases(role).contains(base)) {
 633  0
                 throw new IllegalArgumentException("base is not allowed for "
 634  
                         + "this role");
 635  
             }
 636  0
             role.setBase(base);
 637  0
             ClassifierRole sender = (ClassifierRole) modelImpl.getCoreHelper()
 638  
                     .getSource(role);
 639  0
             ClassifierRole receiver = (ClassifierRole) modelImpl
 640  
                     .getCoreHelper().getDestination(role);
 641  0
             Collection<Classifier> senderBases = sender.getBase();
 642  0
             Collection<Classifier> receiverBases = receiver.getBase();
 643  
 
 644  0
             AssociationEndRole senderRole = (AssociationEndRole) modelImpl.
 645  
                 getCoreHelper().getAssociationEnd(sender, role);
 646  0
             AssociationEndRole receiverRole = (AssociationEndRole) modelImpl.
 647  
                 getCoreHelper().getAssociationEnd(receiver, role);
 648  
 
 649  0
             if (base != null) {
 650  0
                 for (AssociationEnd end : base.getConnection()) {
 651  0
                     if (senderBases.contains(end.getParticipant())) {
 652  0
                         senderRole.setBase(end);
 653  0
                     } else if (receiverBases.contains(end.getParticipant())) {
 654  0
                         receiverRole.setBase(end);
 655  
                     }
 656  
                 }
 657  
             }
 658  
     
 659  0
             return;
 660  0
         } else if (arole instanceof AssociationEndRole) {
 661  0
             AssociationEndRole role = (AssociationEndRole) arole;
 662  0
             AssociationEnd base = (AssociationEnd) abase;
 663  
 
 664  0
             role.setBase(base);
 665  
     
 666  0
             return;
 667  
         }
 668  
 
 669  0
         throw new IllegalArgumentException("role");
 670  
     }
 671  
 
 672  
 
 673  
     public boolean isAddingCollaborationAllowed(Object context) {
 674  0
         return (
 675  
                 context instanceof Classifier 
 676  
                 || context instanceof Operation
 677  
                 //|| context instanceof Collaboration
 678  
                 //|| context instanceof Model
 679  
                 );
 680  
     }
 681  
 
 682  
 
 683  
     public void removeBase(Object handle, Object c) {
 684  
         try {
 685  0
             if (handle instanceof ClassifierRole && c instanceof Classifier) {
 686  0
                 ((ClassifierRole) handle).getBase().remove(c);
 687  
                 
 688  0
                 return;
 689  
             }
 690  0
         } catch (InvalidObjectException e) {
 691  0
             throw new InvalidElementException(e);
 692  0
         }
 693  0
         throw new IllegalArgumentException(
 694  
                 "There must be a ClassifierRole and a Classifier");
 695  
     }
 696  
 
 697  
 
 698  
     public void removeConstrainingElement(Object handle, Object constraint) {
 699  
         try {
 700  0
             if (handle instanceof Collaboration
 701  
                     && constraint instanceof ModelElement) {
 702  0
                 Collaboration collab = (Collaboration) handle;
 703  0
                 collab.getConstrainingElement().remove(constraint);
 704  
                 
 705  0
                 return;
 706  
             }
 707  0
         } catch (InvalidObjectException e) {
 708  0
             throw new InvalidElementException(e);
 709  0
         }
 710  0
         throw new IllegalArgumentException("handle: " + handle
 711  
                 + " or constraint: " + constraint);
 712  
     }
 713  
 
 714  
 
 715  
     public void removeMessage(Object handle, Object message) {
 716  
         try {
 717  0
             if (handle instanceof Interaction && message instanceof Message) {
 718  0
                 ((Interaction) handle).getMessage().remove(message);
 719  0
                 return;
 720  
             }
 721  0
             if (handle instanceof AssociationRole 
 722  
                     && message instanceof Message) {
 723  0
                 ((AssociationRole) handle).getMessage().remove(message);
 724  0
                 return;
 725  
             }
 726  0
         } catch (InvalidObjectException e) {
 727  0
             throw new InvalidElementException(e);
 728  0
         }
 729  0
         throw new IllegalArgumentException("handle: " + handle
 730  
                 + " or message: " + message);
 731  
     }
 732  
 
 733  
 
 734  
     public void removeSuccessor(Object handle, Object mess) {
 735  
         try {
 736  0
             if (handle instanceof Message && mess instanceof Message) {
 737  0
                 ((org.omg.uml.UmlPackage) ((Message) handle)
 738  
                         .refOutermostPackage()).getCollaborations()
 739  
                         .getAPredecessorSuccessor().remove((Message) handle,
 740  
                                 (Message) mess);
 741  0
                 return;
 742  
             }
 743  0
         } catch (InvalidObjectException e) {
 744  0
             throw new InvalidElementException(e);
 745  0
         }
 746  0
         throw new IllegalArgumentException("predecessor: " + handle
 747  
                 + " or successor: " + mess);
 748  
     }
 749  
 
 750  
 
 751  
     public void removePredecessor(Object handle, Object message) {
 752  
         try {
 753  0
             if (handle instanceof Message && message instanceof Message) {
 754  0
                 ((Message) handle).getPredecessor().remove(message);
 755  0
                 return;
 756  
             }
 757  0
         } catch (InvalidObjectException e) {
 758  0
             throw new InvalidElementException(e);
 759  0
         }
 760  0
         throw new IllegalArgumentException("handle: " + handle
 761  
                 + " or message: " + message);
 762  
     }
 763  
 
 764  
 
 765  
     public void addConstrainingElement(Object handle, Object constraint) {
 766  0
         if (handle instanceof Collaboration
 767  
                 && constraint instanceof ModelElement) {
 768  0
             ((Collaboration) handle).getConstrainingElement().add(
 769  
                     (ModelElement) constraint);
 770  0
             return;
 771  
         }
 772  
 
 773  0
         throw new IllegalArgumentException("handle: " + handle
 774  
                 + " or constraint: " + constraint);
 775  
     }
 776  
 
 777  
 
 778  
     public void addInstance(Object classifierRole, Object instance) {
 779  0
         if (classifierRole instanceof ClassifierRole
 780  
                 && instance instanceof Instance) {
 781  0
             ((ClassifierRole) classifierRole).getConformingInstance().add(
 782  
                     (Instance) instance);
 783  
         }
 784  0
         throw new IllegalArgumentException("classifierRole: " + classifierRole
 785  
                 + " or instance: " + instance);
 786  
     }
 787  
 
 788  
 
 789  
     public void addMessage(Object handle, Object elem) {
 790  0
         if (handle instanceof Interaction && elem instanceof Message) {
 791  0
             final Message message = (Message) elem;
 792  0
             final Interaction interaction = (Interaction) handle;
 793  0
             final Interaction oldInteraction = message.getInteraction();
 794  
             
 795  0
             if (oldInteraction != null) {
 796  0
                 oldInteraction.getMessage().remove(message);
 797  
             }
 798  0
             interaction.getMessage().add(message);
 799  0
             return;
 800  
         }
 801  0
         if (handle instanceof AssociationRole && elem instanceof Message) {
 802  0
             ((AssociationRole) handle).getMessage().add((Message) elem);
 803  0
             return;
 804  
         }
 805  0
         throw new IllegalArgumentException("handle: " + handle + " or elem: "
 806  
                 + elem);
 807  
     }
 808  
  
 809  
 
 810  
     public void addSuccessor(Object handle, Object mess) {
 811  0
         if (handle instanceof Message && mess instanceof Message) {
 812  0
             ((Message) mess).getPredecessor().add((Message) handle);
 813  0
             return;
 814  
         }
 815  
 
 816  0
         throw new IllegalArgumentException("predecessor: " + handle
 817  
                 + " or successor: " + mess);
 818  
     }
 819  
 
 820  
 
 821  
     public void addPredecessor(Object handle, Object predecessor) {
 822  0
         if (handle != null && handle instanceof Message && predecessor != null
 823  
                 && predecessor instanceof Message) {
 824  0
             ((Message) handle).getPredecessor().add((Message) predecessor);
 825  0
             return;
 826  
         }
 827  0
         throw new IllegalArgumentException("handle: " + handle
 828  
                 + " or predecessor: " + predecessor);
 829  
     }
 830  
 
 831  
 
 832  
     public void setAction(Object handle, Object action) {
 833  0
         if (handle instanceof Message
 834  
                 && (action == null || action instanceof Action)) {
 835  0
             ((Message) handle).setAction((Action) action);
 836  0
             return;
 837  
         }
 838  0
         throw new IllegalArgumentException("handle: " + handle + " or action: "
 839  
                 + action);
 840  
     }
 841  
 
 842  
 
 843  
     public void setContext(Object handle, Object col) {
 844  0
         if (handle instanceof Interaction
 845  
                 && (col instanceof Collaboration || col == null)) {
 846  0
             ((Interaction) handle).setContext((Collaboration) col);
 847  
     
 848  0
             return;
 849  
         }
 850  0
         throw new IllegalArgumentException("handle: " + handle + " or col: "
 851  
                 + col);
 852  
     }
 853  
 
 854  
 
 855  
     public void setSuccessors(Object handle, Collection messages) {
 856  0
         if (handle instanceof Message) {
 857  0
             Collection currentMessages = 
 858  
                 Model.getFacade().getSuccessors(handle);
 859  0
             if (!currentMessages.isEmpty()) {
 860  0
                 Collection successors = new ArrayList(currentMessages);
 861  0
                 for (Object msg : successors) {
 862  0
                     removeSuccessor(handle, msg);
 863  
                 }
 864  
             }
 865  0
             for (Object msg : messages) {
 866  0
                 addSuccessor(handle, msg);
 867  
             }
 868  0
             return;
 869  
         }
 870  
 
 871  0
         throw new IllegalArgumentException("predecessor: " + handle
 872  
                 + " or messages: " + messages);
 873  
     }
 874  
 
 875  
 
 876  
     public void setPredecessors(Object handle, Collection predecessors) {
 877  0
         if (handle instanceof Message) {
 878  0
             CollectionHelper.update(
 879  
                     ((Message) handle).getPredecessor(), predecessors);
 880  0
             return;
 881  
         }
 882  0
         throw new IllegalArgumentException("handle: " + handle
 883  
                 + " or predecessors: " + predecessors);
 884  
     }
 885  
 
 886  
 
 887  
     public void setRepresentedClassifier(Object handle, Object classifier) {
 888  0
         if (handle instanceof Collaboration
 889  
                 && ((classifier == null) || classifier instanceof Classifier)) {
 890  0
             ((Collaboration) handle).
 891  
                 setRepresentedClassifier((Classifier) classifier);
 892  0
             return;
 893  
         }
 894  0
         throw new IllegalArgumentException("handle: " + handle
 895  
                 + " or classifier: " + classifier);
 896  
     }
 897  
 
 898  
 
 899  
     public void setRepresentedOperation(Object handle, Object operation) {
 900  0
         if (handle instanceof Collaboration
 901  
                 && ((operation == null) || operation instanceof Operation)) {
 902  0
             ((Collaboration) handle).
 903  
                 setRepresentedOperation((Operation) operation);
 904  
     
 905  0
             return;
 906  
         }
 907  0
         throw new IllegalArgumentException("handle: " + handle
 908  
                 + " or operation: " + operation);
 909  
     }
 910  
 
 911  
 
 912  
     public void setSender(Object handle, Object sender) {
 913  0
         if (handle instanceof Message
 914  
                 && (sender instanceof ClassifierRole || sender == null)) {
 915  0
             ((Message) handle).setSender((ClassifierRole) sender);
 916  0
             return;
 917  
         }
 918  0
         if (handle instanceof Stimulus && sender instanceof Instance) {
 919  0
             ((Stimulus) handle).setSender((Instance) sender);
 920  0
             return;
 921  
         }
 922  0
         throw new IllegalArgumentException("handle: " + handle + " or sender: "
 923  
                 + sender);
 924  
     }
 925  
 
 926  
 
 927  
     public void removeInteraction(Object collab, Object interaction) {
 928  
         try {
 929  0
             if (collab instanceof Collaboration
 930  
                     && interaction instanceof Interaction) {
 931  0
                 ((Collaboration) collab).getInteraction().remove(interaction);
 932  0
                 return;
 933  
             }
 934  0
         } catch (InvalidObjectException e) {
 935  0
             throw new InvalidElementException(e);
 936  0
         }
 937  0
         throw new IllegalArgumentException("collab: " + collab
 938  
                 + " or interaction: " + interaction);
 939  
     }
 940  
 }