~fredericp/zaluum/rt

« back to all changes in this revision

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

  • Committer: Frederic Perez Ordeig
  • Date: 2010-04-07 07:55:38 UTC
  • mfrom: (308.1.63 wip-scala)
  • Revision ID: frederic@zaluum.com-20100407075538-muaneuoz134fqu5o
merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
package org.zaluum.model.basicbox;
21
21
 
22
 
import java.util.Set;
23
 
import java.util.Map.Entry;
24
 
 
25
22
import org.zaluum.model.Box;
26
 
import org.zaluum.model.OpenBox;
27
23
 
28
 
import com.google.common.base.Preconditions;
29
 
import com.google.common.collect.HashBiMap;
30
 
import com.google.common.collect.ImmutableSet;
31
 
import com.google.common.collect.Sets;
 
24
import com.google.common.collect.Iterables;
32
25
 
33
26
/**
34
27
 * @author frede
35
28
 * 
36
29
 */
37
 
public class FSMBox extends OpenBox {
38
 
        private static final long serialVersionUID = 1L;
39
 
        private final Set<State> states;
40
 
 
41
 
        /**
42
 
         * @param name
43
 
         * @param bounds
44
 
         */
 
30
public class FSMBox extends Box<FSMBox> {
45
31
        public FSMBox() {
46
 
                super();
47
32
                setName("fsm");
48
 
                states = Sets.newHashSet();
49
 
        }
50
 
 
51
 
        public FSMBox(FSMBox toCopy) {
52
 
                super(toCopy);
53
 
                states = Sets.newHashSet();
54
 
                HashBiMap<State, State> oldToNew = HashBiMap.create();
55
 
                for (State s : toCopy.states) {
56
 
                        State newState = new State(s, this);
57
 
                        oldToNew.put(s, newState);
58
 
                }
59
 
                for (Entry<State, State> e : oldToNew.entrySet()) {
60
 
                        State oldfrom = e.getKey();
61
 
                        State newfrom = e.getValue();
62
 
                        for (Transition trans : oldfrom.getFrom()) {
63
 
                                State oldTo = trans.getTarget();
64
 
                                State newTo = oldToNew.get(oldTo);
65
 
                                Transition.create(newfrom, newTo, trans);
66
 
                        }
67
 
                }
68
 
 
69
 
        }
70
 
 
71
 
        protected void addStateInternal(State s) {
72
 
                states.add(s);
73
 
                observable.notifyObservers();
74
 
        }
75
 
 
76
 
        public void restoreState(State s) {
77
 
                Preconditions.checkArgument(s.getBox() == this);
78
 
                addStateInternal(s);
79
 
        }
80
 
 
81
 
        public ImmutableSet<State> getStates() {
82
 
                return ImmutableSet.copyOf(states);
83
 
        }
84
 
 
85
 
        public void removeState(State stateModel) {
86
 
                stateModel.disconnectAll();
87
 
                states.remove(stateModel);
88
 
                observable.notifyObservers();
89
 
        }
90
 
 
 
33
        }
 
34
        public FSMBox(FSMBox fsmBox) {
 
35
                super(fsmBox);
 
36
                for (State s : fsmBox.getStates()){
 
37
                        s.copy().setParent(this);
 
38
                }
 
39
                for (Transition t : fsmBox.getTransitions()){
 
40
                        t.copyTo(this).setParent(this);
 
41
                }
 
42
        }
 
43
        public Iterable<State> getStates() {
 
44
                return Iterables.filter(child, State.class);
 
45
        }
 
46
        public Iterable<Transition> getTransitions() {
 
47
                return Iterables.filter(child,Transition.class);
 
48
        }
 
49
        public State getStateByName(String from) {
 
50
                for (State s : getStates())
 
51
                        if (s.getName().equals(from)) return s;
 
52
                return null;
 
53
        }
91
54
        @Override
92
 
        public Box deepCopy() {
 
55
        public FSMBox copy() {
93
56
                return new FSMBox(this);
94
57
        }
95
58
        @Override
96
59
        public String getResourceKey() {
97
60
                return "fsm";
98
61
        }
99
 
 
100
 
        public State getStateByName(String name) {
101
 
                for (State s : states){
102
 
                        if (s.getName().equals(name))
103
 
                                return s;
104
 
                }
105
 
                return null;
106
 
        }
107
 
 
108
 
 
109
62
}