Conditionals

We are taking a plate from a stacker and randomly deciding with a dice roll and a coin flip whether it goes to the bin, stays where is is or makes it through to a plate hotel.

from linq.task import (
    ActionTask,
    CodeTask,
    ConditionalSourceTask,
    If,
    LabwareSource,
    LuaFunction,
)

HEADS = 1
TAILS = 0
plate_type = labware.LabwareType(id="microplate_96")

plate = labware.Labware(
    id="plate_1",
    labware_type=plate_type,
    starting_location=LabwareLocation(
        instrument=stacker,
        slot=1
    )
    batch=1,
)

workflow = Workflow(
    workcell=conditional_workcell,
    name="Conditional Workflow",
    author="Automata",
    description="Conditional Workflow",
    version="0.1",
    scheduler_version=get_latest_scheduler_version(),
    labware_types=[plate_type],
    labware=[plate],
)

Single conditional

workflow.If(roll_the_dice.out().equals_any([1, 6]), id="dice-roll-conditional").Then(
    # Move to hotel
    move_plate_dice_roll := ActionTask(
        id="move-plate-to-hotel-on-dice-roll",
        description="Dice is 1 or 6, the plate survives and moves to the hotel.",
        time_estimate=30,
        instrument_type=hotel.type,
        labware_sources=[LabwareSource(labware=plate)],
    ),
).ElseIf(roll_the_dice.out() == 2).Then(
    # Bin it
    ActionTask(
        id="bin-plate",
        description="Dice is 2, the plate does not make it.",
        time_estimate=30,
        instrument_type=bin1.type,
        labware_sources=[LabwareSource(labware=plate)],
    ),
).Else(
    # Leave it where it is
    leave_the_plate_on_stacker := ActionTask(
        id="leave-plate-on-stacker",
        description="Dice is not 1, 2 or 6, the plate stays where it is.",
        time_estimate=30,
        instrument_type=stacker.type,
        action="do_nothing",
        labware_sources=[LabwareSource(labware=plate)],
    )

)

Nested conditional

workflow.If(roll_the_dice.out().equals_any([1, 6]), id="dice-roll-conditional").Then(
    move_plate_dice_roll := ActionTask(
        id="move-plate-to-hotel-on-dice-roll",
        description="Dice is 1 or 6, the plate survives.",
        time_estimate=30,
        instrument_type=hotel.type,
        labware_sources=[LabwareSource(labware=plate)],
    ),
).Else(
    If(flip_the_coin.out() == HEADS, id="flip-coin-conditional")
    .Then(
        move_plate_coin_flip := ActionTask(
            id="move-plate-to-hotel-on-coin-flip",
            description="Dice is not 1 or 6 but Coin is heads, the plate survives.",
            time_estimate=30,
            instrument_type=hotel.type,
            labware_sources=[LabwareSource(labware=plate)],
        ),
    )
    .Else(
        ActionTask(
            id="bin-plate",
            description="Dice is not 1 or 6, Coin is tails, the plate does not make it.",
            time_estimate=30,
            instrument_type=bin1.type,
            labware_sources=[LabwareSource(labware=plate)],
        ),
    ),
    # NOTE: We need a do nothing task here to ensure that the labware source is defined for
    # the final task store-in-hotel which exists outside the If block
    do_nothing := ActionTask(
        id="do-nothing-after-move_plate_coin_flip",
        description="Do nothing task is required to give tasks outside the If block a labware source",
        time_estimate=1,
        instrument_type=hotel.type,
        action="do_nothing",
        labware_sources=[LabwareSource(labware=plate, source_task=move_plate_coin_flip)],
    ),
)