simple refactoring
This commit is contained in:
parent
2a2eccad1e
commit
4428c70007
@ -6,7 +6,7 @@ import java.io.StringWriter;
|
|||||||
/**
|
/**
|
||||||
* Created by bvn13 on 28.12.2017.
|
* Created by bvn13 on 28.12.2017.
|
||||||
*/
|
*/
|
||||||
public class FSMException extends Exception {
|
public class FSMException extends RuntimeException {
|
||||||
protected String message;
|
protected String message;
|
||||||
public FSMException(String message) {
|
public FSMException(String message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
|
@ -16,20 +16,14 @@ public class FSM {
|
|||||||
private State initialState;
|
private State initialState;
|
||||||
private State currentState;
|
private State currentState;
|
||||||
private State previousState;
|
private State previousState;
|
||||||
private Map<String, State> states;
|
private final Map<String, State> states;
|
||||||
private Map<String, Map<String, ConditionBehaviour>> transitions;
|
private final Map<String, Map<String, ConditionBehaviour>> transitions;
|
||||||
|
|
||||||
public FSM() {
|
public FSM() {
|
||||||
this.states = new HashMap<>();
|
this.states = new HashMap<>();
|
||||||
this.transitions = new HashMap<>();
|
this.transitions = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkStateExist(String name) throws StateExistsException {
|
|
||||||
if (this.states.containsKey(name)) {
|
|
||||||
throw new StateExistsException(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() throws NotInitedException {
|
public void init() throws NotInitedException {
|
||||||
currentState = initialState;
|
currentState = initialState;
|
||||||
if (currentState == null) {
|
if (currentState == null) {
|
||||||
@ -91,13 +85,6 @@ public class FSM {
|
|||||||
nextState(getPreviousState());
|
nextState(getPreviousState());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void nextState(State state) {
|
|
||||||
state.beforeEvent();
|
|
||||||
previousState = currentState;
|
|
||||||
currentState = state;
|
|
||||||
currentState.process();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initState(State state) throws FSMException {
|
public void initState(State state) throws FSMException {
|
||||||
state.setFSM(this);
|
state.setFSM(this);
|
||||||
addState(state);
|
addState(state);
|
||||||
@ -129,6 +116,19 @@ public class FSM {
|
|||||||
addTransition(fromState, toState.getName(), condition);
|
addTransition(fromState, toState.getName(), condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void nextState(State state) {
|
||||||
|
state.beforeEvent();
|
||||||
|
previousState = currentState;
|
||||||
|
currentState = state;
|
||||||
|
currentState.process();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkStateExist(String name) throws StateExistsException {
|
||||||
|
if (this.states.containsKey(name)) {
|
||||||
|
throw new StateExistsException(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void storeTransition(String fromState, String toState, Condition condition) throws FSMException {
|
private void storeTransition(String fromState, String toState, Condition condition) throws FSMException {
|
||||||
if (!transitions.containsKey(fromState)) {
|
if (!transitions.containsKey(fromState)) {
|
||||||
transitions.put(fromState, new HashMap<>());
|
transitions.put(fromState, new HashMap<>());
|
||||||
|
71
src/test/java/ru/bvn13/fsm/tests/FsmTest.java
Normal file
71
src/test/java/ru/bvn13/fsm/tests/FsmTest.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package ru.bvn13.fsm.tests;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import ru.bvn13.fsm.Condition;
|
||||||
|
import ru.bvn13.fsm.FSM;
|
||||||
|
import ru.bvn13.fsm.State;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by bvn13 on 28.12.2017.
|
||||||
|
*/
|
||||||
|
public class FsmTest {
|
||||||
|
|
||||||
|
public static class NamedFSM extends FSM {
|
||||||
|
|
||||||
|
public NamedFSM() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public NamedFSM setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void creatingFSM() {
|
||||||
|
|
||||||
|
NamedFSM fsm = (new NamedFSM()).setName("TEST FSM");
|
||||||
|
|
||||||
|
fsm.initState(new State("init") {
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("" + fsm + " -> " + getName() + ": processed init");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fsm.addTransition("init", new State("first", true) {
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("" + fsm + " -> " + getName() + ": processed first");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fsm.addTransition("init", new State("another", true) {
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
System.out.println("" + fsm + " -> " + getName() + ": processed first");
|
||||||
|
}
|
||||||
|
}, new Condition() {
|
||||||
|
@Override
|
||||||
|
public boolean check() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fsm.init();
|
||||||
|
fsm.next();
|
||||||
|
|
||||||
|
Assert.assertEquals("first", fsm.getCurrentState().getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,87 +0,0 @@
|
|||||||
package ru.bvn13.fsm.tests;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
import ru.bvn13.fsm.Exceptions.FSMException;
|
|
||||||
import ru.bvn13.fsm.Exceptions.NotInitedException;
|
|
||||||
import ru.bvn13.fsm.FSM;
|
|
||||||
import ru.bvn13.fsm.State;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by bvn13 on 28.12.2017.
|
|
||||||
*/
|
|
||||||
public class Tests {
|
|
||||||
|
|
||||||
public static class NamedFSM extends FSM {
|
|
||||||
|
|
||||||
public NamedFSM() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
public NamedFSM setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void creatingFSM() {
|
|
||||||
|
|
||||||
NamedFSM fsm = (new NamedFSM()).setName("TEST FSM");
|
|
||||||
Assert.assertNotNull(fsm);
|
|
||||||
|
|
||||||
try {
|
|
||||||
fsm.initState(new State("init") {
|
|
||||||
@Override
|
|
||||||
public void process() {
|
|
||||||
System.out.println("" + fsm + " -> " + getName() + ": processed init");
|
|
||||||
try {
|
|
||||||
fsm.next();
|
|
||||||
} catch (FSMException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (FSMException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
fsm.addTransition("init", new State("first", true) {
|
|
||||||
@Override
|
|
||||||
public void process() {
|
|
||||||
System.out.println("" + fsm + " -> " + getName() + ": processed first");
|
|
||||||
try {
|
|
||||||
fsm.next();
|
|
||||||
} catch (FSMException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (FSMException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
fsm.init();
|
|
||||||
} catch (NotInitedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.assertTrue(fsm.getCurrentState().getName().equals("first"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user