Using a dictionary for transition target states

Hi Chris,

I am trying to design a protocol with 4 different types of blocks. Right now, I am managing one variable that decides the block type and use them to evaluate 4 different conditional transitions in a state to branch out.

I am trying to design the code in a way such that I can also load individual task protocols that only run that type of block. For this, it would be nice if I could somehow use something like a dictionary from block/task type to appropriate state name string, and just have the following transition that will allow me to branch out, so that I can just reuse that code for whatever purposes:

goto (
target = typeToStateDict[taskType]
)

This currently doesn’t seem possible – it seems that the literal string is taken to be the state name. Is this unavoidable? If so, is there any way to make the target state of a transition dependent on a variable, without having to define multiple conditional transitions? While this is not strictly necessary, I think it would help manage the code in a easier way for me.

Separately, on a similar note, is there a functional programming-like way to pass in a macro or function (f1) as a parameter/argument into another macro (f2), so that I can run different f1’s within f2 in a flexible way by simply changing the argument? I would guess that this is difficult, but would be great if this is possible!

Hi Hokyung,

it seems that the literal string is taken to be the state name. Is this unavoidable?

That’s correct. No, there’s no way around it.

If so, is there any way to make the target state of a transition dependent on a variable, without having to define multiple conditional transitions?

I’m not sure I completely understand your protocol design, but one option might be to create a macro that performs the state branching based on a variable value. For example:

%define transition_by_task_type (task_type)
    goto (
        target = 'State for task type 1'
        when = task_type == 'type1'
        )
    goto (
        target = 'State for task type 2'
        when = task_type == 'type2'
        )
    goto (
        target = 'State for task type 3'
        when = task_type == 'type3'
        )
    goto (
        target = 'State for task type 4'
        when = task_type == 'type4'
        )
%end

Then you could use the macro in any state where you want to perform such branching:

state 'Branch state' {
    // Actions here

    // Other possible transitions here
    transition_by_task_type (current_task_type)
    // Additional transitions here
}

is there a functional programming-like way to pass in a macro or function (f1) as a parameter/argument into another macro (f2), so that I can run different f1’s within f2 in a flexible way by simply changing the argument?

No, that isn’t possible at present.

Cheers,
Chris