001    /*--------------------------------------------------------------------------+
002    $Id: StateflowState.java 26285 2010-02-18 11:22:54Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.simulink.model.stateflow;
019    
020    import edu.tum.cs.commons.assertion.CCSMPre;
021    import edu.tum.cs.commons.collections.CollectionUtils;
022    import edu.tum.cs.commons.collections.IdentityHashSet;
023    import edu.tum.cs.commons.collections.UnmodifiableSet;
024    import edu.tum.cs.simulink.model.SimulinkConstants;
025    
026    /**
027     * This class represents Stateflow states.
028     * 
029     * @author deissenb
030     * @author $Author: juergens $
031     * @version $Rev: 26285 $
032     * @levd.rating GREEN Hash: 14862BB26963EB0EF15900847B6EE878
033     */
034    public class StateflowState extends StateflowNodeBase implements
035                    IStateflowNodeContainer<IStateflowNodeContainer<?>> {
036    
037            /** Set of child states. */
038            private final IdentityHashSet<StateflowNodeBase> nodes = new IdentityHashSet<StateflowNodeBase>();
039    
040            /** Create state. */
041            public StateflowState() {
042                    super();
043            }
044    
045            /** Create new state from existing one (for deep cloning). */
046            private StateflowState(StateflowState orig) {
047                    super(orig);
048    
049                    for (StateflowNodeBase child : orig.nodes) {
050                            addNode(child.deepClone());
051                    }
052    
053                    TransitionCloneUtils.cloneTransitions(orig, this);
054            }
055    
056            /** Add a node to this state. */
057            public void addNode(StateflowNodeBase node) {
058                    nodes.add(node);
059                    node.setParent(this);
060            }
061    
062            /** Get state label. */
063            public String getLabel() {
064                    return getParameter(SimulinkConstants.PARAM_labelString);
065            }
066    
067            /** Get child nodes. */
068            public UnmodifiableSet<StateflowNodeBase> getNodes() {
069                    return CollectionUtils.asUnmodifiable(nodes);
070            }
071    
072            /** Remove node. */
073            /* package */void removeNode(StateflowNodeBase node) {
074                    CCSMPre.isTrue(node.getParent() == this,
075                                    "Node does not belong to this chart.");
076                    nodes.remove(node);
077                    node.setParent(null);
078            }
079    
080            /** Returns label and id. */
081            @Override
082            public String toString() {
083                    return getLabel() + " (" + getStateflowId() + ")";
084            }
085    
086            /** Deep clone this state. */
087            @Override
088            public StateflowState deepClone() {
089                    return new StateflowState(this);
090            }
091    }