Compare commits

..

No commits in common. "master" and "2.1.7" have entirely different histories.

6 changed files with 4 additions and 228 deletions

View File

@ -7,7 +7,7 @@
<groupId>me.bvn13.fsm</groupId> <groupId>me.bvn13.fsm</groupId>
<artifactId>fsm</artifactId> <artifactId>fsm</artifactId>
<version>2.2.1</version> <version>2.1.7</version>
<packaging>jar</packaging> <packaging>jar</packaging>

View File

@ -50,31 +50,6 @@ Simple way to use it - to construct an inherited class specified with the type o
.create(); .create();
``` ```
or since 2.2.1
```java
SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init")
.withBeforeHandler(fsm -> initBefore.set(true))
.withAfterHandler(fsm -> initAfter.set(true))
.withProcessor((fsm, event) -> initProcess.set(true))
.withTransition()
.to("finish")
.checking((fsm, event) -> true)
.endTransition()
.end()
.finish("finish")
.withBeforeHandler(fsm -> finishBefore.set(true))
.withAfterHandler(fsm -> finishAfter.set(true))
.withProcessor((fsm, event) -> finishProcess.set(true))
.end()
.create();
}
```
## Releasing ## Releasing
Creating a new release involves the following steps: Creating a new release involves the following steps:

View File

@ -3,20 +3,12 @@ package me.bvn13.fsm;
public class ConditionBuilder<T extends Fsm, E> { public class ConditionBuilder<T extends Fsm, E> {
private final FsmBuilder<T,E> fsmBuilder; private final FsmBuilder<T,E> fsmBuilder;
private final StateBuilder<T, E> stateBuilder;
private String from; private String from;
private String to; private String to;
private Condition<T,E> condition; private Condition<T,E> condition;
ConditionBuilder(FsmBuilder<T,E> fsmBuilder) { ConditionBuilder(FsmBuilder<T,E> fsmBuilder) {
this.fsmBuilder = fsmBuilder; this.fsmBuilder = fsmBuilder;
this.stateBuilder = null;
}
ConditionBuilder(FsmBuilder<T,E> fsmBuilder, StateBuilder<T, E> stateBuilder, String from) {
this.fsmBuilder = fsmBuilder;
this.stateBuilder = stateBuilder;
this.from = from;
} }
public ConditionBuilder<T,E> from(String from) { public ConditionBuilder<T,E> from(String from) {
@ -39,12 +31,4 @@ public class ConditionBuilder<T extends Fsm, E> {
return fsmBuilder; return fsmBuilder;
} }
public StateBuilder<T, E> endTransition() {
if (stateBuilder == null) {
throw new IllegalStateException("Use '.end()' instead");
}
end();
return stateBuilder;
}
} }

View File

@ -52,8 +52,6 @@ import static java.lang.String.format;
* Simple way to use it - to construct an inherited class specified with the type of events to be processed * Simple way to use it - to construct an inherited class specified with the type of events to be processed
* during transitions. * during transitions.
* *
* since 2.1.5
*
* <pre> * <pre>
* {@code * {@code
* SimpleFsm<String> simpleFsm = Fsm * SimpleFsm<String> simpleFsm = Fsm
@ -78,31 +76,6 @@ import static java.lang.String.format;
* } * }
* </pre> * </pre>
* *
* or since 2.2.1
*
* <pre>
* {@code
* SimpleFsm<String> simpleFsm = Fsm
* .<SimpleFsm<String>, String>from(SimpleFsm::new)
* .withStates()
* .from("init")
* .withBeforeHandler(fsm -> initBefore.set(true))
* .withAfterHandler(fsm -> initAfter.set(true))
* .withProcessor((fsm, event) -> initProcess.set(true))
* .withTransition()
* .to("finish")
* .checking((fsm, event) -> true)
* .endTransition()
* .end()
* .finish("finish")
* .withBeforeHandler(fsm -> finishBefore.set(true))
* .withAfterHandler(fsm -> finishAfter.set(true))
* .withProcessor((fsm, event) -> finishProcess.set(true))
* .end()
* .create();
* }
* </pre>
*
* <p> * <p>
* Otherwise you are able to use Old syntax: * Otherwise you are able to use Old syntax:
* *
@ -176,9 +149,9 @@ public class Fsm<T extends Fsm, E> {
return; return;
} }
currentState.process(event); currentState.process(event);
currentState.afterEvent();
if (currentState.isFinish()) { if (currentState.isFinish()) {
done = true; done = true;
currentState.afterEvent();
return; return;
} }
switchToNextState(event); switchToNextState(event);
@ -279,6 +252,7 @@ public class Fsm<T extends Fsm, E> {
throw new NotInitializedException(format("Unable to find state '%s'", name), e); throw new NotInitializedException(format("Unable to find state '%s'", name), e);
} }
this.done = currentState.isFinish(); this.done = currentState.isFinish();
this.currentState.beforeEvent();
} }
private void switchToNextState(E event) { private void switchToNextState(E event) {
@ -305,7 +279,6 @@ public class Fsm<T extends Fsm, E> {
} }
private void nextState(State<E> state, E event) { private void nextState(State<E> state, E event) {
currentState.afterEvent();
previousState = currentState; previousState = currentState;
currentState = state; currentState = state;
currentState.beforeEvent(); currentState.beforeEvent();

View File

@ -56,8 +56,4 @@ public class StateBuilder<T extends Fsm, E> {
return this; return this;
} }
public ConditionBuilder<T,E> withTransition() {
return new ConditionBuilder<>(fsmBuilder, this, name);
}
} }

View File

@ -7,7 +7,6 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Created by bvn13 on 28.12.2017. * Created by bvn13 on 28.12.2017.
@ -180,7 +179,7 @@ public class FsmTest {
Assert.assertFalse(initBefore.get()); Assert.assertFalse(initBefore.get());
Assert.assertFalse(initProcess.get()); Assert.assertFalse(initProcess.get());
Assert.assertFalse(initAfter.get()); Assert.assertFalse(initAfter.get());
Assert.assertFalse(intermediateBefore.get()); Assert.assertTrue(intermediateBefore.get());
Assert.assertTrue(intermediateAfter.get()); Assert.assertTrue(intermediateAfter.get());
Assert.assertTrue(intermediateProcess.get()); Assert.assertTrue(intermediateProcess.get());
Assert.assertTrue(finishBefore.get()); Assert.assertTrue(finishBefore.get());
@ -189,155 +188,4 @@ public class FsmTest {
} }
@Test
public void newSyntaxCountingEveryStep() {
Counter initCounter = new Counter();
Counter firstIntermediateCounter = new Counter();
Counter secondIntermediateCounter = new Counter();
Counter finishCounter = new Counter();
// @formatter:off
SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init")
.withBeforeHandler(fsm -> initCounter.before.incrementAndGet())
.withAfterHandler(fsm -> initCounter.after.incrementAndGet())
.withProcessor((fsm, event) -> initCounter.process.incrementAndGet())
.end()
.state("intermediate-1")
.withBeforeHandler(fsm -> firstIntermediateCounter.before.incrementAndGet())
.withAfterHandler(fsm -> firstIntermediateCounter.after.incrementAndGet())
.withProcessor((fsm, event) -> firstIntermediateCounter.process.incrementAndGet())
.end()
.state("intermediate-2")
.withBeforeHandler(fsm -> secondIntermediateCounter.before.incrementAndGet())
.withAfterHandler(fsm -> secondIntermediateCounter.after.incrementAndGet())
.withProcessor((fsm, event) -> secondIntermediateCounter.process.incrementAndGet())
.end()
.finish("finish")
.withBeforeHandler(fsm -> finishCounter.before.incrementAndGet())
.withAfterHandler(fsm -> finishCounter.after.incrementAndGet())
.withProcessor((fsm, event) -> finishCounter.process.incrementAndGet())
.end()
.withTransition()
.from("init")
.to("intermediate-1")
.checking((fsm, event) -> true)
.end()
.withTransition()
.from("intermediate-1")
.to("intermediate-2")
.checking((fsm, event) -> true)
.end()
.withTransition()
.from("intermediate-2")
.to("finish")
.checking((fsm, event) -> true)
.end()
.create()
;
// @formatter:on
simpleFsm.process("");
simpleFsm.process("");
simpleFsm.process("");
Assert.assertEquals("finish", simpleFsm.getCurrentState().getName());
Assert.assertEquals(1, initCounter.before.get());
Assert.assertEquals(1, initCounter.after.get());
Assert.assertEquals(1, initCounter.process.get());
Assert.assertEquals(1, firstIntermediateCounter.before.get());
Assert.assertEquals(1, firstIntermediateCounter.after.get());
Assert.assertEquals(1, firstIntermediateCounter.process.get());
Assert.assertEquals(1, secondIntermediateCounter.before.get());
Assert.assertEquals(1, secondIntermediateCounter.after.get());
Assert.assertEquals(1, secondIntermediateCounter.process.get());
Assert.assertEquals(1, finishCounter.before.get());
Assert.assertEquals(0, finishCounter.after.get());
Assert.assertEquals(0, finishCounter.process.get());
}
@Test
public void newSyntaxOfTransitions() {
Counter initCounter = new Counter();
Counter firstIntermediateCounter = new Counter();
Counter secondIntermediateCounter = new Counter();
Counter finishCounter = new Counter();
// @formatter:off
SimpleFsm<String> simpleFsm = Fsm
.<SimpleFsm<String>, String>from(SimpleFsm::new)
.withStates()
.from("init")
.withBeforeHandler(fsm -> initCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> initCounter.process.incrementAndGet())
.withAfterHandler(fsm -> initCounter.after.incrementAndGet())
.withTransition()
.to("intermediate-1")
.checking((fsm, event) -> true)
.endTransition()
.end()
.state("intermediate-1")
.withBeforeHandler(fsm -> firstIntermediateCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> firstIntermediateCounter.process.incrementAndGet())
.withAfterHandler(fsm -> firstIntermediateCounter.after.incrementAndGet())
.withTransition()
.from("intermediate-1")
.to("intermediate-2")
.checking((fsm, event) -> true)
.endTransition()
.end()
.state("intermediate-2")
.withBeforeHandler(fsm -> secondIntermediateCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> secondIntermediateCounter.process.incrementAndGet())
.withAfterHandler(fsm -> secondIntermediateCounter.after.incrementAndGet())
.end()
.finish("finish")
.withBeforeHandler(fsm -> finishCounter.before.incrementAndGet())
.withProcessor((fsm, event) -> finishCounter.process.incrementAndGet())
.withAfterHandler(fsm -> finishCounter.after.incrementAndGet())
.withTransition()
.from("intermediate-2")
.to("finish")
.checking((fsm, event) -> true)
.endTransition()
.end()
.create()
;
// @formatter:on
simpleFsm.process("");
simpleFsm.process("");
simpleFsm.process("");
Assert.assertEquals("finish", simpleFsm.getCurrentState().getName());
Assert.assertEquals(1, initCounter.before.get());
Assert.assertEquals(1, initCounter.after.get());
Assert.assertEquals(1, initCounter.process.get());
Assert.assertEquals(1, firstIntermediateCounter.before.get());
Assert.assertEquals(1, firstIntermediateCounter.after.get());
Assert.assertEquals(1, firstIntermediateCounter.process.get());
Assert.assertEquals(1, secondIntermediateCounter.before.get());
Assert.assertEquals(1, secondIntermediateCounter.after.get());
Assert.assertEquals(1, secondIntermediateCounter.process.get());
Assert.assertEquals(1, finishCounter.before.get());
Assert.assertEquals(0, finishCounter.after.get());
Assert.assertEquals(0, finishCounter.process.get());
}
static class Counter {
final AtomicInteger before = new AtomicInteger(0);
final AtomicInteger after = new AtomicInteger(0);
final AtomicInteger process = new AtomicInteger(0);
}
} }