19
19
import com .fasterxml .jackson .databind .ObjectMapper ;
20
20
import io .serverlessworkflow .api .WorkflowFormat ;
21
21
import io .serverlessworkflow .api .types .ExportAs ;
22
- import io .serverlessworkflow .api .types .FlowDirective ;
23
22
import io .serverlessworkflow .api .types .InputFrom ;
24
23
import io .serverlessworkflow .api .types .OutputAs ;
25
24
import io .serverlessworkflow .api .types .SchemaExternal ;
26
25
import io .serverlessworkflow .api .types .SchemaInline ;
27
26
import io .serverlessworkflow .api .types .SchemaUnion ;
28
- import io .serverlessworkflow .api .types .TaskBase ;
29
- import io .serverlessworkflow .api .types .TaskItem ;
30
27
import io .serverlessworkflow .impl .expressions .Expression ;
31
28
import io .serverlessworkflow .impl .expressions .ExpressionFactory ;
32
29
import io .serverlessworkflow .impl .expressions .ExpressionUtils ;
38
35
import java .io .IOException ;
39
36
import java .io .InputStream ;
40
37
import java .io .UncheckedIOException ;
41
- import java .util .List ;
42
- import java .util .ListIterator ;
43
38
import java .util .Map ;
44
39
import java .util .Optional ;
45
40
@@ -48,8 +43,8 @@ public class WorkflowUtils {
48
43
private WorkflowUtils () {}
49
44
50
45
public static Optional <SchemaValidator > getSchemaValidator (
51
- SchemaValidatorFactory validatorFactory , Optional < JsonNode > node ) {
52
- return node .map (n -> validatorFactory .getValidator (n ));
46
+ SchemaValidatorFactory validatorFactory , ResourceLoader resourceLoader , SchemaUnion schema ) {
47
+ return schemaToNode ( resourceLoader , schema ) .map (n -> validatorFactory .getValidator (n ));
53
48
}
54
49
55
50
public static Optional <JsonNode > schemaToNode (ResourceLoader resourceLoader , SchemaUnion schema ) {
@@ -94,18 +89,22 @@ public static Optional<WorkflowFilter> buildWorkflowFilter(
94
89
95
90
public static StringFilter buildStringFilter (
96
91
ExpressionFactory exprFactory , String expression , String literal ) {
97
- return expression != null ? from (buildWorkflowFilter (exprFactory , expression )) : from (literal );
92
+ return expression != null
93
+ ? toString (buildWorkflowFilter (exprFactory , expression ))
94
+ : toString (literal );
98
95
}
99
96
100
97
public static StringFilter buildStringFilter (ExpressionFactory exprFactory , String str ) {
101
- return ExpressionUtils .isExpr (str ) ? from (buildWorkflowFilter (exprFactory , str )) : from (str );
98
+ return ExpressionUtils .isExpr (str )
99
+ ? toString (buildWorkflowFilter (exprFactory , str ))
100
+ : toString (str );
102
101
}
103
102
104
- public static StringFilter from (WorkflowFilter filter ) {
103
+ private static StringFilter toString (WorkflowFilter filter ) {
105
104
return (w , t ) -> filter .apply (w , t , t .input ()).asText ();
106
105
}
107
106
108
- private static StringFilter from (String literal ) {
107
+ private static StringFilter toString (String literal ) {
109
108
return (w , t ) -> literal ;
110
109
}
111
110
@@ -124,76 +123,19 @@ private static WorkflowFilter buildWorkflowFilter(
124
123
throw new IllegalStateException ("Both object and str are null" );
125
124
}
126
125
127
- private static TaskItem findTaskByName (ListIterator <TaskItem > iter , String taskName ) {
128
- int currentIndex = iter .nextIndex ();
129
- while (iter .hasPrevious ()) {
130
- TaskItem item = iter .previous ();
131
- if (item .getName ().equals (taskName )) {
132
- return item ;
133
- }
134
- }
135
- while (iter .nextIndex () < currentIndex ) {
136
- iter .next ();
137
- }
138
- while (iter .hasNext ()) {
139
- TaskItem item = iter .next ();
140
- if (item .getName ().equals (taskName )) {
141
- return item ;
142
- }
143
- }
144
- throw new IllegalArgumentException ("Cannot find task with name " + taskName );
126
+ public static LongFilter buildLongFilter (
127
+ ExpressionFactory exprFactory , String expression , Long literal ) {
128
+ return expression != null
129
+ ? toLong (buildWorkflowFilter (exprFactory , expression ))
130
+ : toLong (literal );
145
131
}
146
132
147
- public static void processTaskList (
148
- List <TaskItem > tasks , WorkflowContext context , TaskContext <?> parentTask ) {
149
- parentTask .position ().addProperty ("do" );
150
- TaskContext <? extends TaskBase > currentContext = parentTask ;
151
- if (!tasks .isEmpty ()) {
152
- ListIterator <TaskItem > iter = tasks .listIterator ();
153
- TaskItem nextTask = iter .next ();
154
- while (nextTask != null ) {
155
- TaskItem task = nextTask ;
156
- parentTask .position ().addIndex (iter .previousIndex ());
157
- currentContext = executeTask (context , parentTask , task , currentContext .output ());
158
- FlowDirective flowDirective = currentContext .flowDirective ();
159
- if (flowDirective .getFlowDirectiveEnum () != null ) {
160
- switch (flowDirective .getFlowDirectiveEnum ()) {
161
- case CONTINUE :
162
- nextTask = iter .hasNext () ? iter .next () : null ;
163
- break ;
164
- case END :
165
- context .instance ().state (WorkflowState .COMPLETED );
166
- case EXIT :
167
- nextTask = null ;
168
- break ;
169
- }
170
- } else {
171
- nextTask = WorkflowUtils .findTaskByName (iter , flowDirective .getString ());
172
- }
173
- parentTask .position ().back ();
174
- }
175
- }
176
- parentTask .position ().back ();
177
- parentTask .rawOutput (currentContext .output ());
133
+ private static LongFilter toLong (WorkflowFilter filter ) {
134
+ return (w , t ) -> filter .apply (w , t , t .input ()).asLong ();
178
135
}
179
136
180
- public static TaskContext <?> executeTask (
181
- WorkflowContext context , TaskContext <?> parentTask , TaskItem task , JsonNode input ) {
182
- parentTask .position ().addProperty (task .getName ());
183
- TaskContext <?> result =
184
- context
185
- .definition ()
186
- .taskExecutors ()
187
- .computeIfAbsent (
188
- parentTask .position ().jsonPointer (),
189
- k ->
190
- context
191
- .definition ()
192
- .taskFactory ()
193
- .getTaskExecutor (task .getTask (), context .definition ()))
194
- .apply (context , parentTask , input );
195
- parentTask .position ().back ();
196
- return result ;
137
+ private static LongFilter toLong (Long literal ) {
138
+ return (w , t ) -> literal ;
197
139
}
198
140
199
141
public static WorkflowFilter buildWorkflowFilter (ExpressionFactory exprFactory , String str ) {
0 commit comments