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.1.5.tar.gz (88.4 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.1.5-py3-none-any.whl (94.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyfcstm-0.1.5.tar.gz
Algorithm Hash digest
SHA256 db6b07c7dfdc0ce284859d98007fa8d2470b58b64ab910f53a232fcd1105c697
MD5 4aff13ed0dcf459a2543144c0bb6865d
BLAKE2b-256 d9528fb6f7bb9142af19762c9424de6ed837f36f25a214de740c18c96d912e44

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyfcstm-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 94.9 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.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 627869df817d175dda6513a89fb8c7c9e596762bc66932224cc3dccb71a0d8c7
MD5 77fadd06f1bdf83b624074a1fc1aec72
BLAKE2b-256 028acf28f1115f24e128bf6f31fca7d945c9758234a42210835eeda2ff1ce3e2

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