From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State machine enqueue order

Solved!
Go to solution

When I enqueue the following states

stateA -> stateB -> StateC (in which stateB does stateB1->stateB2->stateB3)

I get stateA -> stateB -> StateC -> stateB1 -> stateB2 -> stateB3

How do I get stateA -> stateB -> stateB1 -> stateB2 -> stateB3 -> StateC??

If this is not possible using 'enqueue', is there other way to do this?

 

 

0 Kudos
Message 1 of 11
(3,129 Views)

Please show is some simplified code that explains how you are doing things. It seems you just need to enqueue in the right order.

 

0 Kudos
Message 2 of 11
(3,122 Views)

If your “stateB” places its actions on the front of the queue (instead of the back) then you would get the desired order.

0 Kudos
Message 3 of 11
(3,094 Views)

@drjdpowell wrote:

If your “stateB” places its actions on the front of the queue (instead of the back) then you would get the desired order.


True enough, but to me, that is an "emergency" condition and not a part of "normal" operations.  In my opinion, if you have to regularly enqueue in the front end, you probably need to re-examine your dataflow.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 11
(3,078 Views)

@billko wrote:

@drjdpowell wrote:

If your “stateB” places its actions on the front of the queue (instead of the back) then you would get the desired order.


True enough, but to me, that is an "emergency" condition and not a part of "normal" operations.  In my opinion, if you have to regularly enqueue in the front end, you probably need to re-examine your dataflow.


Well, if this is a message queue we’re talking about, I would re-examine any need to enqueue in front, even an “emergency”.  But if the OP is instead just ordering actions internal to a loop, then I think one should ONLY “enqueue in front”, otherwise known as a “stack”.  

0 Kudos
Message 5 of 11
(3,061 Views)

@tomsze wrote:

When I enqueue the following states

stateA -> stateB -> StateC (in which stateB does stateB1->stateB2->stateB3)

I get stateA -> stateB -> StateC -> stateB1 -> stateB2 -> stateB3

How do I get stateA -> stateB -> stateB1 -> stateB2 -> stateB3 -> StateC??


Your question really makes very little sense.

  • If you don't want A>B>C, why do you enqueue them like that?
  • If all  that state C does it place B1.B2.B3 on the queue, what you get is already correct.
  • The stuff that you say you want to get instead places C at the end. Does that mean you want another round of B1>b2>B3 at the end?
  • Maybe state C could contain a case structure that reacts differently if the previous state was B instead of B3.

What exactly are these "states"? What do they do? What do the letters mean (e.g. how is B1 related to B or C? Is there a hierarchy somewhere)?

 

What prevents you from enqueuing everything in the desired final order? Is this a fixed sequence or can branching happen as a function of conditions?

0 Kudos
Message 6 of 11
(3,058 Views)

 

What exactly are these "states"? What do they do? What do the letters mean (e.g. how is B1 related to B or C? Is there a hierarchy somewhere)?


There is a hierarchy.  I think the OP is talking about a common LabVIEW pattern (or antipattern) of a “Queued State Machine”.   A poor name, as these are usually ordering actions, not actual states.   Actions can be composed of subactions.  

 

Think subVIs.  If B is a subVI called in a chain between calls to A and C, and B itself contains calls to the subsubVIs B1, B2, and B3, then the order of calling VIs is A, B, B1, B2, B3, C.  When one tries to do the same things with queued actions, one instead gets A, B, C, B1, B2, B3.

 

 

0 Kudos
Message 7 of 11
(3,049 Views)

Thanks For everyone 's reply~

I think making every states into subvi will cause me a lot of time.

Is it possible to insert state Smiley Happy ?

0 Kudos
Message 8 of 11
(3,017 Views)

@tomsze wrote:

Thanks For everyone 's reply~

I think making every states into subvi will cause me a lot of time.

Is it possible to insert state Smiley Happy ?


Who knows what we are even talking about???  As far as I can see, it's just a lot of conjecture; not much more useful than gossip.  We need to see code before you can receive anything more than a general answer.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 9 of 11
(2,998 Views)
Solution
Accepted by topic author tomsze

Yes, but as people already explored above (or perhaps below, if your post order is the opposite of mine), if you're finding yourself in this position you might be better trying to avoid reaching this situation, rather than having to work around it.

 

As has also been described, it would be easier to give direct advice if we knew what you're using to get 'states' in an order. The most likely, as suggested by jwpowell, is that you're using a Queue with perhaps an Enum+Variant, or String+Variant, or just String/Enum as the datatype.

 

In this case, you can "Enqueue Element at Opposite End" to place new states at the beginning (i.e. next) in the queue. Note that you'd need to do this in reverse order:

  • Enqueue A, B, C normally, using Enqueue Element
  • In State B, enqueue first B3 (the last state) using EEaOE.
  • Then enqueue B2, then B1, again using EEaOE.

You queue would then look like (next state bold):

  • A,B,C
  • B,C
  • B3, C -> B2, B3, C -> B1, B2, B3, C
  • B2, B3, C
  • ...

But as again discussed above, usually people try to avoid using this kind of EEaOE, especially with multiple states, because it becomes more complicated to understand and debug, and can interfere with other states that expected a certain ordering, or other problems.

 

The JKI State Machine advocates using 'Macro' states to enqueue others - they use an array of strings as the state queue and place states at the top of the array in many cases. Some further description can be found here: JKI Best PracticesBest Practice 4: Macros. I haven't used the JKISM yet, but I have read good things about it.


GCentral
0 Kudos
Message 10 of 11
(2,997 Views)