Compare commits
No commits in common. "master" and "2.1.8" have entirely different histories.
2
pom.xml
2
pom.xml
@ -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.8</version>
|
||||||
|
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
|||||||
25
readme.md
25
readme.md
@ -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:
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
@ -305,7 +278,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();
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -262,78 +262,6 @@ public class FsmTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 {
|
static class Counter {
|
||||||
final AtomicInteger before = new AtomicInteger(0);
|
final AtomicInteger before = new AtomicInteger(0);
|
||||||
final AtomicInteger after = new AtomicInteger(0);
|
final AtomicInteger after = new AtomicInteger(0);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user