question

Jon Abbott avatar image
0 Likes"
Jon Abbott asked Jon Abbott commented

How to add or remove a trigger on an operator from within a user command?

I would like to have a user command that can add a couple lines of code to an operator's OnResourceAvailable trigger, and also remove the trigger. Currently there is no code on this trigger so there is no need to worry about it being overwritten. Similarly, when code is removed it can be removed all at once. I'm looking for a solution that works specifically via user command and not a process flow. It would also need to work while the model is running. Thanks in advance for your help.

FlexSim 16.2.1
operatortask executeruser commandtriggeronresourceavailable
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jon Abbott avatar image
1 Like"
Jon Abbott answered

Thanks @Jörg Vogel and @Matthew Gillespie! Here is a copy of the end result:

/**Custom Code*/

// define Operator to consider
string OpName = "Operator 1";

// identify the tree node for the operator in question
treenode Operator = node(OpName,model());

// identify the tree node for the operator's OnResourceAvailable trigger
treenode OperatorORATrigger = getvarnode(Operator, "onresourceavailable");

// define the run mode. commenting out the next line will delete the trigger
var runmode = "add"; // add the trigger

// define a blank trigger code string
string triggercode = "";

if (runmode == "add")
{
    // define code to be added to the operator's OnResourceAvailable trigger
    triggercode = "/**Custom Code*/; pd(1); pr();";
}

setnodestr(OperatorORATrigger,triggercode);
buildnodeflexscript(OperatorORATrigger);
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image
1 Like"
Joerg Vogel answered Jon Abbott commented

The Trigger is first of all a node which contains string data. That data is activated to be flexscript.

setnodestr(getvarnode(centerobject(current, 1), "onresourceavailable"),"pr();pd(1);");
buildnodeflexscript(getvarnode(centerobject(current, 1), "onresourceavailable"));

The centerobject is a Taskexecuter. I add the commands pr() and pd(1) to print a <CR> and "1" into the output console.

· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image Joerg Vogel commented ·

There are several problems creating an executable source code during a simulation run, and maybe the example contains still some errors, but it worked in a small model.

0 Likes 0 ·
Jon Abbott avatar image Jon Abbott Joerg Vogel commented ·

Thanks @Jörg Vogel! I will give this a try. For deleting the trigger, I'm guessing that destroyobject will suffice?

0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Jon Abbott commented ·

Just set the string data to be an empty string and rebuild the FlexScript.

1 Like 1 ·
Show more comments