Skip to content

cfxiao/fsmachine

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FSMachine

CI Status Version License Platform

A straightforward state machine in Obj-C.

Features:

  • Support for Moore and Mealy-style machines
  • Actions on state enter/exit, and during transitions
  • Conditional transitions
  • Up-front machine validation
  • Configurable logging

Installation

FSMachine is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "FSMachine"

Usage

// TODO

Example

// Dispense a can for 3 mBTC. (what do you mean, they aren't actually coins?)
// Don't give any change, but carry credit over to the next person.

FSEvent *insert1 = [FSEvent eventWithName:@"insert 1"];
FSEvent *insert2 = [FSEvent eventWithName:@"insert 2"];

FSMachine *machine = [FSMachine machineWithBuilder:^(FSMachineBuilder *machine) {

    FSState *zero = [machine addState:@"zero"];
    FSState *one = [machine addState:@"one"];
    FSState *two = [machine addState:@"two"];

    FSActionBlock dispenseCan = ^{
        NSLog(@"Kerchunk!");
    };

    [zero transitionTo:one onEvent:insert1];
    [zero transitionTo:two onEvent:insert2];

    [one transitionTo:two onEvent:insert1];
    [one transitionTo:zero onEvent:insert2 withAction:dispenseCan];

    [two transitionTo:zero onEvent:insert1 withAction:dispenseCan];
    [two transitionTo:one onEvent:insert2 withAction:dispenseCan];
}];

[machine post:insert1]; // [zero] -> [one]
[machine post:insert1]; // [one] -> [two]
[machine post:insert2]; // [two] -> [one], "Kerchunk!"

// (next person comes along…)

[machine post:insert2];  // [one] -> [zero], "Kerchunk!"

To do

  • More examples
  • Markov models?

Contributing

…is encouraged. Bug reports and pull requests are welcome.

License

Apache 2.0. See the LICENSE file for more info.

About

A straightforward state machine in Obj-C.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Objective-C 97.9%
  • Other 2.1%