|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.serverlessworkflow.impl.executors; |
| 18 | + |
| 19 | +import static io.serverlessworkflow.impl.executors.JavaCallExecutor.safeObject; |
| 20 | + |
| 21 | +import io.serverlessworkflow.api.types.ForTask; |
| 22 | +import io.serverlessworkflow.api.types.ForTaskFunction; |
| 23 | +import io.serverlessworkflow.api.types.Workflow; |
| 24 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 25 | +import io.serverlessworkflow.impl.WorkflowFilter; |
| 26 | +import io.serverlessworkflow.impl.WorkflowPosition; |
| 27 | +import io.serverlessworkflow.impl.WorkflowUtils; |
| 28 | +import io.serverlessworkflow.impl.executors.ForExecutor.ForExecutorBuilder; |
| 29 | +import io.serverlessworkflow.impl.expressions.LoopPredicateIndex; |
| 30 | +import io.serverlessworkflow.impl.resources.ResourceLoader; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +public class JavaForExecutorBuilder extends ForExecutorBuilder { |
| 34 | + |
| 35 | + protected JavaForExecutorBuilder( |
| 36 | + WorkflowPosition position, |
| 37 | + ForTask task, |
| 38 | + Workflow workflow, |
| 39 | + WorkflowApplication application, |
| 40 | + ResourceLoader resourceLoader) { |
| 41 | + super(position, task, workflow, application, resourceLoader); |
| 42 | + if (task instanceof ForTaskFunction taskFunctions) {} |
| 43 | + } |
| 44 | + |
| 45 | + protected Optional<WorkflowFilter> buildWhileFilter() { |
| 46 | + if (task instanceof ForTaskFunction taskFunctions) { |
| 47 | + LoopPredicateIndex whilePred = taskFunctions.getWhilePredicate(); |
| 48 | + String varName = task.getFor().getEach(); |
| 49 | + String indexName = task.getFor().getAt(); |
| 50 | + if (whilePred != null) { |
| 51 | + return Optional.of( |
| 52 | + (w, t, n) -> { |
| 53 | + Object item = safeObject(t.variables().get(varName)); |
| 54 | + return application |
| 55 | + .modelFactory() |
| 56 | + .from( |
| 57 | + item == null |
| 58 | + || whilePred.test( |
| 59 | + n.asJavaObject(), |
| 60 | + item, |
| 61 | + (Integer) safeObject(t.variables().get(indexName)))); |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + return super.buildWhileFilter(); |
| 66 | + } |
| 67 | + |
| 68 | + protected WorkflowFilter buildCollectionFilter() { |
| 69 | + return task instanceof ForTaskFunction taskFunctions |
| 70 | + ? WorkflowUtils.buildWorkflowFilter(application, null, taskFunctions.getCollection()) |
| 71 | + : super.buildCollectionFilter(); |
| 72 | + } |
| 73 | +} |
0 commit comments