Coverage Report - org.argouml.model.mdr.UseCasesHelperMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UseCasesHelperMDRImpl
1%
3/213
0%
0/152
7.208
 
 1  
 /* $Id: UseCasesHelperMDRImpl.java 18767 2010-09-18 23:19:19Z tfmorris $
 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  
  *    tfmorris
 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.model.mdr;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.HashSet;
 44  
 import java.util.List;
 45  
 import java.util.Set;
 46  
 
 47  
 import javax.jmi.reflect.InvalidObjectException;
 48  
 
 49  
 import org.argouml.model.InvalidElementException;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.model.UseCasesHelper;
 52  
 import org.omg.uml.behavioralelements.usecases.Actor;
 53  
 import org.omg.uml.behavioralelements.usecases.Extend;
 54  
 import org.omg.uml.behavioralelements.usecases.ExtensionPoint;
 55  
 import org.omg.uml.behavioralelements.usecases.Include;
 56  
 import org.omg.uml.behavioralelements.usecases.UseCase;
 57  
 import org.omg.uml.foundation.core.Namespace;
 58  
 import org.omg.uml.foundation.core.UmlClass;
 59  
 import org.omg.uml.foundation.datatypes.BooleanExpression;
 60  
 import org.omg.uml.foundation.datatypes.Expression;
 61  
 import org.omg.uml.modelmanagement.Subsystem;
 62  
 
 63  
 /**
 64  
  * UseCase Helper for MDR ModelImplementation.<p>
 65  
  *
 66  
  * @since ARGO0.19.5
 67  
  * @author Ludovic Ma&icirc;tre
 68  
  * @author Tom Morris
 69  
 
 70  
  */
 71  0
 class UseCasesHelperMDRImpl implements UseCasesHelper {
 72  
 
 73  
     /**
 74  
      * The model implementation.
 75  
      */
 76  
     private MDRModelImplementation modelImpl;
 77  
 
 78  
     /**
 79  
      * Constructor.
 80  
      *
 81  
      * @param implementation
 82  
      *            To get other helpers and factories.
 83  
      */
 84  900
     UseCasesHelperMDRImpl(MDRModelImplementation implementation) {
 85  900
         modelImpl = implementation;
 86  900
     }
 87  
 
 88  
 
 89  
     public Collection<UseCase> getAllUseCases(Object ns) {
 90  0
         if (!(ns instanceof Namespace)) {
 91  0
             throw new IllegalArgumentException();
 92  
         }
 93  
 
 94  0
         List<UseCase> list = new ArrayList<UseCase>();
 95  
         try {
 96  0
             for (Object o : ((Namespace) ns).getOwnedElement()) {
 97  0
                 if (o instanceof Namespace) {
 98  0
                     list.addAll(getAllUseCases(o));
 99  
                 }
 100  0
                 if (o instanceof UseCase) {
 101  0
                     list.add((UseCase) o);
 102  
                 }
 103  
             }
 104  0
         } catch (InvalidObjectException e) {
 105  0
             throw new InvalidElementException(e);
 106  0
         }
 107  0
         return list;
 108  
     }
 109  
 
 110  
 
 111  
     public Collection<Actor> getAllActors(Object ns) {
 112  0
         if (!(ns instanceof Namespace)) {
 113  0
             throw new IllegalArgumentException();
 114  
         }
 115  
 
 116  0
         List<Actor> list = new ArrayList<Actor>();
 117  
         try {
 118  0
             for (Object o : ((Namespace) ns).getOwnedElement()) {
 119  0
                 if (o instanceof Namespace) {
 120  0
                     list.addAll(getAllActors(o));
 121  
                 }
 122  0
                 if (o instanceof Actor) {
 123  0
                     list.add((Actor) o);
 124  
                 }
 125  
             }
 126  0
         } catch (InvalidObjectException e) {
 127  0
             throw new InvalidElementException(e);
 128  0
         }
 129  0
         return list;
 130  
     }
 131  
 
 132  
 
 133  
     public Collection<UseCase> getExtendedUseCases(Object ausecase) {
 134  0
         if (ausecase == null) {
 135  0
             return new ArrayList<UseCase>();
 136  
         }
 137  0
         List<UseCase> list = new ArrayList<UseCase>();
 138  0
         UseCase usecase = (UseCase) ausecase;
 139  
         try {
 140  0
             for (Extend extend : usecase.getExtend()) {
 141  0
                 list.add(extend.getBase());
 142  
             }
 143  0
         } catch (InvalidObjectException e) {
 144  0
             throw new InvalidElementException(e);
 145  0
         }
 146  0
         return list;
 147  
     }
 148  
 
 149  
 
 150  
     public Extend getExtends(Object abase, Object anextension) {
 151  0
         if (!(abase instanceof UseCase)
 152  
                 || !(anextension instanceof UseCase)) {
 153  0
             throw new IllegalArgumentException();
 154  
         }
 155  0
         UseCase base = (UseCase) abase;
 156  0
         UseCase extension = (UseCase) anextension;
 157  
         try {
 158  0
             for (Extend extend : extension.getExtend()) {
 159  0
                 if (extend.getBase() == base) {
 160  0
                     return extend;
 161  
                 }
 162  
             }
 163  0
         } catch (InvalidObjectException e) {
 164  0
             throw new InvalidElementException(e);
 165  0
         }
 166  0
         return null;
 167  
     }
 168  
 
 169  
 
 170  
     public Collection<UseCase> getIncludedUseCases(Object ausecase) {
 171  0
         if (!(ausecase instanceof UseCase)) {
 172  0
             throw new IllegalArgumentException();
 173  
         }
 174  0
         List<UseCase> result = new ArrayList<UseCase>();
 175  0
         UseCase usecase = (UseCase) ausecase;
 176  
         try {
 177  0
             for (Include include : usecase.getInclude()) {
 178  0
                 UseCase addition = include.getBase();
 179  0
                 result.add(addition);
 180  0
             }
 181  0
         } catch (InvalidObjectException e) {
 182  0
             throw new InvalidElementException(e);
 183  0
         }
 184  0
         return result;
 185  
     }
 186  
 
 187  
 
 188  
     public Include getIncludes(Object abase, Object aninclusion) {
 189  0
         if (!(abase instanceof UseCase)
 190  
                 || !(aninclusion instanceof UseCase)) {
 191  0
             throw new IllegalArgumentException();
 192  
         }
 193  0
         UseCase base = (UseCase) abase;
 194  0
         UseCase inclusion = (UseCase) aninclusion;
 195  
         try {
 196  0
             for (Include include : inclusion.getInclude()) {
 197  0
                 if (include.getBase() == base) {
 198  0
                     return include;
 199  
                 }
 200  
             }
 201  0
         } catch (InvalidObjectException e) {
 202  0
             throw new InvalidElementException(e);
 203  0
         }
 204  0
         return null;
 205  
     }
 206  
 
 207  
 
 208  
     public Collection getSpecificationPath(Object ausecase) {
 209  0
         UseCase uc = (UseCase) ausecase;
 210  0
         Set set = new HashSet();
 211  
         try {
 212  0
             set.addAll(modelImpl.getModelManagementHelper().
 213  
                     getAllSurroundingNamespaces(uc));
 214  0
             Set set2 = new HashSet();
 215  0
             for (Object o : set) {
 216  0
                 if (o instanceof Subsystem || o instanceof UmlClass) {
 217  0
                     set2.add(o);
 218  
                 }
 219  
             }
 220  0
             return set2;
 221  0
         } catch (InvalidObjectException e) {
 222  0
             throw new InvalidElementException(e);
 223  
         }
 224  
     }
 225  
 
 226  
 
 227  
     public void setBase(Object extend, Object base) {
 228  0
         if (base == null) {
 229  0
             throw new IllegalArgumentException(
 230  
                     "The base cannot be null");
 231  
         }
 232  
 
 233  0
         if (!(base instanceof UseCase)) {
 234  0
             throw new IllegalArgumentException(
 235  
                     "The base cannot be a " + base.getClass().getName());
 236  
         }
 237  
 
 238  0
         if (extend == null) {
 239  0
             throw new IllegalArgumentException("extend");
 240  
         }
 241  
 
 242  0
         if (extend instanceof Extend) {
 243  0
             Extend theExtend = ((Extend) extend);
 244  0
             if (base == theExtend.getBase()) {
 245  0
                 return;
 246  
             }
 247  0
             for (ExtensionPoint point : theExtend.getExtensionPoint()) {
 248  0
                 removeExtend(point, theExtend);
 249  
             }
 250  0
             ExtensionPoint point =
 251  
                 (ExtensionPoint) modelImpl.
 252  
                     getUseCasesFactory().buildExtensionPoint(base);
 253  0
             theExtend.setBase((UseCase) base);
 254  0
             addExtensionPoint(theExtend, point);
 255  0
         } else if (extend instanceof Include) {
 256  
             // TODO: this can be simplified to just
 257  
             //((Include) extend).setBase((UseCase) base);
 258  0
             Include theInclude = ((Include) extend);
 259  0
             if (base == theInclude.getBase()) {
 260  0
                 return;
 261  
             }
 262  
             // TODO: This looks backwards. Left over from issue 2034?
 263  0
             theInclude.setAddition((UseCase) base);
 264  0
         } else {
 265  0
             throw new IllegalArgumentException();
 266  
         }
 267  0
     }
 268  
 
 269  
 
 270  
     public void removeExtend(Object elem, Object extend) {
 271  
         try {
 272  0
             if (elem instanceof UseCase && extend instanceof Extend) {
 273  0
                 ((UseCase) elem).getExtend().remove(extend);
 274  0
                 return;
 275  
             }
 276  0
             if (elem instanceof ExtensionPoint && extend instanceof Extend) {
 277  0
                 ((Extend) extend).getExtensionPoint().remove(elem);
 278  0
                 return;
 279  
             }
 280  0
         } catch (InvalidObjectException e) {
 281  0
             throw new InvalidElementException(e);
 282  0
         }
 283  0
         throw new IllegalArgumentException("elem: " + elem + " or extend: "
 284  
                 + extend);
 285  
     }
 286  
 
 287  
 
 288  
     public void removeExtensionPoint(Object elem, Object ep) {
 289  
         try {
 290  0
             if (ep instanceof ExtensionPoint) {
 291  0
                 if (elem instanceof UseCase) {
 292  0
                     ((UseCase) elem).getExtensionPoint().remove(ep);
 293  0
                     return;
 294  
                 }
 295  0
                 if (elem instanceof Extend) {
 296  0
                     ((Extend) elem).getExtensionPoint().remove(ep);
 297  0
                     return;
 298  
                 }
 299  
             }
 300  0
         } catch (InvalidObjectException e) {
 301  0
             throw new InvalidElementException(e);
 302  0
         }
 303  0
         throw new IllegalArgumentException("elem: " + elem + " or ep: " + ep);
 304  
     }
 305  
 
 306  
 
 307  
     public void removeInclude(Object usecase, Object include) {
 308  
         try {
 309  0
             if (usecase instanceof UseCase && include instanceof Include) {
 310  0
                 ((UseCase) usecase).getInclude().remove(include);
 311  0
                 return;
 312  
             }
 313  0
         } catch (InvalidObjectException e) {
 314  0
             throw new InvalidElementException(e);
 315  0
         }
 316  0
         throw new IllegalArgumentException("usecase: " + usecase
 317  
                 + " or include: " + include);
 318  
     }
 319  
 
 320  
 
 321  
     public void addExtend(Object elem, Object extend) {
 322  0
         if (elem instanceof UseCase && extend instanceof Extend) {
 323  0
             ((UseCase) elem).getExtend().add((Extend) extend);
 324  0
         } else if (elem instanceof ExtensionPoint && extend instanceof Extend) {
 325  0
             ((Extend) extend).getExtensionPoint().add((ExtensionPoint) elem);
 326  
         } else {
 327  0
             throw new IllegalArgumentException("elem: " + elem + " or extend: "
 328  
                     + extend);
 329  
         }
 330  0
     }
 331  
 
 332  
 
 333  
     public void addExtensionPoint(Object handle, Object extensionPoint) {
 334  0
         if (extensionPoint instanceof ExtensionPoint) {
 335  0
             if (handle instanceof UseCase) {
 336  0
                 ((UseCase) handle).getExtensionPoint().add(
 337  
                         (ExtensionPoint) extensionPoint);
 338  0
                 return;
 339  
             }
 340  0
             if (handle instanceof Extend) {
 341  0
                 ((Extend) handle).getExtensionPoint().add(
 342  
                         (ExtensionPoint) extensionPoint);
 343  0
                 return;
 344  
             }
 345  
         }
 346  0
         throw new IllegalArgumentException("handle: " + handle
 347  
                 + " or extensionPoint: " + extensionPoint);
 348  
     }
 349  
 
 350  
 
 351  
     public void addExtensionPoint(Object handle, int position, 
 352  
             Object extensionPoint) {
 353  0
         if (extensionPoint instanceof ExtensionPoint) {
 354  0
             if (handle instanceof Extend) {
 355  0
                 ((Extend) handle).getExtensionPoint().add(position, 
 356  
                         (ExtensionPoint) extensionPoint);
 357  0
                 return;
 358  
             }
 359  
         }
 360  0
         throw new IllegalArgumentException("handle: " + handle
 361  
                 + " or extensionPoint: " + extensionPoint);
 362  
     }
 363  
 
 364  
 
 365  
     public void addInclude(Object usecase, Object include) {
 366  0
         if (usecase instanceof UseCase && include instanceof Include) {
 367  0
             ((UseCase) usecase).getInclude().add((Include) include);
 368  0
             return;
 369  
         }
 370  
 
 371  0
         throw new IllegalArgumentException("usecase: " + usecase
 372  
                 + " or include: " + include);
 373  
     }
 374  
 
 375  
 
 376  
     public void setAddition(Object handle, Object useCase) {
 377  0
         if (!(useCase instanceof UseCase)) {
 378  0
             throw new IllegalArgumentException("A UseCase was expected ["
 379  
                     + useCase + "]");
 380  
         }
 381  
 
 382  0
         if (handle instanceof Include) {
 383  
             try {
 384  0
                 ((Include) handle).setAddition((UseCase) useCase);
 385  0
                 return;
 386  0
             } catch (InvalidObjectException e) {
 387  0
                 throw new IllegalArgumentException(
 388  
                         "Operation on a removed object [" + handle + " or "
 389  
                                 + useCase + "]");
 390  
             }
 391  
         }
 392  0
         throw new IllegalArgumentException("handle: " + handle);
 393  
     }
 394  
 
 395  
 
 396  
     public void setCondition(Object handle, Object booleanExpression) {
 397  0
         if (handle instanceof Extend
 398  
                 && (booleanExpression == null
 399  
                         || booleanExpression instanceof BooleanExpression)) {
 400  0
             Expression oldExp = ((Extend) handle).getCondition();
 401  0
             if (!equal(oldExp, (Expression) booleanExpression)) {
 402  0
                 ((Extend) handle)
 403  
                         .setCondition((BooleanExpression) booleanExpression);
 404  0
                 if (oldExp != null) {
 405  0
                     Model.getUmlFactory().delete(oldExp);
 406  
                 }
 407  
             }
 408  0
             return;
 409  
         }
 410  0
         throw new IllegalArgumentException("handle: " + handle
 411  
                 + " or booleanExpression: " + booleanExpression);
 412  
     }
 413  
 
 414  
     private boolean equal(Expression expr1, Expression expr2) {
 415  0
         if (expr1 == null) {
 416  0
             if (expr2 == null) {
 417  0
                 return true;
 418  
             } else {
 419  0
                 return false;
 420  
             }
 421  
         } else {
 422  0
             return expr1.equals(expr2);
 423  
         }
 424  
     }
 425  
     
 426  
     
 427  
     public void setExtension(Object handle, Object useCase) {
 428  0
         if (!(useCase instanceof UseCase)) {
 429  0
             throw new IllegalArgumentException("A use case must be supplied");
 430  
         }
 431  
 
 432  0
         if (handle instanceof Extend
 433  
                 && (useCase instanceof UseCase)) {
 434  
             try {
 435  0
                 ((Extend) handle).setExtension((UseCase) useCase);
 436  0
                 return;
 437  0
             } catch (InvalidObjectException e) {
 438  0
                 throw new IllegalStateException(
 439  
                         "Operation on a removed object [" + handle + " or "
 440  
                                 + useCase + "]");
 441  
             }
 442  
         }
 443  0
         throw new IllegalArgumentException(
 444  
                 "handle: " + handle + " or ext: "
 445  
                 + useCase);
 446  
     }
 447  
 
 448  
 
 449  
     public void setExtensionPoints(Object handle, Collection extensionPoints) {
 450  0
         if (handle instanceof UseCase || handle instanceof Extend) {
 451  
             // TODO: This should use a minimal update strategy instead of
 452  
             // removing all and then adding all - tfm - 20070806
 453  0
             Collection<ExtensionPoint> eps = 
 454  
                 Model.getFacade().getExtensionPoints(handle);
 455  0
             if (!eps.isEmpty()) {
 456  0
                 Collection<ExtensionPoint> extPts = 
 457  
                     new ArrayList<ExtensionPoint>(eps);
 458  0
                 for (ExtensionPoint ep : extPts) {
 459  0
                     removeExtensionPoint(handle, ep);
 460  
                 }
 461  
             }
 462  0
             for (Object ep : extensionPoints) {
 463  0
                 addExtensionPoint(handle, ep);
 464  
             }
 465  0
             return;
 466  
         }
 467  0
         throw new IllegalArgumentException("handle: " + handle
 468  
                 + " or extensionPoints: " + extensionPoints);
 469  
     }
 470  
 
 471  
 
 472  
     public void setIncludes(Object handle, Collection includes) {
 473  0
         if (handle instanceof UseCase) {
 474  0
             Collection<Include> inc = Model.getFacade().getIncludes(handle);
 475  0
             if (!inc.isEmpty()) {
 476  0
                 Collection<Include> in = new ArrayList<Include>(inc);
 477  0
                 for (Include i : in) {
 478  0
                     removeInclude(handle, i);
 479  
                 }
 480  
             }
 481  0
             for (Include i : (Collection<Include>) includes) {
 482  0
                 addInclude(handle, i);
 483  
             }
 484  0
             return;
 485  
         }
 486  0
         throw new IllegalArgumentException("handle: " + handle
 487  
                 + " or includes: " + includes);
 488  
     }
 489  
 
 490  
 
 491  
     public void setLocation(Object handle, String loc) {
 492  0
         if (handle instanceof ExtensionPoint) {
 493  0
             ((ExtensionPoint) handle).setLocation(loc);
 494  0
             return;
 495  
         }
 496  0
         throw new IllegalArgumentException("handle: " + handle);
 497  
     }
 498  
 
 499  
 
 500  
     public void setUseCase(Object elem, Object usecase) {
 501  0
         if (elem instanceof ExtensionPoint
 502  
                 && (usecase instanceof UseCase || usecase == null)) {
 503  0
             ((ExtensionPoint) elem).setUseCase((UseCase) usecase);
 504  0
             return;
 505  
         }
 506  0
         throw new IllegalArgumentException("elem: " + elem + " or usecase: "
 507  
                 + usecase);
 508  
     }
 509  
 }