Skip to content

Replace resetStateMachine with resetStateMachineReactively #950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public interface StateMachineAccess<S, E> extends ReactiveStateMachineAccess<S,
* Reset state machine.
*
* @param stateMachineContext the state machine context
* @see #resetStateMachineReactively(StateMachineContext)
*/
@Deprecated
void resetStateMachine(StateMachineContext<S, E> stateMachineContext);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void stateMachineJoined(final StateMachine<S, E> stateMachine, final Stat
log.debug("Joining with context " + context);
}

delegate.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
delegate.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
}
log.info("Requesting to start delegating state machine " + delegate);
log.info("Delegating machine id " + delegate.getUuid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public final void persist(StateMachine<S, E> stateMachine, T contextObj) throws
public final StateMachine<S, E> restore(StateMachine<S, E> stateMachine, T contextObj) throws Exception {
final StateMachineContext<S, E> context = stateMachinePersist.read(contextObj);
stateMachine.stopReactively().block();
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
stateMachine.startReactively().block();
return stateMachine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected StateMachine<S, E> restoreStateMachine(StateMachine<S, E> stateMachine
}
stateMachine.stopReactively().block();
// only go via top region
stateMachine.getStateMachineAccessor().doWithRegion(function -> function.resetStateMachine(stateMachineContext));
stateMachine.getStateMachineAccessor().doWithRegion(function -> function.resetStateMachineReactively(stateMachineContext).block());
return stateMachine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
*/
package org.springframework.statemachine.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
Expand Down Expand Up @@ -69,10 +59,19 @@
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* Base implementation of a {@link StateMachine} loosely modelled from UML state
* machine.
Expand Down Expand Up @@ -763,20 +762,13 @@ public Mono<Void> resetStateMachineReactively(StateMachineContext<S, E> stateMac
.then();
mono = mono.then(resetMono);
} else if (s.isOrthogonal() && stateMachineContext.getChilds() != null) {
Collection<Region<S, E>> regions = ((AbstractState<S, E>)s).getRegions();
Mono<Void> resetMono = Flux.fromIterable(regions)
.flatMap(region -> {
return Flux.fromIterable(stateMachineContext.getChilds())
.flatMap(child -> {
return Mono.fromRunnable(() -> {
((StateMachine<S, E>)region).getStateMachineAccessor()
.doWithRegion(function -> function.resetStateMachine(child));
});
})
.then();
})
.then();
mono = mono.then(resetMono);
Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions();
Mono<Void> resetMono = Flux.fromIterable(regions).flatMap(region ->
Flux.fromIterable(stateMachineContext.getChilds()).flatMap(child ->
((StateMachine<S, E>) region).getStateMachineAccessor().withRegion().resetStateMachineReactively(child)
).then()
).then();
mono = mono.thenEmpty(resetMono);
}

if (log.isDebugEnabled()) {
Expand All @@ -787,21 +779,17 @@ public Mono<Void> resetStateMachineReactively(StateMachineContext<S, E> stateMac
break;
} else if (stateMachineContext.getChilds() != null && !stateMachineContext.getChilds().isEmpty()) {
if (s.isOrthogonal()) {
Collection<Region<S, E>> regions = ((AbstractState<S, E>)s).getRegions();
Mono<Void> resetMono = Flux.fromIterable(regions)
.flatMap(region -> {
return Flux.fromIterable(stateMachineContext.getChilds())
.flatMap(child -> {
return Mono.fromRunnable(() -> {
if (ObjectUtils.nullSafeEquals(region.getId(), child.getId())) {
((StateMachine<S, E>)region).getStateMachineAccessor()
.doWithRegion(function -> function.resetStateMachine(child));
}
});
})
.then();
})
.then();
Collection<Region<S, E>> regions = ((AbstractState<S, E>) s).getRegions();
Mono<Void> resetMono = Flux.fromIterable(regions).flatMap(region ->
Flux.fromIterable(stateMachineContext.getChilds()).flatMap(child -> {
if (ObjectUtils.nullSafeEquals(region.getId(), child.getId())) {
return ((StateMachine<S, E>) region).getStateMachineAccessor()
.withRegion().resetStateMachineReactively(child);
} else {
return Mono.empty();
}
}).then()
).then();
monos.add(resetMono);
} else {
Mono<Void> mono = Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testResetSubStates1() throws Exception {
ExtendedState extendedState = new DefaultExtendedState(variables);
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S12, Events.I, null, extendedState);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S12);
Expand All @@ -95,7 +95,7 @@ public void testResetSubStates2() throws Exception {
ExtendedState extendedState = new DefaultExtendedState(variables);
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S211, Events.C, null, extendedState);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S2, States.S21, States.S211);
Expand All @@ -113,7 +113,7 @@ public void testResetSubStates3() throws Exception {
ExtendedState extendedState = new DefaultExtendedState(variables);
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S2, Events.C, null, extendedState);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S2, States.S21, States.S211);
Expand All @@ -138,7 +138,7 @@ public void testResetRegions1() {
DefaultStateMachineContext<TestStates, TestEvents> stateMachineContext =
new DefaultStateMachineContext<TestStates, TestEvents>(childs, TestStates.S2, TestEvents.E1, null, null);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(TestStates.S2, TestStates.S21, TestStates.S31);
Expand All @@ -162,7 +162,7 @@ public void testResetRegions2() {
DefaultStateMachineContext<TestStates, TestEvents> stateMachineContext =
new DefaultStateMachineContext<TestStates, TestEvents>(childs, TestStates.S2, null, null, null);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(TestStates.S2, TestStates.S21, TestStates.S31);
Expand All @@ -184,7 +184,7 @@ public void testResetUpdateExtendedStateVariables() {
ExtendedState extendedState = new DefaultExtendedState(variables);
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S0, null, null, extendedState);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat((Integer)machine.getExtendedState().getVariables().get("count")).isEqualTo(1);
Expand All @@ -207,7 +207,7 @@ public void testResetWithNullContext() throws Exception {
assertThat((Integer)machine.getExtendedState().getVariables().get("foo")).isZero();

doStopAndAssert(machine);
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(null));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(null).block());
doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S11);
assertThat(machine.getExtendedState().getVariables()).isEmpty();
Expand All @@ -229,7 +229,7 @@ public void testResetWithEnumToCorrectStartState() throws Exception {
DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(
States.S11, null, null, null);
machine.getStateMachineAccessor()
.doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
.doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(States.S0, States.S1, States.S11);
Expand All @@ -246,7 +246,7 @@ public void testRestoreWithTimer() throws Exception {

DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S1, null,
null, null);
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
Thread.sleep(1100);
Expand Down Expand Up @@ -290,7 +290,7 @@ public void testResetKeepsExtendedStateIntactInSubmachine() {
ExtendedState extendedState = new DefaultExtendedState(variables);
DefaultStateMachineContext<States,Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S0, null, null, extendedState);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());
doStartAndAssert(machine);

assertThat((Integer)machine.getExtendedState().getVariables().get("count1")).isEqualTo(1);
Expand Down Expand Up @@ -321,7 +321,7 @@ public void testResetFunkyEnumTypes1() throws Exception {
DefaultStateMachineContext<MyState, MyEvent> stateMachineContext = new DefaultStateMachineContext<MyState, MyEvent>(
SubState.SUB_NEXT, null, null, null);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(SuperState.PARENT, SubState.SUB_NEXT);
Expand All @@ -337,7 +337,7 @@ public void testResetFunkyEnumTypes2() throws Exception {
DefaultStateMachineContext<MyState, MyEvent> stateMachineContext = new DefaultStateMachineContext<MyState, MyEvent>(
SuperState.INITIAL, null, null, null);

machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(stateMachineContext));
machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block());

doStartAndAssert(machine);
assertThat(machine.getState().getIds()).containsOnly(SuperState.INITIAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.statemachine.support.LifecycleObjectSupport;
import org.springframework.statemachine.support.StateMachineInterceptorAdapter;
import org.springframework.statemachine.transition.Transition;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Iterator;
Expand Down Expand Up @@ -63,7 +64,7 @@ public boolean handleEventWithState(Message<E> event, S state) {
stateMachine.stopReactively().block();
List<StateMachineAccess<S, E>> withAllRegions = stateMachine.getStateMachineAccessor().withAllRegions();
for (StateMachineAccess<S, E> a : withAllRegions) {
a.resetStateMachine(new DefaultStateMachineContext<S, E>(state, null, null, null));
a.resetStateMachineReactively(new DefaultStateMachineContext<S, E>(state, null, null, null)).block();
}
stateMachine.startReactively().block();
return stateMachine.sendEvent(event);
Expand All @@ -77,18 +78,18 @@ public boolean handleEventWithState(Message<E> event, S state) {
* @return mono for completion
*/
public Mono<Void> handleEventWithStateReactively(Message<E> event, S state) {
StateMachine<S, E> stateMachine = getInitStateMachine();
// TODO: REACTOR add docs and revisit this function concept
return Mono.from(stateMachine.stopReactively())
.then(Mono.fromRunnable(() -> {
List<StateMachineAccess<S, E>> withAllRegions = stateMachine.getStateMachineAccessor().withAllRegions();
for (StateMachineAccess<S, E> a : withAllRegions) {
a.resetStateMachine(new DefaultStateMachineContext<S, E>(state, null, null, null));
}
}))
.then(stateMachine.startReactively())
.thenMany(stateMachine.sendEvent(Mono.just(event)))
.then();
return Mono.defer(() -> {
StateMachine<S, E> stateMachine = getInitStateMachine();
// TODO: REACTOR add docs and revisit this function concept
return Mono.from(stateMachine.stopReactively())
.thenEmpty(
Flux.fromIterable(stateMachine.getStateMachineAccessor().withAllRegions())
.flatMap(region -> region.resetStateMachineReactively(new DefaultStateMachineContext<S, E>(state, null, null, null)))
)
.then(stateMachine.startReactively())
.thenMany(stateMachine.sendEvent(Mono.just(event)))
.then();
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void resetFromPersistStore() {
}

stateMachine.stopReactively().block();
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachine(context));
stateMachine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(context).block());
stateMachine.startReactively().block();
}

Expand Down