question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional State Transitions?

See original GitHub issue

Fully recognize this question may reflect bad practice or might just run counter to the opinion of the framework (which is great!), but do you have any plan to implement conditional state transitions?

Borrowing from your example, if the coffee maker needs, say, ‘5 beans’ in order to brew coffee, is there any way (within the framework) to check that condition is satisfied, or would something like the following be the ‘automatic’ way to implement that? (pythonic -> automat-ic? get it?)

def put_in_some_beans(self, beans):
    self._bean_counter += beans.count
    if self._bean_counter >= 5:
        self._put_in_enough_beans(beans)
    else:
        self._put_in_not_enough_beans(beans)

@_machine.input()
def _put_in_enough_beans(self, beans):
    "There are enough beans in the hopper"

@_machine.input()
def _put_in_not_enough_beans(beans):
    "There aren't enough beans"

@_machine.output()
def bean_error(self, beans):
   return f'Missing {5-self._bean_counter} beans'

dont_have_beans.upon(_put_in_enough_beans, enter=have_beans, outputs=[])
dont_have_beans.upon(_put_in_not_enough_beans, enter=dont_have_beans, outputs=[bean_error])

I don’t have any ideas or thoughts about how an alternative method would be implemented, or whether it even belongs. The reason I’m asking is that I see what could end up being a lot of state-checking and perhaps a lot of branching logic outside of the automat framework -> and maybe thats ok? Just making sure I’m not missing something.

As an aside: I’m working through this document: and trying to learn automat by implementing the examples; which is helpful in terms of thinking about setting up more complicated machines from primitives (cascading, parallel, etc), but their implementation philosophy is so different… if I can grok it, it might make for some helpful documentation additions.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
glyphcommented, Jul 15, 2017

do you have any plan to implement conditional state transitions?

In a word: no. Conditional transitions would compromise the purity of the machine, and make it more difficult to reason about what it takes to reach a particular state, which would obviate much of the usefulness of being able to diagram the machine automatically et cetera. But, obviously we need some way to handle the case that you’ve brought up. This is, in my mind, yet another use-case for #41 .

0reactions
lucaswimancommented, Aug 24, 2017

Another use case for this kind of logic is a retry loop, where you want to retry something up to N times until you get a success. It is possible to represent both of these use cases as a state machine, but you need N copies of each of the states involved in processing a submission. This is similar to the (foo){1,50} syntax in regular expressions, where the state machine needed to recognize “between one and fifty copies of ‘foo’” is much larger than the state machine for just foo. I don’t know a good syntax for duplicating states and transitions, but that would be another way of addressing this fairly common use case that still gives the ability to diagram the output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Condition state - Glossary - Statecharts
A condition state is a state that essentially consists solely of automatic, guarded transitions, so that upon entry, the state will always immediately...
Read more >
A state-transition diagram illustrating a conditional transition ...
A generic mathematical model of state transition simulates through Ordinary Differential Equations (ODEs) the evolution of single cell behaviour during various ...
Read more >
Conditionally Transition to States with Guards in XState
When an event is sent to the machine and it encounters a transition object with a cond property set to a guard function,...
Read more >
State Machine Diagram - UML 2 Tutorial - Sparx Systems
Junctions are semantic-free. A junction which splits an incoming transition into multiple outgoing transitions realizes a static conditional branch, as opposed ...
Read more >
Conditional Transitions - scion
While it isn't time to change lights, we transition to the same state after adding 1 to interval . This is totally legal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found