16
16
package io .serverlessworkflow .impl ;
17
17
18
18
import com .fasterxml .jackson .databind .JsonNode ;
19
- import io .serverlessworkflow .api .types .FlowDirective ;
20
- import io .serverlessworkflow .api .types .FlowDirectiveEnum ;
21
19
import io .serverlessworkflow .api .types .TaskBase ;
20
+ import io .serverlessworkflow .impl .executors .TransitionInfo ;
22
21
import java .time .Instant ;
23
22
import java .util .HashMap ;
24
23
import java .util .Map ;
24
+ import java .util .Optional ;
25
25
26
- public class TaskContext < T extends TaskBase > {
26
+ public class TaskContext {
27
27
28
28
private final JsonNode rawInput ;
29
- private final T task ;
29
+ private final TaskBase task ;
30
30
private final WorkflowPosition position ;
31
31
private final Instant startedAt ;
32
+ private final String taskName ;
33
+ private final Map <String , Object > contextVariables ;
34
+ private final Optional <TaskContext > parentContext ;
32
35
33
36
private JsonNode input ;
34
37
private JsonNode output ;
35
38
private JsonNode rawOutput ;
36
- private FlowDirective flowDirective ;
37
- private Map <String , Object > contextVariables ;
38
39
private Instant completedAt ;
40
+ private TransitionInfo transition ;
39
41
40
- public TaskContext (JsonNode input , WorkflowPosition position ) {
41
- this (input , null , position , Instant .now (), input , input , input , null , new HashMap <>());
42
- }
43
-
44
- public TaskContext (JsonNode input , TaskContext <?> taskContext , T task ) {
45
- this (
46
- input ,
47
- task ,
48
- taskContext .position ,
49
- Instant .now (),
50
- input ,
51
- input ,
52
- input ,
53
- task .getThen (),
54
- new HashMap <>(taskContext .variables ()));
42
+ public TaskContext (
43
+ JsonNode input ,
44
+ WorkflowPosition position ,
45
+ Optional <TaskContext > parentContext ,
46
+ String taskName ,
47
+ TaskBase task ) {
48
+ this (input , parentContext , taskName , task , position , Instant .now (), input , input , input );
55
49
}
56
50
57
51
private TaskContext (
58
52
JsonNode rawInput ,
59
- T task ,
53
+ Optional <TaskContext > parentContext ,
54
+ String taskName ,
55
+ TaskBase task ,
60
56
WorkflowPosition position ,
61
57
Instant startedAt ,
62
58
JsonNode input ,
63
59
JsonNode output ,
64
- JsonNode rawOutput ,
65
- FlowDirective flowDirective ,
66
- Map <String , Object > contextVariables ) {
60
+ JsonNode rawOutput ) {
67
61
this .rawInput = rawInput ;
62
+ this .parentContext = parentContext ;
63
+ this .taskName = taskName ;
68
64
this .task = task ;
69
65
this .position = position ;
70
66
this .startedAt = startedAt ;
71
67
this .input = input ;
72
68
this .output = output ;
73
69
this .rawOutput = rawOutput ;
74
- this .flowDirective = flowDirective ;
75
- this . contextVariables = contextVariables ;
70
+ this .contextVariables =
71
+ parentContext . map ( p -> new HashMap <>( p . contextVariables )). orElseGet ( HashMap :: new ) ;
76
72
}
77
73
78
- public TaskContext <T > copy () {
79
- return new TaskContext <T >(
80
- rawInput ,
81
- task ,
82
- position .copy (),
83
- startedAt ,
84
- input ,
85
- output ,
86
- rawOutput ,
87
- flowDirective ,
88
- new HashMap <>(contextVariables ));
74
+ public TaskContext copy () {
75
+ return new TaskContext (
76
+ rawInput , parentContext , taskName , task , position , startedAt , input , output , rawOutput );
89
77
}
90
78
91
79
public void input (JsonNode input ) {
@@ -102,54 +90,64 @@ public JsonNode rawInput() {
102
90
return rawInput ;
103
91
}
104
92
105
- public T task () {
93
+ public TaskBase task () {
106
94
return task ;
107
95
}
108
96
109
- public void rawOutput (JsonNode output ) {
97
+ public TaskContext rawOutput (JsonNode output ) {
110
98
this .rawOutput = output ;
111
99
this .output = output ;
100
+ return this ;
112
101
}
113
102
114
103
public JsonNode rawOutput () {
115
104
return rawOutput ;
116
105
}
117
106
118
- public void output (JsonNode output ) {
107
+ public TaskContext output (JsonNode output ) {
119
108
this .output = output ;
109
+ return this ;
120
110
}
121
111
122
112
public JsonNode output () {
123
113
return output ;
124
114
}
125
115
126
- public void flowDirective (FlowDirective flowDirective ) {
127
- this .flowDirective = flowDirective ;
128
- }
129
-
130
- public FlowDirective flowDirective () {
131
- return flowDirective == null
132
- ? new FlowDirective ().withFlowDirectiveEnum (FlowDirectiveEnum .CONTINUE )
133
- : flowDirective ;
116
+ public WorkflowPosition position () {
117
+ return position ;
134
118
}
135
119
136
120
public Map <String , Object > variables () {
137
121
return contextVariables ;
138
122
}
139
123
140
- public WorkflowPosition position () {
141
- return position ;
142
- }
143
-
144
124
public Instant startedAt () {
145
125
return startedAt ;
146
126
}
147
127
148
- public void completedAt (Instant instant ) {
128
+ public Optional <TaskContext > parent () {
129
+ return parentContext ;
130
+ }
131
+
132
+ public String taskName () {
133
+ return taskName ;
134
+ }
135
+
136
+ public TaskContext completedAt (Instant instant ) {
149
137
this .completedAt = instant ;
138
+ return this ;
150
139
}
151
140
152
141
public Instant completedAt () {
153
142
return completedAt ;
154
143
}
144
+
145
+ public TransitionInfo transition () {
146
+ return transition ;
147
+ }
148
+
149
+ public TaskContext transition (TransitionInfo transition ) {
150
+ this .transition = transition ;
151
+ return this ;
152
+ }
155
153
}
0 commit comments