diff --git a/src/main/java/ru/bvn13/fsm/Exceptions/FSMException.java b/src/main/java/ru/bvn13/fsm/Exceptions/FSMException.java index f1e4aa1..55c6cac 100644 --- a/src/main/java/ru/bvn13/fsm/Exceptions/FSMException.java +++ b/src/main/java/ru/bvn13/fsm/Exceptions/FSMException.java @@ -6,7 +6,7 @@ import java.io.StringWriter; /** * Created by bvn13 on 28.12.2017. */ -public class FSMException extends Exception { +public class FSMException extends RuntimeException { protected String message; public FSMException(String message) { this.message = message; diff --git a/src/main/java/ru/bvn13/fsm/FSM.java b/src/main/java/ru/bvn13/fsm/FSM.java index a220ddf..4195262 100644 --- a/src/main/java/ru/bvn13/fsm/FSM.java +++ b/src/main/java/ru/bvn13/fsm/FSM.java @@ -16,20 +16,14 @@ public class FSM { private State initialState; private State currentState; private State previousState; - private Map states; - private Map> transitions; + private final Map states; + private final Map> transitions; public FSM() { this.states = 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 { currentState = initialState; if (currentState == null) { @@ -91,13 +85,6 @@ public class FSM { nextState(getPreviousState()); } - private void nextState(State state) { - state.beforeEvent(); - previousState = currentState; - currentState = state; - currentState.process(); - } - public void initState(State state) throws FSMException { state.setFSM(this); addState(state); @@ -129,6 +116,19 @@ public class FSM { 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 { if (!transitions.containsKey(fromState)) { transitions.put(fromState, new HashMap<>()); diff --git a/src/test/java/ru/bvn13/fsm/tests/FsmTest.java b/src/test/java/ru/bvn13/fsm/tests/FsmTest.java new file mode 100644 index 0000000..9032d4d --- /dev/null +++ b/src/test/java/ru/bvn13/fsm/tests/FsmTest.java @@ -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()); + + } + +} diff --git a/src/test/java/ru/bvn13/fsm/tests/Tests.java b/src/test/java/ru/bvn13/fsm/tests/Tests.java deleted file mode 100644 index e644e5a..0000000 --- a/src/test/java/ru/bvn13/fsm/tests/Tests.java +++ /dev/null @@ -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")); - - } - -}