Search in sources :

Example 1 with IfElseBlock

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

the class ProtoUtilTest method shouldSerializeBranchNode.

@Test
void shouldSerializeBranchNode() {
    ComparisonExpression comparison = ComparisonExpression.builder().operator(ComparisonExpression.Operator.EQ).leftValue(Operand.ofVar("a")).rightValue(Operand.ofVar("b")).build();
    Condition.ComparisonExpression comparisonProto = Condition.ComparisonExpression.newBuilder().setOperator(Condition.ComparisonExpression.Operator.EQ).setLeftValue(Condition.Operand.newBuilder().setVar("a").build()).setRightValue(Condition.Operand.newBuilder().setVar("b").build()).build();
    IfBlock ifBlock = IfBlock.builder().condition(BooleanExpression.ofComparison(comparison)).thenNode(Node.builder().id("node-1").upstreamNodeIds(ImmutableList.of()).inputs(ImmutableList.of()).build()).build();
    Workflow.IfBlock ifBlockProto = Workflow.IfBlock.newBuilder().setThenNode(Workflow.Node.newBuilder().setId("node-1").build()).setCondition(Condition.BooleanExpression.newBuilder().setComparison(comparisonProto).build()).build();
    IfElseBlock ifElse = IfElseBlock.builder().case_(ifBlock).other(ImmutableList.of(ifBlock)).elseNode(ifBlock.thenNode()).build();
    Workflow.IfElseBlock ifElseProto = Workflow.IfElseBlock.newBuilder().setCase(ifBlockProto).addOther(ifBlockProto).setElseNode(ifBlockProto.getThenNode()).build();
    BranchNode branchNode = BranchNode.builder().ifElse(ifElse).build();
    Workflow.BranchNode branchNodeProto = Workflow.BranchNode.newBuilder().setIfElse(ifElseProto).build();
    assertThat(ProtoUtil.serialize(branchNode), equalTo(branchNodeProto));
}
Also used : Condition(flyteidl.core.Condition) ComparisonExpression(org.flyte.api.v1.ComparisonExpression) IfElseBlock(org.flyte.api.v1.IfElseBlock) BranchNode(org.flyte.api.v1.BranchNode) Workflow(flyteidl.core.Workflow) IfBlock(org.flyte.api.v1.IfBlock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with IfElseBlock

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

the class SdkWorkflowBuilderTest method testConditionalWorkflowIdl.

@Test
void testConditionalWorkflowIdl() {
    SdkWorkflowBuilder builder = new SdkWorkflowBuilder();
    new ConditionalWorkflow().expand(builder);
    Node caseNode = Node.builder().id("neq").taskNode(TaskNode.builder().referenceId(PartialTaskIdentifier.builder().name("org.flyte.flytekit.SdkWorkflowBuilderTest$MultiplicationTask").build()).build()).inputs(asList(Binding.builder().var_("a").binding(BindingData.ofOutputReference(OutputReference.builder().var("in").nodeId(Node.START_NODE_ID).build())).build(), Binding.builder().var_("b").binding(BindingData.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(2L)))).build())).upstreamNodeIds(emptyList()).build();
    IfElseBlock ifElse = IfElseBlock.builder().case_(IfBlock.builder().condition(BooleanExpression.ofComparison(ComparisonExpression.builder().leftValue(Operand.ofVar("$0")).rightValue(Operand.ofPrimitive(Primitive.ofIntegerValue(2L))).operator(ComparisonExpression.Operator.NEQ).build())).thenNode(caseNode).build()).error(NodeError.builder().message("No cases matched").failedNodeId("square").build()).other(emptyList()).build();
    Node expectedNode = Node.builder().id("square").branchNode(BranchNode.builder().ifElse(ifElse).build()).inputs(singletonList(Binding.builder().var_("$0").binding(BindingData.ofOutputReference(OutputReference.builder().var("in").nodeId(Node.START_NODE_ID).build())).build())).upstreamNodeIds(emptyList()).build();
    WorkflowTemplate expected = WorkflowTemplate.builder().metadata(WorkflowMetadata.builder().build()).interface_(expectedInterface()).outputs(expectedOutputs()).nodes(singletonList(expectedNode)).build();
    assertEquals(expected, builder.toIdlTemplate());
}
Also used : IfElseBlock(org.flyte.api.v1.IfElseBlock) WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) TaskNode(org.flyte.api.v1.TaskNode) BranchNode(org.flyte.api.v1.BranchNode) Node(org.flyte.api.v1.Node) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with IfElseBlock

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

the class SdkBranchNode method toIdl.

@Override
public Node toIdl() {
    NodeError nodeError = NodeError.builder().failedNodeId(nodeId).message("No cases matched").build();
    Map<String, Binding> extraInputs = new HashMap<>();
    @Var IfElseBlock ifElseBlock = IfBlockIdl.toIdl(ifElse, extraInputs);
    if (ifElseBlock.elseNode() == null) {
        ifElseBlock = ifElseBlock.toBuilder().error(nodeError).build();
    }
    return Node.builder().id(nodeId).branchNode(BranchNode.builder().ifElse(ifElseBlock).build()).inputs(unmodifiableList(new ArrayList<>(extraInputs.values()))).upstreamNodeIds(upstreamNodeIds).build();
}
Also used : NodeError(org.flyte.api.v1.NodeError) Binding(org.flyte.api.v1.Binding) IfElseBlock(org.flyte.api.v1.IfElseBlock) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Var(com.google.errorprone.annotations.Var) ArrayList(java.util.ArrayList)

Aggregations

IfElseBlock (org.flyte.api.v1.IfElseBlock)3 BranchNode (org.flyte.api.v1.BranchNode)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Var (com.google.errorprone.annotations.Var)1 Condition (flyteidl.core.Condition)1 Workflow (flyteidl.core.Workflow)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Binding (org.flyte.api.v1.Binding)1 ComparisonExpression (org.flyte.api.v1.ComparisonExpression)1 IfBlock (org.flyte.api.v1.IfBlock)1 Node (org.flyte.api.v1.Node)1 NodeError (org.flyte.api.v1.NodeError)1 TaskNode (org.flyte.api.v1.TaskNode)1 WorkflowTemplate (org.flyte.api.v1.WorkflowTemplate)1