策略调用增加参数

增加策略调用的无级嵌套
This commit is contained in:
bryan.zhang
2017-12-18 20:25:13 +08:00
parent eefbe197d8
commit 809b063425
11 changed files with 165 additions and 8 deletions

View File

@@ -5,7 +5,7 @@
<artifactId>liteflow</artifactId>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<version>1.2.3</version>
<version>1.2.4</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@@ -64,8 +64,8 @@ public class FlowExecutor {
return execute(chainId, param, slotClazz,null,false);
}
public <T> T invoke(String chainId,Class<? extends Slot> slotClazz,Integer slotIndex){
return execute(chainId,null, slotClazz,slotIndex,true);
public void invoke(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex){
execute(chainId, param, slotClazz,slotIndex,true);
}
public <T> T execute(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex,boolean isInnerChain){
@@ -95,8 +95,10 @@ public class FlowExecutor {
throw new NoAvailableSlotException("the slot is not exist");
}
if(!isInnerChain && param != null) {
if(!isInnerChain) {
slot.setRequestData(param);
}else {
slot.setChainReqData(chainId, param);
}
List<Condition> conditionList = chain.getConditionList();

View File

@@ -34,6 +34,8 @@ public abstract class AbsSlot implements Slot{
private final String NODE_OUTPUT_PREFIX = "output_";
private final String CHAIN_REQ_PREFIX = "chain_req_";
private Deque<CmpStep> executeSteps = new ArrayDeque<CmpStep>();
protected ConcurrentHashMap<String, Object> dataMap = new ConcurrentHashMap<String, Object>();
@@ -70,6 +72,14 @@ public abstract class AbsSlot implements Slot{
dataMap.put(RESPONSE, t);
}
public <T> T getChainReqData(String chainId) {
return (T)dataMap.get(CHAIN_REQ_PREFIX + chainId);
}
public <T> void setChainReqData(String chainId, T t) {
dataMap.put(CHAIN_REQ_PREFIX + chainId, t);
}
public <T> T getData(String key){
return (T)dataMap.get(key);
}

View File

@@ -24,6 +24,10 @@ public interface Slot {
public <T> T getResponseData();
public <T> void setChainReqData(String chainId, T t);
public <T> T getChainReqData(String chainId);
public <T> void setResponseData(T t);
public <T> T getData(String key);

View File

@@ -26,7 +26,7 @@ public class HComponent extends NodeComponent {
@Override
public void process() {
System.out.println("Hcomponent executed!");
flowExecutor.invoke("strategy", DefaultSlot.class, this.getSlotIndex());
flowExecutor.invoke("strategy1",3, DefaultSlot.class, this.getSlotIndex());
}
}

View File

@@ -0,0 +1,32 @@
/**
* <p>Title: liteFlow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2017-8-1
* @version 1.0
*/
package com.thebeastshop.liteflow.test.component;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.thebeastshop.liteflow.core.FlowExecutor;
import com.thebeastshop.liteflow.core.NodeComponent;
import com.thebeastshop.liteflow.entity.data.DefaultSlot;
@Component("m3")
public class M3Component extends NodeComponent {
@Resource
private FlowExecutor flowExecutor;
@Override
public void process() {
System.out.println("m3 component executed!");
flowExecutor.invoke("strategy2",10, DefaultSlot.class, this.getSlotIndex());
}
}

View File

@@ -26,7 +26,15 @@ public class MComponent extends NodeCondComponent {
@Override
protected String processCond() throws Exception {
System.out.println("m conponent executed");
return "m1";
Integer flag = this.getSlot().getChainReqData("strategy1");
if(flag == 1) {
return "m1";
}else if(flag == 2){
return "m2";
}else {
return "m3";
}
}
}

View File

@@ -0,0 +1,30 @@
/**
* <p>Title: liteFlow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2017-8-1
* @version 1.0
*/
package com.thebeastshop.liteflow.test.component;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.thebeastshop.liteflow.core.FlowExecutor;
import com.thebeastshop.liteflow.core.NodeComponent;
@Component("p1")
public class P1Component extends NodeComponent {
@Resource
private FlowExecutor flowExecutor;
@Override
public void process() {
System.out.println("p1 component executed!");
}
}

View File

@@ -0,0 +1,30 @@
/**
* <p>Title: liteFlow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2017-8-1
* @version 1.0
*/
package com.thebeastshop.liteflow.test.component;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.thebeastshop.liteflow.core.FlowExecutor;
import com.thebeastshop.liteflow.core.NodeComponent;
@Component("p2")
public class P2Component extends NodeComponent {
@Resource
private FlowExecutor flowExecutor;
@Override
public void process() {
System.out.println("p2 component executed!");
}
}

View File

@@ -0,0 +1,37 @@
/**
* <p>Title: liteFlow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2017-8-1
* @version 1.0
*/
package com.thebeastshop.liteflow.test.component;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.thebeastshop.liteflow.core.FlowExecutor;
import com.thebeastshop.liteflow.core.NodeCondComponent;
@Component("p")
public class PComponent extends NodeCondComponent {
@Resource
private FlowExecutor flowExecutor;
@Override
protected String processCond() throws Exception {
System.out.println("p conponent executed");
Integer flag = this.getSlot().getChainReqData("strategy2");
if(flag == 10) {
return "p1";
}else {
return "p2";
}
}
}

View File

@@ -27,7 +27,11 @@
<then value="a,c,h,g"/>
</chain>
<chain name="strategy">
<then value="m(m1|m2)"/>
<chain name="strategy1">
<then value="m(m1|m2|m3)"/>
</chain>
<chain name="strategy2">
<then value="p(p1|p2)"/>
</chain>
</flow>