001package org.andromda.metafacades.uml14;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.LinkedHashSet;
006import java.util.Set;
007import org.andromda.metafacades.uml.PseudostateFacade;
008import org.andromda.metafacades.uml.TransitionFacade;
009import org.apache.commons.collections.Predicate;
010import org.omg.uml.behavioralelements.statemachines.CompositeState;
011import org.omg.uml.behavioralelements.statemachines.FinalState;
012import org.omg.uml.behavioralelements.statemachines.Pseudostate;
013import org.omg.uml.behavioralelements.statemachines.State;
014import org.omg.uml.behavioralelements.statemachines.StateMachine;
015import org.omg.uml.foundation.core.ModelElement;
016import org.omg.uml.foundation.datatypes.PseudostateKindEnum;
017
018/**
019 * MetafacadeLogic implementation for org.andromda.metafacades.uml.StateMachineFacade.
020 *
021 * @see org.andromda.metafacades.uml.StateMachineFacade
022 * @author Bob Fields
023 */
024public class StateMachineFacadeLogicImpl
025    extends StateMachineFacadeLogic
026{
027    private static final long serialVersionUID = -7445143171579144836L;
028
029    /**
030     * @param metaObject
031     * @param context
032     */
033    public StateMachineFacadeLogicImpl (StateMachine metaObject, String context)
034    {
035        super (metaObject, context);
036    }
037
038    /**
039     * @see org.andromda.metafacades.uml.StateMachineFacade#getContextElement()
040     */
041    @Override
042    protected ModelElement handleGetContextElement()
043    {
044        return metaObject.getContext();
045    }
046
047    /**
048     * @see org.andromda.metafacades.uml.StateMachineFacade#getFinalStates()
049     */
050    @Override
051    protected Collection handleGetFinalStates()
052    {
053        final Predicate filter =
054            new Predicate()
055            {
056                public boolean evaluate(Object object)
057                {
058                    return object instanceof FinalState;
059                }
060            };
061        return getSubvertices(filter);
062    }
063
064    /**
065     * @see org.andromda.metafacades.uml.StateMachineFacade#getStates()
066     */
067    @Override
068    protected Collection handleGetStates()
069    {
070        final Predicate filter =
071            new Predicate()
072            {
073                public boolean evaluate(Object object)
074                {
075                    return object instanceof State;
076                }
077            };
078        return getSubvertices(filter);
079    }
080
081    /**
082     * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialTransition()
083     */
084    @Override
085    protected TransitionFacade handleGetInitialTransition()
086    {
087        TransitionFacade transition = null;
088
089        final PseudostateFacade initialState = getInitialState();
090        if (initialState != null)
091        {
092            final Collection<TransitionFacade> transitions = initialState.getOutgoings();
093            if (!transitions.isEmpty())
094            {
095                transition = transitions.iterator().next();
096            }
097        }
098
099        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}