-
Notifications
You must be signed in to change notification settings - Fork 58
/
WorkflowFunction.java
74 lines (66 loc) · 2.55 KB
/
WorkflowFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.serving.workflow.function;
import ai.djl.serving.wlm.WorkLoadManager;
import ai.djl.serving.workflow.Workflow;
import ai.djl.serving.workflow.WorkflowExpression.Item;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* A lambda function that can be run within a {@link Workflow}.
*
* @see #run(Workflow.WorkflowExecutor, List)
*/
public abstract class WorkflowFunction implements AutoCloseable {
/**
* The lambda function that is run.
*
* @param executor an executor that can be used to run expressions or models
* @param args the list of function arguments
* @return a future containing the input
*/
public abstract CompletableFuture<Item> run(
Workflow.WorkflowExecutor executor, List<Workflow.WorkflowArgument> args);
/**
* A helper to evaluate all function arguments.
*
* @param args the arguments to evaluate
* @return a future with the list of evaluated arguments
*/
@SuppressWarnings("unchecked")
protected CompletableFuture<List<Item>> evaluateArgs(List<Workflow.WorkflowArgument> args) {
CompletableFuture<Item>[] processedArgs =
args.stream()
.map(Workflow.WorkflowArgument::evaluate)
.toArray(CompletableFuture[]::new);
return CompletableFuture.allOf(processedArgs)
.thenApply(
v ->
Arrays.stream(processedArgs)
.map(CompletableFuture::join)
.collect(Collectors.toList()));
}
/**
* Prepares this {@link WorkflowFunction}.
*
* @param wlm the wlm
* @param configs the workflow configs
*/
public void prepare(WorkLoadManager wlm, Map<String, Map<String, Object>> configs) {}
/** {@inheritDoc} */
@Override
public void close() {}
}