Updates to docs

This commit is contained in:
Janne Valkealahti
2015-03-06 15:01:23 +00:00
parent de848cda20
commit ed81b1b578
24 changed files with 673 additions and 30 deletions

View File

@@ -0,0 +1,47 @@
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class AbstractStateMachineCommands<S, E> implements CommandMarker {
@Autowired
private StateMachine<S, E> stateMachine;
protected StateMachine<S, E> getStateMachine() {
return stateMachine;
}
@Autowired
@Qualifier("stateChartModel")
private String stateChartModel;
@CliCommand(value = "sm state", help = "Prints state machine state")
public String state() {
return StringUtils.collectionToCommaDelimitedString(stateMachine.getState().getIds());
}
@CliCommand(value = "sm start", help = "Start a state machine")
public String start() {
stateMachine.start();
return "State machine started";
}
@CliCommand(value = "sm stop", help = "Stop a state machine")
public String stop() {
stateMachine.stop();
return "State machine stopped";
}
@CliCommand(value = "sm print", help = "Print state machine")
public String print() {
return stateChartModel;
}
}

View File

@@ -0,0 +1,67 @@
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.statemachine.event.OnStateChangedEvent;
import org.springframework.statemachine.event.OnTransitionEvent;
import org.springframework.statemachine.event.StateMachineEvent;
import org.springframework.statemachine.event.StateMachineEventPublisherConfiguration;
@Configuration
public class CommonConfiguration {
private final static Log log = LogFactory.getLog(CommonConfiguration.class);
@Configuration
@Import(StateMachineEventPublisherConfiguration.class)
static class ApplicationConfig {
@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
@Bean
public TestEventListener testEventListener() {
return new TestEventListener();
}
@Bean
public String stateChartModel() throws IOException {
ClassPathResource model = new ClassPathResource("statechartmodel.txt");
InputStream inputStream = model.getInputStream();
Scanner scanner = new Scanner(inputStream);
String content = scanner.useDelimiter("\\Z").next();
scanner.close();
return content;
}
}
static class TestEventListener implements ApplicationListener<StateMachineEvent> {
@Override
public void onApplicationEvent(StateMachineEvent event) {
if (event instanceof OnStateChangedEvent) {
OnStateChangedEvent e = (OnStateChangedEvent)event;
log.info("State changed to " + e.getTargetState().getId());
} else if (event instanceof OnTransitionEvent) {
OnTransitionEvent e = (OnTransitionEvent)event;
log.info("Transition " + e.getTransition().toString());
}
}
}
}

View File

@@ -0,0 +1,23 @@
package demo;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.shell.plugin.support.DefaultPromptProvider;
import org.springframework.stereotype.Component;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class StateMachinePromptProvider extends DefaultPromptProvider {
@Override
public String getPrompt() {
return "sm>";
}
@Override
public String getProviderName() {
return "State machine prompt provider";
}
}