package jetbrains.mps.samples.secretCompartmentLanguage.runtime;
/*Generated by MPS */
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class State {
private String name;
private Map<Event, State> transitions = new HashMap<Event, State>();
private Map<String, State> transitionsByCode = new HashMap<String, State>();
private List<Command> commands = new ArrayList<Command>();
public State(String name) {
this.name = name;
}
public void addTransition(Event event, State targetState) {
transitions.put(event, targetState);
transitionsByCode.put(event.getCode(), targetState);
}
public boolean hasTransition(String eventCode) {
return transitionsByCode.containsKey(eventCode);
}
public State targetState(String eventCode) {
return transitionsByCode.get(eventCode);
}
public void executeActions(CommandChannel commandsChannel) {
for (Command c : commands) {
commandsChannel.send(c.getCode());
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("State: %s\n", name));
for (Map.Entry<Event, State> e : transitions.entrySet()) {
result.append(String.format(" %s => %s\n", e.getKey(), e.getValue().name));
}
for (Command c : commands) {
result.append(String.format(" #%s\n", c.getName()));
}
return result.toString();
}
public void addCommand(Command command) {
commands.add(command);
}
/*package*/ Collection<State> getAllTargets() {
return Collections.unmodifiableCollection(transitions.values());
}
public Collection<Event> getEvents() {
return Collections.unmodifiableCollection(transitions.keySet());
}
public String getName() {
return name;
}
/*package*/ void compare(State other, Notification note) {
assert name.equals(other.name);
compareTransitions(other, note);
compareActions(other, note);
}
private void compareActions(State other, Notification note) {
if (!(commands.equals(other.commands))) {
note.error("%s has different commands", name);
}
}
/*package*/ void compareTransitions(State other, Notification note) {
for (Map.Entry<Event, State> e : transitions.entrySet()) {
compareTransition(e.getKey(), e.getValue(), other, note);
}
for (Map.Entry<Event, State> e : other.transitions.entrySet()) {
if (!(this.transitions.containsKey(e.getKey()))) {
note.error("%s has extra transition with %s", name, e.getKey());
}
}
}
/*package*/ void compareTransition(Event myEvent, State myTarget, State otherState, Notification note) {
if (otherState.transitions.containsKey(myEvent)) {
State otherTarget = otherState.transitions.get(myEvent);
if (!(myTarget.getName().equals(otherTarget.getName()))) {
note.error("%s transitions to %s instead of %s", myEvent, otherTarget.getName(), myTarget.getName());
}
} else {
note.error("%s has missing transition for %s", name, myEvent);
}
}
public void toDot(StringBuilder result) {
String dotLabel = String.format("{%s", name);
if (!(commands.isEmpty())) {
dotLabel += "|";
for (Command c : commands) {
dotLabel += String.format("%s\\n", c.getName());
}
}
dotLabel += "}";
result.append(String.format("%s [shape = Mrecord fontsize = 12 label = \"%s\"]\n", name, dotLabel));
for (Map.Entry<Event, State> e : transitions.entrySet()) {
result.append(String.format("%s -> %s [ label = \"%s\" fontsize = 10 arrowhead = open];\n", name, e.getValue().name, e.getKey().getName()));
}
}
}