20
20
package org.zaluum.model.basicbox;
23
import java.util.Map.Entry;
25
22
import org.zaluum.model.Box;
26
import org.zaluum.model.OpenBox;
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;
37
public class FSMBox extends OpenBox {
38
private static final long serialVersionUID = 1L;
39
private final Set<State> states;
30
public class FSMBox extends Box<FSMBox> {
48
states = Sets.newHashSet();
51
public FSMBox(FSMBox 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);
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);
71
protected void addStateInternal(State s) {
73
observable.notifyObservers();
76
public void restoreState(State s) {
77
Preconditions.checkArgument(s.getBox() == this);
81
public ImmutableSet<State> getStates() {
82
return ImmutableSet.copyOf(states);
85
public void removeState(State stateModel) {
86
stateModel.disconnectAll();
87
states.remove(stateModel);
88
observable.notifyObservers();
34
public FSMBox(FSMBox fsmBox) {
36
for (State s : fsmBox.getStates()){
37
s.copy().setParent(this);
39
for (Transition t : fsmBox.getTransitions()){
40
t.copyTo(this).setParent(this);
43
public Iterable<State> getStates() {
44
return Iterables.filter(child, State.class);
46
public Iterable<Transition> getTransitions() {
47
return Iterables.filter(child,Transition.class);
49
public State getStateByName(String from) {
50
for (State s : getStates())
51
if (s.getName().equals(from)) return s;
92
public Box deepCopy() {
55
public FSMBox copy() {
93
56
return new FSMBox(this);
96
59
public String getResourceKey() {
100
public State getStateByName(String name) {
101
for (State s : states){
102
if (s.getName().equals(name))