Coder Social home page Coder Social logo

smlang-rs's Introduction

smlang: A no_std State Machine Language DSL in Rust

Build Status

A state machine language DSL based on the syntax of Boost-SML.

Aim

The aim of this DSL is to facilitate the use of state machines, as they quite fast can become overly complicated to write and get an overview of.

Transition DSL

The DSL is defined as follows:

statemachine!{
    *SrcState1 + Event1 [ guard1 ] / action1 = DstState2, // * denotes starting state
    SrcState2 + Event2 [ guard2 ] / action2 = DstState1,
    // ...
}

Where guard and action are optional and can be left out. A guard is a function which returns true if the state transition should happen, and false if the transition should not happen, while action are functions that are run during the transition which are guaranteed to finish before entering the new state.

This implies that any state machine must be written as a list of transitions.

State machine context

The state machine needs a context to be defined. The StateMachineContext is generated from the statemachine! proc-macro and is what implements guards and actions, and data that is available in all states within the state machine and persists between state transitions:

statemachine!{
    State1 + Event1 = State2,
    // ...
}

pub struct Context;

impl StateMachineContext for Context {}

fn main() {
    let mut sm = StateMachine::new(Context);

    // ...
}

See example examples/context.rs for a usage example.

State data

Any stat may have some data associated with it (except the starting state), which means that this data is only exists while in this state.

pub struct MyStateData(pub u32);

statemachine!{
    State1(MyStateData) + Event1 = State2,
    // ...
}

See example examples/state_with_data.rs for a usage example.

Event data

Data may be passed along with an event into the guard and action:

pub struct MyEventData(pub u32);

statemachine!{
    State1 + Event1(MyEventData) [guard] = State2,
    // ...
}

Event data may also have associated lifetimes which the statemachine! macro will pick up and add the Events structure. This means the following will also work:

pub struct MyEventData<'a>(pub &'a u32);

statemachine!{
    State1 + Event1(MyEventData<'a>) [guard1] = State2,
    State1 + Event2(&'a [u8]) [guard2] = State3,
    // ...
}

See example examples/event_with_data.rs for a usage example.

Guard and Action syntax

See example examples/guard_action_syntax.rs for a usage-example.

State Machine Examples

Here are some examples of state machines converted from UML to the State Machine Language DSL. Runnable versions of each example is available in the examples folder.

Linear state machine

alt text

DSL implementation:

statemachine!{
    *State1 + Event1 = State2,
    State2 + Event2 = State3,
}

This example is available in ex1.rs.

Looping state machine

alt text

DSL implementation:

statemachine!{
    *State1 + Event1 = State2,
    State2 + Event2 = State3,
    State3 + Event3 = State2,
}

This example is available in ex2.rs.

Using guards and actions

alt text

DSL implementation:

statemachine!{
    *State1 + Event1 [guard] / action = State2,
}

This example is available in ex3.rs.

Contributors

List of contributors in alphabetical order:


License

Licensed under either of

at your option.

smlang-rs's People

Contributors

bgourlie avatar jedrzejboczar avatar korken89 avatar

Watchers

 avatar

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.