Skip to content

How to represent if - elif - else #22

@samwaseda

Description

@samwaseda

Now we come to the final part of our trilogy. Let's take the following example:

def my_condition_one(x, y):
    return x > y

def my_condition_two(x):
    return x > 0

def add(x, y):
    return x + y

def multiply(x, y):
    return x * y

def my_workflow(x, y):
    if my_condition_one(x, y):
        result = add(x, y)
    elif my_condition_two(x):
        z = multiply(x, y)
        result = add(x, z)
    else:
        z = add(x, y)
        result = multiply(y, z)
    return result

Following the structure of while (cf. #24), the three blocks are wrapped in independent injected nodes:

  • if-node
    • test
    • body
  • elif-node
    • test
    • body
  • else-node
    • body

(body stands for inputs, outputs, nodes and edges)

That means the whole thing would look like this:

{
    "inputs": {"x": {}, "y": {}},
    "outputs": {"result": {}},
    "nodes": {
        "injected_if_0": {
            "test": {
                "function": my_condition_one(x, y),
                "inputs": {"x": {}, "y": {}},
                "outputs": {"output": {}},
            },
            "inputs": {"y": {}, "x": {}},
            "outputs": {"result": {}},
            "nodes": {
                "add_0": {
                    "function": add(x, y),
                    "inputs": {"x": {}, "y": {}},
                    "outputs": {"output": {}},
                }
            },
            "data_edges": [
                ("inputs.x", "test.inputs.x"),
                ("inputs.x", "add_0.inputs.x"),
                ("inputs.y", "test.inputs.y"),
                ("inputs.y", "add_0.inputs.y"),
                ("add_0.outputs.output", "outputs.result"),
            ],
            "label": "injected_if_0",
        },
        "injected_elif_0": {
            "test": {
                "function": my_condition_two,
                "inputs": {"x": {}},
                "outputs": {"output": {}},
            },
            "inputs": {"y": {}, "x": {}, "z": {}},
            "outputs": {"result": {}, "z": {}},
            "nodes": {
                "multiply_0": {
                    "function": multiply,
                    "inputs": {"x": {}, "y": {}},
                    "outputs": {"output": {}},
                },
                "add_0": {
                    "function": add,
                    "inputs": {"x": {}, "y": {}},
                    "outputs": {"output": {}},
                },
            },
            "data_edges": [
                ("inputs.x", "test.inputs.x"),
                ("inputs.x", "multiply_0.inputs.x"),
                ("inputs.x", "add_0.inputs.x"),
                ("inputs.y", "multiply_0.inputs.y"),
                ("multiply_0.outputs.output", "add_0.inputs.y"),
                ("multiply_0.outputs.output", "outputs.z"),
                ("add_0.outputs.output", "outputs.result"),
            ],
            "label": "injected_elif_0",
        },
        "injected_else_0": {
            "inputs": {"y": {}, "x": {}, "z": {}},
            "outputs": {"result": {}, "z": {}},
            "nodes": {
                "add_0": {
                    "function": add,
                    "inputs": {"x": {}, "y": {}},
                    "outputs": {"output": {}},
                },
                "multiply_0": {
                    "function": multiply,
                    "inputs": {"x": {}, "y": {}},
                    "outputs": {"output": {}},
                },
            },
            "data_edges": [
                ("inputs.x", "add_0.inputs.x"),
                ("inputs.y", "add_0.inputs.y"),
                ("inputs.y", "multiply_0.inputs.x"),
                ("add_0.outputs.output", "multiply_0.inputs.y"),
                ("add_0.outputs.output", "outputs.z"),
                ("multiply_0.outputs.output", "outputs.result"),
            ],
            "label": "injected_else_0",
        },
    },
    "data_edges": [],
    "label": "my_workflow",
}

Consideration: As we can see already at this point, we actually have to wrap the whole blocks as an injected if block, because there is no edge from the global input to the if blocks, as well as the if blocks to the global output. We could potentially do the same with the while loop. Otherwise I don't necessarily see a problem here. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions