~fredericp/zaluum/wip-ide

« back to all changes in this revision

Viewing changes to org.zaluum.model/src/org/zaluum/model/FSMBox.java

  • Committer: Frederic Perez Ordeig
  • Date: 2009-12-18 16:21:23 UTC
  • Revision ID: frederic@zaluum.com-20091218162123-9fp07r8kr8raec5b
moved all fsm to one plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *  This file is part of Zaluum
3
 
 *   
4
 
 *  Zaluum is free software: you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU Lesser General Public License as published by
6
 
 *  the Free Software Foundation, either version 3 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  Zaluum is distributed in the hope that it will be useful,
10
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *  GNU Lesser General Public License for more details.
13
 
 *
14
 
 *  You should have received a copy of the GNU Lesser General Public License
15
 
 *  along with Zaluum.  If not, see <http://www.gnu.org/licenses/>.
16
 
 *  
17
 
 *  Copyright   2008,2009       Frederic Perez Ordeig
18
 
 *  Copyright   2008,2009       Elias Hortas Garcia
19
 
 */
20
 
package org.zaluum.model;
21
 
 
22
 
import java.util.Set;
23
 
import java.util.Map.Entry;
24
 
 
25
 
import com.google.common.base.Preconditions;
26
 
import com.google.common.collect.HashBiMap;
27
 
import com.google.common.collect.ImmutableSet;
28
 
import com.google.common.collect.Sets;
29
 
 
30
 
/**
31
 
 * @author frede
32
 
 *
33
 
 */
34
 
public class FSMBox extends OpenBox{
35
 
        private static final long serialVersionUID = 1L;
36
 
        private final Set<State> states;
37
 
        /**
38
 
         * @param name
39
 
         * @param bounds 
40
 
         */
41
 
        public FSMBox() {
42
 
                super();
43
 
                setName("fsm");
44
 
                states = Sets.newHashSet();
45
 
        }
46
 
        public FSMBox(FSMBox toCopy){
47
 
                super(toCopy);
48
 
                states = Sets.newHashSet();
49
 
                HashBiMap<State, State> oldToNew = HashBiMap.create();
50
 
                for (State s : toCopy.states){
51
 
                        State newState = new State(s, this);
52
 
                        oldToNew.put(s, newState);
53
 
                }
54
 
                for (Entry<State, State> e : oldToNew.entrySet()){
55
 
                        State oldfrom = e.getKey();
56
 
                        State newfrom = e.getValue();
57
 
                        for (Transition trans : oldfrom.getFrom()){
58
 
                                State oldTo = trans.getTarget();
59
 
                                State newTo = oldToNew.get(oldTo);
60
 
                                Transition transition = Transition.create(newfrom, newTo);
61
 
                                transition.setCondition(trans.getCondition());
62
 
                        }
63
 
                }
64
 
 
65
 
        }
66
 
 
67
 
        protected void addStateInternal(State s){
68
 
                states.add(s);
69
 
                observable.notifyObservers();
70
 
        }
71
 
        public void restoreState(State s){
72
 
                Preconditions.checkArgument(s.getBox()==this);
73
 
                addStateInternal(s);
74
 
        }
75
 
        public ImmutableSet<State> getStates() {
76
 
                return ImmutableSet.copyOf(states);
77
 
        }
78
 
        public void removeState(State stateModel) {
79
 
                stateModel.disconnectAll();
80
 
                states.remove(stateModel);
81
 
                observable.notifyObservers();
82
 
        }
83
 
        @Override
84
 
        public Box deepCopy(){
85
 
                return  new FSMBox(this);
86
 
        }
87
 
        @Override
88
 
        public BoxDescriptor getDescriptor() {
89
 
                return BoxDescriptors.FSMBox;
90
 
        }
91
 
 
92
 
}