Skip to content

Commit

Permalink
remove dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fulan.zjf committed Feb 11, 2020
1 parent f696802 commit 3310936
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 44 deletions.
25 changes: 0 additions & 25 deletions cola-statemachine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,6 @@
</properties>

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
public class StateMachineFactory {
static Map<String /* machineId */, StateMachine> stateMachineMap = new ConcurrentHashMap<>();

public static void register(StateMachine stateMachine){
public static <S, E, C> void register(StateMachine<S, E, C> stateMachine){
String machineId = stateMachine.getMachineId();
if(stateMachineMap.get(machineId) != null){
throw new StateMachineException("The state machine with id ["+machineId+"] is already built, no need to build again");
}
stateMachineMap.put(stateMachine.getMachineId(), stateMachine);
}

public static StateMachine get(String machineId){
public static <S, E, C> StateMachine<S, E, C> get(String machineId){
StateMachine stateMachine = stateMachineMap.get(machineId);
if(stateMachine == null){
throw new StateMachineException("There is no stateMachine instance for "+machineId+", please build it first");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.alibaba.cola.statemachine.Transition;
import com.alibaba.cola.statemachine.impl.StateHelper;
import com.alibaba.cola.statemachine.impl.TransitionType;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

Expand All @@ -16,7 +15,6 @@
* @author Frank Zhang
* @date 2020-02-07 10:20 PM
*/
@Slf4j
class TransitionBuilderImpl<S,E,C> implements ExternalTransitionBuilder<S,E,C>, InternalTransitionBuilder<S,E,C>, From<S,E,C>, On<S,E,C>, To<S,E,C> {

final Map<S, State<S, E, C>> stateMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.cola.statemachine.impl;

/**
* Debugger, This is used to decouple Logging framework dependency
*
* @author Frank Zhang
* @date 2020-02-11 11:08 AM
*/
public class Debugger {

private static boolean isDebugOn = false;

public static void debug(String message){
if(isDebugOn){
System.out.println(message);
}
}

public static void enableDebug(){
isDebugOn = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.alibaba.cola.statemachine.State;
import com.alibaba.cola.statemachine.Transition;
import com.alibaba.cola.statemachine.Visitor;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.HashMap;
Expand All @@ -15,7 +14,6 @@
* @author Frank Zhang
* @date 2020-02-07 11:19 PM
*/
@Slf4j
public class StateImpl<S,E,C> implements State<S,E,C> {
protected final S stateId;
private HashMap<E, Transition<S, E,C>> transitions = new HashMap<>();
Expand All @@ -32,7 +30,7 @@ public Transition<S, E, C> addTransition(E event, State<S,E,C> target, Transitio
newTransition.setEvent(event);
newTransition.setType(transitionType);

log.debug("Begin to add new transition: "+ newTransition);
Debugger.debug("Begin to add new transition: "+ newTransition);
verify(event, newTransition);
transitions.put(event, newTransition);
return newTransition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.Transition;
import com.alibaba.cola.statemachine.Visitor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Optional;
Expand All @@ -21,16 +18,12 @@
* @author Frank Zhang
* @date 2020-02-07 5:40 PM
*/
@Slf4j
public class StateMachineImpl<S,E,C> implements StateMachine<S, E, C> {

@Getter
@Setter
private String machineId;

private final Map<S, State<S,E,C>> stateMap;

@Setter
private boolean ready;

public StateMachineImpl(Map<S, State< S, E, C>> stateMap){
Expand All @@ -48,7 +41,7 @@ private State<S, E, C> doTransition(State sourceState, E event, C ctx) {
if(transition.isPresent()){
return transition.get().transit(ctx);
}
log.warn("There is no Transition for " + event);
Debugger.debug("There is no Transition for " + event);
return sourceState;
}

Expand Down Expand Up @@ -88,4 +81,16 @@ public void generatePlantUML(){
accept(plantUMLVisitor);
}

@Override
public String getMachineId() {
return machineId;
}

public void setMachineId(String machineId) {
this.machineId = machineId;
}

public void setReady(boolean ready) {
this.ready = ready;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.alibaba.cola.statemachine.Condition;
import com.alibaba.cola.statemachine.State;
import com.alibaba.cola.statemachine.Transition;
import lombok.extern.slf4j.Slf4j;

/**
* TransitionImpl。
Expand All @@ -14,7 +13,6 @@
* @author Frank Zhang
* @date 2020-02-07 10:32 PM
*/
@Slf4j
public class TransitionImpl<S,E,C> implements Transition<S,E,C> {

private State<S, E, C> source;
Expand Down Expand Up @@ -86,7 +84,7 @@ public void setAction(Action<S, E, C> action) {

@Override
public State<S, E, C> transit(C ctx) {
log.debug("Do transition: "+this);
Debugger.debug("Do transition: "+this);
this.verify();
if(condition == null || condition.isSatisfied(ctx)){
if(action != null){
Expand All @@ -95,7 +93,7 @@ public State<S, E, C> transit(C ctx) {
return target;
}

log.debug("Condition is not satisfied, stay at the "+source+" state ");
Debugger.debug("Condition is not satisfied, stay at the "+source+" state ");
return source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import com.alibaba.cola.statemachine.impl.Debugger;
import org.junit.Before;
import org.junit.Test;

import java.util.stream.Stream;
Expand Down Expand Up @@ -81,6 +83,11 @@ public boolean isSystemEvent(){
}
}

@Before
public void init(){
Debugger.enableDebug();
}

@Test
public void testPlantUML(){
StateMachineBuilder<PriceAdjustmentTaskStatusEnum, PriceAdjustmentTaskEventEnum, StateMachineTest.Context> builder = StateMachineBuilderFactory.create();
Expand Down

0 comments on commit 3310936

Please sign in to comment.