Coverage Report - org.argouml.model.mdr.StateMachinesHelperMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StateMachinesHelperMDRImpl
4%
14/323
2%
7/270
6.767
 
 1  
 /* $Id: StateMachinesHelperMDRImpl.java 18766 2010-09-18 23:18:13Z tfmorris $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009-2010 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  
  *    mvw
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 2005-2007 The Regents of the University of California. All
 18  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 19  
 // software and its documentation without fee, and without a written
 20  
 // agreement is hereby granted, provided that the above copyright notice
 21  
 // and this paragraph appear in all copies. This software program and
 22  
 // documentation are copyrighted by The Regents of the University of
 23  
 // California. The software program and documentation are supplied "AS
 24  
 // IS", without any accompanying services from The Regents. The Regents
 25  
 // does not warrant that the operation of the program will be
 26  
 // uninterrupted or error-free. The end-user understands that the program
 27  
 // was developed for research purposes and is advised not to rely
 28  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 29  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 30  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 31  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 32  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 33  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 34  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 36  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 37  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 38  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 39  
 
 40  
 package org.argouml.model.mdr;
 41  
 
 42  
 import java.util.ArrayList;
 43  
 import java.util.Collection;
 44  
 import java.util.Iterator;
 45  
 import java.util.List;
 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.StateMachinesHelper;
 52  
 import org.omg.uml.behavioralelements.commonbehavior.Action;
 53  
 import org.omg.uml.behavioralelements.commonbehavior.Argument;
 54  
 import org.omg.uml.behavioralelements.statemachines.ChangeEvent;
 55  
 import org.omg.uml.behavioralelements.statemachines.CompositeState;
 56  
 import org.omg.uml.behavioralelements.statemachines.Event;
 57  
 import org.omg.uml.behavioralelements.statemachines.Guard;
 58  
 import org.omg.uml.behavioralelements.statemachines.State;
 59  
 import org.omg.uml.behavioralelements.statemachines.StateMachine;
 60  
 import org.omg.uml.behavioralelements.statemachines.StateVertex;
 61  
 import org.omg.uml.behavioralelements.statemachines.StubState;
 62  
 import org.omg.uml.behavioralelements.statemachines.SubmachineState;
 63  
 import org.omg.uml.behavioralelements.statemachines.SynchState;
 64  
 import org.omg.uml.behavioralelements.statemachines.TimeEvent;
 65  
 import org.omg.uml.behavioralelements.statemachines.Transition;
 66  
 import org.omg.uml.foundation.core.BehavioralFeature;
 67  
 import org.omg.uml.foundation.core.Classifier;
 68  
 import org.omg.uml.foundation.core.Feature;
 69  
 import org.omg.uml.foundation.core.ModelElement;
 70  
 import org.omg.uml.foundation.core.Namespace;
 71  
 import org.omg.uml.foundation.core.Operation;
 72  
 import org.omg.uml.foundation.datatypes.BooleanExpression;
 73  
 import org.omg.uml.foundation.datatypes.Expression;
 74  
 import org.omg.uml.foundation.datatypes.TimeExpression;
 75  
 import org.omg.uml.modelmanagement.UmlPackage;
 76  
 
 77  
 /**
 78  
  * The State Machines Helper Implementation for MDR.
 79  
  *
 80  
  * @since ARGO0.19.5
 81  
  * @author Ludovic Maître
 82  
  * @author Tom Morris
 83  
  */
 84  573
 class StateMachinesHelperMDRImpl implements StateMachinesHelper {
 85  
 
 86  
     private MDRModelImplementation modelImpl;
 87  
 
 88  
     /**
 89  
      * Constructor.
 90  
      *
 91  
      * @param impl The ModelImplementation
 92  
      */
 93  
     public StateMachinesHelperMDRImpl(MDRModelImplementation impl) {
 94  900
         super();
 95  900
         this.modelImpl = impl;
 96  900
     }
 97  
 
 98  
 
 99  
     public Object getSource(Object trans) {
 100  
         try {
 101  0
             if (trans instanceof Transition) {
 102  0
                 return ((Transition) trans).getSource();
 103  
             }
 104  0
         } catch (InvalidObjectException e) {
 105  0
             throw new InvalidElementException(e);
 106  0
         }
 107  0
         throw new IllegalArgumentException("bad argument to "
 108  
                 + "getSource() - " + trans);
 109  
     }
 110  
 
 111  
 
 112  
     public Object getDestination(Object trans) {
 113  
         try {
 114  0
             if (trans instanceof Transition) {
 115  0
                 return ((Transition) trans).getTarget();
 116  
             }
 117  0
         } catch (InvalidObjectException e) {
 118  0
             throw new InvalidElementException(e);
 119  0
         }
 120  0
         throw new IllegalArgumentException("bad argument to "
 121  
                 + "getDestination() - " + trans);
 122  
     }
 123  
 
 124  
 
 125  
     public Object getStateMachine(Object handle) {
 126  570
         if (handle == null) {
 127  0
             throw new IllegalArgumentException("bad argument to "
 128  
                     + "getStateMachine() - " + handle);
 129  
         }
 130  
         try {
 131  570
             Object container =
 132  
                 modelImpl.getFacade().getModelElementContainer(handle);
 133  570
             while (container != null) {
 134  570
                 if (Model.getFacade().isAStateMachine(container)) {
 135  570
                     return container;
 136  
                 }
 137  0
                 container =
 138  
                     modelImpl.getFacade()
 139  
                         .getModelElementContainer(container);
 140  
             }
 141  
             /* In this case, either the container was not set,
 142  
              * or it was not contained in a statemachine.
 143  
              */
 144  0
             return null;
 145  
 
 146  0
         } catch (InvalidObjectException e) {
 147  0
             throw new InvalidElementException(e);
 148  
         }
 149  
     }
 150  
 
 151  
 
 152  
     public void setEventAsTrigger(Object transition, Object event) {
 153  0
         if (!(transition instanceof Transition)) {
 154  0
             throw new IllegalArgumentException("Transition either null or not "
 155  
                     + "an instance of MTransition");
 156  
         }
 157  0
         if (event != null && !(event instanceof Event)) {
 158  0
             throw new IllegalArgumentException("Event not an "
 159  
                     + "instance of MEvent");
 160  
         }
 161  0
         ((Transition) transition).setTrigger((Event) event);
 162  0
     }
 163  
 
 164  
 
 165  
     public boolean isAddingStatemachineAllowed(Object context) {
 166  39
         return (context instanceof BehavioralFeature
 167  
                 || context instanceof Classifier);
 168  
     }
 169  
 
 170  
 
 171  
     public boolean isTopState(Object o) {
 172  
         try {
 173  1026
             if (o instanceof CompositeState) {
 174  0
                 CompositeState cs = (CompositeState) o;
 175  0
                 return (cs.getContainer() == null);
 176  
             }
 177  1026
             return false;
 178  0
         } catch (InvalidObjectException e) {
 179  0
             throw new InvalidElementException(e);
 180  
         }
 181  
     }
 182  
 
 183  
 
 184  
     public Collection<StateMachine> getAllPossibleStatemachines(Object model,
 185  
             Object oSubmachineState) {
 186  
         try {
 187  0
             if (oSubmachineState instanceof SubmachineState) {
 188  0
                 Collection<StateMachine> statemachines =
 189  
                     Model.getModelManagementHelper()
 190  
                         .getAllModelElementsOfKind(model, StateMachine.class);
 191  0
                 statemachines.remove(getStateMachine(oSubmachineState));
 192  0
                 return statemachines;
 193  
             }
 194  0
         } catch (InvalidObjectException e) {
 195  0
             throw new InvalidElementException(e);
 196  0
         }
 197  0
         throw new IllegalArgumentException(
 198  
                 "Argument must be a SubmachineState");
 199  
     }
 200  
 
 201  
 
 202  
     // TODO: getAllPossibleSubvertices and getAllSubStates are duplicates - tfm
 203  
     public Collection<StateVertex> getAllPossibleSubvertices(Object oState) {
 204  0
         Collection<StateVertex> result = new ArrayList<StateVertex>();
 205  
         try {
 206  0
             if (oState instanceof CompositeState) {
 207  
                 for (StateVertex vertex 
 208  0
                         : ((CompositeState) oState).getSubvertex()) {
 209  0
                     result.add(vertex);
 210  0
                     result.addAll(getAllPossibleSubvertices(vertex));
 211  
                 }
 212  
             }
 213  0
         } catch (InvalidObjectException e) {
 214  0
             throw new InvalidElementException(e);
 215  0
         }
 216  0
         return result;
 217  
     }
 218  
 
 219  
 
 220  
     public void setStatemachineAsSubmachine(Object oSubmachineState,
 221  
             Object oStatemachine) {
 222  0
         if (oSubmachineState instanceof SubmachineState
 223  
                 && oStatemachine instanceof StateMachine) {
 224  0
             SubmachineState mss = (SubmachineState) oSubmachineState;
 225  0
             mss.setSubmachine((StateMachine) oStatemachine);
 226  0
             return;
 227  
         }
 228  0
         throw new IllegalArgumentException("oSubmachineState: "
 229  
                 + oSubmachineState + ",oStatemachine: " + oStatemachine);
 230  
     }
 231  
 
 232  
 
 233  
     public State getTop(Object sm) {
 234  573
         if (!(sm instanceof StateMachine)) {
 235  0
             throw new IllegalArgumentException();
 236  
         }
 237  
 
 238  
         try  {
 239  573
             return ((StateMachine) sm).getTop();
 240  0
         } catch (InvalidObjectException e) {
 241  0
             throw new InvalidElementException(e);
 242  
         }
 243  
     }
 244  
 
 245  
 
 246  
     public Collection<StateVertex> getOutgoingStates(Object ostatevertex) {
 247  
         try {
 248  0
             if (ostatevertex instanceof StateVertex) {
 249  0
                 StateVertex statevertex = (StateVertex) ostatevertex;
 250  0
                 Collection<StateVertex> result = new ArrayList<StateVertex>();
 251  0
                 for (Transition transition : statevertex.getOutgoing()) {
 252  0
                     result.add(transition.getTarget());
 253  
                 }
 254  0
                 return result;
 255  
             }
 256  0
         } catch (InvalidObjectException e) {
 257  0
             throw new InvalidElementException(e);
 258  0
         }
 259  0
         throw new IllegalArgumentException(
 260  
                 "Argument must be a StateVertex");
 261  
     }
 262  
 
 263  
 
 264  
     public Object findOperationByName(Object trans, String opname) {
 265  0
         if (!(trans instanceof Transition)) {
 266  0
             throw new IllegalArgumentException();
 267  
         }
 268  
         try {
 269  0
             Object sm = getStateMachine(trans);
 270  0
             Object context = Model.getFacade().getContext(sm);
 271  0
             Classifier classifier = null;
 272  0
             if (context instanceof Classifier) {
 273  0
                 classifier = (Classifier) context;
 274  
             }
 275  0
             if (context instanceof BehavioralFeature) {
 276  0
                 classifier = ((BehavioralFeature) context).getOwner();
 277  
             }
 278  0
             if (classifier != null) {
 279  0
                 List<Feature> features = classifier.getFeature();
 280  0
                 for (Feature f : features) {
 281  0
                     if (f instanceof Operation) {
 282  0
                         String on = f.getName();
 283  0
                         if (on.equals(opname)) {
 284  0
                             return f;
 285  
                         }   
 286  0
                     }
 287  
                 }
 288  
             }
 289  0
             Namespace pack = null;
 290  0
             if (context instanceof UmlPackage) {
 291  
                 /* according WFR: in case of ActivityGraph only. */
 292  0
                 pack = (Namespace) context;
 293  
             } else {
 294  0
                 if (classifier != null) {
 295  0
                     Namespace parent = null;
 296  0
                     parent = classifier.getNamespace();
 297  0
                     while (parent instanceof Classifier) {
 298  0
                         if (parent.getNamespace() == null) {
 299  0
                             break;
 300  
                         }
 301  0
                         parent = parent.getNamespace();
 302  
                     }
 303  0
                     if (parent != null) {
 304  0
                         pack = parent;
 305  
                     }
 306  
                 }
 307  
             }
 308  0
             if (pack != null) {
 309  0
                 Collection<ModelElement> mes = pack.getOwnedElement();
 310  0
                 for (ModelElement me : mes) {
 311  0
                     if (me instanceof Classifier) {
 312  0
                         Classifier classifier2 = (Classifier) me;
 313  0
                         List<Feature> features = classifier2.getFeature();
 314  0
                         for (Feature f : features) {
 315  0
                             if (f instanceof Operation) {
 316  0
                                 String on = f.getName();
 317  0
                                 if (on.equals(opname)) {
 318  0
                                     return f;
 319  
                                 }   
 320  0
                             }
 321  
                         }
 322  0
                     }
 323  
                 }
 324  
             }
 325  0
         } catch (InvalidObjectException e) {
 326  0
             throw new InvalidElementException(e);
 327  0
         }
 328  0
         return null;
 329  
     }
 330  
 
 331  
 
 332  
     // TODO: getAllPossibleSubvertices and getAllSubStates are duplicates - tfm
 333  
     public Collection<StateVertex> getAllSubStates(Object compState) {
 334  
         try {
 335  0
             if (compState instanceof CompositeState) {
 336  0
                 Collection<StateVertex> result = new ArrayList<StateVertex>();
 337  0
                 for (Object subState : Model.getFacade().getSubvertices(
 338  
                         compState)) {
 339  0
                     if (subState instanceof CompositeState) {
 340  0
                         result.addAll(getAllSubStates(subState));
 341  
                     }
 342  0
                     result.add((StateVertex) subState);
 343  
                 }
 344  0
                 return result;
 345  
             }
 346  0
         } catch (InvalidObjectException e) {
 347  0
             throw new InvalidElementException(e);
 348  0
         }
 349  0
         throw new IllegalArgumentException(
 350  
                 "Argument is not a composite state");
 351  
     }
 352  
 
 353  
 
 354  
     public void removeSubvertex(Object handle, Object subvertex) {
 355  
         try {
 356  0
             if (handle instanceof CompositeState
 357  
                     && subvertex instanceof StateVertex) {
 358  0
                 ((CompositeState) handle).getSubvertex().remove(subvertex);
 359  0
                 return;
 360  
             }
 361  0
         } catch (InvalidObjectException e) {
 362  0
             throw new InvalidElementException(e);
 363  0
         }
 364  0
         throw new IllegalArgumentException("handle: " + handle
 365  
                 + " or subvertex: " + subvertex);
 366  
     }
 367  
 
 368  
 
 369  
     public void addSubvertex(Object handle, Object subvertex) {
 370  0
         if (handle instanceof CompositeState
 371  
                 && subvertex instanceof StateVertex) {
 372  0
             ((StateVertex) subvertex).setContainer((CompositeState) handle);
 373  0
             return;
 374  
         }
 375  0
         throw new IllegalArgumentException("handle: " + handle
 376  
                 + " or subvertex: " + subvertex);
 377  
     }
 378  
 
 379  
 
 380  
     public void setBound(Object handle, int bound) {
 381  0
         if (handle instanceof SynchState) {
 382  0
             ((SynchState) handle).setBound(bound);
 383  0
             return;
 384  
         }
 385  0
         throw new IllegalArgumentException("handle: " + handle + " or bound: "
 386  
                 + bound);
 387  
     }
 388  
 
 389  
 
 390  
     public void setConcurrent(Object handle, boolean concurrent) {
 391  0
         if (handle instanceof CompositeState) {
 392  0
             ((CompositeState) handle).setConcurrent(concurrent);
 393  0
             return;
 394  
         }
 395  0
         throw new IllegalArgumentException("handle: " + handle);
 396  
     }
 397  
 
 398  
 
 399  
     public void setContainer(Object handle, Object compositeState) {
 400  0
         if (handle instanceof StateVertex
 401  
                 && (compositeState == null
 402  
                         || compositeState instanceof CompositeState)) {
 403  0
             ((StateVertex) handle).
 404  
                     setContainer((CompositeState) compositeState);
 405  0
             return;
 406  
         }
 407  0
         throw new IllegalArgumentException("handle: " + handle
 408  
                 + " or compositeState: " + compositeState);
 409  
     }
 410  
 
 411  
 
 412  
     public void setDoActivity(Object handle, Object value) {
 413  0
         if (handle instanceof State
 414  
                 && (value == null || value instanceof Action)) {
 415  0
             ((State) handle).setDoActivity((Action) value);
 416  0
             return;
 417  
         }
 418  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 419  
                 + value);
 420  
     }
 421  
 
 422  
 
 423  
     public void setEffect(Object handle, Object value) {
 424  0
         if (handle instanceof Transition
 425  
                 && (value == null || value instanceof Action)) {
 426  0
             ((Transition) handle).setEffect((Action) value);
 427  0
             return;
 428  
         }
 429  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 430  
                 + value);
 431  
     }
 432  
 
 433  
 
 434  
     public void setEntry(Object handle, Object value) {
 435  0
         if (handle instanceof State
 436  
                 && (value == null || value instanceof Action)) {
 437  0
             ((State) handle).setEntry((Action) value);
 438  0
             return;
 439  
         }
 440  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 441  
                 + value);
 442  
     }
 443  
 
 444  
 
 445  
     public void setExit(Object handle, Object value) {
 446  0
         if (handle instanceof State
 447  
                 && (value == null || value instanceof Action)) {
 448  0
             ((State) handle).setExit((Action) value);
 449  0
             return;
 450  
         }
 451  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 452  
                 + value);
 453  
     }
 454  
 
 455  
 
 456  
     public void setExpression(Object handle, Object value) {
 457  0
         if (handle instanceof Guard
 458  
                 && (value == null || value instanceof BooleanExpression)) {
 459  0
             Expression oldExp = ((Guard) handle).getExpression();
 460  0
             if (!equal(oldExp,(Expression) value)) {
 461  0
                 ((Guard) handle).setExpression((BooleanExpression) value);
 462  0
                 if (oldExp != null) {
 463  0
                     Model.getUmlFactory().delete(oldExp);
 464  
                 }
 465  
             }
 466  0
             return;
 467  
         }
 468  0
         if (handle instanceof ChangeEvent
 469  
                 && (value == null || value instanceof BooleanExpression)) {
 470  0
             ChangeEvent ce = (ChangeEvent) handle;
 471  0
             Expression oldExp = ce.getChangeExpression();
 472  0
             if (!equal(oldExp,(Expression) value)) {
 473  0
                 ce.setChangeExpression((BooleanExpression) value);
 474  0
                 if (oldExp != null) {
 475  0
                     Model.getUmlFactory().delete(oldExp);
 476  
                 }
 477  
             }
 478  0
             return;
 479  
         }
 480  0
         if (handle instanceof Argument
 481  
                 && (value == null || value instanceof Expression)) {
 482  0
             Argument arg = (Argument) handle;
 483  0
             Expression oldExp = arg.getValue();
 484  0
             if (!equal(oldExp,(Expression) value)) {
 485  0
                 arg.setValue((Expression) value);
 486  0
                 if (oldExp != null) {
 487  0
                     Model.getUmlFactory().delete(oldExp);
 488  
                 }
 489  
             }
 490  0
             return;
 491  
         }
 492  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 493  
                 + value);
 494  
     }
 495  
 
 496  
 
 497  
     private boolean equal(Expression expr1, Expression expr2) {
 498  0
         if (expr1 == null) {
 499  0
             if (expr2 == null) {
 500  0
                 return true;
 501  
             } else {
 502  0
                 return false;
 503  
             }
 504  
         } else {
 505  0
             return expr1.equals(expr2);
 506  
         }
 507  
     }
 508  
     
 509  
     
 510  
     public void setGuard(Object handle, Object guard) {
 511  0
         if (handle instanceof Transition
 512  
                 && (guard == null || guard instanceof Guard)) {
 513  0
             ((Transition) handle).setGuard((Guard) guard);
 514  0
             return;
 515  
         }
 516  0
         throw new IllegalArgumentException("handle: " + handle + " or guard: "
 517  
                 + guard);
 518  
     }
 519  
 
 520  
 
 521  
     public void setInternalTransitions(Object handle, Collection intTrans) {
 522  0
         if (handle instanceof State) {
 523  0
             Collection internalTransitions =
 524  
                 Model.getFacade().getInternalTransitions(handle);
 525  0
             if (!internalTransitions.isEmpty()) {
 526  0
                 Collection trans = new ArrayList(internalTransitions);
 527  0
                 for (Object transition : trans) {
 528  0
                     removeTransition(handle, transition);
 529  
                 }
 530  
             }
 531  0
             for (Object transition : intTrans) {
 532  0
                 addTransition(handle, transition);
 533  
             }
 534  0
             return;
 535  
         }
 536  0
         throw new IllegalArgumentException("handle: " + handle);
 537  
     }
 538  
 
 539  
     /**
 540  
      * Remove a transition.
 541  
      * @param handle The state
 542  
      * @param intTrans The internal transition to remove
 543  
      */
 544  
     private void removeTransition(Object handle, Object intTrans) {
 545  
         try {
 546  0
             if (handle instanceof State && intTrans instanceof Transition) {
 547  0
                 ((State) handle).getInternalTransition().remove(intTrans);
 548  0
                 return;
 549  
             }
 550  0
         } catch (InvalidObjectException e) {
 551  0
             throw new InvalidElementException(e);
 552  0
         }
 553  0
         throw new IllegalArgumentException("handle: " + handle
 554  
                 + " or intTrans: " + intTrans);
 555  
     }
 556  
 
 557  
     /**
 558  
      * Add a transition.
 559  
      * 
 560  
      * @param handle The state
 561  
      * @param intTrans The internal transition to add
 562  
      */
 563  
     private void addTransition(Object handle, Object intTrans) {
 564  0
         if (handle instanceof State && intTrans instanceof Transition) {
 565  0
             ((State) handle).getInternalTransition().add((Transition) intTrans);
 566  0
             return;
 567  
         }
 568  0
         throw new IllegalArgumentException("handle: " + handle
 569  
                 + " or subvertex: " + intTrans);
 570  
     }
 571  
 
 572  
 
 573  
     public void setSource(Object handle, Object state) {
 574  0
         if (handle instanceof Transition && 
 575  
                 (state == null || state instanceof StateVertex)) {
 576  0
             ((Transition) handle).setSource((StateVertex) state);
 577  0
             return;
 578  
         }
 579  0
         throw new IllegalArgumentException("handle: " + handle + " or state: "
 580  
                 + state);
 581  
     }
 582  
 
 583  
 
 584  
     public void setState(Object handle, Object element) {
 585  0
         if (handle instanceof Transition && element instanceof State) {
 586  0
             addTransition(element, handle);
 587  0
             return;
 588  
         }
 589  0
         throw new IllegalArgumentException("handle: " + handle
 590  
                 + " or element: " + element);
 591  
     }
 592  
 
 593  
 
 594  
     public void setStateMachine(Object handle, Object stm) {
 595  0
         if (handle instanceof State
 596  
                 && (stm == null || stm instanceof StateMachine)) {
 597  0
             ((State) handle).setStateMachine((StateMachine) stm);
 598  0
             return;
 599  
         }
 600  0
         if (handle instanceof Transition
 601  
                 && (stm == null || stm instanceof StateMachine)) {
 602  0
             ((Transition) handle).setStateMachine((StateMachine) stm);
 603  0
             return;
 604  
         }
 605  0
         throw new IllegalArgumentException("handle: " + handle + " or stm: "
 606  
                 + stm);
 607  
     }
 608  
 
 609  
 
 610  
     public void setSubvertices(Object handle, Collection subvertices) {
 611  0
         if (handle instanceof CompositeState) {
 612  0
             ((CompositeState) handle).getSubvertex().clear();
 613  0
             ((CompositeState) handle).getSubvertex().addAll(subvertices);
 614  0
             return;
 615  
         }
 616  0
         throw new IllegalArgumentException("handle: " + handle
 617  
                 + " or subvertices: " + subvertices);
 618  
     }
 619  
 
 620  
 
 621  
     public void setTrigger(Object handle, Object event) {
 622  0
         if (handle instanceof Transition
 623  
                 && (event == null || event instanceof Event)) {
 624  0
             ((Transition) handle).setTrigger((Event) event);
 625  0
             return;
 626  
         }
 627  0
         throw new IllegalArgumentException("handle: " + handle + " or event: "
 628  
                 + event);
 629  
     }
 630  
 
 631  
 
 632  
     public void setWhen(Object handle, Object value) {
 633  0
         if (handle instanceof TimeEvent
 634  
                 && (value == null || value instanceof TimeExpression)) {
 635  0
             Expression oldExp = ((TimeEvent) handle).getWhen();
 636  0
             if (!equal(oldExp,(Expression) value)) {
 637  0
                 ((TimeEvent) handle).setWhen((TimeExpression) value);
 638  0
                 if (oldExp != null) {
 639  0
                     Model.getUmlFactory().delete(oldExp);
 640  
                 }
 641  
             }
 642  0
             return;
 643  
         }
 644  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 645  
                 + value);
 646  
     }
 647  
 
 648  
 
 649  
     public void setChangeExpression(Object handle, Object value) {
 650  0
         if (handle instanceof ChangeEvent
 651  
                 && (value == null || value instanceof BooleanExpression)) {
 652  0
             Expression oldExp = ((ChangeEvent) handle).getChangeExpression();
 653  0
             if (!equal(oldExp, (Expression) value)) {
 654  0
                 ((ChangeEvent) handle)
 655  
                         .setChangeExpression((BooleanExpression) value);
 656  0
                 if (oldExp != null) {
 657  0
                     Model.getUmlFactory().delete(oldExp);
 658  
                 }
 659  
             }
 660  0
             return;
 661  
         }
 662  0
         throw new IllegalArgumentException("handle: " + handle + " or value: "
 663  
                 + value);
 664  
     }
 665  
 
 666  
 
 667  
     public String getPath(Object o) {
 668  
         try {
 669  0
             if (o instanceof StateVertex) {
 670  0
                 Object o1 = o;
 671  0
                 Object o2 = Model.getFacade().getContainer(o1);
 672  0
                 String path = Model.getFacade().getName(o1);
 673  0
                 while ((o2 != null) && (!Model.getFacade().isTop(o2))) {
 674  0
                     path = Model.getFacade().getName(o2) + "::" + path;
 675  0
                     o1 = o2;
 676  0
                     o2 = Model.getFacade().getContainer(o1);
 677  
                 }
 678  0
                 return path;
 679  
             }
 680  0
         } catch (InvalidObjectException e) {
 681  0
             throw new InvalidElementException(e);
 682  0
         }
 683  0
         throw new IllegalArgumentException(
 684  
                 "Argument must be a StateVertex");
 685  
     }
 686  
 
 687  
 
 688  
     public Object getStatebyName(String path, Object container) {
 689  
         try {
 690  
             // TODO: This should probably throw an exception if it gets
 691  
             // invalid arguments rather than just returning null
 692  0
             if (container != null 
 693  
                     && Model.getFacade().isACompositeState(container)
 694  
                     && path != null) {
 695  
 
 696  0
                 Iterator it = getAllPossibleSubvertices(container).iterator();
 697  0
                 int index = path.lastIndexOf("::");
 698  0
                 if (index != -1) {
 699  0
                     index += 2;
 700  
                 } else {
 701  0
                     index += 1;
 702  
                 }
 703  
                 
 704  0
                 path = path.substring(index);
 705  0
                 while (it.hasNext()) {
 706  0
                     Object o = it.next();
 707  0
                     Object oName = Model.getFacade().getName(o);
 708  0
                     if (oName != null && oName.equals(path)) {
 709  0
                         return o;
 710  
                     }
 711  0
                 }
 712  
                 
 713  
             }
 714  0
         } catch (InvalidObjectException e) {
 715  0
             throw new InvalidElementException(e);
 716  0
         }
 717  0
         return null;
 718  
     }
 719  
 
 720  
 
 721  
     public void setReferenceState(Object o, String referenced) {
 722  0
         if (o instanceof StubState) {
 723  0
             ((StubState) o).setReferenceState(referenced);
 724  0
             return;
 725  
         }
 726  0
         throw new IllegalArgumentException("handle: " + o);
 727  
     }
 728  
 
 729  
 
 730  
     public Object findNamespaceForEvent(Object trans, Object model) {
 731  
         try {
 732  0
             Object enclosing =
 733  
                 Model.getStateMachinesHelper().getStateMachine(trans);
 734  
             while ((!Model.getFacade().isAPackage(enclosing))
 735  0
                     && (enclosing != null)) {
 736  0
                 enclosing = Model.getFacade().getNamespace(enclosing);
 737  
             }
 738  0
             if (enclosing == null) {
 739  0
                 enclosing = model;
 740  
             }
 741  0
             return enclosing;
 742  0
         } catch (InvalidObjectException e) {
 743  0
             throw new InvalidElementException(e);
 744  
         }
 745  
     }
 746  
 
 747  
 
 748  
     public void addDeferrableEvent(Object state, Object deferrableEvent) {
 749  0
         if (state instanceof State && deferrableEvent instanceof Event) {
 750  0
             ((State) state).getDeferrableEvent().add((Event) deferrableEvent);
 751  0
             return;
 752  
         }
 753  0
         throw new IllegalArgumentException("handle: " + state + " or evt: "
 754  
                 + deferrableEvent);
 755  
     }
 756  
 
 757  
     
 758  
     public void removeDeferrableEvent(Object state, Object deferrableEvent) {
 759  
         try {
 760  0
             if (state instanceof State && deferrableEvent instanceof Event) {
 761  0
                 ((State) state).getDeferrableEvent().remove(deferrableEvent);
 762  0
                 return;
 763  
             }
 764  0
         } catch (InvalidObjectException e) {
 765  0
             throw new InvalidElementException(e);
 766  0
         }
 767  0
         throw new IllegalArgumentException("handle: " + state + " or evt: "
 768  
                 + deferrableEvent);
 769  
     }
 770  
 
 771  
 
 772  
     public void setContext(Object statemachine, Object modelElement) {
 773  0
         if (statemachine instanceof StateMachine
 774  
                 && modelElement instanceof ModelElement) {
 775  0
             ((StateMachine) statemachine)
 776  
                     .setContext((ModelElement) modelElement);
 777  0
             return;
 778  
         }
 779  0
         throw new IllegalArgumentException("handle: " + statemachine
 780  
                 + " or me: " + modelElement);
 781  
     }
 782  
 }