Expose extended state

- ExtendedState is now exposed via
  StateMachine interface.
- Add "sm variables" command to samples.
This commit is contained in:
Janne Valkealahti
2015-04-04 10:24:50 +01:00
parent cceec5500a
commit a4fcb9da97
3 changed files with 36 additions and 1 deletions

View File

@@ -37,6 +37,13 @@ public interface StateMachine<S, E> extends Region<S, E> {
*/
State<S,E> getInitialState();
/**
* Gets the state machine extended state.
*
* @return extended state
*/
ExtendedState getExtendedState();
/**
* Adds the state listener.
*

View File

@@ -177,6 +177,11 @@ public abstract class AbstractStateMachine<S, E> extends LifecycleObjectSupport
return initialState;
}
@Override
public ExtendedState getExtendedState() {
return extendedState;
}
@Override
public boolean sendEvent(Message<E> event) {
if (isComplete() || !isRunning()) {

View File

@@ -1,5 +1,9 @@
package demo;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.shell.core.CommandMarker;
@@ -22,7 +26,7 @@ public class AbstractStateMachineCommands<S, E> implements CommandMarker {
@Qualifier("stateChartModel")
private String stateChartModel;
@CliCommand(value = "sm state", help = "Prints state machine state")
@CliCommand(value = "sm state", help = "Prints current state")
public String state() {
return StringUtils.collectionToCommaDelimitedString(stateMachine.getState().getIds());
}
@@ -44,4 +48,23 @@ public class AbstractStateMachineCommands<S, E> implements CommandMarker {
return stateChartModel;
}
@CliCommand(value = "sm variables", help = "Prints extended state variables")
public String variables() {
StringBuilder buf = new StringBuilder();
Set<Entry<Object, Object>> entrySet = stateMachine.getExtendedState().getVariables().entrySet();
Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
if (entrySet.size() > 0) {
while (iterator.hasNext()) {
Entry<Object, Object> e = iterator.next();
buf.append(e.getKey() + "=" + e.getValue());
if (iterator.hasNext()) {
buf.append("\n");
}
}
} else {
buf.append("No variables");
}
return buf.toString();
}
}