001: package org.andromda.metafacades.emf.uml2;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005: import java.util.LinkedHashSet;
006: import java.util.LinkedList;
007: import java.util.Set;
008:
009: import org.andromda.metafacades.uml.PseudostateFacade;
010: import org.apache.commons.collections.Predicate;
011: import org.eclipse.uml2.Element;
012: import org.eclipse.uml2.FinalState;
013: import org.eclipse.uml2.Pseudostate;
014: import org.eclipse.uml2.PseudostateKind;
015: import org.eclipse.uml2.Region;
016: import org.eclipse.uml2.State;
017:
018: /**
019: * MetafacadeLogic implementation for
020: * org.andromda.metafacades.uml.StateMachineFacade.
021: *
022: * @see org.andromda.metafacades.uml.StateMachineFacade
023: */
024: public class StateMachineFacadeLogicImpl extends
025: StateMachineFacadeLogic {
026: public StateMachineFacadeLogicImpl(
027: final org.eclipse.uml2.StateMachine metaObject,
028: final String context) {
029: super (metaObject, context);
030: }
031:
032: /**
033: * @see org.andromda.metafacades.uml.StateMachineFacade#getContextElement()
034: */
035: protected java.lang.Object handleGetContextElement() {
036: Element context = this .metaObject.getContext();
037: if (context == null) {
038: context = this .metaObject.getOwner();
039: }
040: return context;
041: }
042:
043: /**
044: * @see org.andromda.metafacades.uml.StateMachineFacade#getFinalStates()
045: */
046: protected java.util.Collection handleGetFinalStates() {
047: final Predicate filter = new Predicate() {
048: public boolean evaluate(Object object) {
049: return object instanceof FinalState;
050: }
051: };
052: return this .getSubvertices(filter);
053: }
054:
055: /**
056: * @see org.andromda.metafacades.uml.StateMachineFacade#getStates()
057: */
058: protected java.util.Collection handleGetStates() {
059: final Predicate filter = new Predicate() {
060: public boolean evaluate(Object object) {
061: return object instanceof State;
062: }
063: };
064: return this .getSubvertices(filter);
065: }
066:
067: /**
068: * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialTransition()
069: */
070: protected java.lang.Object handleGetInitialTransition() {
071: Object transition = null;
072:
073: final PseudostateFacade initialState = this .getInitialState();
074: if (initialState != null) {
075: final Collection transitions = initialState.getOutgoing();
076: if (!transitions.isEmpty()) {
077: transition = transitions.iterator().next();
078: }
079: }
080:
081: return transition;
082: }
083:
084: /**
085: * @see org.andromda.metafacades.uml.StateMachineFacade#getTransitions()
086: */
087: protected java.util.Collection handleGetTransitions() {
088: Collection regions = this .metaObject.getRegions();
089: Collection transitions = new LinkedList();
090: for (Iterator it = regions.iterator(); it.hasNext();) {
091: Region region = (Region) it.next();
092: transitions.addAll(region.getTransitions());
093: }
094: return transitions;
095: }
096:
097: /**
098: * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialStates()
099: */
100: protected java.util.Collection handleGetInitialStates() {
101: final Predicate filter = new Predicate() {
102: public boolean evaluate(Object object) {
103: return (object instanceof Pseudostate)
104: && (PseudostateKind.INITIAL == ((Pseudostate) object)
105: .getKind().getValue());
106: }
107: };
108: return this .getSubvertices(filter);
109: }
110:
111: /**
112: * @see org.andromda.metafacades.uml.StateMachineFacade#getInitialState()
113: */
114: protected java.lang.Object handleGetInitialState() {
115: Object initialState = null;
116:
117: final Collection initialStates = this .getInitialStates();
118: if (!initialStates.isEmpty()) {
119: initialState = initialStates.iterator().next();
120: }
121:
122: return initialState;
123: }
124:
125: /**
126: * @see org.andromda.metafacades.uml.StateMachineFacade#getPseudostates()
127: */
128: protected java.util.Collection handleGetPseudostates() {
129: final Predicate filter = new Predicate() {
130: public boolean evaluate(Object object) {
131: return (object instanceof Pseudostate);
132: }
133: };
134: return this .getSubvertices(filter);
135: }
136:
137: protected Collection getSubvertices(final Predicate collectionFilter) {
138: Collection regions = this .metaObject.getRegions();
139: Collection subvertices = new LinkedList();
140: for (Iterator it = regions.iterator(); it.hasNext();) {
141: Region region = (Region) it.next();
142: subvertices.addAll(region.getSubvertices());
143: }
144: return this .filter(subvertices, collectionFilter);
145: }
146:
147: private Collection filter(final Collection collection,
148: final Predicate collectionFilter) {
149: final Set filteredCollection = new LinkedHashSet();
150: for (final Iterator iterator = collection.iterator(); iterator
151: .hasNext();) {
152: final Object object = iterator.next();
153: if (collectionFilter.evaluate(object)) {
154: filteredCollection.add(object);
155: }
156: }
157: return filteredCollection;
158: }
159:
160: public Object getValidationOwner() {
161: return this.getPackage();
162: }
163: }
|