Skip to content

Commit

Permalink
Add more customization
Browse files Browse the repository at this point in the history
  • Loading branch information
avidraccoon committed Nov 20, 2024
1 parent c7db46f commit efd8ec0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
61 changes: 54 additions & 7 deletions controls/src/main/java/state_machine/State/StateConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public class StateConfiguration<State, Trigger> {

private List<Transition<State, Trigger>> transitions;
private State source;
private Consumer<Transition> onEntry;
private Consumer<Transition> onExit;
private Consumer<Transition> onEntryAction;
private Consumer<Transition> onExitAction;
private Consumer<Transition> transitionAction;
private boolean runDefaultTransitionAction = true;
private boolean runDefaultEntryAction = true;
private boolean runDefaultExitAction = true;

public StateConfiguration(State source) {
this.source = source;
// Temp solution
transitions = new ArrayList<Transition<State, Trigger>>();
transitions = new ArrayList<>();
}

public StateConfiguration<State, Trigger> permit(Trigger trigger, State destination) {
Expand Down Expand Up @@ -85,14 +89,57 @@ public Optional<Transition<State, Trigger>> getFilteredTransition(Trigger trigge
}

public void runOnEntry(Transition transition) {
if (onEntry != null) {
onEntry.accept(transition);
if (onEntryAction != null) {
onEntryAction.accept(transition);
}
}

public void runOnExit(Transition transition) {
if (onExit != null) {
onExit.accept(transition);
if (onExitAction != null) {
onExitAction.accept(transition);
}
}

public StateConfiguration<State, Trigger> disableDefaultTransitionAction(){
this.runDefaultTransitionAction = false;
return this;
}

public StateConfiguration<State, Trigger> disableDefualtOnEntry(){
this.runDefaultEntryAction = false;
return this;
}

public StateConfiguration<State, Trigger> disableDefualtOnExit(){
this.runDefaultExitAction = false;
return this;
}

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

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

public StateConfiguration<State, Trigger> configureTransitionAction(Consumer<Transition> action){
this.transitionAction = action;
return this;
}

public boolean doRunDefaultEntryAction(){
return runDefaultEntryAction;
}

public boolean doRunDefaultExitAction(){
return runDefaultExitAction;
}

public boolean doRunDefaultTransitionAction(){
return runDefaultTransitionAction;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

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

public StateMachineConfiguration() {
// temp solution
Expand Down Expand Up @@ -49,4 +53,19 @@ public Optional<Transition<State, Trigger>> getTransition(State state, Trigger t

return transition;
}

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

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

public StateMachineConfiguration<State, Trigger> configureTransitionAction(Consumer<Transition> action){
this.transitionAction = action;
return this;
}
}
11 changes: 6 additions & 5 deletions controls/src/test/java/StateContainerStateMachineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;

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

public class StateContainerStateMachineTests {

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

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


//stateContainerTestMachineConfig
Expand All @@ -89,10 +90,10 @@ public static void setup() {
//.parentState(testStateContainer.SOME_STATE) Not in first Implemenation
.permit(testEnumTriggers.PREPARE, testStateContainer.READY)
.permitInternal(testEnumTriggers.PREPARE, testStateContainer.READY)
.addTransitionAction(testStateContainer.IDLE::customTransitionAction)
.configureTransitionAction(IdleState::customTransitionAction)
.disableDefaultTransitionAction()
.addOnEntryAction(testStateContainer.IDLE::customOnEntry)
.addOnExitAction(testStateContainer.IDLE::customOnExit)
.configureOnEntryAction(IdleState::customOnEntry)
.configureOnExitAction(IdleState::customOnExit)
.disableDefualtOnEntry()
.disableDefualtOnExit();

Expand Down

0 comments on commit efd8ec0

Please sign in to comment.