1919import com .fasterxml .jackson .databind .ObjectMapper ;
2020import io .serverlessworkflow .api .WorkflowFormat ;
2121import io .serverlessworkflow .api .types .ExportAs ;
22- import io .serverlessworkflow .api .types .FlowDirective ;
2322import io .serverlessworkflow .api .types .InputFrom ;
2423import io .serverlessworkflow .api .types .OutputAs ;
2524import io .serverlessworkflow .api .types .SchemaExternal ;
2625import io .serverlessworkflow .api .types .SchemaInline ;
2726import io .serverlessworkflow .api .types .SchemaUnion ;
28- import io .serverlessworkflow .api .types .TaskBase ;
29- import io .serverlessworkflow .api .types .TaskItem ;
3027import io .serverlessworkflow .impl .expressions .Expression ;
3128import io .serverlessworkflow .impl .expressions .ExpressionFactory ;
3229import io .serverlessworkflow .impl .expressions .ExpressionUtils ;
3835import java .io .IOException ;
3936import java .io .InputStream ;
4037import java .io .UncheckedIOException ;
41- import java .util .List ;
42- import java .util .ListIterator ;
4338import java .util .Map ;
4439import java .util .Optional ;
4540
@@ -48,11 +43,12 @@ public class WorkflowUtils {
4843 private WorkflowUtils () {}
4944
5045 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 ));
5348 }
5449
55- public static Optional <JsonNode > schemaToNode (ResourceLoader resourceLoader , SchemaUnion schema ) {
50+ private static Optional <JsonNode > schemaToNode (
51+ ResourceLoader resourceLoader , SchemaUnion schema ) {
5652 if (schema != null ) {
5753 if (schema .getSchemaInline () != null ) {
5854 SchemaInline inline = schema .getSchemaInline ();
@@ -94,18 +90,22 @@ public static Optional<WorkflowFilter> buildWorkflowFilter(
9490
9591 public static StringFilter buildStringFilter (
9692 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 );
9896 }
9997
10098 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 );
102102 }
103103
104- public static StringFilter from (WorkflowFilter filter ) {
104+ private static StringFilter toString (WorkflowFilter filter ) {
105105 return (w , t ) -> filter .apply (w , t , t .input ()).asText ();
106106 }
107107
108- private static StringFilter from (String literal ) {
108+ private static StringFilter toString (String literal ) {
109109 return (w , t ) -> literal ;
110110 }
111111
@@ -124,76 +124,19 @@ private static WorkflowFilter buildWorkflowFilter(
124124 throw new IllegalStateException ("Both object and str are null" );
125125 }
126126
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 );
145132 }
146133
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 ();
178136 }
179137
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 ;
197140 }
198141
199142 public static WorkflowFilter buildWorkflowFilter (ExpressionFactory exprFactory , String str ) {
0 commit comments