P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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();
    }
 
}