package foundation.action;
|
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
|
import foundation.dao.DataPackage;
|
import foundation.dao.DataReader;
|
import foundation.dao.DataWriter;
|
import foundation.handler.ResultPool;
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.util.ContentBuilder;
|
|
public class WorkFlowRuntime implements IWorkflowRuntime, Iterable<ActionContext>, IJSONProvider {
|
|
private String dataName;
|
private String operator;
|
private DataPackage dataPackage;
|
private StateContext stateContext;
|
private ContextBucket workFlowContexts;
|
private List<ActionContext> actionContexts;
|
private double transactionScore;
|
private boolean terminated;
|
private DataReader dataReader;
|
private DataWriter dataWriter;
|
|
|
public WorkFlowRuntime(DataReader dataReader, DataWriter dataWriter, IWorkStep... steps) throws Exception {
|
//1.
|
this.dataReader = dataReader;
|
this.dataWriter = dataWriter;
|
|
transactionScore = 0;
|
terminated = false;
|
actionContexts = new ArrayList<ActionContext>();
|
|
stateContext = new StateContext();
|
workFlowContexts = new ContextBucket();
|
workFlowContexts.addOne(stateContext);
|
|
//2.
|
if (steps == null || steps.length == 0) {
|
return;
|
}
|
|
for (IWorkStep step: steps) {
|
if (step == null) {
|
continue;
|
}
|
|
ActionContext actionContext = new ActionContext(stateContext, this, step);
|
loadOne(actionContext);
|
}
|
}
|
|
public void loadMainSteps(String dataName, String operator, String event, WorkflowBuckets workflowBuckets) throws Exception {
|
this.dataName = dataName;
|
this.operator = operator;
|
|
List<WorkStep> stepList = workflowBuckets.getWorkflow(dataName, operator, event);
|
|
if (stepList == null) {
|
return;
|
}
|
|
for (WorkStep step: stepList) {
|
ActionContext actionContext = new ActionContext(stateContext, this, step);
|
loadOne(actionContext);
|
}
|
}
|
|
public void loadReferToSteps(DataPackage dataPackage, Moment moment, String event, WorkflowBuckets workflowBuckets) {
|
String dataName = dataPackage.getName();
|
List<WorkStep> steps = workflowBuckets.getWorkflow(dataName, moment, event);
|
|
if (steps == null) {
|
return;
|
}
|
|
for (WorkStep step: steps) {
|
ActionContext actionContext = new ActionContext(stateContext, this, step);
|
actionContext.setDataPackage(dataPackage);
|
loadOne(actionContext);
|
}
|
}
|
|
public void loadOne(ActionContext action) {
|
actionContexts.add(action);
|
transactionScore = transactionScore + action.getTransactionScore();
|
}
|
|
public int getActionLength() {
|
return actionContexts.size();
|
}
|
|
public boolean nesessaryTransaction() {
|
return transactionScore >= 1;
|
}
|
|
@Override
|
public Iterator<ActionContext> iterator() {
|
return actionContexts.iterator();
|
}
|
|
public String getDataName() {
|
return dataName;
|
}
|
|
public void terminate() {
|
terminated = true;
|
}
|
|
public boolean isTerminated() {
|
return terminated;
|
}
|
|
public void setDataPackage(DataPackage dataPackage) {
|
this.dataPackage = dataPackage;
|
}
|
|
public DataReader getDataReader() {
|
return dataReader;
|
}
|
|
public void setDataReader(DataReader dataReader) {
|
this.dataReader = dataReader;
|
}
|
|
public DataWriter getDataWriter() {
|
return dataWriter;
|
}
|
|
public void setDataWriter(DataWriter dataWriter) {
|
this.dataWriter = dataWriter;
|
}
|
|
public ResultPool getResultPool() {
|
return dataWriter.getResultPool();
|
}
|
|
public DataPackage getDataPackage() {
|
return dataPackage;
|
}
|
|
public <T> T getContext(Class<?> clazz) {
|
return workFlowContexts.getOne(clazz);
|
}
|
|
public void addContext(Object context) {
|
workFlowContexts.addOne(context);
|
}
|
|
public StateContext getStateContext() {
|
return stateContext;
|
}
|
|
@Override
|
public String toString() {
|
String title = "[" + dataName + "-" + operator + "]: ";
|
ContentBuilder result = new ContentBuilder(title, ", ");
|
|
for (ActionContext action: actionContexts) {
|
action.toString(result);
|
}
|
|
return result.toString();
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginObject();
|
|
writer.write("data_name", dataName);
|
writer.write("operator", operator);
|
writer.write("transaction_score", transactionScore);
|
|
writer.beginArray("actions");
|
for (ActionContext action: actionContexts) {
|
writer.write(action.getDescription());
|
}
|
writer.endArray();
|
|
writer.endObject();
|
}
|
|
}
|