Skip to main content

A Python framework for parsing finite state machine DSL and generating executable code in multiple target languages.

Project description

pyfcstm

PyPI PyPI - Python Version PyPI - Implementation PyPI - Downloads

Loc Comments Maintainability codecov Ask DeepWiki

Docs Deploy Code Test Badge Creation Package Release

GitHub stars GitHub forks GitHub commit activity GitHub issues GitHub pulls Contributors GitHub license

A Python framework for parsing finite state machine DSL and generating executable code in multiple target languages.

Installation

You can simply install it with pip command line from the official PyPI site.

pip install pyfcstm

For more information about installation, you can refer to Installation Documentation.

How To Use It

Use With CLI

You can use this with CLI command

pyfcstm --help
  • Generate Plantuml Code For Visualization
pyfcstm plantuml -i test_dsl_code.fcstm

Here is a simple code example, you can try this out

def int a = 0;
def int b = 0x0;
def int round_count = 0;  // define variables
state TrafficLight {
    >> during before {
        a = 0;
    }
    >> during before abstract FFT;
    >> during before abstract TTT;
    >> during after {
        a = 0xff;
        b = 0x1;
    }

    !InService -> [*] :: Error;

    state InService {
        enter {
            a = 0;
            b = 0;
            round_count = 0;
        }

        enter abstract InServiceAbstractEnter /*
            Abstract Operation When Entering State 'InService'
            TODO: Should be Implemented In Generated Code Framework
        */

        // for non-leaf state, either 'before' or 'after' aspect keyword should be used for during block
        during before abstract InServiceBeforeEnterChild /*
            Abstract Operation Before Entering Child States of State 'InService'
            TODO: Should be Implemented In Generated Code Framework
        */

        during after abstract InServiceAfterEnterChild /*
            Abstract Operation After Entering Child States of State 'InService'
            TODO: Should be Implemented In Generated Code Framework
        */

        exit abstract InServiceAbstractExit /*
            Abstract Operation When Leaving State 'InService'
            TODO: Should be Implemented In Generated Code Framework
        */

        state Red {
            during {  // no aspect keywords ('before', 'after') should be used for during block of leaf state
                a = 0x1 << 2;
            }
        }
        state Yellow;
        state Green;
        [*] -> Red :: Start effect {
            b = 0x1;
        };
        Red -> Green effect {
            b = 0x3;
        };
        Green -> Yellow effect {
            b = 0x2;
        };
        Yellow -> Red : if [a >= 10] effect {
            b = 0x1;
            round_count = round_count + 1;
        };
        Green -> Yellow : /Idle.E2;
        Yellow -> Yellow : /E2;
    }
    state Idle;

    [*] -> InService;
    InService -> Idle :: Maintain;
    Idle -> Idle :: E2;
    Idle -> [*];
}
  • Generate Code With Given Template
pyfcstm generate -i test_dsl_code.fcstm -t template_dir/ -o generated_code_dir/

Use With Pythonic API

You can use this with pythonic API.

from pyfcstm.dsl import parse_with_grammar_entry
from pyfcstm.model.model import parse_dsl_node_to_state_machine
from pyfcstm.render import StateMachineCodeRenderer

if __name__ == '__main__':
    # Load AST Node From DSL Code
    ast_node = parse_with_grammar_entry("""
    def int a = 0;
    def int b = 0x0;
    def int round_count = 0;  // define variables
    state TrafficLight {
        state InService {
            enter {
                a = 0;
                b = 0;
                round_count = 0;
            }
            
            enter abstract InServiceAbstractEnter /*
                Abstract Operation When Entering State 'InService'
                TODO: Should be Implemented In Generated Code Framework
            */
            
            // for non-leaf state, either 'before' or 'after' aspect keyword should be used for during block
            during before abstract InServiceBeforeEnterChild /*
                Abstract Operation Before Entering Child States of State 'InService'
                TODO: Should be Implemented In Generated Code Framework
            */
            
            during after abstract InServiceAfterEnterChild /*
                Abstract Operation After Entering Child States of State 'InService'
                TODO: Should be Implemented In Generated Code Framework
            */
            
            exit abstract InServiceAbstractExit /*
                Abstract Operation When Leaving State 'InService'
                TODO: Should be Implemented In Generated Code Framework
            */
        
            state Red {
                during {  // no aspect keywords ('before', 'after') should be used for during block of leaf state
                    a = 0x1 << 2;
                }
            }
            state Yellow;
            state Green;
            [*] -> Red :: Start effect {
                b = 0x1;
            };
            Red -> Green effect {
                b = 0x3;
            };
            Green -> Yellow effect {
                b = 0x2;
            };
            Yellow -> Red : if [a >= 10] effect {
                b = 0x1;
                round_count = round_count + 1;
            };
        }
        state Idle;
        
        [*] -> InService;
        InService -> Idle :: Maintain;
        Idle -> [*];
    }
    """, entry_name='state_machine_dsl')
    # Load DSL Model From DSL AST Node
    model = parse_dsl_node_to_state_machine(ast_node)

    # Load Template Directory
    renderer = StateMachineCodeRenderer(
        template_dir='../fsm_generation_template'
    )
    # Render to Given Directory Via Template Directory
    renderer.render(model, 'test_output_x')

For more information about this DSL, see: PyFCSTM DSL Syntax Tutorial.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfcstm-0.2.0.tar.gz (89.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfcstm-0.2.0-py3-none-any.whl (95.6 kB view details)

Uploaded Python 3

File details

Details for the file pyfcstm-0.2.0.tar.gz.

File metadata

  • Download URL: pyfcstm-0.2.0.tar.gz
  • Upload date:
  • Size: 89.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyfcstm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d4264e4b5dbd172cff5ddae4c20b5b33f36ecbda81f57f7f385ffb2861e91df1
MD5 1cae4b04704fe2b773be00a633200a14
BLAKE2b-256 0deee529e56e2ae0a027995e61884f96307ac7989baff444d1f0731bdb3cd1eb

See more details on using hashes here.

File details

Details for the file pyfcstm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyfcstm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 95.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyfcstm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 486c25e57eb9f98a4ba1ae70ff3b3802d01b8f627ac176d3d136d564216b35df
MD5 f98c26808acd6dc8b8c4032ea2118370
BLAKE2b-256 9865dd53ca27aa5e75c134ce3a7826844fff3765c86c2680e80435e6ee6ba36f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page