P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package foundation.workflow;
 
import java.util.ArrayList;
import java.util.List;
 
import foundation.dao.DataPackage;
import foundation.dao.DataReader;
import foundation.dao.DataWriter;
import foundation.dao.OperatorCode;
import foundation.dao.bizlogic.IActionProvider;
import foundation.handler.ResultPool;
import foundation.json.IJSONWriter;
import foundation.util.ContentBuilder;
import foundation.util.ID;
import foundation.util.Util;
 
public class WorkStep {
 
    private DataReader dataReader;
    private DataWriter dataWriter;
    private WorkFlowRuntime workFlow;
    private WorkStepMeta actionMeta;
    private String dataName;
    private DataPackage dataPackage;
    private IActionProvider provider;
    private String param;
    private Event event;
    private List<WorkStep> children;
    private int currentStepNo;
    private String instanceId;
    
    public WorkStep() {
        workFlow = new WorkFlowRuntime();
        instanceId = ID.newValue();
    }
    
    public WorkStep(DataReader dataReader, DataWriter dataWriter, IWorkStep step) {
        //1.
        this.dataName = step.getDataName();
        
        this.dataReader = dataReader;
        this.dataWriter = dataWriter;
        this.actionMeta = (WorkStepMeta)step;
        this.param = step.getParam();
        //2.
        provider = step.createActionProvider();
    }    
    
    public WorkStep(WorkFlowRuntime workFlow, IWorkStep step) {
        //1.
        this.workFlow = workFlow;
        this.dataName = step.getDataName();
        
        this.dataReader = workFlow.getDataReader();
        this.dataWriter = workFlow.getDataWriter();    
        
        this.actionMeta = (WorkStepMeta)step;
        this.param = step.getParam();
        //2.
        provider = step.createActionProvider();
    }
 
    public WorkStep newInstance() {
        WorkStep result = new WorkStep();
        
        result.workFlow = workFlow;
        result.dataReader = dataReader;
        result.dataWriter = dataWriter;
        result.dataName = dataName;
            
        return result;
    }
 
    public void setDataPackage(DataPackage dataPackage) {
        this.dataPackage = dataPackage;
    }
 
    public String getInstanceId() {
        return instanceId;
    }
 
    public WorkFlowRuntime getWorkFlow() {
        return workFlow;
    }
 
    public double getTransactionScore() {
        return actionMeta.getTransactionScore();
    }
    
    public void terminateTask() {
        workFlow.terminate();
    }
 
    public boolean isTerminate() {
        return workFlow.isTerminated();
    }
    
    public void setSimulate() {
        workFlow.setSimulate(true);
    }
    
    public boolean isSimulate() {
        return workFlow.isSimulate();
    }
    
    public int getCurrentStepNo() {
        return currentStepNo;
    }
    
    public void goNextStep() {
        currentStepNo++;
    }
 
    public OperatorCode getOperatorCode() {
        return actionMeta.getOperatorCode();
    }
    
    public DataReader getDataReader() {
        return dataReader;
    }
 
    public DataWriter getDataWriter() {
        return dataWriter;
    }
 
    public ResultPool getResultPool() {
        return dataWriter.getResultPool();
    }
 
    public String getDataName() throws Exception {
        if (dataName == null) {
            return dataReader.getString("dataname");
        }
 
        return dataName;
    }
 
    public DataPackage getDataPackage() throws Exception {
        if (dataPackage == null) {
            return dataReader.getDataPackage();
        }
 
        return dataPackage;
    }
 
    public IActionProvider getActionProvider() {
        return provider;
    }
 
    public String getStepParam() {
        if (Util.isEmpty(param)) {
            return actionMeta.getActionParam();
        }
        
        return param;
    }
 
    public String getParam() {
        return param;
    }
    
    public String getActionName() {
        return param;
    }
    
    public String getMethodName() {
        if (actionMeta != null) {
            return actionMeta.getMethodName();
        }
        
        return null;
    }
    
    public String getDescription() {
        String actionName = actionMeta.getActionName();
        
        if (event != null) {
            String onEvent = event.getCode();
            actionName = actionName + "(" + onEvent + ")";
        }
        
        return actionName;
    }
 
    public void setWorkFlow(WorkFlowRuntime workFlow) {
        this.workFlow = workFlow;
    }
 
    public List<WorkStep> getChildren() {
        return children;
    }
 
    public void addOneChild(WorkStepMeta meta) {
        if (meta == null) {
            return;
        }
        
        if (children == null) {
            children = new ArrayList<WorkStep>();
        }
        
        WorkStep step = new WorkStep(dataReader, dataWriter, meta);
        children.add(step);
    }
 
    public void addChildren(List<WorkStepMeta> metas) {
        if (metas == null || metas.isEmpty()) {
            return;
        }
        
        if (children == null) {
            children = new ArrayList<WorkStep>();
        }
        
        for (WorkStepMeta stepMeta: metas) {
            WorkStep step = new WorkStep(workFlow, stepMeta);
            children.add(step);
        }
    }
 
    public void setEvent(Event event) {
        this.event = event;
    }
 
    public Event getEvent() {
        return event;
    }
    
    public <T>T getSender(Class<T> clazz, Moment moment) {
        if (children != null) {
            for (WorkStep child : children) {
                T result = child.getSender(clazz, moment);
 
                if (result != null) {
                    return (T)result;
                }
            }
        }
        
        if (event == null || event.getMoment() != moment) {
            return null;
        }
        
        return event.getSender(clazz);
    }
 
    public <T>T getWorkflowSender(Class<T> clazz, Moment moment, EventScope scope) {
        if (EventScope.CurrentStep == scope) {
            return getSender(clazz, moment);
        }
        else if (EventScope.CurrentSteps == scope) {
            return getSender(clazz, moment);
        }
        else if (EventScope.Children == scope) {
            for (WorkStep child : children) {
                T result = child.getSender(clazz, moment);
 
                if (result != null) {
                    return (T)result;
                }
            }
            
            return null;
        }
        
        return workFlow.getSender(clazz, moment);
    }
 
    public void setProvider(IActionProvider provider) {
        this.provider = provider;
    }
 
    public void setParam(String param) {
        this.param = param;
    }
 
    public String getParam(String paramName, String defaultValue) {
        if(dataReader != null) {
            return dataReader.getString(paramName, defaultValue);
        }
        
        return defaultValue;
    }
 
    public void toString(ContentBuilder result) {
        result.append(actionMeta.toString());
    }
    
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        
        writer.write("param", param);
        writer.write("description", getDescription());
        
        writer.beginArray("children");
        
        if (children != null) {
            for (WorkStep child: children) {
                child.writeJSON(writer);
            }
        }
        
        writer.endArray();
        
        writer.endObject();
    }
}