Skip to content

Commit

Permalink
Mostly ready state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
avidraccoon committed Nov 21, 2024
1 parent efd8ec0 commit 21040fa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public void runOnExit(Transition transition) {
}
}

public void runTransition(Transition transition) {
if (transitionAction != null) {
transitionAction.accept(transition);
}
}

public StateConfiguration<State, Trigger> disableDefaultTransitionAction(){
this.runDefaultTransitionAction = false;
return this;
Expand Down
4 changes: 4 additions & 0 deletions controls/src/main/java/state_machine/StateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public boolean successfulTransition() {
public TransitionInfo getTransitionInfo() {
return transitionInfo;
}

public boolean inState(State state){
return currentState.equals(state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.BiConsumer;

public class StateMachineConfiguration<State, Trigger> {
private Map<State, StateConfiguration<State, Trigger>> stateConfigurations;
private Consumer<Transition> onEntryAction;
private Consumer<Transition> onExitAction;
private Consumer<Transition> transitionAction;
private BiConsumer<State, Transition> onEntryAction;
private BiConsumer<State, Transition> onExitAction;
private BiConsumer<State, Transition> transitionAction;

public StateMachineConfiguration() {
// temp solution
Expand Down Expand Up @@ -54,17 +54,17 @@ public Optional<Transition<State, Trigger>> getTransition(State state, Trigger t
return transition;
}

public StateMachineConfiguration<State, Trigger> configureOnEntryAction(Consumer<Transition> action){
public StateMachineConfiguration<State, Trigger> configureDefaultOnEntryAction(BiConsumer<State, Transition> action){
this.onEntryAction = action;
return this;
}

public StateMachineConfiguration<State, Trigger> configureOnExitAction(Consumer<Transition> action){
public StateMachineConfiguration<State, Trigger> configureDefaultOnExitAction(BiConsumer<State, Transition> action){
this.onExitAction = action;
return this;
}

public StateMachineConfiguration<State, Trigger> configureTransitionAction(Consumer<Transition> action){
public StateMachineConfiguration<State, Trigger> configureDefaultTransitionAction(BiConsumer<State, Transition> action){
this.transitionAction = action;
return this;
}
Expand Down
12 changes: 5 additions & 7 deletions controls/src/test/java/StateContainerStateMachineTests.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package coppercore.controls.test;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import coppercore.controls.PeriodicStateInterface;
import coppercore.controls.StateContainer;
import coppercore.controls.StateMachine;
import coppercore.controls.StateMachineConfiguration;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import coppercore.controls.StateInterface;
import coppercore.controls.Transition;

public class StateContainerStateMachineTests {
Expand Down Expand Up @@ -75,8 +73,8 @@ public static void setup() {
stateContainerTestMachineConfig = new StateMachineConfiguration<>();

stateContainerTestMachineConfig
.configureDefaultEntryAction((testStateContainer state, Transition transition) -> state.onEntry(transition));
.configureDefaultTransitionAction((testStateContainer state, Transition transition) -> state.onEntry(transition));
.configureDefaultOnEntryAction((testStateContainer state, Transition transition) -> state.getState().onEntry(transition))
.configureDefaultTransitionAction((testStateContainer state, Transition transition) -> state.getState().onEntry(transition));


//stateContainerTestMachineConfig
Expand Down

0 comments on commit 21040fa

Please sign in to comment.