Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Are state machines just for parsing?

by sleepingsquirrel (Hermit)
on Dec 07, 2004 at 22:55 UTC ( #413036=note: print w/ replies, xml ) Need Help??


in reply to Are state machines just for parsing?

Why doesn't the simple-minded method work?

#!/usr/bin/perl -w # State transition table # # Current Inputs Next Action $next{'initial'}{'left' } = ['red', sub { print "Red \n"} ]; $next{'initial'}{'right'} = ['blue', sub { print "Blue\n"} ]; $next{'red'} {'left' } = ['blue', sub { print "Blue\n"} ]; $next{'red'} {'right'} = ['end', sub { print "End \n"} ]; $next{'blue'} {'left' } = ['blue', sub { print "Blue\n"} ]; $next{'blue'} {'right'} = ['red', sub { print "Red \n"} ]; $next{'end'} {'left' } = ['end', sub { print "End \n"} ]; $next{'end'} {'right'} = ['end', sub { print "End \n"} ]; @arbitrary_inputs = qw/left left right left left right right left/; $state = 'initial'; for (@arbitrary_inputs) { $action = $next{$state}{$_}->[1]; $action->(); $state = $next{$state}{$_}->[0]; }


-- All code is 100% tested and functional unless otherwise noted.


Comment on Re: Are state machines just for parsing?
Download Code
Re^2: Are state machines just for parsing?
by samtregar (Abbot) on Dec 08, 2004 at 02:55 UTC
    Well, first off because my problem doesn't have states with just one possible next state, it has a set. Sure I could enhance your example until it could handle a fully generalized state machine. Then I could find a good name and put our solution on CPAN. But if there's already a solution out there I can use, wouldn't it be better to use that?

    -sam

      But if there's already a solution out there I can use, wouldn't it be better to use that?
      See also: Abstraction Inversion.
Re^2: Are state machines just for parsing?
by sleepingsquirrel (Hermit) on Dec 08, 2004 at 16:47 UTC
    It occured to me that it might be beneficial to show the state transition diagram for the above state machine.
    +--------------------------------------------------+ | | | _______ | | left / \ right | | +------------|initial|-----------------+ | | | \_______/ | | | | | | | | +--------+ | | | ___V___ | | | | |left / \ right | _V__V_V_ +-----| red |--------------+ | left / \ right \_______/ | +------| blue |------+ ^ | \________/ | | | | | | | | __V____ | | left / \ right | | +-----| end |------+ | | | \_______/ | | | | ^ ^ | | | | | | | | | +-------+ +---------+ | | | +-------------------------------------------------+


    -- All code is 100% tested and functional unless otherwise noted.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://413036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2015-07-04 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls