View Javadoc
1   package org.andromda.metafacades.uml14;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import java.util.LinkedHashSet;
6   import java.util.Set;
7   import org.andromda.metafacades.uml.PseudostateFacade;
8   import org.andromda.metafacades.uml.TransitionFacade;
9   import org.apache.commons.collections.Predicate;
10  import org.omg.uml.behavioralelements.statemachines.CompositeState;
11  import org.omg.uml.behavioralelements.statemachines.FinalState;
12  import org.omg.uml.behavioralelements.statemachines.Pseudostate;
13  import org.omg.uml.behavioralelements.statemachines.State;
14  import org.omg.uml.behavioralelements.statemachines.StateMachine;
15  import org.omg.uml.foundation.core.ModelElement;
16  import org.omg.uml.foundation.datatypes.PseudostateKindEnum;
17  
18  /**
19   * MetafacadeLogic implementation for org.andromda.metafacades.uml.StateMachineFacade.
20   *
21   * @see org.andromda.metafacades.uml.StateMachineFacade
22   * @author Bob Fields
23   */
24  public class StateMachineFacadeLogicImpl
25      extends StateMachineFacadeLogic
26  {
27      private static final long serialVersionUID = -7445143171579144836L;
28  
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public StateMachineFacadeLogicImpl (StateMachine metaObject, String context)
34      {
35          super (metaObject, context);
36      }
37  
38      /**
39       * @see org.andromda.metafacades.uml.StateMachineFacade#getContextElement()
40       */
41      @Override
42      protected ModelElement handleGetContextElement()
43      {
44          return metaObject.getContext();
45      }
46  
47      /**
48       * @see org.andromda.metafacades.uml.StateMachineFacade#getFinalStates()
49       */
50      @Override
51      protected Collection handleGetFinalStates()
52      {
53          final Predicate filter =
54              new Predicate()
55              {
56                  public boolean evaluate(Object object)
57                  {
58                      return object instanceof FinalState;
59                  }
60              };
61          return getSubvertices(filter);
62      }
63  
64      /**
65       * @see org.andromda.metafacades.uml.StateMachineFacade#getStates()
66       */
67      @Override
68      protected Collection handleGetStates()
69      {
70          final Predicate filter =
71              new Predicate()
72              {
73                  public boolean evaluate(Object object)
74                  {
75                      return object instanceof State;
76                  }
77              };
78          return getSubvertices(filter);
79      }
80  
81      /**
82       * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialTransition()
83       */
84      @Override
85      protected TransitionFacade handleGetInitialTransition()
86      {
87          TransitionFacade transition = null;
88  
89          final PseudostateFacade initialState = getInitialState();
90          if (initialState != null)
91          {
92              final Collection<TransitionFacade> transitions = initialState.getOutgoings();
93              if (!transitions.isEmpty())
94              {
95                  transition = transitions.iterator().next();
96              }
97          }
98  
99          return transition;
100     }
101 
102     /**
103      * @see org.andromda.metafacades.uml.StateMachineFacade#getTransitions()
104      */
105     @Override
106     protected Collection handleGetTransitions()
107     {
108         return metaObject.getTransitions();
109     }
110 
111     /**
112      * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialStates()
113      */
114     @Override
115     protected Collection handleGetInitialStates()
116     {
117         final Predicate filter =
118             new Predicate()
119             {
120                 public boolean evaluate(Object object)
121                 {
122                     return (object instanceof Pseudostate) &&
123                     (PseudostateKindEnum.PK_INITIAL.equals(((Pseudostate)object).getKind()));
124                 }
125             };
126         return getSubvertices(filter);
127     }
128 
129     /**
130      * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialState()
131      */
132     @Override
133     protected PseudostateFacade handleGetInitialState()
134     {
135         PseudostateFacade initialState = null;
136 
137         final Collection<PseudostateFacade> initialStates = getInitialStates();
138         if (!initialStates.isEmpty())
139         {
140             initialState = initialStates.iterator().next();
141         }
142 
143         return initialState;
144     }
145 
146     /**
147      * @see org.andromda.metafacades.uml.StateMachineFacade#getPseudostates()
148      */
149     @Override
150     protected Collection handleGetPseudostates()
151     {
152         final Predicate filter =
153             new Predicate()
154             {
155                 public boolean evaluate(Object object)
156                 {
157                     return (object instanceof Pseudostate);
158                 }
159             };
160         return getSubvertices(filter);
161     }
162 
163     /**
164      * @param collectionFilter
165      * @return subvertices
166      */
167     protected Collection getSubvertices(Predicate collectionFilter)
168     {
169         final CompositeState compositeState = (CompositeState)metaObject.getTop();
170         return filter(compositeState.getSubvertex(), collectionFilter);
171     }
172 
173     private Collection filter(
174         Collection collection,
175         Predicate collectionFilter)
176     {
177         final Set filteredCollection = new LinkedHashSet();
178         for (final Iterator iterator = collection.iterator(); iterator.hasNext();)
179         {
180             final Object object = iterator.next();
181             if (collectionFilter.evaluate(object))
182             {
183                 filteredCollection.add(object);
184             }
185         }
186         return filteredCollection;
187     }
188 
189     /**
190      * @return getPackage()
191      */
192     public Object handleGetValidationOwner()
193     {
194         return getPackage();
195     }
196 }