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,11 +43,12 @@ 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
- public static Optional <JsonNode > schemaToNode (ResourceLoader resourceLoader , SchemaUnion schema ) {
50
+ private static Optional <JsonNode > schemaToNode (
51
+ ResourceLoader resourceLoader , SchemaUnion schema ) {
56
52
if (schema != null ) {
57
53
if (schema .getSchemaInline () != null ) {
58
54
SchemaInline inline = schema .getSchemaInline ();
@@ -94,18 +90,22 @@ public static Optional<WorkflowFilter> buildWorkflowFilter(
94
90
95
91
public static StringFilter buildStringFilter (
96
92
ExpressionFactory exprFactory , String expression , String literal ) {
97
- return expression != null ? from (buildWorkflowFilter (exprFactory , expression )) : from (literal );
93
+ return expression != null
94
+ ? toString (buildWorkflowFilter (exprFactory , expression ))
95
+ : toString (literal );
98
96
}
99
97
100
98
public static StringFilter buildStringFilter (ExpressionFactory exprFactory , String str ) {
101
- return ExpressionUtils .isExpr (str ) ? from (buildWorkflowFilter (exprFactory , str )) : from (str );
99
+ return ExpressionUtils .isExpr (str )
100
+ ? toString (buildWorkflowFilter (exprFactory , str ))
101
+ : toString (str );
102
102
}
103
103
104
- public static StringFilter from (WorkflowFilter filter ) {
104
+ private static StringFilter toString (WorkflowFilter filter ) {
105
105
return (w , t ) -> filter .apply (w , t , t .input ()).asText ();
106
106
}
107
107
108
- private static StringFilter from (String literal ) {
108
+ private static StringFilter toString (String literal ) {
109
109
return (w , t ) -> literal ;
110
110
}
111
111
@@ -124,76 +124,19 @@ private static WorkflowFilter buildWorkflowFilter(
124
124
throw new IllegalStateException ("Both object and str are null" );
125
125
}
126
126
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 );
127
+ public static LongFilter buildLongFilter (
128
+ ExpressionFactory exprFactory , String expression , Long literal ) {
129
+ return expression != null
130
+ ? toLong (buildWorkflowFilter (exprFactory , expression ))
131
+ : toLong (literal );
145
132
}
146
133
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 ());
134
+ private static LongFilter toLong (WorkflowFilter filter ) {
135
+ return (w , t ) -> filter .apply (w , t , t .input ()).asLong ();
178
136
}
179
137
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 ;
138
+ private static LongFilter toLong (Long literal ) {
139
+ return (w , t ) -> literal ;
197
140
}
198
141
199
142
public static WorkflowFilter buildWorkflowFilter (ExpressionFactory exprFactory , String str ) {
0 commit comments