23. State Machine Accessor

StateMachine is a main interface to communicate with a state machine itself. Time to time there is a need to get more dynamical and programmatic access to internal structures of a state machine and its nested machines and regions. For these use cases a StateMachine is exposing a functional interface StateMachineAccessor which provides an interface to get access to individual StateMachine and Region instances.

StateMachineFunction is a simple functional interface which allows to apply StateMachineAccess interface into a state machine. With jdk7 these will create a little verbose code but with jdk8 lambdas things look relatively non-verbose.

Method doWithAllRegions gives access to all Region instances in a state machine.

stateMachine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<String,String>>() {

    @Override
    public void apply(StateMachineAccess<String, String> function) {
        function.setRelay(stateMachine);
    }
});

stateMachine.getStateMachineAccessor()
    .doWithAllRegions(access -> access.setRelay(stateMachine));

Method doWithRegion gives access to single Region instance in a state machine.

stateMachine.getStateMachineAccessor().doWithRegion(new StateMachineFunction<StateMachineAccess<String,String>>() {

    @Override
    public void apply(StateMachineAccess<String, String> function) {
        function.setRelay(stateMachine);
    }
});

stateMachine.getStateMachineAccessor()
    .doWithRegion(access -> access.setRelay(stateMachine));

Method withAllRegions gives access to all Region instances in a state machine.

for (StateMachineAccess<String, String> access : stateMachine.getStateMachineAccessor().withAllRegions()) {
    access.setRelay(stateMachine);
}

stateMachine.getStateMachineAccessor().withAllRegions()
    .stream().forEach(access -> access.setRelay(stateMachine));

Method withRegion gives access to single Region instance in a state machine.

stateMachine.getStateMachineAccessor()
    .withRegion().setRelay(stateMachine);