package easysm.stores;
import java.util.List;
import java.util.Set;
import easysm.datatypes.information.StateMachineInfo;
import easysm.datatypes.visualization.StateMachineVis;
import easysm.factories.ListFactory;
import easysm.factories.SetFactory;
/**
* @author FourCheeses Software Solutions
*/
public class StateMachine extends Element
{
/**
* Required field to make serialization more robust. See documentation for
* technical details.
*/
private static final long serialVersionUID = 6234333889305994242L;
private List<State> states;
private Set<Transition> transitions;
private StateMachineVis vis;
public StateMachine(StateMachineInfo smInfo)
{
super(smInfo);
changeStates(smInfo.states());
changeTransitions(smInfo.transitions());
vis = new StateMachineVis(this);
}
/*
*
* Properties
*
*/
public List<State> states()
{
return states;
}
public void changeStates(List<State> newStates)
{
states = (newStates != null) ? newStates : ListFactory.createList(State.class);
}
public Set<Transition> transitions()
{
return transitions;
}
public void changeTransitions(Set<Transition> newTransitions)
{
transitions = (newTransitions != null) ? newTransitions : SetFactory.createSet(Transition.class);
}
public StateMachineVis vis()
{
return vis;
}
/*
*
* Public methods
*
*/
@Override
public String toString()
{
if (states.isEmpty()) {
return ""; // Nothing to put in the string
}
String fsp = " "; // 4 spaces, tabs seem too large
StringBuilder builder = new StringBuilder();
// States
builder.append("states {\n");
boolean first = true;
for (State st : states) {
if (first) {
first = false;
} else {
builder.append(",\n");
}
builder.append(fsp + st);
if (st.isFinal()) {
builder.append(" (FINAL)");
}
}
builder.append("\n}\n\n");
// Transitions
for (Transition tr : transitions) {
builder.append("transition from " + tr.source() + " to " + tr.target() + " {\n" + fsp);
String transString = transWithOffset(tr, fsp);
if (transString.isEmpty()) {
builder.append("// EMPTY");
}
builder.append(transString + "\n}\n\n");
}
return builder.toString();
}
private String transWithOffset(Transition tr, String offset)
{
StringBuilder builder = new StringBuilder();
for (char ch : tr.toString().toCharArray()) {
builder.append(ch);
if (ch == '\n') {
builder.append(offset);
}
}
return builder.toString();
}
}