Coder Social home page Coder Social logo

fsm's Introduction

fsm

FSM is a lightweight finite-state machine class. Both Hierarchical FSM and simple FSM implementations are provided.

Features

  • Expressive. Basic usage around on(state,trigger) -> do lambda expression.
  • Tiny, cross-platform, stand-alone, header-only.
  • ZLIB/libPNG licensed.

Links

Finite-State Machines: Theory and Implementation

Showcase

// basic hfsm sample

#include <iostream>
#include "fsm.hpp"

// custom states (gerunds) and actions (infinitives)

enum {
    walking = 'WALK',
    defending = 'DEFN',

    tick = 'tick',
};

struct ant_t {
    fsm::stack fsm;
    int health, distance, flow;

    ant_t() : health(0), distance(0), flow(1) {
        // define fsm transitions: on(state,trigger) -> do lambda
        fsm.on(walking, 'init') = [&]( const fsm::args &args ) {
            std::cout << "initializing" << std::endl;
        };
        fsm.on(walking, 'quit') = [&]( const fsm::args &args ) {
            std::cout << "exiting" << std::endl;
        };
        fsm.on(walking, 'push') = [&]( const fsm::args &args ) {
            std::cout << "pushing current task." << std::endl;
        };
        fsm.on(walking, 'back') = [&]( const fsm::args &args ) {
            std::cout << "back from another task. remaining distance: " << distance << std::endl;
        };
        fsm.on(walking, tick) = [&]( const fsm::args &args ) {
            std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
            distance += flow;
            if( 1000 == distance ) {
                std::cout << "at food!" << std::endl;
                flow = -flow;
            }
            if( -1000 == distance ) {
                std::cout << "at home!" << std::endl;
                flow = -flow;
            }
        };
        fsm.on(defending, 'init') = [&]( const fsm::args &args ) {
            health = 1000;
            std::cout << "somebody is attacking me! he has " << health << " health points" << std::endl;
        };
        fsm.on(defending, tick) = [&]( const fsm::args &args ) {
            std::cout << "\r" << "\\|/-$"[ health % 4 ] << " health: (" << health << ")   ";
            --health;
            if( health < 0 ) {
                std::cout << std::endl;
                fsm.pop();
            }
        };

        // set initial fsm state
        fsm.set( walking );
    }
};

int main() {
    ant_t ant;
    for(int i = 0; i < 12000; ++i) {
        if( 0 == rand() % 10000 ) {
            ant.fsm.push(defending);
        }
        ant.fsm.command(tick);
    }
}

Changelog

  • v1.0.0 (2015/11/29): Code revisited to use fourcc integers (much faster); clean ups suggested by Chang Qian
  • v0.0.0 (2014/02/15): Initial version

fsm's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fsm's Issues

duplicate symbol


case 1


A.h
class A
{
fsm::stack fsm
}


A.cpp
fsm::state fighting("fighting");


B.h include A.h
C.h include A.h
In this case I can't do: fsm.start(fighting)


case 2


A.h
fsm::state fighting("fighting");
class A
{
fsm::stack fsm
}


A.cpp


B.h include A.h
C.h include A.h
In this case "duplicate symbol" error ,

How B & C can call A.fsm.start(fighting)

How to understand the [State] and [Event] in Fsm

Dear author:
Today,i am coufused, when i saw the codes。As follows:
` // setup
fsm::call &on(const fsm::state &from, const fsm::state &to) {

		//XXX 这个返回应该是值传递还是什么呢?
		//XXX 但是这个可是全局变量啊
		//XXX 会不会出现重复添加的情况呢?
		callbacks[bistate(from, to)];
		cout << "Size is " << this->callbacks.size() << endl;
		return callbacks[bistate(from, to)];
	}`

i think the author confuses the concept of [State] and [Event].
How do you think?

crash

It should crash if the queue std::deque< states::iterator > aborted has more than 2 elements .
Your Code is useful , Thank you very much !

Find a bug in line 206

Dear author
I found unable to pass parameters when using your code.
my code:

   void test(){
        int ret = 4;
        fsm::state s(STATE_ERROR);
        fsm.set(s(ret));
    }
    fsm.on(STATE_ERROR, 'init') = [&](const fsm::args &args){
        std::vector<std::string> a = args;
        int s = a.size();// s = 0?
    };

Make the following changes to line 206 and it's back to normal.

bool call( const fsm::state &from, const fsm::state &to ) const {
    std::map< bistate, fsm::call >::const_iterator found = callbacks.find(bistate(from,to));
    if( found != callbacks.end() ) {
        log.push_back( { from, current_trigger, to } );
        if( log.size() > 50 ) {
            log.pop_front();
        }
        //found->second( to.args );  //old  ways
        found->second( from.args ); //new ways
        return true;
    }
    return false;
}

Looking forward to your reply and correction

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.