DFA Build Process

Ovid on 2004-12-14T02:31:42

The build process for the next version of Bricolage was becoming too complicated. Because we want it to be considerably easier to install, we're putting a lot of effort into ensuring that everything "just works." My code was getting too complex handling all of the rules so we're making the switch to a DFA engine and simply describing what to do when particular conditions occur. Unfortunately, DFA::Simple has a rather confusing interface and even after reading the Perl.com article about it I didn't quite "get" it. That led me to rewrite the interface to something I can actually use. Here's how to play ping-pong with a state machine:

my ($goto, $count) = ('', 0);
my %state_machine = (
    ping => {
        enter => sub { $goto = 'pong' },
        leave => sub { warn "leaving state 'ping'" },
        goto  => [
            # goto         when             and do this
            pong => [sub {'pong' eq $goto}, sub {$count++}],
        ],
    },
    pong => {
        enter => sub { $goto = 'ping' },
        leave => sub { warn "leaving state 'pong'" },
        goto  => [
            ping => [sub {'ping' eq $goto}, sub {$count++}],
        ]
    }
);
my $dfa = Bric::Util::DFA->new(
    \%state_machine, 
    'ping', # state to start in 
    sub {$count >= 20} # done when true
);
$dfa->next_state while ! $dfa->done;

Yeah, it needs some work, but it's much easier for me to understand.