Skip to content

Commit

Permalink
State to figure out new functions and start polishing StateMachine
Browse files Browse the repository at this point in the history
  • Loading branch information
avidraccoon committed Nov 19, 2024
1 parent 8833d54 commit c7db46f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface StateContainer {

public StateInterface getState();

}
4 changes: 2 additions & 2 deletions controls/src/test/java/SimpleEnumStateMachineTests.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package coppercore.controls.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import coppercore.controls.StateMachine;
import coppercore.controls.StateMachineConfiguration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class SimpleEnumStateMachineTests {

Expand Down
76 changes: 64 additions & 12 deletions controls/src/test/java/StateContainerStateMachineTests.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
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;

public class StateContainerStateMachineTests {

static class IdleState implements PeriodicStateInterface {};
static class ReadyState implements PeriodicStateInterface {};
static class WaitingState implements PeriodicStateInterface {};
static class DoneState implements PeriodicStateInterface {};
static class ShutdownState implements PeriodicStateInterface {};
static class IdleState implements PeriodicStateInterface {
public static void customOnEntry(Transition transition){

}

public static void customOnExit(Transition transition){

}

public static void customTransitionAction(Transition transition){

}
}
;

static class ReadyState implements PeriodicStateInterface {}
;

static class WaitingState implements PeriodicStateInterface {}
;

static class DoneState implements PeriodicStateInterface {}
;

static class ShutdownState implements PeriodicStateInterface {}
;

static enum testStateContainer implements StateContainer {
IDLE(new IdleState()),
Expand All @@ -24,11 +47,12 @@ static enum testStateContainer implements StateContainer {
SHUTDOWN(new ShutdownState());

private final PeriodicStateInterface state;

testStateContainer(PeriodicStateInterface state) {
this.state = state;
}
public PeriodicStateInterface getState(){

public PeriodicStateInterface getState() {
return state;
}
}
Expand All @@ -42,15 +66,36 @@ static enum testEnumTriggers {
ERROR
}

public static StateMachineConfiguration<testStateContainer, testEnumTriggers> stateContainerTestMachineConfig;
public static StateMachineConfiguration<testStateContainer, testEnumTriggers>
stateContainerTestMachineConfig;

@BeforeAll
public static void setup() {
stateContainerTestMachineConfig = new StateMachineConfiguration<>();

stateContainerTestMachineConfig
.configureDefaultTransitionAction((State state, Transition transition) -> state.onEntry(transition));
.addTransitionAction((State state, Transition transition) -> state.onEntry(transition));


//stateContainerTestMachineConfig
// .registerBlankParent(testStateContainer.SOME_PARENT_STATE); Not in first Implemenation

//stateContainerTestMachineConfig
// .configure(testStateContainer.SOME_STATE); Not in first Implemenation

stateContainerTestMachineConfig
.configure(testStateContainer.IDLE)
.permit(testEnumTriggers.PREPARE, testStateContainer.READY);
//.parentState(testStateContainer.SOME_STATE) Not in first Implemenation
.permit(testEnumTriggers.PREPARE, testStateContainer.READY)
.permitInternal(testEnumTriggers.PREPARE, testStateContainer.READY)
.addTransitionAction(testStateContainer.IDLE::customTransitionAction)
.disableDefaultTransitionAction()
.addOnEntryAction(testStateContainer.IDLE::customOnEntry)
.addOnExitAction(testStateContainer.IDLE::customOnExit)
.disableDefualtOnEntry()
.disableDefualtOnExit();


stateContainerTestMachineConfig
.configure(testStateContainer.READY)
Expand All @@ -65,12 +110,19 @@ public static void setup() {
.configure(testStateContainer.DONE)
.permit(testEnumTriggers.PREPARE, testStateContainer.READY)
.permit(testEnumTriggers.SHUTDOWN, testStateContainer.SHUTDOWN);

}

@Test
void stateMachineTransitionNoErrorTest() {
StateMachine<testStateContainer, testEnumTriggers> stateMachine =
new StateMachine<>(stateContainerTestMachineConfig, testStateContainer.IDLE);
testStateContainer stateContainer = stateMachine.getCurrentState();
//stateMachine.inState(testStateContainer.SOME_STATE); Not in first Implemenation //True
stateMachine.inState(testStateContainer.IDLE); //True
//stateMachine.inStateExactly(testStateContainer.SOME_STATE); Not in first Implemenation //False
//stateMachine.inState(testStateContainer.SOME_PARENT_STATE); Not in first Implemenation //False
stateContainer.getState().periodic();
}

@Test
Expand Down

0 comments on commit c7db46f

Please sign in to comment.