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 (Chaplain)
on Dec 07, 2004 at 22:55 UTC ( [id://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.

Replies are listed 'Best First'.
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 (Chaplain) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://413036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found