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