001    /*--------------------------------------------------------------------------+
002    $Id: StateflowChart.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 java.util.ArrayList;
021    
022    import edu.tum.cs.commons.assertion.CCSMPre;
023    import edu.tum.cs.commons.collections.CollectionUtils;
024    import edu.tum.cs.commons.collections.IdentityHashSet;
025    import edu.tum.cs.commons.collections.UnmodifiableSet;
026    import edu.tum.cs.simulink.model.SimulinkConstants;
027    
028    /**
029     * This class represents Stateflow charts. There is a one-to-one association
030     * between {@link StateflowBlock}s and {@link StateflowChart}s.
031     * 
032     * @author deissenb
033     * @author $Author: juergens $
034     * @version $Rev: 26285 $
035     * @levd.rating GREEN Hash: FA7533BD5DCDE2CA69B9515DC8600D67
036     */
037    public class StateflowChart extends
038                    StateflowDeclContainerBase<StateflowMachine> implements
039                    IStateflowNodeContainer<StateflowMachine> {
040    
041            /** The Stateflow block associated with this chart. */
042            private StateflowBlock stateflowBlock;
043    
044            /** List of top level nodes. */
045            private final IdentityHashSet<StateflowNodeBase> nodes = new IdentityHashSet<StateflowNodeBase>();
046    
047            /** Create new Stateflow block. */
048            public StateflowChart() {
049                    super();
050            }
051    
052            /**
053             * Create new Stateflow chart from existing chart (for deep cloning).
054             */
055            /* package */StateflowChart(StateflowChart origChart) {
056                    super(origChart);
057    
058                    for (StateflowNodeBase element : origChart.getNodes()) {
059                            addNode(element.deepClone());
060                    }
061    
062                    TransitionCloneUtils.cloneTransitions(origChart, this);
063            }
064    
065            /**
066             * Add node.
067             */
068            public void addNode(StateflowNodeBase node) {
069                    nodes.add(node);
070                    node.setParent(this);
071            }
072    
073            /**
074             * Deep clone this chart.
075             */
076            public StateflowChart deepClone() {
077                    return new StateflowChart(this);
078            }
079    
080            /** Get the Stateflow machine this chart belongs to. */
081            public StateflowMachine getMachine() {
082                    return getParent();
083            }
084    
085            /** Returns the name of the chart. */
086            public String getName() {
087                    return getParameter(SimulinkConstants.PARAM_name);
088            }
089    
090            /** Returns the nodes of this chart. */
091            public UnmodifiableSet<StateflowNodeBase> getNodes() {
092                    return CollectionUtils.asUnmodifiable(nodes);
093            }
094    
095            /** Get Stateflow block this chart belongs to. */
096            public StateflowBlock getStateflowBlock() {
097                    return stateflowBlock;
098            }
099    
100            /**
101             * This method throws an {@link UnsupportedOperationException}. You must
102             * remove the associated {@link StateflowBlock} to remove a chart.
103             */
104            @Override
105            public void remove() {
106                    throw new UnsupportedOperationException(
107                                    "Cannot remove chart without removing Stateflow block!");
108            }
109    
110            /** Returns the name of the chart. */
111            @Override
112            public String toString() {
113                    return getName();
114            }
115    
116            /**
117             * Remove all nodes from this chart.
118             */
119            /* package */void removeNodes() {
120                    for (StateflowNodeBase node : new ArrayList<StateflowNodeBase>(nodes)) {
121                            node.remove();
122                    }
123            }
124    
125            /** Remove node. */
126            /* package */void removeNode(StateflowNodeBase node) {
127                    CCSMPre.isTrue(node.getParent() == this,
128                                    "Node does not belong to this chart.");
129                    nodes.remove(node);
130                    node.setParent(null);
131            }
132    
133            /** Set Stateflow block this chart belongs to. */
134            /* package */void setStateflowBlock(StateflowBlock stateflowBlock) {
135                    if (stateflowBlock != null) {
136                            CCSMPre.isTrue(this.stateflowBlock == null,
137                                            "Cannot set new Stateflow block.");
138                    }
139                    this.stateflowBlock = stateflowBlock;
140            }
141    }