Search in sources :

Example 21 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LiteralMapDeserializer method deserialize.

@Override
public JacksonLiteralMap deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    Map<String, Literal> literalMap = new HashMap<>();
    verifyToken(p, JsonToken.START_OBJECT);
    p.nextToken();
    while (p.currentToken() != JsonToken.END_OBJECT) {
        verifyToken(p, JsonToken.FIELD_NAME);
        String fieldName = p.currentName();
        LiteralType literalType = literalTypeMap.get(fieldName);
        if (literalType == null) {
            throw new IllegalStateException("Unexpected field [" + fieldName + "]");
        }
        p.nextToken();
        literalMap.put(fieldName, deserialize(p, ctxt, literalType));
        p.nextToken();
    }
    return new JacksonLiteralMap(unmodifiableMap(literalMap));
}
Also used : HashMap(java.util.HashMap) Literal(org.flyte.api.v1.Literal) LiteralType(org.flyte.api.v1.LiteralType)

Example 22 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LiteralMapDeserializer method deserialize.

private static Literal deserialize(JsonParser p, DeserializationContext ctxt, LiteralType literalType) throws IOException {
    switch(literalType.getKind()) {
        case SIMPLE_TYPE:
            return deserialize(p, literalType.simpleType());
        case COLLECTION_TYPE:
            List<Literal> collection = new ArrayList<>();
            verifyToken(p, JsonToken.START_ARRAY);
            while (p.nextToken() != JsonToken.END_ARRAY) {
                collection.add(deserialize(p, ctxt, literalType.collectionType()));
            }
            return Literal.ofCollection(unmodifiableList(collection));
        case MAP_VALUE_TYPE:
            Map<String, Literal> map = new HashMap<>();
            verifyToken(p, JsonToken.START_OBJECT);
            p.nextToken();
            while (p.currentToken() != JsonToken.END_OBJECT) {
                verifyToken(p, JsonToken.FIELD_NAME);
                String fieldName = p.currentName();
                p.nextToken();
                map.put(fieldName, deserialize(p, ctxt, literalType.mapValueType()));
                p.nextToken();
            }
            return Literal.ofMap(unmodifiableMap(map));
        case BLOB_TYPE:
            JavaType type = ctxt.constructType(Blob.class);
            Blob blob = (Blob) ctxt.findNonContextualValueDeserializer(type).deserialize(p, ctxt);
            return Literal.ofScalar(Scalar.ofBlob(blob));
        case SCHEMA_TYPE:
            throw new IllegalArgumentException(String.format("Unsupported LiteralType.Kind: [%s]", literalType.getKind()));
    }
    throw new AssertionError(String.format("Unexpected LiteralType.Kind: [%s]", literalType.getKind()));
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) Blob(org.flyte.api.v1.Blob) HashMap(java.util.HashMap) Literal(org.flyte.api.v1.Literal) ArrayList(java.util.ArrayList)

Example 23 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LocalEngineTest method testBindingMap.

@Test
public void testBindingMap() {
    String workflowName = new MapWorkflow().getName();
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    WorkflowTemplate workflow = workflows.get(workflowName);
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of());
    // 3 = 1 + 2, 7 = 3 + 4
    Literal i3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3)));
    Literal i7 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(7)));
    assertEquals(ImmutableMap.of("map", Literal.ofMap(ImmutableMap.of("e", i3, "f", i7))), outputs);
}
Also used : WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) RunnableTask(org.flyte.api.v1.RunnableTask) MapWorkflow(org.flyte.localengine.examples.MapWorkflow) Test(org.junit.jupiter.api.Test)

Example 24 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LocalEngineTest method testFibonacci.

@Test
void testFibonacci() {
    String workflowName = new FibonacciWorkflow().getName();
    Literal fib0 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(0L)));
    Literal fib1 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
    Literal fib2 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
    Literal fib3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(2L)));
    Literal fib4 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3L)));
    Literal fib5 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(5L)));
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    TestingListener listener = new TestingListener();
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflows.get(workflowName), tasks, emptyMap(), ImmutableMap.of("fib0", fib0, "fib1", fib1), listener);
    assertEquals(ImmutableMap.of("fib4", fib4, "fib5", fib5), outputs);
    assertEquals(ImmutableList.<List<Object>>builder().add(ofPending("fib-2")).add(ofPending("fib-3")).add(ofPending("fib-4")).add(ofPending("fib-5")).add(ofStarting("fib-2", ImmutableMap.of("a", fib0, "b", fib1))).add(ofCompleted("fib-2", ImmutableMap.of("a", fib0, "b", fib1), ImmutableMap.of("c", fib2))).add(ofStarting("fib-3", ImmutableMap.of("a", fib1, "b", fib2))).add(ofCompleted("fib-3", ImmutableMap.of("a", fib1, "b", fib2), ImmutableMap.of("c", fib3))).add(ofStarting("fib-4", ImmutableMap.of("a", fib2, "b", fib3))).add(ofCompleted("fib-4", ImmutableMap.of("a", fib2, "b", fib3), ImmutableMap.of("c", fib4))).add(ofStarting("fib-5", ImmutableMap.of("a", fib3, "b", fib4))).add(ofCompleted("fib-5", ImmutableMap.of("a", fib3, "b", fib4), ImmutableMap.of("c", fib5))).build(), listener.actions);
}
Also used : WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) List(java.util.List) RunnableTask(org.flyte.api.v1.RunnableTask) FibonacciWorkflow(org.flyte.localengine.examples.FibonacciWorkflow) Test(org.junit.jupiter.api.Test)

Example 25 with Literal

use of org.flyte.api.v1.Literal in project flytekit-java by flyteorg.

the class LocalEngineTest method testBindingCollection.

@Test
public void testBindingCollection() {
    String workflowName = new ListWorkflow().getName();
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    WorkflowTemplate workflow = workflows.get(workflowName);
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of());
    // 3 = 1 + 2, 7 = 3 + 4
    Literal i3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3)));
    Literal i7 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(7)));
    assertEquals(ImmutableMap.of("list", Literal.ofCollection(ImmutableList.of(i3, i7))), outputs);
}
Also used : WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) RunnableTask(org.flyte.api.v1.RunnableTask) ListWorkflow(org.flyte.localengine.examples.ListWorkflow) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (org.flyte.api.v1.Literal)28 Test (org.junit.jupiter.api.Test)16 HashMap (java.util.HashMap)9 Instant (java.time.Instant)7 WorkflowTemplate (org.flyte.api.v1.WorkflowTemplate)7 LiteralType (org.flyte.api.v1.LiteralType)6 RunnableTask (org.flyte.api.v1.RunnableTask)6 Map (java.util.Map)5 Duration (java.time.Duration)4 List (java.util.List)4 Blob (org.flyte.api.v1.Blob)4 Variable (org.flyte.api.v1.Variable)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Literals (flyteidl.core.Literals)3 Struct (org.flyte.api.v1.Struct)3 Var (com.google.errorprone.annotations.Var)2 UncheckedIOException (java.io.UncheckedIOException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Collections.emptyMap (java.util.Collections.emptyMap)2